Presentation is loading. Please wait.

Presentation is loading. Please wait.

REST &.NET James Crisp.NET Practice Lead ThoughtWorks Australia.

Similar presentations


Presentation on theme: "REST &.NET James Crisp.NET Practice Lead ThoughtWorks Australia."— Presentation transcript:

1 REST &.NET James Crisp.NET Practice Lead ThoughtWorks Australia

2 James Crisp

3

4 The Web

5 *-ilities

6 Scalability

7 Recoverability & Reliability

8 Security

9 Discoverability

10

11 How to bring to Web services?

12 Tunneling RPC URLs POX SOAP & WS-*

13 REpresentational State Transfer

14

15 REST contributes... the rationale behind the modern Web's software architecture... -- Roy Fielding

16 Resources e.g. Person, Car, Post

17 Representations Bill Gates m

18

19 Addressability http://mysite.com/people/joe_citizen http://mysite.com/people/joe_citizen/friends

20 Uniform Interface

21 GET POST HEAD PUT DELETE OPTIONS Support

22 GET GET /people/joe_citizen HTTP/1.1 Host: mysite.com Accept: application/xml

23 Response 200 OK Content-Type: application/xml Last-Modified: 2008-1-1 15:00.. Joe Citizen 42 m

24 HEAD HEAD /people/joe_citizen HTTP/1.1 Host: mysite.com 200 OK Content-Type: application/xml Last-Modified: 2008-1-1 15:00.. ETag: a32daf15-b33da2a4d Response

25 POST POST /people HTTP/1.1 Content-Type: text/xml Host: mysite.com.... Tina Jones 25 F

26 Response 201 CREATED Location: /people/tina_jones (optionally with body)

27 PUT PUT /people/tina_jones HTTP/1.1 Content-Type: text/xml Host: mysite.com.... Tina Jones 24 F

28 Response 200 OK Location: /people/tina_jones (optionally with body)

29 DELETE DELETE /people/joe_citizen HTTP/1.1 Host: mysite.com 200 OK Content-Type: application/xml joe_citizen Response

30 OPTIONS OPTIONS /people HTTP/1.1 Host: mysite.com 200 OK Allowed: GET,HEAD,POST Response

31 Status Codes

32 2xx = All good 200 – OK 201 – Created

33 3xx = Redirect 301 – Moved permanently 307 – Temporary redirect 304 – Not modified (ie, see cache)

34 4xx = Client error 400 – Bad request 401 – Unauthorized 403 – Forbidden 405 – Method not allowed 409 – Conflict

35 5xx = Server error 500 – Internal server error

36 Headers

37 Content-Type: text/html application/xml image/jpeg....

38 WWW-Authenticate: Authorization:

39 Last-Modified: If-Modified-Since: ETag: If-None-Match:

40 Location:

41 Describe your services?

42 URI Templates http://s3.amazon.com/{bucket-name}/{object-name} http://mysite.com/users/{user-name}/photos http://google.com/search?q={search-query}

43 Start URL + Links https://bank.com/accounts/345095 Tina Jones

44 Microformats Tina Jones

45 State Management http://google.com Search google.com/ search?q=rest google.com/search?q=rest& start=10 Next Page Searching Searched More Results

46 Data Format?

47 XHTML Accounts Tina Jones.....

48 JSON { "account_name": "Tina Jones", "links": {"history": "/tjones/history"} }

49

50 Framework

51 Ordering Pizza

52 http://epizza.com GET / HTTP/1.1 Host: epizza.com Content-Type: application/xml

53 Response 200 OK Location: http://epizza.com Content-Type: application/xml Content-Length:... Welcome!

54 Let's see the menu! GET /menu HTTP/1.1 Host: epizza.com Content-Type: application/xml

55 200 OK Location:......... Menu Response

56 Ordering time! POST /orders HTTP/1.1 Host: epizza.com Content-Type: application/xml Content-Length:...

57 201 Created Location: http://epizza.com/orders/413 Content-Type:... Place Order Response

58 Beer with that?

59 OPTIONS /orders/413 HTTP/1.1 Host: epizza.com 200 OK Allowed: GET, HEAD, POST, PUT

60 Add Beer POST /orders/413 HTTP/1.1 Host: epizza.com Content-Type: application/xml Content-Length:...

61 200 OK Location: http://epizza.com/orders/413 Content-Type:... Response

62 Destination Address PUT /orders/413/address HTTP/1.1 Host: epizza.com Content-Type: application/xml Content-Length:... 35 Rue Rd Potts Point

63 Destination Response 200 OK Host: epizza.com Content-Type: application/xml Content-Length:... 35 Rue Rd Potts Point

64 200 OK Location: http://epizza.com/orders/413 Content-Type:... GET /orders/413

65

66 Can we? OPTIONS /orders/413 HTTP/1.1 Host: epizza.com 200 OK Allowed: GET, HEAD

67 Too late!

68

69 Back at the ePizza Kitchen..

70 GET /orders

71 Implementation

72 Client var request = (HttpWebRequest) WebRequest.Create(URL); request.Method request.ContentType request.GetRequestStream() var response = (HttpWebResponse)request.GetResponse(); response.StatusCode response.Headers response.GetResponseStream()

73

74 Server

75 IHttpHandler public class RestHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "application/xml"; if (context.Request.HttpMethod == "GET") { context.Response.Write("... "); }

76 WCF REST [ServiceContract] public interface IPizzaService { [WebGet(UriTemplate="/orders/{orderId}")] [OperationContract] Order GetOrder(int orderId); }

77 Status codes? Headers? Links? Microformats?

78 ASP.NET MVC

79 Routing Table Controller Method ModelView

80 Routes In GlobalApplication : HttpApplication void RegisterRoutes(RouteCollection routes) { routes.MapRoute( "AddToOrder", "orders/{id}", new { controller = "Orders", action = "AddToOrder" }, new { httpMethod = new HttpMethodConstraint("POST")});... }

81 Controller public class OrdersController : Controller { public ActionResult AddToOrder(int id) { var order = Order.Load(id); var itemToAdd = CreateMenuItemFromRequest(); order.Items.Add(itemToAdd); order.Save(); // response.StatusCode = 200; return View(order); }

82 Model public class Order { public MenuItem[] Items { get {... }; } public Address DeliveryAddress { get; set; } }

83 View " /> { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/4/1465624/slides/slide_83.jpg", "name": "View />

84 MVC REST Wrinkles

85

86 See also... Slides & Code http://www.jamescrisp.org MVC http://www.asp.net/mvc/ RESTful Web Services

87 http://flickr.com/photos/misserion/2190827125/ - web http://flickr.com/photos/hyougushi/412600118/ - bridge http://en.wikipedia.org/wiki/Image:Internet_map_1024.jpg - web http://flickr.com/photos/gradin/3361527/ - defibrillator http://flickr.com/photos/keoki/1418303458/ - padlocks http://roadmap.cbdiforum.com/reports/protocols/ - ws* stack http://flickr.com/photos/chainsawpanda/1528894/ - tunnel http://flickr.com/photos/psd/421186578/ - fielding http://flickr.com/photos/esparta/187132368/ - bill gates http://flickr.com/photos/acme_explosives/117276632/ - network plugs http://flickr.com/photos/walterhertman/101489766/ - speedometers http://flickr.com/photos/sis/5908199/ - Petco explosion http://flickr.com/photos/pleasewait/476776136/ - letter http://flickr.com/photos/bigpinkcookie/108895725/ - Eiffel tower http://flickr.com/photos/bala_/2077047513/ - pizza restaurant http://flickr.com/photos/57231735@N00/201482385/ - pepperoni pizza http://flickr.com/photos/sheeshoo/10321250/ - corona http://flickr.com/photos/freddy/39340695/ - salad http://flickr.com/photos/huthfamily/1304336305/ - 42 pizza boxes http://www.crummy.com/writing/RESTful-Web-Services/cover.png – REST book Image References

88 Questions?


Download ppt "REST &.NET James Crisp.NET Practice Lead ThoughtWorks Australia."

Similar presentations


Ads by Google