# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Python Requests
#
# Tutorial on using the [Requests](http://docs.python-requests.org/en/master/user/quickstart/) library to access HTTP requests, GET, POST, PUT, DELETE, HEAD, OPTIONS.  
# This notebook also covers how to use the Python [JSON](https://docs.python.org/3/library/json.html) library to parse values out of a GET response.  
# If you don't have the requests library installed you can run 'pip install requests' or some equivalent command for your system in the console window.  

import requests
import json

r = requests.get('https://api.github.com/events')
r = requests.post('https://httpbin.org/post', data = {'name':'Joe'})
r = requests.put('https://httpbin.org/put', data = {'name':'Joe'})
r = requests.delete('https://httpbin.org/delete')
r = requests.head('https://httpbin.org/get')
r = requests.options('https://httpbin.org/get')

# ### GET Requests - Passing Parameters in URLs
# A URL that returns an HTTP response in JSON format is called an API endpoint.  
# Here's an example, https://httpbin.org/get  
#
# With GET requests we can add parameters onto the URL to retrieve specific data.  
# We define the params as a dictionary, and add params=payload to the Request.  
# The Requests library builds the whole URL for us.

payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get('https://httpbin.org/get', params=payload)
print(r.url)

# **Passing a List as a parameter**  
# Still use key:value pairs, with the list as the value.  
# You can see here all the different attributes included in an HTTP Request response.

payload = {'key1': 'value1', 'key2': ['value2', 'value3']}
r = requests.get('https://httpbin.org/get', params=payload)
print('URL: ', r.url)
print('ENCODING: ', r.encoding)
print('STATUS_CODE: ', r.status_code)
print('HEADERS: ', r.headers)
print('TEXT: ', r.text)
print('CONTENT: ', r.content)
print('JSON: ', r.json)

# ### POST Requests
# We can add parameters to a POST request in Dictionary format, but we use data=payload.  
# POST requests are used to upload new records to the server.  
# POST would typically be used to get data from a web form and submit it to the server. 

# +
r = requests.post('https://httpbin.org/post', data = {'name':'Joe'})

payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.post("https://httpbin.org/post", data=payload)
print(r.text)
# -

# ### Using Requests to GET Currency Exchange Data
# Here's a handy endpoint where we can GET foreign currency exchange rates in JSON format, https://api.exchangeratesapi.io/latest

url = 'https://api.exchangeratesapi.io/latest'
r = requests.get(url)
print(r.text)

# **It looks like the default is base:EUR, but we want exchange rates for USD, so we can pass in a parameter for base.  
# We can also put in any date we want.**

url = 'https://api.exchangeratesapi.io/2018-01-15'
r = requests.get(url, params={'base':'USD'})
print(r.text)

# ### Decoding JSON data
# Now we have the rates in JSON format. We need to convert that to usable data. 
# The JSON library basically has two functions:  
# - json.loads( ) converts a text string into Python dict/list objects.  
# - json.dumps( ) converts dict/list objects into a string.  
#
# We need to decode the JSON data into a dictionary, then get the rate for GBP, convert it to a float, and do a conversion.

rates_json = json.loads(r.text)['rates']
print(rates_json)
print(rates_json['GBP'])
gbp = float(rates_json['GBP'])
print('200USD = ', str(gbp * 200), 'GBP')

# ### Using Requests to GET Song Data
# Every API has documentation on how to use it.  
# You can find the docs for this Song Data API [here.](https://documenter.getpostman.com/view/3719697/RzfarXB4)

url = 'https://musicdemons.com/api/v1/artist'
r = requests.get(url)
print(r.text[:700])

url = 'https://musicdemons.com/api/v1/artist/21'
r = requests.get(url)
print(r.text)

url = 'https://musicdemons.com/api/v1/artist/21'
headers = {'with': 'songs,members'}
r = requests.get(url, headers=headers)
print(r.text[:700])

import json
text_json = json.loads(r.text)
print(text_json['name'])
for song in text_json['songs']:
    print(song['title'])

# ### Tips on breaking down JSON  
# To get data out of a JSON object, which is a combination of lists and dictionaries,  
# just remember for lists you need a numerical index, and for key-value pairs you need a text index.  
# So if the object looks like this, {"cars":["id":1,"model":"Camry"... you can access the model of the first car with text['cars'][0]['model']



StarcoderdataJupyterScriptsDedupFiltered