RESTful Web ServicesGrzegorz Borkowski                       Confidential
RESTful Web ServicesAgenda Introduction REST principles Designing RESTful API Miscellaneous Implementions and Frameworks I...
RESTful Web ServicesAgenda IntroductionREST principles Designing RESTful API Miscellaneous Implementions and Frameworks Im...
IntroductionMaterials The most recommended reading:   Materials - Richardson, Leonard; Ruby, Sam (2007-05), RESTful Web ...
IntroductionREST origins REST = Representational state transfer - a style of software architecture The term representati...
IntroductionAn HTTP GET request for http://www.oreilly.com/index.htmlGET /index.html HTTP/1.1Host: www.oreilly.comUser-Age...
RESTful Web ServicesAgenda Introduction REST principles Designing RESTful API Miscellaneous Implementions and Frameworks I...
REST principlesREST keywords: Resources Addressability Statelessness Uniform API Representations Cacheability Conne...
REST principlesResource Every data or abstraction which can be created/updated/retrieved/removed and somehow represented,...
REST principlesAddressability Why addressability is important?      Bookmarking      sending links      resource-level...
REST principlesAddressability – cont.Permanent URIs vs Readable URIs (vs Hybrid URIs)“Clean URLs” - http://en.wikipedia....
REST principlesStatelessness Every request is independent of previous one No server-side sessions, conversation state is...
REST principlesUniform API  Uniform operations: CRUD      POST  Create      GET  Read (retrieve single resource, or que...
REST principlesUniform API  Uniform response codes      1xx - info      2xx - ok      3xx - redirect      4xx – client er...
REST principlesRepresentations  The same resource, but different representations - content types:      HTML, XHTML      X...
REST principlesCacheability All HTTP clients are allowed to cache resources Caching controlled by standard headers (Unif...
RESTful Web ServicesAgenda Introduction REST principlesDesigning RESTful API Miscellaneous Implementions and Frameworks Im...
Designing RESTful APIRESTful web service exampleSimple RESTful web service Amazon S3 (Simple Storage Service) A service f...
Designing RESTful APIDesigning the read-only API Design resources Design representations served by the service Specify ...
Designing RESTful APIExerciseDesign the read-only market data information service for traders Service should deliver info...
Designing RESTful APIDesigning the read-write API Design resources Design representations served by the service Design ...
RESTful Web ServicesAgenda Introduction REST principles Designing RESTful APIMiscellaneous Implementions and Frameworks Im...
MiscellaneousVersioningVersioning   Versioning of resources – e.g. http://myservice.example.com/rest/customers/11235.4  ...
MiscellaneousHTTP – additional capabilitiesCompression   Accept-Encoding: gzip,deflateAsync requests   Response code: ...
RESTful Web ServicesAgenda Introduction REST principles Designing RESTful API MiscellaneousImplementions and Frameworks Im...
Implementions and FrameworksClient side requirementsSupport for SSL/TLSSupport PUT, DELETE, HEAD, not only GET and POST...
Implementions and FrameworksServer side requirements - all as client side, plus boiler-plate code reducing featuresURI Te...
RESTful Web ServicesAgenda Introduction REST principles Designing RESTful API Miscellaneous Implementions and Frameworks I...
It’s time to code something!                               Confidential
Upcoming SlideShare
Loading in …5
×

Restful web services rule financial

1,769 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,769
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
28
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Restful web services rule financial

  1. 1. RESTful Web ServicesGrzegorz Borkowski Confidential
  2. 2. RESTful Web ServicesAgenda Introduction REST principles Designing RESTful API Miscellaneous Implementions and Frameworks Implementing RESTful API © Rule Financial 2011 2
  3. 3. RESTful Web ServicesAgenda IntroductionREST principles Designing RESTful API Miscellaneous Implementions and Frameworks Implementing RESTful API © Rule Financial 2011 3
  4. 4. IntroductionMaterials The most recommended reading:  Materials - Richardson, Leonard; Ruby, Sam (2007-05), RESTful Web Services, OReilly (warning: code examples are written in Ruby!) © Rule Financial 2011 4
  5. 5. IntroductionREST origins REST = Representational state transfer - a style of software architecture The term representational state transfer was introduced and defined in 2000 by Roy Fielding in his doctoral dissertation REST = applying HTTP principles to the software architecture RESTful = conforming to the REST priniciples. → RESTful Web Services Why has HTTP got so popular?  Because of simplicity. Probably every programming language supports HTTP - because it’s easy!  Compare to SOAP – SOAP is much more complicated.  Internet. Internet is efficient, scalable, easy to use… we want our software to be like the Internet! SOAP – alternate approach to web services.  To better understand REST, its helpful to compare it to SOAP.  REST is a simpler alternative to SOAP, a simpler way to build web services.  Compare especially WSDL! © Rule Financial 2011 5
  6. 6. IntroductionAn HTTP GET request for http://www.oreilly.com/index.htmlGET /index.html HTTP/1.1Host: www.oreilly.comUser-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.12)...Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,...Accept-Language: us,en;q=0.5Accept-Encoding: gzip,deflateAccept-Charset: ISO-8859-15,utf-8;q=0.7,*;q=0.7Keep-Alive: 300Connection: keep-aliveThe response to an HTTP GET request for http://www.oreilly.com/index.htmlHTTP/1.1 200 OKDate: Fri, 17 Nov 2006 15:36:32 GMTServer: ApacheLast-Modified: Fri, 17 Nov 2006 09:05:32 GMTEtag: "7359b7-a7fa-455d8264Content-Length: 43302Content-Type: text/htmlKeep-Alive: timeout=15, max=1000Connection: Keep-Alive<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><title>oreilly.com -- Welcome to OReilly Media, Inc.</title>... © Rule Financial 2011 6
  7. 7. RESTful Web ServicesAgenda Introduction REST principles Designing RESTful API Miscellaneous Implementions and Frameworks Implementing RESTful API © Rule Financial 2011 7
  8. 8. REST principlesREST keywords: Resources Addressability Statelessness Uniform API Representations Cacheability Connectedness and discoverability © Rule Financial 2011 8
  9. 9. REST principlesResource Every data or abstraction which can be created/updated/retrieved/removed and somehow represented, and is addressable.  Web page is a resource;  but also a blog post,  a comment,  a cart,  a cart item etc. © Rule Financial 2011 9
  10. 10. REST principlesAddressability Why addressability is important?  Bookmarking  sending links  resource-level authorization Compare FTP, JSF 1.x, most of poorly written Ajax applications, Flash-based applications – they are not addressable. URIs vs URLs vs URNs  URI doesnt have to be URL. For example, XML namespace is an URI, but its not an URL  URLs that point to the same resource: http://mylib.example.com/releases/1.1 http://mylib.example.com/releases/1.2 http://mylib.example.com/releases/latest  at this time, it can be the same as 1.2  URIs of the single resources vs URIs of the resource lists/queries http://mylib.example.com/releases/  list of all releases http://mylib.example.com/releases/?majorVersion=1 http://mylib.example.com/releases/1.2 © Rule Financial 2011 10
  11. 11. REST principlesAddressability – cont.Permanent URIs vs Readable URIs (vs Hybrid URIs)“Clean URLs” - http://en.wikipedia.org/wiki/Clean_URLs - advantages:  SEO  user experience  hide underlying technology (index.jsp, index.php, index.aspx)  mod rewrite, UrlRewriteFilterNote: percent-encoding  URL-encoding – URI specification, closely related to HTTP (” ” -> ”%20”)  Form encoding – part of HTML specification for submitting forms (” ” -> ”+”) as a query string (part of URL!) in GET requests, as a body in POST requests © Rule Financial 2011 11
  12. 12. REST principlesStatelessness Every request is independent of previous one No server-side sessions, conversation state is kept on the client side only Advantages: Simplicity Scalability Addressability Application (conversation) state vs resource state  Resource state is the same for all clients/applications  Application (conversation) state is client-specific © Rule Financial 2011 12
  13. 13. REST principlesUniform API  Uniform operations: CRUD POST  Create GET  Read (retrieve single resource, or query to get list of matches) PUT  Update (or Create) DELETE  Delete  SOAP is more like Java/.NET/etc – no uniform API. REST is more like SQL – uniform API (insert, select, update, delete).  Special meaning of POST („Overloaded POST”)  trigger some algorithm/procedure/operation  note: PUT/DELETE over POST (for example for HTML 4.x forms)  Safe and indempotent operations Take care to keep PUT indempotent (eg. Dont increment value in PUT) Safe – e.g. safe for webcrawlers. Safe and indempotent calls are great for unreliable networks – if request times out, just resend it. Why its important – example of Google Accelerator (2005) © Rule Financial 2011 13
  14. 14. REST principlesUniform API  Uniform response codes 1xx - info 2xx - ok 3xx - redirect 4xx – client error 5xx – server error  Uniform headers, e.g.: Accept-Encoding: gzip,deflate Last-Modified: Fri, 17 Nov 2006 09:05:32 GMT Content-Type: text/plain (but you are not limited to the standard headers, you can use your own ones)  Compare to RPC © Rule Financial 2011 14
  15. 15. REST principlesRepresentations  The same resource, but different representations - content types: HTML, XHTML XML JSON Form-encoded (application/x-www-form-urlencoded) binary (PDF, JPG,...) Base64 Plaintext  Content negotiation using headers or the representation specified in the URI (…/projects/12.xml - …/projects/12.json)  Short/medium/long representation, e.g.: – Users list for dropdowns, only id and label – short representation – Users list with details – medium representation – Users list with related projects, tasks, absences etc. - long representation © Rule Financial 2011 15
  16. 16. REST principlesCacheability All HTTP clients are allowed to cache resources Caching controlled by standard headers (Uniform API!) – HTTP 1.0: Expires, Last-Modified, If-Modified-Since – HTTP 1.1: Cache-Control, Etag, If-None-Matches Cache forever and never ask again – great value for versioned resources Cache and ask whether it has changed (Conditional GET) IE problems for Ajax requests – broken caching, use the random element in URLConnectedness Navigation from one resource to related resources by links Discoverability © Rule Financial 2011 16
  17. 17. RESTful Web ServicesAgenda Introduction REST principlesDesigning RESTful API Miscellaneous Implementions and Frameworks Implementing RESTful API © Rule Financial 2011 17
  18. 18. Designing RESTful APIRESTful web service exampleSimple RESTful web service Amazon S3 (Simple Storage Service) A service for reliable storage of objects (files) Two types of resources – Buckets – Objects API: GET HEAD PUT DELETE The bucket list List your buckets - - - / A bucket List the bucket’s objects - Create the bucket Delete the bucket /{bucket} An object Get the object’s value Get the object’s metadata Set the object’s value Delete the object /{bucket}/{object} and metadata and metadata © Rule Financial 2011 18
  19. 19. Designing RESTful APIDesigning the read-only API Design resources Design representations served by the service Specify supported methods Specify caching © Rule Financial 2011 19
  20. 20. Designing RESTful APIExerciseDesign the read-only market data information service for traders Service should deliver information about:  Exchanges (name, location, public holidays, …)  Indices (name, current value, past values)  Stocks (name, fundamental data, current price, past prices)  Derivatives  Fx pairs Design  Resources  Their addresses  Supported methods  Representations © Rule Financial 2011 20
  21. 21. Designing RESTful APIDesigning the read-write API Design resources Design representations served by the service Design accepted incoming representations Specify supported methods Specify caching Security  Authentication (preferred stateless: keep authentication state on the client side, reauthenticate on each request)  Authorization Error handlingNote: Basic authentication Server returns code and header: 401 Authorization Required WWW-Authenticate: Basic realm=„Realm name" Client sends (with each request) header: Authorization: Basic Base64Encoded(username:password) © Rule Financial 2011 21
  22. 22. RESTful Web ServicesAgenda Introduction REST principles Designing RESTful APIMiscellaneous Implementions and Frameworks Implementing RESTful API © Rule Financial 2011 22
  23. 23. MiscellaneousVersioningVersioning  Versioning of resources – e.g. http://myservice.example.com/rest/customers/11235.4  Versioning of API – e.g. http://myservice.example.com/rest/v1/customers/ © Rule Financial 2011 23
  24. 24. MiscellaneousHTTP – additional capabilitiesCompression  Accept-Encoding: gzip,deflateAsync requests  Response code: 202 Accepted  „jobs”Conditional PUT  Expect reponse code: 100 Continue © Rule Financial 2011 24
  25. 25. RESTful Web ServicesAgenda Introduction REST principles Designing RESTful API MiscellaneousImplementions and Frameworks Implementing RESTful API © Rule Financial 2011 25
  26. 26. Implementions and FrameworksClient side requirementsSupport for SSL/TLSSupport PUT, DELETE, HEAD, not only GET and POSTSupport manipulation of request headersAccess to response headers and status codeSupport proxiesSupport compressionSupport cachingSupport authentication mechanisms (basic, digest, wsse, o-auth)Support redirects (3xx)Java examplesjava.net. HttpURLConnectionApache HttpClientRestlet Client © Rule Financial 2011 26
  27. 27. Implementions and FrameworksServer side requirements - all as client side, plus boiler-plate code reducing featuresURI Templates with data conversion and binding: /project/{projectName}/tasks/{taskId}content-negotiationPUT/DELETE over POSTBody parsing, conversion and binding (plain, form-encoded, json, xml)data validationJava examplesJAX-RSRestletSpring MVC © Rule Financial 2011 27
  28. 28. RESTful Web ServicesAgenda Introduction REST principles Designing RESTful API Miscellaneous Implementions and Frameworks Implementing RESTful API © Rule Financial 2011 28
  29. 29. It’s time to code something! Confidential

×