Lesson 2: Accessing Published APIs
HTTP
HTTP Requests
header:
- request line (HTTP verb, URI, HTTP version number)
- optional request headers
blank line
body (optional)
Sending API Requests with Postman and Curl
Check out the official cURL documentation and this blog post for getting started with cURL.
Check out the Postman Documentation as well.
Google Geocoding API
import httplib2
import json
def getGeocodeLocation(inputString):
google_api_key = "PASTE_YOUR_KEY_HERE"
locationString = inputString.replace(" ", "+")
url = ('https://maps.googleapis.com/maps/api/ \
geocode/json?address=%s&key=%s'% (locationString,
google_api_key))
h = httplib2.Http()
response, content = h.request(url, 'GET')
result = json.loads(content)
#print response
latitude = result['results'][0]['g''eometry']['location']['lat']
longitude = result['results'][0]['geometry']['location']['lng']
return (latitude, longitude)
API Mashup
findARestaurant(mealType, location)
- Geocode the location
- Search for restaurants
- Parse response and return one restaurant
Download the starter code for this exercise.
View the solution code for this exercise.