... | @@ -10,6 +10,8 @@ apis |
... | @@ -10,6 +10,8 @@ apis |
|
|
|
|
|
# step 0: consuming apis
|
|
# step 0: consuming apis
|
|
|
|
|
|
|
|
_note that step-0 has been updated to reflect changes in urllib in python 3_
|
|
|
|
|
|
## using the OpenWeatherMap api
|
|
## using the OpenWeatherMap api
|
|
|
|
|
|
* to use the open weather map, sign up at http://openweathermap.org/appid
|
|
* to use the open weather map, sign up at http://openweathermap.org/appid
|
... | @@ -26,13 +28,17 @@ apis |
... | @@ -26,13 +28,17 @@ apis |
|
url = "http://api.openweathermap.org/data/2.5/weather?q=London%2C%20UK&units=metric&appid=<YOUR API KEY HERE>"
|
|
url = "http://api.openweathermap.org/data/2.5/weather?q=London%2C%20UK&units=metric&appid=<YOUR API KEY HERE>"
|
|
|
|
|
|
* note the location "London, UK" is url encoded in the url string
|
|
* note the location "London, UK" is url encoded in the url string
|
|
* import the urllib2 library so you can request the url
|
|
* import urlopen from urllib.request so you can request the url (see https://docs.python.org/dev/tutorial/stdlib.html#internet-access)
|
|
|
|
|
|
import urllib2
|
|
from urllib.request import urlopen
|
|
|
|
|
|
* read the data returned by the url
|
|
* read the data returned by the url
|
|
|
|
|
|
data = urllib2.urlopen(url).read()
|
|
data = urlopen(url).read()
|
|
|
|
|
|
|
|
* note that python is making an http request so the data is returned as binary data; to use it we need to decode it according to the charset returned by the server (which will usually be utf-8)
|
|
|
|
|
|
|
|
data = data.decode(data.headers.get_content_charset())
|
|
|
|
|
|
## parsing the data
|
|
## parsing the data
|
|
|
|
|
... | @@ -49,7 +55,7 @@ apis |
... | @@ -49,7 +55,7 @@ apis |
|
|
|
|
|
* we want to check the structure of this data, so we can 'pretty print' the json data using json.dumps
|
|
* we want to check the structure of this data, so we can 'pretty print' the json data using json.dumps
|
|
|
|
|
|
print json.dumps(weather, indent=4, sort_keys=True)
|
|
print(json.dumps(weather, indent=4, sort_keys=True))
|
|
|
|
|
|
## accesssing the data
|
|
## accesssing the data
|
|
|
|
|
... | @@ -81,11 +87,9 @@ apis |
... | @@ -81,11 +87,9 @@ apis |
|
* remember from step 0 that the most common format for returning api data is json
|
|
* remember from step 0 that the most common format for returning api data is json
|
|
* we will create a simple api for our mytwits flask app using a flask function called jsonify http://flask.pocoo.org/docs/0.12/api/#flask.json.jsonify
|
|
* we will create a simple api for our mytwits flask app using a flask function called jsonify http://flask.pocoo.org/docs/0.12/api/#flask.json.jsonify
|
|
* this will directly return your json structure to the browser
|
|
* this will directly return your json structure to the browser
|
|
* you will need to import jsonify at the top of your app i.e.
|
|
* to use jsonify you will need to import it i.e.
|
|
|
|
|
|
from flask import jsonify
|
|
from flask import jsonify
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## db -> api
|
|
## db -> api
|
|
|
|
|
... | @@ -128,7 +132,7 @@ apis |
... | @@ -128,7 +132,7 @@ apis |
|
|
|
|
|
|
|
|
|
|
|
|
|
# using flask-restful
|
|
# step 2: using flask-restful
|
|
|
|
|
|
* now we'll create a fulled-specced api using the flask-restful extension https://flask-restful.readthedocs.io/en/latest/
|
|
* now we'll create a fulled-specced api using the flask-restful extension https://flask-restful.readthedocs.io/en/latest/
|
|
* this requires a bit more set up but rewards us with more powerful functionality that handles the full range of RESTful HTTP methods
|
|
* this requires a bit more set up but rewards us with more powerful functionality that handles the full range of RESTful HTTP methods
|
... | @@ -162,6 +166,33 @@ apis |
... | @@ -162,6 +166,33 @@ apis |
|
|
|
|
|
api.add_resource(TwitsApi,'/api')
|
|
api.add_resource(TwitsApi,'/api')
|
|
|
|
|
|
|
|
## no jsonify, just dictionaries
|
|
|
|
|
|
|
|
flask-restful will deal with converting the data to json and adding the correct headers, so we don't need to use jsonify.
|
|
|
|
|
|
|
|
however, to take advantage of flask-restful we need to pass it our data in the form of dictionaries. in our previous use of pymysql in mytwits_mysql.py we used the default cursor which returns the results as tuples. however, we can force pymysql to return the sql results as a python dictionary using pymysql.cursors.DictCursor (see the bottom of the pymysql api ref on cursors https://pymysql.readthedocs.io/en/latest/modules/cursors.html).
|
|
|
|
|
|
|
|
so for queries that we want to pass back to the api we replace
|
|
|
|
|
|
|
|
with self.db.cursor() as cursor:
|
|
|
|
|
|
|
|
with
|
|
|
|
|
|
|
|
with self.db.cursor(pymysql.cursors.DictCursor) as cursor:
|
|
|
|
|
|
|
|
(
|
|
|
|
Note that this also means tweaking our jinja templates for the html output, where we will need to replace
|
|
|
|
|
|
|
|
<div class="col-md-4">{{ twit[0] }}</div>
|
|
|
|
|
|
|
|
|
|
|
|
with
|
|
|
|
|
|
|
|
<div class="col-md-4">{{ twit['username'] }}</div>
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## data formatting and argument parsing
|
|
## data formatting and argument parsing
|
|
|
|
|
... | | ... | |