more on distributed web services rest
play

More on Distributed Web Services: REST Architecture REST, or - PowerPoint PPT Presentation

Web Services REresentation State Transfer (REST) More on Distributed Web Services: REST Architecture REST, or Representational State Transfer, is a distributed communication architecture fast becoming the lingua franca for Cloud Computing. The


  1. Web Services REresentation State Transfer (REST) More on Distributed Web Services: REST Architecture REST, or Representational State Transfer, is a distributed communication architecture fast becoming the lingua franca for Cloud Computing. The central abstraction in REST is the Resource. A resource in the RESTful sense is anything that has an URI. In practice, a resource is an informational item that has hyperlinks to it. 1 / 17

  2. Web Services REresentation State Transfer (REST) Contrast Between SOAP & REST Essence: REST & SOAP are quite different SOAP is a messaging protocol, REST is a architectural style. SOAP uses WSDL for comms, REST exchanges data in XML/JSON a . SOAP calls services by RPCs, REST invokes them via URL path. SOAP can be over other protocols, REST is only over HTTP . a a more lightweight, human–parsable data–exchange format than XML 2 / 17

  3. Web Services REresentation State Transfer (REST) Contrast Between SOAP & REST Cont’d REST tries to isolate complexity at endpoints (Clients & Service): Service: could need to logic/computation to process XML to maintain Resources & generate their representation. Client: may have to process XML to extract info from XML representation. But this complexity is kept from the transport level. SOAP complicates the transport level as a SOAP message is encapsulated as transport message body. 3 / 17

  4. Web Services REresentation State Transfer (REST) More on Resources Resources have certain properties: Representation: usually MIME (commonly text/html , text/xml ). State: i.e. they are mutable. Note: In a RESTful request on a resource, the resource itself stays on the service-side. If the request succeeds, the requester gets the resource’s representation (this transfers from the server to the requester machine). For a successful request to read a resource, it’s typed representation (e.g. text/html ) transfers from the resource’s server to the requester. 4 / 17

  5. Web Services REresentation State Transfer (REST) Recall: HTTP (REST) Operations Essence Communication in the Web is generally based on HTTP . Operation Idempotent? Description Head Yes Request to return the header of a document Get Yes Request to return a document to the client Put Yes Request to store a document Post No Provide data that are to be added to a document (collection) Delete Yes Request to delete a document 5 / 17

  6. Web Services REresentation State Transfer (REST) Example in REST for the resource. Each HTTP request includes a verb to indicate which CRUD operation Shown below is a client comms with a resource & the typed responses should be performed on the resource. A good representation is precisely one that it gets: matches the requested operation and captures the resource’s state in some appropriate way. For example, in this depiction a GET request could return my biography as a GET request could return my biography ( html or video). hacker as either an HTML document or a short video summary. The video would fail to capture the state of the resource if it depicted, say, only the major disasters in my Typical html resource representations could have other resource links. brother’s career rather than those in my own. A typical HTML representation of the These could then be the target of HTTP requests with appropriate resource would include hyperlinks to other resources, which in turn could be the target of HTTP requests with the appropriate CRUD verbs. CRUD verbs. Identifying URL: http://my.life.job/hacker Resource: My life as a hacker HTTP requests HTTP responses GET: Read MIME-typed representations of the resource such as: POST: Create GET: HTML page with my hacker’s bio PUT: Update GET: Short video of major disasters DELETE: Delete PUT: Plain text acknowledgement of update POST: Fancy HTML acknowledgement of resource creation RESTful client Figure 4-1. A small slice of a RESTful system 6 / 17 HTTP also has standard response codes, such as 404 to signal that the requested re- source could not be found, and 200 to signal that the request was handled successfully. In short, HTTP provides request verbs and MIME types for client requests and status codes (and MIME types) for service responses. Modern browsers generate only GET and POST requests. Moreover, many applications treat these two types of requests interchangeably. For example, Java s have callback methods such as and that handle GET and POST requests, re- spectively. Each callback has the same parameter types, (the key/ value pairs from the requester) and (a typed response to the re- quester). It is common to have the two callbacks execute the same code (for instance, by having one invoke the other), thereby conflating the original HTTP distinction be- tween read and create . A key guiding principle of the RESTful style is to respect the original meanings of the HTTP verbs. In particular, any GET request should be side effect-free (or, in jargon, idempotent ) because a GET is a read rather than a create , update , or delete operation. A GET as a read with no side effects is called a safe GET. The REST approach does not imply that either resources or the processing needed to generate adequate representations of them are simple. A REST-style web service might be every bit as subtle and complicated as a SOAP-based service. The RESTful approach

  7. Web Services REresentation State Transfer (REST) A Subtlety: Opacity of URIs A URI is meant to be opaque This means that the URI: http://bedrock/citizens/fred has no inherent connection to the URI: http://bedrock/citizens , although Fred happens to be a citizen of Bedrock. A Note of caution Of course, good designers devise URIs akin to what they identify, but URIs have no intrinsic hierarchical structure. URI syntax resembles that for file system navigation, but this can mislead; URIs are opaque identifiers, each naming exactly one resource. 7 / 17

  8. Web Services REresentation State Transfer (REST) A Ruby Client on a Web Service Using Ruby’s Rest Client.gem with CrunchBase REST interface Here we use Rest Client.gem (e.g. with interactive Ruby shell irb ). URI is a CrunchBase request given to Rest Client ’s Get method. If you want, emit the resp.body , (& see all JSON data returned). You can use the JSON.parse method to parse the response into a Ruby object structure. Finally, extract the desired value ( ' number of employees=50 ' here). 8 / 17

  9. Web Services REresentation State Transfer (REST) A Ruby Client on a Web Service (cont’d) Example Here, a Ruby Client uses Rest Client.gem to make life easier. #!/usr/bin/env ruby require 'rest_client' #require 'json' // don't need as rest_client.gem has all these #require 'net/http' // don't need as rest_client.gem has all these class Crunchery @record=nil def initialize (name="IBM") @name=name end def print_data (company="IBM") // default value is "IBM" base_url="http://api.crunchbase.com" key="6y hw" // my key; you have to get your own company="whatsapp" // the company url="#{base_url}/v/1/company/#{company}.js?api_key=#{key}" puts url // echo url variable to screen resp=RestClient.get url // call RestClient.get @record=JSON.parse(resp) puts @record['number_of_employees'] // output whatsapps roll no. end end if __FILE__ == $0 // Main File to run the above mg = Crunchery.new ("whatsapp") mg.print_data puts "end reached" // See output below C:\Ruby193>ruby Crunchery.rb http://api.crunchbase.com/v/1/company/whatsapp.js?api key=6y hw 50 end reached 9 / 17

  10. Web Services REresentation State Transfer (REST) A User Interface Client on a Web Service Example The RestClient UI Get ’s Bookmarks from Bibsonomy.com . Note: password is user hash from registration with Bibsonomy.com . 10 / 17

  11. Web Services REresentation State Transfer (REST) A User Interface Client on a Web Service (cont’d) Example The bookmark results of the previous Get operation. 11 / 17

  12. Web Services REresentation State Transfer (REST) A User Interface Client on a Web Service (cont’d) Example RestClient uses Post to add a Bookmark to Bibsonomy.com . Note: Change content-type to application/xml & charset to UTF-8 . Nb! 12 / 17

  13. Web Services REresentation State Transfer (REST) A User Interface Client on a Web Service (cont’d) Example The bookmark results of the previous Post operation. Success! Take note of hash – needed later! 13 / 17

  14. Web Services REresentation State Transfer (REST) A User Interface Client on a Web Service (cont’d) Example RestClient uses Put to change a Bookmark thus http://www.bibsonomy.org/api/users/martycrane/posts/hash Use of hash to alter/delete Nb! 14 / 17

  15. Web Services REresentation State Transfer (REST) A User Interface Client on a Web Service (cont’d) Example The bookmark results of the previous Put operation. New Tag: “ HypochondriaStuff ” Success! 15 / 17

  16. Web Services REresentation State Transfer (REST) A User Interface Client on a Web Service (cont’d) Example RestClient uses Delete to remove a Bookmark thus http://www.bibsonomy.org/api/users/martycrane/posts/hash Use of hash to alter/delete Nb! 16 / 17

  17. Web Services REresentation State Transfer (REST) A User Interface Client on a Web Service (cont’d) Example The bookmark results of the previous Delete operation. Success! 17 / 17

Recommend


More recommend