APIs

There are a few cases when a data set isn't a good enough solution. An Application Program Interface API is an alternative, it allows you to dinamically query andretrive data


In [1]:
import csv
import requests

Status Codes

200 -- everything went okay, and the result has been returned (if any) 301 -- the server is redirecting you to a different endpoint. This can happen when a company switches domain names, or an endpoint name is changed. 401 -- the server thinks you're not authenticated. This happens when you don't send the right credentials to access an API (we'll talk about this in a later mission). 400 -- the server thinks you made a bad request. This can happen when you don't send along the right data, among other things. 403 -- the resource you're trying to access is forbidden -- you don't have the right permissions to see it. 404 -- the resource you tried to access wasn't found on the server.


In [ ]:
response = requests.get("http://api.open-notify.org/iss-now.json")
response.status_code

Query Parameters

A 400 status code indicates a bad request, in this case it means that we need to add some parameters to the request.


In [3]:
# Set up the parameters we want to pass to the API.
# This is the latitude and longitude of New York City.
parameters = {"lat": 40.71, "lon": -74}

# Make a get request with the parameters.
response = requests.get("http://api.open-notify.org/iss-pass.json", params=parameters)

# Print the content of the response (the data the server returned)
print(response.content)

# This gets the same data as the command above
response = requests.get("http://api.open-notify.org/iss-pass.json?lat=40.71&lon=-74")
print(response.content)


{
  "message": "success", 
  "request": {
    "altitude": 100, 
    "datetime": 1461270432, 
    "latitude": 40.71, 
    "longitude": -74.0, 
    "passes": 5
  }, 
  "response": [
    {
      "duration": 628, 
      "risetime": 1461275305
    }, 
    {
      "duration": 605, 
      "risetime": 1461281099
    }, 
    {
      "duration": 522, 
      "risetime": 1461335341
    }, 
    {
      "duration": 640, 
      "risetime": 1461341044
    }, 
    {
      "duration": 583, 
      "risetime": 1461346886
    }
  ]
}
{
  "message": "success", 
  "request": {
    "altitude": 100, 
    "datetime": 1461270432, 
    "latitude": 40.71, 
    "longitude": -74.0, 
    "passes": 5
  }, 
  "response": [
    {
      "duration": 628, 
      "risetime": 1461275305
    }, 
    {
      "duration": 605, 
      "risetime": 1461281099
    }, 
    {
      "duration": 522, 
      "risetime": 1461335341
    }, 
    {
      "duration": 640, 
      "risetime": 1461341044
    }, 
    {
      "duration": 583, 
      "risetime": 1461346886
    }
  ]
}

JSON format

Forcing everithing in to JSON JavaScript Object Notation is teh way to encode data structures, like lists and dictionaries JSON is the primary format in which informaton is passed in applications. Python has great support with the JSON librarie and converts lists to string and to dictionaries. The json library has two main methods:

dumps -- Takes in a Python object, and converts it to a string. loads -- Takes a json string, and converts it to a Python object.