Unerstanding and Using RESTful APIs
-
Upload
socialdevcamp-chicago -
Category
Technology
-
view
2.232 -
download
4
description
Transcript of Unerstanding and Using RESTful APIs
![Page 1: Unerstanding and Using RESTful APIs](https://reader036.fdocuments.net/reader036/viewer/2022062303/55519d68b4c9053c488b48cf/html5/thumbnails/1.jpg)
REST 11.07.2009Understanding and Using RESTful APIs
![Page 2: Unerstanding and Using RESTful APIs](https://reader036.fdocuments.net/reader036/viewer/2022062303/55519d68b4c9053c488b48cf/html5/thumbnails/2.jpg)
About Me
•Developing software for 15 years
•Bulk of experience on enterprise-scale applications
•Microsoft platform (mostly)
•CTO at KeyLimeTie
![Page 3: Unerstanding and Using RESTful APIs](https://reader036.fdocuments.net/reader036/viewer/2022062303/55519d68b4c9053c488b48cf/html5/thumbnails/3.jpg)
Overview
Although REST is 10 years old, APIs based on the REST model continue to become an integral part of the technology landscape.
ProgrammableWeb.com lists 1051 RESTful APIs, 4422 Mashups.
We’ll look at
• Basic REST Concepts• Using REST• WADL
![Page 4: Unerstanding and Using RESTful APIs](https://reader036.fdocuments.net/reader036/viewer/2022062303/55519d68b4c9053c488b48cf/html5/thumbnails/4.jpg)
REST OVERVIEW
![Page 5: Unerstanding and Using RESTful APIs](https://reader036.fdocuments.net/reader036/viewer/2022062303/55519d68b4c9053c488b48cf/html5/thumbnails/5.jpg)
REST Overview
APIs based on the Representational State Transfer (REST) architecture dominate web 2.0 mash-up and social media development.
There are a number of frameworks available that support the development of RESTful APIs (WCF, JAX-RS, Wicket, Zend)
We are still missing an established mechanism for documentation and discovery and client-side tool support for rapid implementation.
![Page 6: Unerstanding and Using RESTful APIs](https://reader036.fdocuments.net/reader036/viewer/2022062303/55519d68b4c9053c488b48cf/html5/thumbnails/6.jpg)
REST Overview
REST-style architectures consist of clients and servers.
Requests and responses are built around the transfer of "representations" of "resources".
A resource is any entity that can be addressed in a URI (an account, an employee, a physical file)
A representation of a resource is any information that captures the current or intended state of a resource. All information necessary to process and complete a request is contained within it.
![Page 7: Unerstanding and Using RESTful APIs](https://reader036.fdocuments.net/reader036/viewer/2022062303/55519d68b4c9053c488b48cf/html5/thumbnails/7.jpg)
Example
Resource (Twitter Public Timeline)http://twitter.com/statuses/public_timeline.xml
Resource (Twitter User) http://twitter.com/users/petermorano.xml
![Page 8: Unerstanding and Using RESTful APIs](https://reader036.fdocuments.net/reader036/viewer/2022062303/55519d68b4c9053c488b48cf/html5/thumbnails/8.jpg)
REST Overview
REST-style architectures consist of clients and servers.
Requests and responses are built around the transfer of "representations" of "resources".
A resource is any entity that can be addressed in a URI (an account, an employee, a physical file)
A representation of a resource is any information that captures the current or intended state of a resource. All information necessary to process and complete a request is contained within it.
![Page 9: Unerstanding and Using RESTful APIs](https://reader036.fdocuments.net/reader036/viewer/2022062303/55519d68b4c9053c488b48cf/html5/thumbnails/9.jpg)
Wait a Minute
If this sounds a lot like the Web, it’s because the Web is a REST
implementation
Roy Fielding, who created REST, was also one of the principal authors of HTTP
![Page 10: Unerstanding and Using RESTful APIs](https://reader036.fdocuments.net/reader036/viewer/2022062303/55519d68b4c9053c488b48cf/html5/thumbnails/10.jpg)
REST Basics
The REST architecture was designed around a few key principles:
• Use HTTP Methods (and Response Codes)
• Be Stateless and Cacheable
• Use Addressable Resources
• Support the transfer of Representations
![Page 11: Unerstanding and Using RESTful APIs](https://reader036.fdocuments.net/reader036/viewer/2022062303/55519d68b4c9053c488b48cf/html5/thumbnails/11.jpg)
REST Basics
The REST architecture was designed around a few key principles:
• Use HTTP Methods (and Response Codes)
• Be Stateless and Cacheable
• Use Addressable Resources
• Support the transfer of Representations
![Page 12: Unerstanding and Using RESTful APIs](https://reader036.fdocuments.net/reader036/viewer/2022062303/55519d68b4c9053c488b48cf/html5/thumbnails/12.jpg)
HTTP Methods
Action
Create
Read
Update
Delete
SQL
Insert
Select
Update
Delete
HTTP Method
Post
Get
Put
Delete
Map to CRUD Operations
![Page 13: Unerstanding and Using RESTful APIs](https://reader036.fdocuments.net/reader036/viewer/2022062303/55519d68b4c9053c488b48cf/html5/thumbnails/13.jpg)
URI Examples
GET
GET
POST
PUT
http://www.example.com/v1/employeeshttp://www.example.com/v1/employees/1824http://www.example.com/v1/employeeshttp://www.example.com/v1/employees/1513
DELETEhttp://www.example.com/v1/employees/1222
![Page 14: Unerstanding and Using RESTful APIs](https://reader036.fdocuments.net/reader036/viewer/2022062303/55519d68b4c9053c488b48cf/html5/thumbnails/14.jpg)
REST Basics
The REST architecture was designed around a few key principles:
• Use HTTP Methods (and Response Codes)
• Be Stateless and Cacheable
• Use Addressable Resources
• Support the transfer of Representations
![Page 15: Unerstanding and Using RESTful APIs](https://reader036.fdocuments.net/reader036/viewer/2022062303/55519d68b4c9053c488b48cf/html5/thumbnails/15.jpg)
Stateless and Cacheable
• No State Stored on the Server
• Every HTTP request executes in complete isolation on the server
• Easier to scale because of GET method
![Page 16: Unerstanding and Using RESTful APIs](https://reader036.fdocuments.net/reader036/viewer/2022062303/55519d68b4c9053c488b48cf/html5/thumbnails/16.jpg)
REST Basics
The REST architecture was designed around a few key principles:
• Use HTTP Methods (and Response Codes)
• Be Stateless and Cacheable
• Use Addressable Resources
• Support the transfer of Representations
![Page 17: Unerstanding and Using RESTful APIs](https://reader036.fdocuments.net/reader036/viewer/2022062303/55519d68b4c9053c488b48cf/html5/thumbnails/17.jpg)
Resources
• Any THING – person, concept, artifact
• Anything you can point to
• Explicit Request and Response – No State
![Page 18: Unerstanding and Using RESTful APIs](https://reader036.fdocuments.net/reader036/viewer/2022062303/55519d68b4c9053c488b48cf/html5/thumbnails/18.jpg)
REST Basics
The REST architecture was designed around a few key principles:
• Use HTTP Methods (and Response Codes)
• Be Stateless and Cacheable
• Addressable Resources
• Support the transfer of Representations
![Page 19: Unerstanding and Using RESTful APIs](https://reader036.fdocuments.net/reader036/viewer/2022062303/55519d68b4c9053c488b48cf/html5/thumbnails/19.jpg)
Representations
• A serializable description of a Resource
• Resources are modifiable through Representations
• XML
• JSON
• Binary Formats (jpg, gif, mpeg)
![Page 20: Unerstanding and Using RESTful APIs](https://reader036.fdocuments.net/reader036/viewer/2022062303/55519d68b4c9053c488b48cf/html5/thumbnails/20.jpg)
REST Pros
Cacheable
Scalable
Different Representations
Human Readable Results
Lightweight
![Page 21: Unerstanding and Using RESTful APIs](https://reader036.fdocuments.net/reader036/viewer/2022062303/55519d68b4c9053c488b48cf/html5/thumbnails/21.jpg)
REST Cons
Must be HTTP
No Atomic Transactions
No Standards for security except HTTPS
No Standardized Discovery
![Page 22: Unerstanding and Using RESTful APIs](https://reader036.fdocuments.net/reader036/viewer/2022062303/55519d68b4c9053c488b48cf/html5/thumbnails/22.jpg)
REST vs. SOAP
Unlike SOAP-based web services, there is no "official" standard for RESTful web service.
Though not a standard, a RESTful implementation uses standards like HTTP, URL, XML, GIF, etc.
![Page 23: Unerstanding and Using RESTful APIs](https://reader036.fdocuments.net/reader036/viewer/2022062303/55519d68b4c9053c488b48cf/html5/thumbnails/23.jpg)
REST vs. SOAP
SOAP
•Protocol
•Verbose Payload• Envelope
•Not easily readable
•Requires Development Tools
•Uses POST Method
•Supports Transactions
•Standardized Discovery• WSDL
REST
•Architecture
•Lightweight Payload• Postcard
•Human Readable Results
•Easy to work with, Brower-testable
•GET Method supports caching
•No Transactions
•Discovery• WADL?
![Page 24: Unerstanding and Using RESTful APIs](https://reader036.fdocuments.net/reader036/viewer/2022062303/55519d68b4c9053c488b48cf/html5/thumbnails/24.jpg)
USING REST
![Page 25: Unerstanding and Using RESTful APIs](https://reader036.fdocuments.net/reader036/viewer/2022062303/55519d68b4c9053c488b48cf/html5/thumbnails/25.jpg)
USING REST
Think of a REST API as another data source
Details depend on the language, but there are common steps:
• Serialize the representation (if PUT or POST)
• Format the URI (parameters, variables)
• Encode Authentication into the Header
• Make Call
• Deserialize Response
![Page 26: Unerstanding and Using RESTful APIs](https://reader036.fdocuments.net/reader036/viewer/2022062303/55519d68b4c9053c488b48cf/html5/thumbnails/26.jpg)
USING REST
Three basic options for consuming RESTful APIs
• Use the service’s toolkit (not always available)
• Community-built library/wrapper• code.google.com• www.codeplex.com
• Code your own implementation
![Page 27: Unerstanding and Using RESTful APIs](https://reader036.fdocuments.net/reader036/viewer/2022062303/55519d68b4c9053c488b48cf/html5/thumbnails/27.jpg)
Community BuiltUpside
• Ready to use
Downside
• It’s not your code
• You may be waiting for the library to become available.
• Enterprise clients may have concerns
![Page 28: Unerstanding and Using RESTful APIs](https://reader036.fdocuments.net/reader036/viewer/2022062303/55519d68b4c9053c488b48cf/html5/thumbnails/28.jpg)
Writing Your OwnUpside
• You know the code
Downside
• Manual Discovery Process
• Implementation can be tedious
![Page 29: Unerstanding and Using RESTful APIs](https://reader036.fdocuments.net/reader036/viewer/2022062303/55519d68b4c9053c488b48cf/html5/thumbnails/29.jpg)
Tools
Tools for testing RESTful APIs
• Curl
• Eclipse Http4e
• Fiddler
• Your Browser (limited)
![Page 30: Unerstanding and Using RESTful APIs](https://reader036.fdocuments.net/reader036/viewer/2022062303/55519d68b4c9053c488b48cf/html5/thumbnails/30.jpg)
WADL
![Page 31: Unerstanding and Using RESTful APIs](https://reader036.fdocuments.net/reader036/viewer/2022062303/55519d68b4c9053c488b48cf/html5/thumbnails/31.jpg)
WADL
Web Application Discovery Language (WADL)
Created in 2005 by Marc Hadley of Sun Microsystems
Submitted to W3C on 8/31/2009
REST’s Answer to WSDL
Specification: https://wadl.dev.java.net/
![Page 32: Unerstanding and Using RESTful APIs](https://reader036.fdocuments.net/reader036/viewer/2022062303/55519d68b4c9053c488b48cf/html5/thumbnails/32.jpg)
What WADL Gets You
Reduced Errors in Discovery Opportunities for Code Generation
Spend more time building your application and less time implementing APIs
![Page 33: Unerstanding and Using RESTful APIs](https://reader036.fdocuments.net/reader036/viewer/2022062303/55519d68b4c9053c488b48cf/html5/thumbnails/33.jpg)
So Where Is It?
Currently in Submission to W3C API Providers are slow to adopt, so tool providers are slow to implement
There are projects underway to generate code from WADL, and at least one tool that helps in creating WADL
![Page 34: Unerstanding and Using RESTful APIs](https://reader036.fdocuments.net/reader036/viewer/2022062303/55519d68b4c9053c488b48cf/html5/thumbnails/34.jpg)
WADL DemoProject on code.google.com
Online Version: http://tomayac.de/rest-describe/latest/RestDescribe.html
![Page 35: Unerstanding and Using RESTful APIs](https://reader036.fdocuments.net/reader036/viewer/2022062303/55519d68b4c9053c488b48cf/html5/thumbnails/35.jpg)
Referenceshttp://en.wikipedia.org/wiki/Representational_State_Transfer
http://weblogs.java.net/blog/mhadley/archive/2005/05/introducing_wad.html
http://blog.tomayac.de/index.php?date=2007-05-23
RESTful Web Services – Richardson, Ruby – O’Reilly Press