1 / 0

Rest and Json

Rest and Json. Web 2.0. What is Web 2.0? Commonly associated with web applications that facilitate interactive information sharing, interoperability, user-centered design and collaboration on the WWW.

purity
Télécharger la présentation

Rest and Json

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Rest and Json

  2. Web 2.0 What is Web 2.0? Commonly associated with web applications that facilitate interactive information sharing, interoperability, user-centered design and collaboration on the WWW. Usually connected with the notions of read-write web, social web but also programmable web. Typical characteristics of Web 2.0 applications Users can produce and consume data on a Web 2.0 site Web is used as a participation platform Users can run software applications entirely through a Web browser Data and services can be easily combined to create mashups
  3. Web 2.0 File Sharing: Flickr (Images), YouTube (Videos), Wikipedia (Online Encyclopedia), Blogs, Open Source Community (Linux). File Management Tagging Social Websites and Communication: Facebook, LastFM, StudiVZ, LinkedIn, Xing. Open Systems: APIs, partly open source; allowextensionsbyusers.
  4. Facebook More than 400 million active users 50% of our active users log on to Facebook in any given day More than 35 million users update their status each day More than 60 million status updates posted each day More than 3 billion photos uploaded to the site each month More than 5 billion pieces of content (web links, news stories, blog posts, notes, photo albums, etc.) shared each week More than 3.5 million events created each month More than 3 million active Pages on Facebook More than 1.5 million local businesses have active Pages on Facebook 1 Taken from http://www.facebook.com/press/info.php?statistics on June 6th, 2010.
  5. Wikipedia 3,315,577 Articles (English Wikipedia)1 12,485,100 registered users1 Clever mechanisms combined with human intelligence High quality articles Self-organizedcontrol Semi-openess 1 Taken from http://en.wikipedia.org/wiki/Special:Statistics on June 6th, 2010.
  6. YouTube Exceeds 2 billion views a day1 24 hours of video uploaded every minute 70% of YouTube’s traffic comes from outside the U.S. Google paid 1.6 Billion dollars for YouTube in 2006. 1 Taken from http://www.viralblog.com/research/youtube-statistics on June 6th, 2010.
  7. Web 2.0 Large quantities of data are on the Web The data needs to be managed in an appropriate manner Retrieved, queried, analyzed, transformed, transferred, stored, etc. Technical solutions are needed to enable a truly Programmable Web Easy integration of data and services on the Web Desktop apps should work with Web apps Flickr uploadr, Google calendar update/sync Web apps should work with the other Web apps LinkedIn can import your Facebook friends Facebook can import your Dopplr trips Mashups should be enabled Easy service composition The solution can be seen in the form of Web 2.0 services
  8. Mashups
  9. Example Mashup: Housingmaps.com Housingmaps.com is a mashup created of Craigslist A centralized network of online communities, featuring free online classified advertisements – with sections devoted to jobs, housing, personals, for sale, services, community, gigs, résumés, and discussion forums. Google Maps The properties described in Craigslist are placed on a map. The true power of the applied Web 2.0 approach comes from the fact that it is "in no way affiliated with craigslist or Google” It consumes Web 2.0 services provided by Craigslist and Google
  10. Web APIs & Services Data providers usually have an incentive to offer Web APIs Web 2.0 services enable easier access to data Google maps, Geonames, phone location… Microformats (vcard, calendar, review…) Data feeds Various functionalities are offered through Web APIs Publishing, messaging, payment… Web APIs are powering the vision of the Web as a computational platform
  11. Not just the Internet Also the intranet Within a business Government Consider Utah government website and applications Services are provided and used by other services and applications Model-view-controller architecture Model can be viewed as a resource Services are used to interact with the model Business applications Distribute the services
  12. Representational State Transfer (REST) Representational State Transfer (REST) A style of software architecture for distributed hypermedia systems such as the World Wide Web. REST is basically client/server architectural style Requests and responses are built around the transfer of "representations" of "resources". Architectural style means Set of architectural constraints. Not a concrete architecture. An architecture may adopt REST constraints. HTTP is the main and the best example of a REST style implementation But it should not be confused with REST
  13. REST principles Information is organized in the form of resources Sources of specific information, Referenced with a global identifier (e.g., a URI in HTTP). Components of the network (user agents and origin servers) communicate via a standardized interface (e.g., HTTP) exchange representations of these resources (the actual documents conveying the information). Any number of connectors (e.g., clients, servers, caches, tunnels, etc.) can mediate the request, but each does so without being concern about anything but its own request an application can interact with a resource by knowing two things: the identifier of the resource and the action required no need to know whether there are caches, proxies, gateways, firewalls, tunnels, or anything else between it and resource The application needs to understand the format of the information (representation) returned.
  14. REST Architecture Client-server Separation of concerns Clients are separated from servers by a uniform interface. Networking Clients are not concerned with data storage, which remains internal to each server the portability of client code is improved. Servers are not concerned with the user interface or user state servers can be simpler and more scalable. Independent evolution Servers and clients may also be replaced and developed independently, as long as the interface is not altered.
  15. REST Architecture Stateless communication Scalability, reliability No client context being stored on the server between requests. Each request from any client contains all of the information necessary to service the request. Resources are conversationally stateless Any conversational state is held in the client. Uniform Interface Simplicity (vs. efficiency) Large-grained hypermedia data transfer Example: Create, Retrieve, Update, Delete
  16. REST Architecture Caching Efficiency, scalability Well-managed caching partially or completely eliminates some client-server interactions, further improving scalability and performance. Consistency issues As on the World Wide Web, clients are able to cache responses. Responses must implicitly or explicitly, define themselves as cacheable or not, to prevent clients reusing stale or inappropriate data in response to further requests. Code-on-demand Extending client functionality Servers are able to temporarily extend or customize the functionality of a client by transferring to it logic that it can execute. Examples may include compiled components such as Java applets and client-side scripts such as JavaScript.
  17. RESTfulWeb Service A RESTful Web service is: A set of Web resources. Interlinked. Data-centric, not functionality-centric. Machine-oriented.
  18. Hotel booking service hotel info search results service description payment my bookings confirmation Example: hotel booking
  19. Example: hotel booking Hotel booking workflow Retrieve service description Submit search criteria according to description Retrieve linked details of interesting hotels Submit payment details according to selected rate description Retrieve confirmation of booking 2b. Retrieve list of user's bookings
  20. Example: hotel booking hypermedia -> operations search(date, city)  list of hotels & rates getHotelDetails(hotel)  hotel details reserve(rate, creditCard)  confirmationID getConfirmationDetails(confID)  confirmation details listMyBookings()  list of confirmationIDs
  21. REST Simple Typically, each action is a url Arguments often become part of the query string in a GET HTTP request methods indicate the desired action to be performed on the identified resource: GET Requests a representation of the specified resource. GET should not be used for operations that cause side-effects (problematic with robots and crawlers). Those operations are called safe operations. POST Submits data to be processed (e.g., from an HTML form) to the identified resource. The data is included in the body of the request. PUT Uploads a representation of the specified resource. DELETE Deletes the specified resource.
  22. REST Issues Security Stateless Authentication? Authorization? Integrity? etc Control Data Transfer JSON, XML, etc. Security issues here too
  23. REST - Not a Standard REST is not a standard You will not see the W3C putting out a REST specification. You will not see IBM or Microsoft or Sun selling a REST developer's toolkit. REST is just a design pattern You can't bottle up a pattern. You can only understand it and design your Web services to it. REST does prescribe the use of standards: HTTP URL XML/HTML/GIF/JPEG/etc.(Resource Representations) text/xml, text/html, image/gif, image/jpeg, etc. (Resource Types, MIME Types)
  24. Learn by Example The REST design pattern is best explained with an example. Example: a company deploying three Web services using the REST design pattern.
  25. Parts Depot Web Services Parts Depot, Inc has deployed some web services to enable its customers to: get a list of parts get detailed information about a particular part submit a Purchase Order (PO)
  26. URL 2 URL 1 HTTP GET request HTTP GET request Response (HTML/XML doc) Response (HTML/XML doc) HTTP response HTTP response URL to submitted PO HTTP response The REST way of Designing the Web Services Parts List Part Data Web Server PO (HTML/XML) PO URL 3 HTTP POST
  27. Web Service for Clients to Retrieve a List of Parts Service: Get a list of parts The web service makes available a URL to a parts list resource. A client uses this URL to get the parts list: http://www.parts-depot.com/parts Note that how the web service generates the parts list is completely transparent to the client. This is loose coupling.
  28. REST Fundamentals Create a resource for every service. Identify each resource using a URL.
  29. Data Returned - Parts List <?xml version="1.0"?> <Parts> <Part id="00345" href="http://www.parts-depot.com/parts/00345"/> <Part id="00346" href="http://www.parts-depot.com/parts/00346"/> <Part id="00347" href="http://www.parts-depot.com/parts/00347"/> <Part id="00348" href="http://www.parts-depot.com/parts/00348"/> </Parts> Note that the parts list has links to get detailed info about each part. This is a key feature of the REST design pattern. The client transfers from one state to the next by examining and choosing from among the alternative URLs in the response document.
  30. Web Service for Clients to Retrieve a Particular Part Service: Get detailed information about a particular part The web service makes available a URL to each part resource. For example, here's how a client requests a specific part: http://www.parts-depot.com/parts/00345
  31. Data Returned - Part 00345 <?xml version="1.0"?> <Part> <Part-ID>00345</Part-ID> <Name>Widget-A</Name> <Description>This part is used within the frap assembly</Description> <Specification href="http://www.parts-depot.com/parts/00345/specification"/> <UnitCost currency="USD">0.10</UnitCost> <Quantity>10</Quantity> </Part> Again observe how this data is linked to still more data - the specification for this part may be found by traversing the hyperlink. Each response document allows the client to drill down to get more detailed information.
  32. Questions and Answers

  33. Question What if Parts Depot has a million parts, will there be a million static pages? For example: http://www.parts-depot/parts/000000 http://www.parts-depot/parts/000001 ... http://www.parts-depot/parts/999999
  34. Answer We need to distinguish between a logical and a physical entity. The above URLs are logical. They express what resource is desired. They do not identify a physical object. The advantage of using a logical identifier (URL) is that changes to the underlying implementation of the resource will be transparent to clients (that's loose coupling!). Quite likely, Parts Depot will store all parts data in a database. Code at the Parts Depot web site will receive each logical URL request, parse it to determine which part is being requested, query the database, and generate the part response document to return to the client.
  35. Answer (cont.) Contrast the above logical URLs with these physical URLs: http://www.parts-depot/parts/000000.html http://www.parts-depot/parts/000001.html ... http://www.parts-depot/parts/999999.html These URLs are clearly pointing to physical (HTML) pages. If there are a million parts it will not be very attractive to have a million static pages. Furthermore, changes to how these parts data is represented will effect all clients that were using the old representation.
  36. Question What if I have a complex query? For example: Show me all parts whose unit cost is under $0.50 and for which the quantity is less than 10 How would you do that with a simple URL?
  37. Answer For complex queries, Parts Depot will provide a service (resource) for a client to retrieve a form that the client then fills in. When the client hits "Submit" the browser will gather up the client’s responses and generate a URL based on the responses. Thus, oftentimes the client doesn't generate the URL (think about using Amazon - you start by entering the URL to amazon.com; from then on you simply fill in forms, and the URLs are automatically created for you).
  38. Summary of theREST Design Pattern

  39. The REST Design Pattern Create a resource for every service. Uniquely identify each resource with a logical URL. Design your information to link to other information. That is, the information that a resource returns to a client should link to other information in a network of related information.
  40. The REST Design Pattern (cont.) All interactions between a client and a web service are done with simple operations. Most web interactions are done using HTTP and just four operations: retrieve information (HTTP GET) create information (HTTP PUT) update information (HTTP POST) delete information (HTTP DELETE)
  41. json

    Data transfer encoding
  42. JSON JavaScript Object Notation Minimal Text-based. Subset of JavaScript Language Independent. Light-weight. Easy to parse. “JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate” – JSON.org JSON is a subset of JavaScript
  43. What does it look like? { "firstName": "John", "lastName": "Smith", "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": 10021 }, "phoneNumbers": [ "212 555-1234", "646 555-4567" ] } Name/Value Pairs Child properties String Array Number data type
  44. Values Strings Numbers Booleans Objects Arrays null
  45. Value
  46. Strings Sequence of 0 or more Unicode characters No separate character type A character is represented as a string with a length of 1 Wrapped in "double quotes" Backslash escapement
  47. String
  48. Numbers Integer Real Scientific No octal or hex No NaN or Infinity Use null instead
  49. Number
  50. Booleans true false null A value that isn't anything
  51. Object Objects are unordered containers of key/value pairs Objects are wrapped in { } , separates key/value pairs : separates keys and values Keys are strings Values are JSON values struct, record, hashtable, object
  52. Object
  53. Object {"name":"Jack B. Nimble","at large": true,"grade":"A","level":3, "format":{"type":"rect","width":1920, "height":1080,"interlace":false, "framerate":24}}
  54. Object { "name": "Jack B. Nimble", "at large": true, "grade": "A", "format": { "type": "rect", "width": 1920, "height": 1080, "interlace": false, "framerate": 24 } }
  55. Array Arrays are ordered sequences of values Arrays are wrapped in [] , separates values JSON does not talk about indexing. An implementation can start array indexing at 0 or 1.
  56. Array
  57. Array ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] [ [0, -1, 0], [1, 0, 0], [0, 0, 1] ]
  58. Arrays vs Objects Use objects when the key names are arbitrary strings. Use arrays when the key names are sequential integers.
  59. JSON in Ajax & Javascript XMLHttpRequest Obtain responseText Parse the responseText responseData= JSON.parse(responseText); jsonstring = '{ "name": "Jack B. Nimble", "at large": true, "grade": "A", "format": { "type": "rect", "width": 1920, "height": 1080, "interlace": false, "framerate": 24 } }'; //jsonobject = eval('(' + jsonstring + ')'); jsonobject = JSON.parse(jsonstring); document.write("The object<br>"); document.write("name: ", jsonobject.name, "<br>"); document.write("grade: ", jsonobject.grade, "<br>"); document.write("format: ", jsonobject.format, "<br>");
  60. Security Is it safe to use eval with XMLHttpRequest? The JSON data comes from the same server that vended the page. eval of the data is no less secure than the original html. Best to use a parser JSON.parse(string)instead of eval. Never trust the client The client cannot and will not keep our secrets and cannot and will not protect our interests. Do not trust machines not under your absolute control. The server must validate everything the client tells it.
  61. JSON Simple, common representation of data. Communication between servers and browser clients. Communication between peers. Language independent data interchange. Simultaneously human- and machine-readable format; Support for Unicode, allowing almost any information in any human language to be communicated; Self-documenting format that describes structure and field names as well as specific values; Strict syntax and parsing requirements that allow the necessary parsing algorithms to remain simple, efficient, and consistent; Ability to represent the most general computer science data structures: records, lists and trees.
  62. JSON vs. XML
  63. JSON vs. XML which to use? Scenario 1: You have a website (say Twitter.com) and you want to expose a public API to build apps.
  64. JSON vs. XML which to use? Scenario 2: You have a website (say gmail.com) and your front end needs to show entries from a mailbox, but needs to be dynamic and so you will use a lot of JavaScript.
  65. JSON vs. XML Which of them should you use? Use Both – They both have strengths and weaknesses and you need to identify when one is stronger than the other.
More Related