API 4-2: REST APIs#

Non-GET, Caching Strategies, Azure

HTTP Methods beyond GET#

So far we have only seen GET requests. But there are other HTTP methods that are used to perform different operations such as:

  • POST: Create a new resource

  • PUT: Update an existing resource

  • DELETE: Delete a resource

From the perspective of requests these behave the same as a GET. The only subtle difference is that you can pass a data parameter when there are large payloads which cannot fit on the URL.

Example of a POST request#

Let’s do an example with the Azure sentiment API from the IoT Portal. This REST API requires the text to analyze, and will return the sentiment or “mood” of the text. Since there can be a substantial amount of text, the API requires the POST method, and the data to be sent in the body of the request.

First, Try it out in the IoT portal:

/api/azure/sentiment

Text: “I love IST356. It is the best course I’ve ever taken.”

Then, let’s convert the curl command to Python requests code.

 1import requests
 2
 3'''
 4curl -X 'POST' \
 5  'https://cent.ischool-iot.net/api/azure/sentiment' \
 6  -H 'accept: application/json' \
 7  -H 'X-API-KEY: APIKEY' \
 8  -H 'Content-Type: application/x-www-form-urlencoded' \
 9  -d 'text=I%20love%20IST356.%20It%20is%20the%20best%20course%20I'\''ve%20ever%20taken.'
10'''
11
12apikey = 'YOURAPIKEYHERE'
13url = 'https://cent.ischool-iot.net/api/azure/sentiment'
14headers = {
15    'accept': 'application/json',
16    'X-API-KEY': apikey,
17    'Content-Type': 'application/x-www-form-urlencoded'
18}
19data = {
20    'text': 'I love IST356. It is the best course I\'ve ever taken.'
21}
22response = requests.post(url, headers=headers, data=data)
23response.raise_for_status()
24results = response.json()
25sentiment = results['results']['documents'][0]['sentiment']
26print(sentiment)
positive

Challenge 4-2-1#

For this challenge, use Azure entity recognition API to extract entities from the following text.

“The Dallas Cowboys are a far better team than the New York Giants this year. The Giants have not won a conference game yet.”

Using the API output, print each extracted entity and its type.

Caching Strategies#

When you are making a lot of requests to an API, it is a good idea to cache the results. We don’t want to make the same request over and over again if we don’t have to as this can effect our rate limits and thus our pricing.

Caching can be done in a number of ways. The simplest method is a Python dictionary where the key is the request and the value is the response. You can use Python pickle library to save the dictionary to disk so that you can use it across multiple sessions.

The caching strategy looks like this:

1. Check if the request is in the cache
2. If it is, we have a CACHE hit:
3.    return the response from the ca
4. else (cache miss)
5. Call the API to get the response

Let’s do an example with the Google Geocoding API on the IoT Portal.

 1import requests
 2import requests_cache as rq
 3
 4apikey = 'YOURAPIKEYHERE'
 5pickle_file = 'geocode_cache.pkl'
 6location = 'Syracuse, NY'
 7cache_key = location.lower()
 8cache = rq.clear_cache(pickle_file)
 9for i in range(3):
10    if cache_key in cache.keys():
11        print('Using cache')
12        geo = cache[cache_key]
13
14    else:
15        print('Making request')
16        url = 'https://cent.ischool-iot.net/api/google/geocode'
17        headers = { 'X-API-KEY': apikey }
18        params = {'location': location}
19        response = requests.get(url, params=params, headers=headers)
20        response.raise_for_status()
21        geo = response.json()
22        cache[cache_key] = geo
23        rq.save_cache(cache, pickle_file)
24
25    print(geo['results'][0]['geometry']['location'])
Making request
{'lat': 43.0481221, 'lng': -76.14742439999999}
Using cache
{'lat': 43.0481221, 'lng': -76.14742439999999}
Using cache
{'lat': 43.0481221, 'lng': -76.14742439999999}

Challenge 4-2-2#

Come up with a caching strategy for the Azure sentiment API. Use the requests_cache.py module. You will need to decided on the cache key.

For the following input:

for each text in the list:
output the text, the sentiment, and if the results came from the API or cache.

texts = [
    "I love IST356. It is the best course I've ever taken.", 
    "I hate the New York Giants.",
    "I love IST356. It is the best course I've ever taken.", 
    "I don't like the New York Giants."
]