CS 205

Data Driven Discovery

11/05/2015

API

Application Programming Interface

Provides any interface to allow for a programmer to get data from another source in a programmatic way.

Two types of APIs

1. RESTful API: Allows a client to request the current data from the server.

(Ex: What is the current temperature in Champaign, IL?)


2. Publish/Subscribe API: Allows a client to subscribe to recieve updates about changes/events in data.

(Ex: An update will be published to all clients when the current temperature in Champaign, IL changes.)

CUMTD API

The CUMTD API is a RESTful API.
Thier documentation tells us:

The RESTful endpoints use the HTTP GET verb with query string parameters.

HTTP GET

http://cs.illinois.edu/?a=3&b=5&s=Hello
http://cs.illinois.edu/?a=3&b=5&s=Hello
----------------------- ---------------
 URL                     Query String
http://cs.illinois.edu/?a=3&b=5&s=Hello
----------------------- ---------------
 URL                    { a = "3",
                          b = "5",
                          s = "Hello" }

CUMTD API

GetDeparturesByStop Method Documentation



Workbook Branch

demo_cumtd

Puzzle #2

# Load the urllib library
import urllib
...

# Construct a Python dictionary
parameters = {
  "key": myKey,
  "stop_id": "IU"
}

# Construct the full URL
url = "https://developer.cumtd.com/api/v2.2/json/" +
      "GetDeparturesByStop?" +
      urllib.urlencode( parameters )

# Read the response
response = urllib.urlopen( url )

Puzzle #3

# Load the json library
import json
...

# Parse a JSON string into a Python dictionary
dictionary = json.load( response )

Desired Output

[
  { "route": "5E Green", "expected": 3 },
  { "route": "12W Teal", "expected": 5 },
  { "route": "22S Illini", "expected": 6 },
  { "route": "13N Silver", "expected": 10 },
]