SlideShare a Scribd company logo
1 of 60
Using Web Services
Data on the Web
• With the HTTP Request/Response well understood and well
supported, there was a natural move toward exchanging data
between programs using these protocols
• We needed to come up with an agreed way to represent data
going between applications and across networks
• There are two commonly used formats: “eXtensible Markup
Language” (XML) and “JavaScript Object Notation “(JSON)
Sending Data across the “Net”
Python
Dictionary
Java
HashMap
a.k.a. “Wire Protocol” - What we send on the “wire”
Agreeing on a “Wire Format”
Python
Dictionary
Java
HashMap
Serialize
<person>
<name>
Chuck
</name>
<phone>
303 4456
</phone>
</person>
De-Serialize
XML
Agreeing on a “Wire Format”
Python
Dictionary
Java
HashMap
Serialize
{
"name" :
"Chuck",
"phone" :
"303-4456"
}
De-Serialize
JSON
XML “Elements” (or Nodes)
• Simple Element
• Complex Element
<people>
<person>
<name>Chuck</name>
<phone>303 4456</phone>
</person>
<person>
<name>Noah</name>
<phone>622 7421</phone>
</person>
</people>
XML
Marking up data to send across the network...
eXtensible Markup Language
• XML is a simple tag-based language for describing
information in a structured way
• Primary purpose is to help information systems share structured
data
• It started as a simplified subset of the Standard Generalized
Markup Language (SGML), and is designed to be relatively
human-legible
• XML looks very similar to HTML, but XML is more structured than
HTML.
XML Basics – Sample XML Document
• Start Tag
• End Tag
• Text Content
• Attribute
• Self Closing Tag
<person>
<name>Chuck</name>
<phone type=”intl”>
+1 734 303 4456
</phone>
<email hide=”yes” />
</person>
White Space
<person>
<name>Chuck</name>
<phone type=”intl”>
+1 734 303 4456
</phone>
<email hide=”yes” />
</person>
<person>
<name>Chuck</name>
<phone type=”intl”>+1 734 303
4456</phone>
<email hide=”yes” />
</person>
Line ends do not matter. White
space is generally discarded on
text elements. We indent only to
be readable.
Some XML...
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
XML as a Tree
<a>
<b>X</b>
<c>
<d>Y</d>
<e>Z</e>
</c>
</a>
a
b c
X d e
Y Z
Elements Text
XML Text and Attributes
<a>
<b w=”5”>X</b>
<c>
<d>Y</d>
<e>Z</e>
</c>
</a>
a
b c
X d e
Y Z
Elements Text
5
w
attrib
text
node
XML as Paths
<a>
<b>X</b>
<c>
<d>Y</d>
<e>Z</e>
</c>
</a>
a
b c
X d e
Y Z
Elements Text
/a/b X
/a/c/d Y
/a/c/e Z
XML as a Tree
<person>
<name>Chuck</name>
<phone type=”intl”>
+1 734 303 4456
</phone>
<email hide=”yes” />
</person>
Parsing XML
Simple application that parses some XML and extracts some data
elements from the XML
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'))
XML parser :
ElementTree
Looping through nodes
import xml.etree.ElementTree as ET
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")
XML Schema
Describing a “contract” as to what is acceptable XML.
XML Schema
• Description of the legal format of an XML document
• Expressed in terms of constraints on the structure and content of
documents
• Often used to specify a “contract” between systems - “My
system will only accept XML that conforms to this particular
Schema.”
• If a particular piece of XML meets the specification of the
Schema - it is said to “validate”
Validator
XML Schema Contract
XML Document
XML Validation
Validator
<person>
<lastname>Severance</lastname>
<age>17</age>
<dateborn>2001-04-17</dateborn>
</person>
<xs:complexType name=”person”>
<xs:sequence>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
<xs:element name="dateborn" type="xs:date"/>
</xs:sequence>
</xs:complexType>
XML Schema Contract
XML Document
XML Validation
Many XML Schema Languages
• Document Type Definition (DTD)
http://en.wikipedia.org/wiki/Document_Type_Definition
• Standard Generalized Markup Language (ISO 8879:1986
SGML)
http://en.wikipedia.org/wiki/SGML
• XML Schema from W3C - (XSD)
• http://en.wikipedia.org/wiki/XML_Schema_(W3C)
XSD XML Schema (W3C spec)
• We will focus on the World Wide Web Consortium (W3C) version
• It is often called “W3C Schema” because “Schema” is considered
generic
• More commonly it is called XSD because the file names end in .xsd
XSD
Structure
• xs:element
• xs:sequence
• xs:complexType
<xs:complexType name=”person”>
<xs:sequence>
<xs:element name="lastname" type="xs:string"/
<xs:element name="age" type="xs:integer"/>
<xs:element name="dateborn" type="xs:date"/>
</xs:sequence>
</xs:complexType>
<person>
<lastname>Severance</lastname>
<age>17</age>
<dateborn>2001-04-17</dateborn>
</person>
XSD
Constraints
http://www.w3schools.com/Schema/schema_complex_indicators.asp
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="full_name" type="xs:string"
minOccurs="1" maxOccurs="1" />
<xs:element name="child_name" type="xs:string"
minOccurs="0" maxOccurs="10" />
</xs:sequence>
</xs:complexType>
</xs:element>
<person>
<full_name>Tove Refsnes</full_name
<child_name>Hege</child_name>
<child_name>Stale</child_name>
<child_name>Jim</child_name>
<child_name>Borge</child_name>
</person>
XSD Data
Types
http://www.w3schools.com/Schema/schema_dtypes_numeric.asp
<xs:element name="customer" type="xs:string"/>
<xs:element name="start" type="xs:date"/>
<xs:element name="startdate"
type="xs:dateTime"/>
<xs:element name="prize" type="xs:decimal"/>
<xs:element name="weeks" type="xs:integer"/>
<customer>John Smith</customer>
<start>2002-09-24</start>
<startdate>2002-05-30T09:30:10Z</startdate>
<prize>999.50</prize>
<weeks>30</weeks>
It is common to represent
time in UTC/GMT given that
servers are often scattered
around the world.
JavaScript Object Notation
(JSON)
JSON
• Used to format data, Text-based, Light-weight, Easy to parse, Subset of JavaScript
• Commonly used in Web as a vehicle to describe data being sent between systems
• Despite the name, JSON is a (mostly) language-independent way of specifying
objects as name-value pairs
{"skills": {
"web":[
{ "name": "html",
"years": 5
},
{ "name": "css",
"years": 3
}]
"database":[
{ "name": "sql",
"years": 7
}]
}}
JSON syntax
• JSON is built on TWO Structures
• An object is an unordered set of name/value pairs
- The pairs are enclosed within braces, { }
- There is a colon between the name and the value
- Pairs are separated by commas
- Example: { "name": "html", "years": 5 }
• An array is an ordered collection of values
- The values are enclosed within brackets, [ ]
- Values are separated by commas
- Example: [ "html", ”xml", "css" ]
• A value can be: A string, a number, true, false, null, an object, or an array
• Values can be nested
• Strings are enclosed in double quotes, and can contain the usual assortment of escaped characters
• Numbers have the usual C/C++/Java syntax, All numbers are decimal--no octal or hexadecimal
• Whitespace can be used between any pair of tokens
JSON Is Not...
• JSON is not a document format.
• JSON is not a markup language.
• JSON is not a general serialization format.
• No cyclical/recurring structures.
• No invisible structures.
• No functions.
import json
data = '''{
"name" : "Chuck",
"phone" : {
"type" : "intl",
"number" : "+1 734 303 4456"
},
"email" : {
"hide" : "yes"
}
}'''
info = json.loads(data)
print 'Name:',info["name"]
print 'Hide:',info["email"]["hide"]
json1.py
JSON represents data
as nested "lists" and
"dictionaries"
import json
input = '''[
{ "id" : "001",
"x" : "2",
"name" : "Chuck"
} ,
{ "id" : "009",
"x" : "7",
"name" : "Chuck"
}
]'''
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']
json2.py
JSON represents data
as nested "lists" and
"dictionaries"
Comparison of JSON and XML
Similarities:
• Both are human readable
• Both have very simple syntax
• Both are hierarchical
• Both are language independent
• Both can be used by Ajax
• Both supported in APIs of many
programming languages
Differences:
• Syntax is different
• JSON is less verbose
• JSON can be parsed by JavaScript’s
eval method
• JSON includes arrays
• Names in JSON must not be JavaScript
reserved words
• XML can be validated
• In general, JSON structures are simpler than XML because JSON has fewer capabilities than XML.
• JSON has the advantage that it maps directly to some combination of dictionaries and lists.
• Since nearly all programming languages have something equivalent to Python’s dictionaries and lists, JSON is a very
natural format to have two cooperating programs exchange data.
• JSON is quickly becoming the format of choice for nearly all data exchange between applications because of its
relative simplicity compared to XML
Service Oriented Approach
Service Oriented Approach
• Most non-trivial web applications use
services
• They use services from other applications
• Credit Card Charge
• Hotel Reservation systems
• Services publish the "rules" applications
must follow to make use of the service (API)
Application
APIs
Service
Service
Auto Hotel Airline
Rental Reservation Reservation
Service Service Service
API
Travel
Application
Figure: Service Oriented Architecture
Service Oriented Architecture
Service Oriented Architectures
Technologies capable of:
•Exchanging messages
•Describing Web services
•Publishing and discovering Web
service descriptions
Component
Role
Operation
service
requestor
retrieves a
service
description
A service publishes its
description
One-way,
conversational,
many-to-many
Web Services
Web Services - Definition from W3C
“ A Web service is a software system identified by a URI, whose
public interfaces and bindings are defined and described using XML.
Its definition can be discovered by other software systems. These
systems may then interact with the Web service in a manner
prescribed by its definition, using XML based messages conveyed
by internet protocols.”
PO Service
Credit
Service
Purchase
Order
Invoice
Consolidate
Results
Example: Web based purchase
What are Web Services
Web services are self contained, self describing, modular applications that can be published, located,
and invoked across the web. Web services perform functions, which can be anything from simple
requests to complicated business processes". ---- According to IBM
- Runs on a Web server
Web Service is based on:
• HTTP (Hypertext Transport
Protocol)
• SOAP (Simple Object Access
Protocol)
• UDDI (Universal Description,
Discovery and Integration)
• WS-POLICY (Web Services Policy)
Sample Web Service
Calc.asmx
<%@ WebService Language="C#" CodeBehind="~/App_Code/WebService.cs" Class="WebService" %>
using System;
using System.Web.Services;
[WebService (Name="Calculator Web Service",
Description = "Perform simple math over the Web")]
class CalcService
{
[WebMethod (Description = "Computes the sum of two integers")]
public int Add (int a, int b) { return a+b;}
[WebMethod (Description = "Computes the difference between two integers")]
public int Subtract (int a, int b) { return a-b;}
}
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.
Web Service Technologies
• SOAP - Simple Object Access Protocol (software)
• Remote programs/code which we use over the network
Note: Dr. Chuck does not like SOAP because it is overly complex
• REST - Representational State Transfer (resource focused)
• Remote resources which we create, read, update and delete
remotely
Simple Object Access Protocol
• SOAP is a technology to support the exchange of XML-coded
messages over a transport protocol, such as HTTP and SMTP.
– SOAP provides a simple and lightweight mechanism for exchanging structured and typed
information between peers in a decentralized, distributed environment using XML. Contains
three elements: <Envelope>, <Header>, and <Body>.
HTTP server
Skeleton
Server
HTTP client
Stub
Client
XML XML
POST…
HTTP 1.1 200 OK
SOAP SOAP
HTTP
SOAP
XML
Protocols Folding
SOAP basic mechanism
Simple Object Access Protocol
A SOAP runtime engine basically adds a XML envelope to an existing
XML document
SOAP Envelope
XML
Document
<soap:Envelope>
<soap:Header>
<axis:SessionKey>
SDHH37TYEW7R7
</axis:SessionKey>
</soap:Header>
<soap:Body>
<GetPrice>
<Item>Apples</Item>
</GetPrice>
</soap:Body>
</soap:Envelope>
SOAP Body
SOAP Header
Communicatio
n Info
Example
Document Container Session, Authentication,
Routing, Security
What is REST ? Representational State
Transfer
• Coined by Roy Fielding to describe an architecture style of networked
systems.
• Underlying Architectural model of the world wide web.
• Guiding framework for Web protocol standards.
• Elements
– Components – Proxy , gateway etc
– Connectors – client , server etc
– Data – resource , representation etc
• Ignores component implementation details.
• Focus on roles of components, their interactions and their
interpretation of data elements.
REST based web services
• Online shopping
• Search services
• Dictionary services
https://developers.google.com/maps/documentation/geocoding/
{
"status": "OK",
"results": [
{
"geometry": {
"location_type": "APPROXIMATE",
"location": {
"lat": 42.2808256,
"lng": -83.7430378
}
},
"address_components": [
{
"long_name": "Ann Arbor",
"types": [
"locality",
"political"
],
"short_name": "Ann Arbor"
}
],
"formatted_address": "Ann Arbor, MI, USA",
"types": [
"locality",
"political"
]
}
]
}
geojson.py
http://maps.googleapis.com/maps/api/g
eocode/json?sensor=false&address=A
nn+Arbor%2C+MI
import urllib
import json
serviceurl = 'http://maps.googleapis.com/maps/api/geocode/json?'
while True:
address = raw_input('Enter location: ')
if len(address) < 1 : break
url = serviceurl + urllib.urlencode({'sensor':'false',
'address': address})
print 'Retrieving', url
uh = urllib.urlopen(url)
data = uh.read()
print 'Retrieved',len(data),'characters'
try: js = json.loads(str(data))
except: js = None
if 'status' not in js or js['status'] != 'OK':
print '==== Failure To Retrieve ===='
print data
continue
print json.dumps(js, indent=4)
lat = js["results"][0]["geometry"]["location"]["lat"]
lng = js["results"][0]["geometry"]["location"]["lng"]
print 'lat',lat,'lng',lng
location = js['results'][0]['formatted_address']
print location
geojson.py
Enter location: Ann Arbor, MI
Retrieving
http://maps.googleapis.com/maps/api/geocode
/json?sensor=false&address=Ann+Arbor%2C+MI
Retrieved 1669 characters
lat 42.2808256 lng -83.7430378
Ann Arbor, MI, USA
Enter location:
API Security and Rate Limiting
• The compute resources to run these APIs are not "free"
• The data provided by these APIs is usually valuable
• The data providers might limit the number of requests per day,
demand an API "key" or even charge for usage
• They might change the rules as things progress...
import urllib
import twurl
TWITTER_URL = 'https://api.twitter.com/1.1/statuses/user_timeline.json'
while True:
print ''
acct = raw_input('Enter Twitter Account:')
if ( len(acct) < 1 ) : break
url = twurl.augment(TWITTER_URL,
{'screen_name': acct, 'count': '2'} )
print 'Retrieving', url
connection = urllib.urlopen(url)
data = connection.read()
print data[:250]
headers = connection.info().dict
# print headers
print 'Remaining', headers['x-rate-limit-remaining']
twitter1.py
Enter Twitter Account:drchuck
Retrieving
https://api.twitter.com/1.1/statuses/user_timeline.json?count=2&oauth_version=
1.0&oauth_token=10185562-
eibxCp9n2OizWNT1VrmsDoK40g3feclP4GEQQOSGI&screen_name=drchuck&oauth_nonce=4188
7363&oauth_timestamp=1414535902&oauth_signature=C9C4p0weq7ayRxTOh1fsqszl4fY%3D
&oauth_consumer_key=I8cTTYmdplNsnJCiSZWQ&oauth_signature_method=HMAC-SHA1
[{"created_at":"Tue Oct 28 21:46:48 +0000
2014","id":527214927829037057,"id_str":"527214927829037057","text":"RT
@ImRegularJohn: Madly in love with @coursera. Graduated in IHTS with @drchuck,
now @philscimooc, next Astronomy at @DukeU &amp; Sustainab
Remaining 177
Enter Twitter Account:injenuity
Retrieving
https://api.twitter.com/1.1/statuses/user_timeline.json?count=2&oauth_version=
1.0&oauth_token=10185562-
eibxCp9n2OizWNT1VrmsDoK40g3feclP4GEQQOSGI&screen_name=injenuity&oauth_nonce=27
065523&oauth_timestamp=1414535967&oauth_signature=5ETRfBJubkvJ4LnP1kZIHGwRiSk%
3D&oauth_consumer_key=I8cTTYmdplNsnJCiSZWQ&oauth_signature_method=HMAC-SHA1
[{"created_at":"Tue Oct 28 20:57:44 +0000
2014","id":527202579701194752,"id_str":"527202579701194752","text":"@dlnorman
I wasn't even thinking about a camera. More that I'll be talking
funny.","source":"u003ca href="http://twitter.com" rel="nof
Remaining 176
twitter1.py
import urllib
import twurl
import json
TWITTER_URL = 'https://api.twitter.com/1.1/friends/list.json'
while True:
print ''
acct = raw_input('Enter Twitter Account:')
if ( len(acct) < 1 ) : break
url = twurl.augment(TWITTER_URL,
{'screen_name': acct, 'count': '5'} )
print 'Retrieving', url
connection = urllib.urlopen(url)
data = connection.read()
headers = connection.info().dict
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]
twitter2.py
Enter Twitter Account:drchuck
Retrieving https://api.twitter.com/1.1/friends ...
Remaining 14
{
"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&amp;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,
twitter2.py
Summary
• Service Oriented Architecture - allows an application to be
broken into parts and distributed across a network
• An Application Program Interface (API) is a contract for
interaction
• Web Services provide infrastructure for applications cooperating
(an API) over a network - SOAP and REST are two styles of web
services
• XML and JSON are serialization formats

More Related Content

Similar to GTR-Python-Using Web Services notesgtr.ppt

01_XMLDataModel.pptx
01_XMLDataModel.pptx01_XMLDataModel.pptx
01_XMLDataModel.pptxPrerak10
 
Working with XML and JSON Serializing
Working with XML and JSON SerializingWorking with XML and JSON Serializing
Working with XML and JSON Serializingssusere19c741
 
xMLDataModel.pdf
xMLDataModel.pdfxMLDataModel.pdf
xMLDataModel.pdfPrerak10
 
Web Service Workshop - 3 days
Web Service Workshop - 3 daysWeb Service Workshop - 3 days
Web Service Workshop - 3 daysDavid Ionut
 
[3.3] Detection & exploitation of Xpath/Xquery Injections - Boris Savkov
[3.3] Detection & exploitation of Xpath/Xquery Injections - Boris Savkov[3.3] Detection & exploitation of Xpath/Xquery Injections - Boris Savkov
[3.3] Detection & exploitation of Xpath/Xquery Injections - Boris SavkovOWASP Russia
 
javascript.pptx
javascript.pptxjavascript.pptx
javascript.pptxhasiny2
 
json.ppt download for free for college project
json.ppt download for free for college projectjson.ppt download for free for college project
json.ppt download for free for college projectAmitSharma397241
 
JSON & AJAX.pptx
JSON & AJAX.pptxJSON & AJAX.pptx
JSON & AJAX.pptxdyumna2
 
Contacto server API in PHP
Contacto server API in PHPContacto server API in PHP
Contacto server API in PHPHem Shrestha
 
Import web resources using R Studio
Import web resources using R StudioImport web resources using R Studio
Import web resources using R StudioRupak Roy
 
04 data accesstechnologies
04 data accesstechnologies04 data accesstechnologies
04 data accesstechnologiesBat Programmer
 
Xml and databases
Xml and databasesXml and databases
Xml and databasesRaghu nath
 
Xml And JSON Java
Xml And JSON JavaXml And JSON Java
Xml And JSON JavaHenry Addo
 

Similar to GTR-Python-Using Web Services notesgtr.ppt (20)

01_XMLDataModel.pptx
01_XMLDataModel.pptx01_XMLDataModel.pptx
01_XMLDataModel.pptx
 
Working with XML and JSON Serializing
Working with XML and JSON SerializingWorking with XML and JSON Serializing
Working with XML and JSON Serializing
 
xMLDataModel.pdf
xMLDataModel.pdfxMLDataModel.pdf
xMLDataModel.pdf
 
Web Service Workshop - 3 days
Web Service Workshop - 3 daysWeb Service Workshop - 3 days
Web Service Workshop - 3 days
 
[3.3] Detection & exploitation of Xpath/Xquery Injections - Boris Savkov
[3.3] Detection & exploitation of Xpath/Xquery Injections - Boris Savkov[3.3] Detection & exploitation of Xpath/Xquery Injections - Boris Savkov
[3.3] Detection & exploitation of Xpath/Xquery Injections - Boris Savkov
 
javascript.pptx
javascript.pptxjavascript.pptx
javascript.pptx
 
Xml overview
Xml overviewXml overview
Xml overview
 
Cloud
CloudCloud
Cloud
 
2310 b 12
2310 b 122310 b 12
2310 b 12
 
json.ppt download for free for college project
json.ppt download for free for college projectjson.ppt download for free for college project
json.ppt download for free for college project
 
JSON & AJAX.pptx
JSON & AJAX.pptxJSON & AJAX.pptx
JSON & AJAX.pptx
 
Contacto server API in PHP
Contacto server API in PHPContacto server API in PHP
Contacto server API in PHP
 
Import web resources using R Studio
Import web resources using R StudioImport web resources using R Studio
Import web resources using R Studio
 
Basics of Xml
Basics of XmlBasics of Xml
Basics of Xml
 
Unit 10: XML and Beyond (Sematic Web, Web Services, ...)
Unit 10: XML and Beyond (Sematic Web, Web Services, ...)Unit 10: XML and Beyond (Sematic Web, Web Services, ...)
Unit 10: XML and Beyond (Sematic Web, Web Services, ...)
 
Json
JsonJson
Json
 
04 data accesstechnologies
04 data accesstechnologies04 data accesstechnologies
04 data accesstechnologies
 
Xml 2
Xml  2 Xml  2
Xml 2
 
Xml and databases
Xml and databasesXml and databases
Xml and databases
 
Xml And JSON Java
Xml And JSON JavaXml And JSON Java
Xml And JSON Java
 

Recently uploaded

ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 

Recently uploaded (20)

ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 

GTR-Python-Using Web Services notesgtr.ppt

  • 2. Data on the Web • With the HTTP Request/Response well understood and well supported, there was a natural move toward exchanging data between programs using these protocols • We needed to come up with an agreed way to represent data going between applications and across networks • There are two commonly used formats: “eXtensible Markup Language” (XML) and “JavaScript Object Notation “(JSON)
  • 3. Sending Data across the “Net” Python Dictionary Java HashMap a.k.a. “Wire Protocol” - What we send on the “wire”
  • 4. Agreeing on a “Wire Format” Python Dictionary Java HashMap Serialize <person> <name> Chuck </name> <phone> 303 4456 </phone> </person> De-Serialize XML
  • 5. Agreeing on a “Wire Format” Python Dictionary Java HashMap Serialize { "name" : "Chuck", "phone" : "303-4456" } De-Serialize JSON
  • 6. XML “Elements” (or Nodes) • Simple Element • Complex Element <people> <person> <name>Chuck</name> <phone>303 4456</phone> </person> <person> <name>Noah</name> <phone>622 7421</phone> </person> </people>
  • 7. XML Marking up data to send across the network...
  • 8. eXtensible Markup Language • XML is a simple tag-based language for describing information in a structured way • Primary purpose is to help information systems share structured data • It started as a simplified subset of the Standard Generalized Markup Language (SGML), and is designed to be relatively human-legible • XML looks very similar to HTML, but XML is more structured than HTML.
  • 9. XML Basics – Sample XML Document • Start Tag • End Tag • Text Content • Attribute • Self Closing Tag <person> <name>Chuck</name> <phone type=”intl”> +1 734 303 4456 </phone> <email hide=”yes” /> </person>
  • 10. White Space <person> <name>Chuck</name> <phone type=”intl”> +1 734 303 4456 </phone> <email hide=”yes” /> </person> <person> <name>Chuck</name> <phone type=”intl”>+1 734 303 4456</phone> <email hide=”yes” /> </person> Line ends do not matter. White space is generally discarded on text elements. We indent only to be readable.
  • 12. 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
  • 13. XML as a Tree <a> <b>X</b> <c> <d>Y</d> <e>Z</e> </c> </a> a b c X d e Y Z Elements Text
  • 14. XML Text and Attributes <a> <b w=”5”>X</b> <c> <d>Y</d> <e>Z</e> </c> </a> a b c X d e Y Z Elements Text 5 w attrib text node
  • 15. XML as Paths <a> <b>X</b> <c> <d>Y</d> <e>Z</e> </c> </a> a b c X d e Y Z Elements Text /a/b X /a/c/d Y /a/c/e Z
  • 16. XML as a Tree <person> <name>Chuck</name> <phone type=”intl”> +1 734 303 4456 </phone> <email hide=”yes” /> </person>
  • 17. Parsing XML Simple application that parses some XML and extracts some data elements from the XML 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')) XML parser : ElementTree
  • 18. Looping through nodes import xml.etree.ElementTree as ET 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")
  • 19. XML Schema Describing a “contract” as to what is acceptable XML.
  • 20. XML Schema • Description of the legal format of an XML document • Expressed in terms of constraints on the structure and content of documents • Often used to specify a “contract” between systems - “My system will only accept XML that conforms to this particular Schema.” • If a particular piece of XML meets the specification of the Schema - it is said to “validate”
  • 21. Validator XML Schema Contract XML Document XML Validation
  • 22. Validator <person> <lastname>Severance</lastname> <age>17</age> <dateborn>2001-04-17</dateborn> </person> <xs:complexType name=”person”> <xs:sequence> <xs:element name="lastname" type="xs:string"/> <xs:element name="age" type="xs:integer"/> <xs:element name="dateborn" type="xs:date"/> </xs:sequence> </xs:complexType> XML Schema Contract XML Document XML Validation
  • 23. Many XML Schema Languages • Document Type Definition (DTD) http://en.wikipedia.org/wiki/Document_Type_Definition • Standard Generalized Markup Language (ISO 8879:1986 SGML) http://en.wikipedia.org/wiki/SGML • XML Schema from W3C - (XSD) • http://en.wikipedia.org/wiki/XML_Schema_(W3C)
  • 24. XSD XML Schema (W3C spec) • We will focus on the World Wide Web Consortium (W3C) version • It is often called “W3C Schema” because “Schema” is considered generic • More commonly it is called XSD because the file names end in .xsd
  • 25. XSD Structure • xs:element • xs:sequence • xs:complexType <xs:complexType name=”person”> <xs:sequence> <xs:element name="lastname" type="xs:string"/ <xs:element name="age" type="xs:integer"/> <xs:element name="dateborn" type="xs:date"/> </xs:sequence> </xs:complexType> <person> <lastname>Severance</lastname> <age>17</age> <dateborn>2001-04-17</dateborn> </person>
  • 26. XSD Constraints http://www.w3schools.com/Schema/schema_complex_indicators.asp <xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="full_name" type="xs:string" minOccurs="1" maxOccurs="1" /> <xs:element name="child_name" type="xs:string" minOccurs="0" maxOccurs="10" /> </xs:sequence> </xs:complexType> </xs:element> <person> <full_name>Tove Refsnes</full_name <child_name>Hege</child_name> <child_name>Stale</child_name> <child_name>Jim</child_name> <child_name>Borge</child_name> </person>
  • 27. XSD Data Types http://www.w3schools.com/Schema/schema_dtypes_numeric.asp <xs:element name="customer" type="xs:string"/> <xs:element name="start" type="xs:date"/> <xs:element name="startdate" type="xs:dateTime"/> <xs:element name="prize" type="xs:decimal"/> <xs:element name="weeks" type="xs:integer"/> <customer>John Smith</customer> <start>2002-09-24</start> <startdate>2002-05-30T09:30:10Z</startdate> <prize>999.50</prize> <weeks>30</weeks> It is common to represent time in UTC/GMT given that servers are often scattered around the world.
  • 28.
  • 29.
  • 31. JSON • Used to format data, Text-based, Light-weight, Easy to parse, Subset of JavaScript • Commonly used in Web as a vehicle to describe data being sent between systems • Despite the name, JSON is a (mostly) language-independent way of specifying objects as name-value pairs {"skills": { "web":[ { "name": "html", "years": 5 }, { "name": "css", "years": 3 }] "database":[ { "name": "sql", "years": 7 }] }}
  • 32. JSON syntax • JSON is built on TWO Structures • An object is an unordered set of name/value pairs - The pairs are enclosed within braces, { } - There is a colon between the name and the value - Pairs are separated by commas - Example: { "name": "html", "years": 5 } • An array is an ordered collection of values - The values are enclosed within brackets, [ ] - Values are separated by commas - Example: [ "html", ”xml", "css" ] • A value can be: A string, a number, true, false, null, an object, or an array • Values can be nested • Strings are enclosed in double quotes, and can contain the usual assortment of escaped characters • Numbers have the usual C/C++/Java syntax, All numbers are decimal--no octal or hexadecimal • Whitespace can be used between any pair of tokens
  • 33. JSON Is Not... • JSON is not a document format. • JSON is not a markup language. • JSON is not a general serialization format. • No cyclical/recurring structures. • No invisible structures. • No functions.
  • 34.
  • 35. import json data = '''{ "name" : "Chuck", "phone" : { "type" : "intl", "number" : "+1 734 303 4456" }, "email" : { "hide" : "yes" } }''' info = json.loads(data) print 'Name:',info["name"] print 'Hide:',info["email"]["hide"] json1.py JSON represents data as nested "lists" and "dictionaries"
  • 36. import json input = '''[ { "id" : "001", "x" : "2", "name" : "Chuck" } , { "id" : "009", "x" : "7", "name" : "Chuck" } ]''' 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'] json2.py JSON represents data as nested "lists" and "dictionaries"
  • 37. Comparison of JSON and XML Similarities: • Both are human readable • Both have very simple syntax • Both are hierarchical • Both are language independent • Both can be used by Ajax • Both supported in APIs of many programming languages Differences: • Syntax is different • JSON is less verbose • JSON can be parsed by JavaScript’s eval method • JSON includes arrays • Names in JSON must not be JavaScript reserved words • XML can be validated • In general, JSON structures are simpler than XML because JSON has fewer capabilities than XML. • JSON has the advantage that it maps directly to some combination of dictionaries and lists. • Since nearly all programming languages have something equivalent to Python’s dictionaries and lists, JSON is a very natural format to have two cooperating programs exchange data. • JSON is quickly becoming the format of choice for nearly all data exchange between applications because of its relative simplicity compared to XML
  • 39. Service Oriented Approach • Most non-trivial web applications use services • They use services from other applications • Credit Card Charge • Hotel Reservation systems • Services publish the "rules" applications must follow to make use of the service (API) Application APIs Service Service
  • 40. Auto Hotel Airline Rental Reservation Reservation Service Service Service API Travel Application Figure: Service Oriented Architecture Service Oriented Architecture
  • 41. Service Oriented Architectures Technologies capable of: •Exchanging messages •Describing Web services •Publishing and discovering Web service descriptions Component Role Operation service requestor retrieves a service description A service publishes its description One-way, conversational, many-to-many
  • 43. Web Services - Definition from W3C “ A Web service is a software system identified by a URI, whose public interfaces and bindings are defined and described using XML. Its definition can be discovered by other software systems. These systems may then interact with the Web service in a manner prescribed by its definition, using XML based messages conveyed by internet protocols.”
  • 44. PO Service Credit Service Purchase Order Invoice Consolidate Results Example: Web based purchase What are Web Services Web services are self contained, self describing, modular applications that can be published, located, and invoked across the web. Web services perform functions, which can be anything from simple requests to complicated business processes". ---- According to IBM - Runs on a Web server Web Service is based on: • HTTP (Hypertext Transport Protocol) • SOAP (Simple Object Access Protocol) • UDDI (Universal Description, Discovery and Integration) • WS-POLICY (Web Services Policy)
  • 45. Sample Web Service Calc.asmx <%@ WebService Language="C#" CodeBehind="~/App_Code/WebService.cs" Class="WebService" %> using System; using System.Web.Services; [WebService (Name="Calculator Web Service", Description = "Perform simple math over the Web")] class CalcService { [WebMethod (Description = "Computes the sum of two integers")] public int Add (int a, int b) { return a+b;} [WebMethod (Description = "Computes the difference between two integers")] public int Subtract (int a, int b) { return a-b;} }
  • 46. 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.
  • 47. Web Service Technologies • SOAP - Simple Object Access Protocol (software) • Remote programs/code which we use over the network Note: Dr. Chuck does not like SOAP because it is overly complex • REST - Representational State Transfer (resource focused) • Remote resources which we create, read, update and delete remotely
  • 48. Simple Object Access Protocol • SOAP is a technology to support the exchange of XML-coded messages over a transport protocol, such as HTTP and SMTP. – SOAP provides a simple and lightweight mechanism for exchanging structured and typed information between peers in a decentralized, distributed environment using XML. Contains three elements: <Envelope>, <Header>, and <Body>. HTTP server Skeleton Server HTTP client Stub Client XML XML POST… HTTP 1.1 200 OK SOAP SOAP HTTP SOAP XML Protocols Folding SOAP basic mechanism
  • 49. Simple Object Access Protocol A SOAP runtime engine basically adds a XML envelope to an existing XML document SOAP Envelope XML Document <soap:Envelope> <soap:Header> <axis:SessionKey> SDHH37TYEW7R7 </axis:SessionKey> </soap:Header> <soap:Body> <GetPrice> <Item>Apples</Item> </GetPrice> </soap:Body> </soap:Envelope> SOAP Body SOAP Header Communicatio n Info Example Document Container Session, Authentication, Routing, Security
  • 50. What is REST ? Representational State Transfer • Coined by Roy Fielding to describe an architecture style of networked systems. • Underlying Architectural model of the world wide web. • Guiding framework for Web protocol standards. • Elements – Components – Proxy , gateway etc – Connectors – client , server etc – Data – resource , representation etc • Ignores component implementation details. • Focus on roles of components, their interactions and their interpretation of data elements. REST based web services • Online shopping • Search services • Dictionary services
  • 52. { "status": "OK", "results": [ { "geometry": { "location_type": "APPROXIMATE", "location": { "lat": 42.2808256, "lng": -83.7430378 } }, "address_components": [ { "long_name": "Ann Arbor", "types": [ "locality", "political" ], "short_name": "Ann Arbor" } ], "formatted_address": "Ann Arbor, MI, USA", "types": [ "locality", "political" ] } ] } geojson.py http://maps.googleapis.com/maps/api/g eocode/json?sensor=false&address=A nn+Arbor%2C+MI
  • 53. import urllib import json serviceurl = 'http://maps.googleapis.com/maps/api/geocode/json?' while True: address = raw_input('Enter location: ') if len(address) < 1 : break url = serviceurl + urllib.urlencode({'sensor':'false', 'address': address}) print 'Retrieving', url uh = urllib.urlopen(url) data = uh.read() print 'Retrieved',len(data),'characters' try: js = json.loads(str(data)) except: js = None if 'status' not in js or js['status'] != 'OK': print '==== Failure To Retrieve ====' print data continue print json.dumps(js, indent=4) lat = js["results"][0]["geometry"]["location"]["lat"] lng = js["results"][0]["geometry"]["location"]["lng"] print 'lat',lat,'lng',lng location = js['results'][0]['formatted_address'] print location geojson.py Enter location: Ann Arbor, MI Retrieving http://maps.googleapis.com/maps/api/geocode /json?sensor=false&address=Ann+Arbor%2C+MI Retrieved 1669 characters lat 42.2808256 lng -83.7430378 Ann Arbor, MI, USA Enter location:
  • 54. API Security and Rate Limiting • The compute resources to run these APIs are not "free" • The data provided by these APIs is usually valuable • The data providers might limit the number of requests per day, demand an API "key" or even charge for usage • They might change the rules as things progress...
  • 55.
  • 56. import urllib import twurl TWITTER_URL = 'https://api.twitter.com/1.1/statuses/user_timeline.json' while True: print '' acct = raw_input('Enter Twitter Account:') if ( len(acct) < 1 ) : break url = twurl.augment(TWITTER_URL, {'screen_name': acct, 'count': '2'} ) print 'Retrieving', url connection = urllib.urlopen(url) data = connection.read() print data[:250] headers = connection.info().dict # print headers print 'Remaining', headers['x-rate-limit-remaining'] twitter1.py
  • 57. Enter Twitter Account:drchuck Retrieving https://api.twitter.com/1.1/statuses/user_timeline.json?count=2&oauth_version= 1.0&oauth_token=10185562- eibxCp9n2OizWNT1VrmsDoK40g3feclP4GEQQOSGI&screen_name=drchuck&oauth_nonce=4188 7363&oauth_timestamp=1414535902&oauth_signature=C9C4p0weq7ayRxTOh1fsqszl4fY%3D &oauth_consumer_key=I8cTTYmdplNsnJCiSZWQ&oauth_signature_method=HMAC-SHA1 [{"created_at":"Tue Oct 28 21:46:48 +0000 2014","id":527214927829037057,"id_str":"527214927829037057","text":"RT @ImRegularJohn: Madly in love with @coursera. Graduated in IHTS with @drchuck, now @philscimooc, next Astronomy at @DukeU &amp; Sustainab Remaining 177 Enter Twitter Account:injenuity Retrieving https://api.twitter.com/1.1/statuses/user_timeline.json?count=2&oauth_version= 1.0&oauth_token=10185562- eibxCp9n2OizWNT1VrmsDoK40g3feclP4GEQQOSGI&screen_name=injenuity&oauth_nonce=27 065523&oauth_timestamp=1414535967&oauth_signature=5ETRfBJubkvJ4LnP1kZIHGwRiSk% 3D&oauth_consumer_key=I8cTTYmdplNsnJCiSZWQ&oauth_signature_method=HMAC-SHA1 [{"created_at":"Tue Oct 28 20:57:44 +0000 2014","id":527202579701194752,"id_str":"527202579701194752","text":"@dlnorman I wasn't even thinking about a camera. More that I'll be talking funny.","source":"u003ca href="http://twitter.com" rel="nof Remaining 176 twitter1.py
  • 58. import urllib import twurl import json TWITTER_URL = 'https://api.twitter.com/1.1/friends/list.json' while True: print '' acct = raw_input('Enter Twitter Account:') if ( len(acct) < 1 ) : break url = twurl.augment(TWITTER_URL, {'screen_name': acct, 'count': '5'} ) print 'Retrieving', url connection = urllib.urlopen(url) data = connection.read() headers = connection.info().dict 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] twitter2.py
  • 59. Enter Twitter Account:drchuck Retrieving https://api.twitter.com/1.1/friends ... Remaining 14 { "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&amp;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, twitter2.py
  • 60. Summary • Service Oriented Architecture - allows an application to be broken into parts and distributed across a network • An Application Program Interface (API) is a contract for interaction • Web Services provide infrastructure for applications cooperating (an API) over a network - SOAP and REST are two styles of web services • XML and JSON are serialization formats