Using Web Services: Python For Everybody
Using Web Services: Python For Everybody
Chapter 13
De-Serialize
{
Python "name" : "Chuck", Java
"phone" : "303-4456"
Dictionary }
HashMap
Serialize
JSON
XML
Marking up data to send across the network...
http://en.wikipedia.org/wiki/XML
XML “Elements” (or Nodes)
<people>
<person>
<name>Chuck</name>
<phone>303 4456</phone>
• Simple Element </person>
<person>
• Complex Element
<name>Noah</name>
<phone>622 7421</phone>
</person>
</people>
eXtensible Markup Language
• Primary purpose is to help information systems share structured
data
http://en.wikipedia.org/wiki/XML
XML Basics
• Start Tag <person>
<name>Chuck</name>
• End Tag
<phone type="intl">
• Text Content +1 734 303 4456
</phone>
• Attribute
<email hide="yes" />
• Self Closing Tag </person>
White Space
<person> Line ends do not matter.
<name>Chuck</name>
White space is generally
<phone type="intl">
+1 734 303 4456
discarded on text elements.
</phone> We indent only to be
<email hide="yes" /> readable.
</person>
<person>
<name>Chuck</name>
<phone type="intl">+1 734 303 4456</phone>
<email hide="yes" />
</person>
XML Terminology
• Tags indicate the beginning and ending of elements
• Attributes - Keyword/value pairs on the opening tag of XML
• Serialize / De-Serialize - Convert data in one program into a
common format that can be stored and/or transmitted between
systems in a programming language-independent manner
http://en.wikipedia.org/wiki/Serialization
XML as a Tree
a
<a>
<b>X</b>
<c>
b c
<d>Y</d>
<e>Z</e>
</c> X d e
</a>
Elements Text Y Z
XML Text and Attributes
a
<a>
<b w="5">X</b>
<c> w
b text
c
<d>Y</d> attrib node
<e>Z</e>
</c> 5 X d e
</a>
Elements Text Y Z
XML as Paths a
<a>
<b>X</b>
b c
<c> /a/b X
<d>Y</d> /a/c/d Y
<e>Z</e> /a/c/e Z X d e
</c>
</a>
Y Z
Elements Text
XML Schema
Describing a “contract” as to what is acceptable XML
http://en.wikipedia.org/wiki/Xml_schema
http://en.wikibooks.org/wiki/XML_Schema
XML Schema
• Description of the legal format of an XML document
XML Schema
Validator
Contract
XML Document XML Validation
<person>
<lastname>Severance</lastname>
<age>17</age>
<dateborn>2001-04-17</dateborn>
</person>
- http://en.wikipedia.org/wiki/Document_Type_Definition
- http://en.wikipedia.org/wiki/SGML
- http://en.wikipedia.org/wiki/XML_Schema_(W3C)
http://en.wikipedia.org/wiki/Xml_schema
XSD XML Schema (W3C spec)
• We will focus on the World Wide Web Consortium (W3C) version
http://www.w3schools.com/Schema/schema_complex_indicators.asp
<xs:element name="customer" type="xs:string"/>
XSD Data
<xs:element name="start" type="xs:date"/>
<xs:element name="startdate" type="xs:dateTime"/>
Types
<xs:element name="prize" type="xs:decimal"/>
<xs:element name="weeks" type="xs:integer"/>
<customer>John Smith</customer>
It is common to represent
<start>2002-09-24</start>
time in UTC/GMT, given
that servers are often <startdate>2002-05-30T09:30:10Z</startdate>
scattered around the world <prize>999.50</prize>
<weeks>30</weeks>
http://www.w3schools.com/Schema/schema_dtypes_numeric.asp
ISO 8601 Date/Time Format
2002-05-30T09:30:10Z
Time of Timezone - typically
Year-month-day specified in UTC / GMT
day
rather than local time
zone
http://en.wikipedia.org/wiki/ISO_8601
http://en.wikipedia.org/wiki/Coordinated_Universal_Time
http://www.w3schools.com/Schema/schema_example.asp
xml1.py
import xml.etree.ElementTree as ET
data = '''<person>
<name>Chuck</name>
<phone type="intl">
+1 734 303 4456
</phone>
<email hide="yes"/>
</person>'''
tree = ET.fromstring(data)
print('Name:',tree.find('name').text)
print('Attr:',tree.find('email').get('hide'))
import xml.etree.ElementTree as ET
xml2.py
input = '''<stuff>
<users>
<user x="2">
<id>001</id>
<name>Chuck</name>
</user>
<user x="7">
<id>009</id>
<name>Brent</name>
</user>
</users>
</stuff>'''
stuff = ET.fromstring(input)
lst = stuff.findall('users/user')
print('User count:', len(lst))
for item in lst:
print('Name', item.find('name').text)
print('Id', item.find('id').text)
print('Attribute', item.get("x"))
JavaScript Object Notation
JavaScript Object Notation
• Douglas Crockford -
“Discovered” JSON
http://www.youtube.com/watch?v=kc8BAR7SHJI
import json json1.py
data = '''{
"name" : "Chuck",
"phone" : {
"type" : "intl",
"number" : "+1 734 303 4456" JSON represents data
}, as nested “lists” and
"email" : { “dictionaries”
"hide" : "yes"
}
}'''
info = json.loads(data)
print('Name:',info["name"])
print('Hide:',info["email"]["hide"])
import json json2.py
input = '''[
{ "id" : "001",
"x" : "2",
"name" : "Chuck"
} ,
{ "id" : "009", JSON represents data
"x" : "7",
"name" : "Chuck" as nested “lists” and
} “dictionaries”
]'''
info = json.loads(input)
print('User count:', len(info))
for item in info:
print('Name', item['name'])
print('Id', item['id'])
print('Attribute', item['x'])
Service Oriented Approach
http://en.wikipedia.org/wiki/Service-oriented_architecture
Service Oriented Approach
• Most non-trivial web applications use services Application
http://www.youtube.com/watch?v=mj-kCFzF0ME 5:15
Web Services
http://en.wikipedia.org/wiki/Web_services
Application Program Interface
The API itself is largely abstract in that it specifies an
interface and controls the behavior of the objects specified
in that interface. The software that provides the functionality
described by an API is said to be an “implementation” of the
API. An API is typically defined in terms of the programming
language used to build an application.
http://en.wikipedia.org/wiki/API
https://developers.google.com/maps/documentation/geocoding/
{
"status": "OK",
"results": [
{
"geometry": {
"location_type": "APPROXIMATE",
"location": {
"lat": 42.2808256,
"lng": -83.7430378 http://maps.googleapis.com/maps/api/geocode/json?
} address=Ann+Arbor%2C+MI
},
"address_components": [
{
"long_name": "Ann Arbor",
"types": [
"locality",
"political"
],
"short_name": "Ann Arbor"
}
],
"formatted_address": "Ann Arbor, MI, USA",
"types": [
"locality",
"political"
] geojson.py
}
]
}
import urllib.request, urllib.parse, urllib.error
import json
serviceurl = 'http://maps.googleapis.com/maps/api/geocode/json?'
while True:
address = input('Enter location: ')
if len(address) < 1: break
lat = js["results"][0]["geometry"]["location"]["lat"]
lng = js["results"][0]["geometry"]["location"]["lng"] geojson.py
print('lat', lat, 'lng', lng)
location = js['results'][0]['formatted_address']
print(location)
API Security and Rate Limiting
• The compute resources to run these APIs are not “free”
• The data providers might limit the number of requests per day,
demand an API “key”, or even charge for usage
while True:
print('')
acct = input('Enter Twitter Account:')
if (len(acct) < 1): break
url = twurl.augment(TWITTER_URL,
{'screen_name': acct, 'count': '5'})
print('Retrieving', url)
connection = urllib.request.urlopen(url)
data = connection.read().decode()
headers = dict(connection.getheaders())
print('Remaining', headers['x-rate-limit-remaining'])
js = json.loads(data)
print(json.dumps(js, indent=4))
for u in js['users']:
print(u['screen_name'])
s = u['status']['text']
print(' ', s[:50])
Enter Twitter Account:drchuck
Retrieving https://api.twitter.com/1.1/friends ...
Remaining 14 twitter2.py
{
"users": [
{
"status": {
"text": "@jazzychad I just bought one .__.",
"created_at": "Fri Sep 20 08:36:34 +0000 2013",
},
"location": "San Francisco, California",
"screen_name": "leahculver",
"name": "Leah Culver",
},
{
"status": {
"text": "RT @WSJ: Big employers like Google ...",
"created_at": "Sat Sep 28 19:36:37 +0000 2013",
},
"location": "Victoria Canada",
"screen_name": "_valeriei",
"name": "Valerie Irvine",
],
}
Leahculver
@jazzychad I just bought one .__._
Valeriei
RT @WSJ: Big employers like Google, AT&T are h
Ericbollens
RT @lukew: sneak peek: my LONG take on the good &a
halherzog
Learning Objects is 10. We had a cake with the LO,
def oauth() : hidden.py
return { "consumer_key" : "h7Lu...Ng",
"consumer_secret" : "dNKenAC3New...mmn7Q",
"token_key" : "10185562-ein2...P4GEQQOSGI",
"token_secret" : "H0ycCFemmwyf1...qoIpBo" }
import urllib
import oauth
twurl.py
import hidden
https://api.twitter.com/1.1/statuses/user_timeline.json?
count=2&oauth_version=1.0&oauth_token=101...SGI&screen_name=drc
huck&oauth_nonce=09239679&oauth_timestamp=1380395644&oauth_sign
ature=rLK...BoD&oauth_consumer_key=h7Lu...GNg&oauth_signature_m
ethod=HMAC-SHA1
Summary
• Service Oriented Architecture - allows an application to be broken
into parts and distributed across a network