representational state transfer. resources representations verbs links headers http status...
TRANSCRIPT
![Page 1: REpresentational State Transfer. Resources Representations Verbs Links Headers HTTP Status Codes](https://reader036.vdocument.in/reader036/viewer/2022082820/5697bf891a28abf838c8a1cf/html5/thumbnails/1.jpg)
RESTREpresentational State Transfer
![Page 2: REpresentational State Transfer. Resources Representations Verbs Links Headers HTTP Status Codes](https://reader036.vdocument.in/reader036/viewer/2022082820/5697bf891a28abf838c8a1cf/html5/thumbnails/2.jpg)
REST - Theory
Resources Representations Verbs Links Headers HTTP Status Codes
![Page 3: REpresentational State Transfer. Resources Representations Verbs Links Headers HTTP Status Codes](https://reader036.vdocument.in/reader036/viewer/2022082820/5697bf891a28abf838c8a1cf/html5/thumbnails/3.jpg)
Resources
A resource is identified by a Uri.
Uri’s are considered Opaque.
http://localhost/api/customershttp://localhost/api/customers/1
![Page 4: REpresentational State Transfer. Resources Representations Verbs Links Headers HTTP Status Codes](https://reader036.vdocument.in/reader036/viewer/2022082820/5697bf891a28abf838c8a1cf/html5/thumbnails/4.jpg)
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>
![Page 5: REpresentational State Transfer. Resources Representations Verbs Links Headers HTTP Status Codes](https://reader036.vdocument.in/reader036/viewer/2022082820/5697bf891a28abf838c8a1cf/html5/thumbnails/5.jpg)
Verbs
Conceptual SQL HTTP (REST)
Find SELECT GET
Create INSERT POST ... Or PUT
Update UPDATE PUT
Destroy DELETE DELETE
HTTP Verbs are used to operate on resources.
GET must be Side Effect Free PUT must be Idempotent.
![Page 6: REpresentational State Transfer. Resources Representations Verbs Links Headers HTTP Status Codes](https://reader036.vdocument.in/reader036/viewer/2022082820/5697bf891a28abf838c8a1cf/html5/thumbnails/6.jpg)
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.
![Page 7: REpresentational State Transfer. Resources Representations Verbs Links Headers HTTP Status Codes](https://reader036.vdocument.in/reader036/viewer/2022082820/5697bf891a28abf838c8a1cf/html5/thumbnails/7.jpg)
Headers
If-Modified-Since Etag Cache-Control Accept Content-Type Authorization
![Page 8: REpresentational State Transfer. Resources Representations Verbs Links Headers HTTP Status Codes](https://reader036.vdocument.in/reader036/viewer/2022082820/5697bf891a28abf838c8a1cf/html5/thumbnails/8.jpg)
HTTP Status Codes (Just a few...)
Code Name Description
200 OK Successful Request.
201 Created New resource created, returns Uri.
202 Accepted Request accepted, but resources haven’t been created yet.
304 Not Modified Conditional get, resource is unchanged (so we can use a previously cached resource)
400 Bad Request Error in request(i.e. request failed validation)
404 Not Found Resource was not found.
405 Method Not Allowed
i.e. If you try to delete a read-only resource, or the whole resource container.
500 Internal Server Error
Something else went wrong (default status for errors in WCF).
![Page 9: REpresentational State Transfer. Resources Representations Verbs Links Headers HTTP Status Codes](https://reader036.vdocument.in/reader036/viewer/2022082820/5697bf891a28abf838c8a1cf/html5/thumbnails/9.jpg)
Demos
WCF – Building a Simple Rest Service
Linq & REST – ADO.Net Data Services▪Entity Framework▪Custom classes
MVC meets REST - MonoRail OAuth – RESTful Authorization
![Page 10: REpresentational State Transfer. Resources Representations Verbs Links Headers HTTP Status Codes](https://reader036.vdocument.in/reader036/viewer/2022082820/5697bf891a28abf838c8a1cf/html5/thumbnails/10.jpg)
Demo #1
WCF Demo - A quick tour of a WCF REST service. webHttpBinding WebGet vs WebInvoke UriTemplates GET, POST, PUT, & Delete Changing the representation.
![Page 11: REpresentational State Transfer. Resources Representations Verbs Links Headers HTTP Status Codes](https://reader036.vdocument.in/reader036/viewer/2022082820/5697bf891a28abf838c8a1cf/html5/thumbnails/11.jpg)
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.
![Page 12: REpresentational State Transfer. Resources Representations Verbs Links Headers HTTP Status Codes](https://reader036.vdocument.in/reader036/viewer/2022082820/5697bf891a28abf838c8a1cf/html5/thumbnails/12.jpg)
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.
![Page 13: REpresentational State Transfer. Resources Representations Verbs Links Headers HTTP Status Codes](https://reader036.vdocument.in/reader036/viewer/2022082820/5697bf891a28abf838c8a1cf/html5/thumbnails/13.jpg)
Demo #2
ADO.Net Data Services (Was Astoria) Building Service using ADO.Net Entity
Framework. Exploring Query Syntax.
![Page 14: REpresentational State Transfer. Resources Representations Verbs Links Headers HTTP Status Codes](https://reader036.vdocument.in/reader036/viewer/2022082820/5697bf891a28abf838c8a1cf/html5/thumbnails/14.jpg)
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(IDataServiceConfiguration config){ config.SetEntitySetAccessRule("*", EntitySetRights.All); config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);}
![Page 15: REpresentational State Transfer. Resources Representations Verbs Links Headers HTTP Status Codes](https://reader036.vdocument.in/reader036/viewer/2022082820/5697bf891a28abf838c8a1cf/html5/thumbnails/15.jpg)
Querying -Simple
Uri Conceptual
/ List of types / tables - “Workspace” in AtomPub.
/Employee List of instances / rows - “Collection” or “Feed’ in AtomPub.
/Employee(1) Instance / Row by Key(s) – Collection “Member” or “Entry” in AtomPub
/Employee(1)/Contact Foreign Key / Relationship
/Employee(1)/Gender Property / Column
/ProductProductPhoto(ProductID=332,ProductPhotoID=160)
Instance (Composite Key)
/Product?$orderby=Name Order the results by the Name property.
/Product?$orderby=ListPrice desc, Name
Order by ListPrice in descending Order, then Name ascending.
![Page 16: REpresentational State Transfer. Resources Representations Verbs Links Headers HTTP Status Codes](https://reader036.vdocument.in/reader036/viewer/2022082820/5697bf891a28abf838c8a1cf/html5/thumbnails/16.jpg)
Querying – Advanced
Uri Conceptual
/Product?$filter=contains(Name,'wheel')
All products matching filter:.Select(p=>p.Name.Contains(“wheel’))
/Shift(1)/EmployeeDepartmentHistory?$filter=EmployeeID eq 30
Navigated Relationship with clause, same as:
Shifs.Select(s => s.Id == 1).SelectMany(s => s.EmployeeDepartmentHistory).Where(history => history.EmployeeID == 30)
/Product(528)?$expand=BillOfMaterials
Will expand the Navigation Property in-line instead of linking to it.
/Product?$top=20 Top 20 results, same as: .Take(20)
/Product?$skip=20&$take=10 Display page 3 (with page size of 10) same as: .Skip(20).Take(10)
![Page 17: REpresentational State Transfer. Resources Representations Verbs Links Headers HTTP Status Codes](https://reader036.vdocument.in/reader036/viewer/2022082820/5697bf891a28abf838c8a1cf/html5/thumbnails/17.jpg)
Custom Objects – Demo #3
ADO.Net Data Services isn’t limited to the EF.
Anything implementing IQueryable interface will work too.
Let’s try!
![Page 18: REpresentational State Transfer. Resources Representations Verbs Links Headers HTTP Status Codes](https://reader036.vdocument.in/reader036/viewer/2022082820/5697bf891a28abf838c8a1cf/html5/thumbnails/18.jpg)
Implementation
![Page 19: REpresentational State Transfer. Resources Representations Verbs Links Headers HTTP Status Codes](https://reader036.vdocument.in/reader036/viewer/2022082820/5697bf891a28abf838c8a1cf/html5/thumbnails/19.jpg)
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).