Web Services - City University of New York

Download Report

Transcript Web Services - City University of New York

Introduction to
Web Services and Web API’s
Richard Holowczak
Baruch College
December, 2014
Evolution of Web Services
• Each web site creates custom scripts and web
application programs to handle all business logic
• Over time, some patterns emerge:
•
•
•
•
•
•
•
•
•
•
Query a catalog of items
Add items to a shopping cart
Calculate shipping methods and costs
Calculate taxes
Generate a Configuration
Generate a Quotation
Approve payment
Generate a Receipt
Handle support questions
Etc.
• Creating custom solutions for each business is costly
Distributed Systems: Variations on
an old theme
• Examples of “tightly-coupled” services:
• Remote procedure call (RPC)
• Common Object Request Broker (CORBA)
• Distributed Component Object Model (DCOM)
• Systems are “brittle” in that often trivial changes to
a service can break the whole system
• Difficult to inter-operate across operating systems
and hardware platforms
• Network security issues
Standardizing Web Services Requirements
• Advertising available Web Services
• Searching for Web Services
• Calling a Web Service
• Handling Web service Responses
• All done using “web” protocols (e.g., over standard
network ports)
• Service – Oriented Architecture (SOA)
Web Service Description Language (WSDL)
• Describes the capabilities of a web service including:
•
•
•
•
Where the web service is located (URL)
What input parameters the web service requires
What operations (calculations, queries, etc.) the web service supports
What output the web service generates
• Written as an XML Document
•
•
•
•
Types
Message
portType
Binding
What Data Types does the service support (string, number, etc.)
The parts that make up the request and response messages
Describes the services in terms of the messages and types
Defines the service locations for each operation
Example:
http://www.w3schools.com/webservices/tempconvert.asmx?WSDL
• Universal Description, Discovery and Integration (UDDI)
• Used to locate available web services
Simple Object Access Protocol (SOAP)
• Communications protocol used to assemble a
distributed SOA application
• Works in a “machine and OS independent” fashion
• Uses HTTP as an underlying transport mechanism
• Request is an XML document
• Response is an XML document
• Contains:
•
•
•
•
Envelope
Header
Body
Fault
Indicates what is inside is a SOAP message
Indicates what the receiver must know to process
Request (or Response) information
Error handling support
• Example:
http://www.w3schools.com/webservices/tempconvert.asmx?op=CelsiusToFahrenheit
Example SOAP Message
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<CelsiusToFahrenheit xmlns="http://www.w3schools.com/webservices/">
<Celsius>string</Celsius>
</CelsiusToFahrenheit>
</soap12:Body>
</soap12:Envelope>
Try it:
http://www.w3schools.com/webservices/tempconvert.asmx?op=CelsiusToFahrenheit
REST Protocol
• SOA using SOAP, WSDL, etc. has high overhead
• What about simple requests and responses?
• REpresentational State Transfer (REST)
• Request uses HTTP with only 4 operations: GET, POST,
PUT, and DELETE
• Response can be XML, JSON, CSV or plain text
• Viewed as a “lightweight” alternative to SOAP
• Example:
http://rpc.geocoder.us/service/csv?address=151+E+25th+Street+New+York+NY
JSON Documents
• JavaScript Object Notation (JSON)
• Easier to parse than XML
• Very little in the way of “type safety”
• About as “loosely connected” as you can get
• Objects have Properties
• Properties can be single valued:
• String
"name" : "Joe Smith“
• Properties can be Sets of values using square
brackets
• "working_on" : ["Project1", "Project2", "Project3" ]
• Example comparing JSON to XML:
http://json.org/example
JSON Document Example
employee1 = {
name:
{
first: "Joe", last: "Smith"
},
birth: new Date('January 5, 1972'),
compensation: {
salary: Number(48500),
bonus: Number(2500)
},
department : NumberLong(5),
projects: [ "NewBenefits",
"Payroll" ]
}
Web APIs
• Web Application Programming Interfaces are sets of web
services that can be incorporated into web or mobile
applications
• Some free services but most require registration to obtain
an “API key” or “API Token”
• API Documentation will provide the set of methods and
acceptable parameters
• Examples:
• Google API: https://developers.google.com/apis-explorer/#p/
• Facebook API: https://developers.facebook.com/docs/graph-api/
• Amazon API: https://developer.amazon.com/public/apis
Facebook Web API
• Facebook Graph API Explorer Tool:
https://developers.facebook.com/tools/explorer/
Facebook Web API
• Get an Access token
• Select Permissions (if necessary, log in to Facebook)
Facebook Web API
• REST Queries
• Show everything about a user: /826693880709096?
• Show Everything about a me: /me?
Facebook Web API
• Show only names of schools attended in education:
me?fields=education{school{name}}
Compare Approaches
Client
Web
Browser
Web
Service
Web
Service
Request
Protocol
HTTP
Response Service
HTML
Application Specific
SOAP XML SOAP XML General Purpose
Service
REST
XML,JSON, General Purpose
CSV
Service