1 / 19

REST

RE presentational S tate T ransfer. REST. REST - Theory. Resources Representations Verbs Links Headers HTTP Status Codes. Resources. A resource is identified by a Uri . Uri’s are considered Opaque . http://localhost/api/customers http://localhost/api/customers/1. Representations.

marilu
Télécharger la présentation

REST

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. REpresentationalState Transfer REST

  2. REST - Theory • Resources • Representations • Verbs • Links • Headers • HTTP Status Codes

  3. Resources • A resource is identified by a Uri. • Uri’s are considered Opaque. http://localhost/api/customers http://localhost/api/customers/1

  4. Representations • Fetching a resource returns a Representation. • Representations have a content type i.e. text/xml, application/json, text/html etc. <customer> <self>http://localhost/api.svc/customers/12</self> <organisation>DevDefined</organisation> <id>1</id> <orders> http://localhost/api.svc/customers/12/orders </orders> </customer>

  5. Verbs • HTTP Verbs are used to operate on resources. • GET must be Side Effect Free • PUT must be Idempotent.

  6. Links • Links are returned in representations. • Clients follow links, allowing them to transition state. • Using Links can provide elegant ways to scale out or integrate 3rd party services.

  7. Headers • If-Modified-Since • Etag • Cache-Control • Accept • Content-Type • Authorization

  8. HTTP Status Codes (Just a few...)

  9. Demos • WCF – Building a Simple Rest Service • Linq & REST – ADO.Net Data Services • Entity Framework • Custom classes • MVC meets REST - MonoRail • OAuth – RESTful Authorization

  10. Demo #1 • WCF Demo - A quick tour of a WCF REST service. • webHttpBinding • WebGetvsWebInvoke • UriTemplates • GET, POST, PUT, & Delete • Changing the representation.

  11. Fresh to Fiddler? • Fiddler is very useful when developing REST services. • There are some catches for first time users: • Disable IPV6 in Fiddler when using Cassini (it doesn’t like the ::1 loopback address) • Use “.” after localhost when testing with IE 7 to stop proxy being bypassed. • i.e. http://localhost.:8080/api.svc/customers/ • FireFox users - you need to manually configure proxy settings.

  12. WCF Gotchas • Exceptions end up as 500 errors on client. • You need to explicitly set status for non-server errors (i.e. For invalid requests) • Uri Templates are fussy • Trailing slashes will cause a match to fail, contrary to many peoples expectations. • HTTP Method names are case-sensitive. • Using “Post” instead of “POST” can make for confusing 405 errors  • The input/output Representation format is defined at the contract operation level. • Targeting multiple representations “across the board” is ugly. • Support for linking resources is non-existent.

  13. Demo #2 • ADO.Net Data Services (Was Astoria) • Building Service using ADO.Net Entity Framework. • Exploring Query Syntax.

  14. Entity Framework • Using EF - Out of scope  • However... Provides easy way to quickly play with Astoria. • Add Item -> Entity Model • Use AdventureWorks database, just the tables should do, call it “AdventureWorks” • Add Item -> ADO.Net Data Service • Edit the .cs file, set entities class for service to AdventureWorksModel.AdventureWorksEntities. • Alter InitializeService method to look like this(Don’t do this for production use) public static void InitializeService(IDataServiceConfigurationconfig) { config.SetEntitySetAccessRule("*", EntitySetRights.All); config.SetServiceOperationAccessRule("*", ServiceOperationRights.All); }

  15. Querying -Simple

  16. Querying – Advanced

  17. Custom Objects – Demo #3 • ADO.Net Data Services isn’t limited to the EF. • Anything implementing IQueryable interface will work too. • Let’s try!

  18. Implementation

  19. ADO.Net Data Entities - Client • Entities comes with a .Net Client. • Found in the System.Data.Services.Client assembly. • Client allows Linq -> Uri -> Linq -> Whatever. • With different Types on the client and Server! • Client types can be POCO objects, even if the server side representations are not. • [DataServiceKey(...)] attribute required to support non-GET requests. • Client implements a “unit of work” like pattern, allowing changes to be collected and then persisted at once. • Changes can be batched in a single request (though this can be viewed as a perversion of HTTP principles).

More Related