REST & .NET

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

description

REST & .NET. James Crisp .NET Practice Lead ThoughtWorks Australia. James Crisp. The Web. *-ilities. Scalability. Recoverability & Reliability. Security. Discoverability. How to bring to Web services?. Tunneling. RPC URLs POX SOAP & WS-*. RE presentational S tate - PowerPoint PPT Presentation

Transcript of REST & .NET

Page 1: REST  &  .NET

REST &

.NET

James Crisp.NET Practice Lead

ThoughtWorks Australia

Page 2: REST  &  .NET

James Crisp

Page 3: REST  &  .NET
Page 4: REST  &  .NET

The Web

Page 5: REST  &  .NET

*-ilities

Page 6: REST  &  .NET

Scalability

Page 7: REST  &  .NET

Recoverability & Reliability

Page 8: REST  &  .NET

Security

Page 9: REST  &  .NET

Discoverability

Page 10: REST  &  .NET
Page 11: REST  &  .NET

How to bring toWeb services?

Page 12: REST  &  .NET

Tunneling

RPC URLs

POX

SOAP & WS-*

Page 13: REST  &  .NET

REpresentational State Transfer

Page 14: REST  &  .NET
Page 15: REST  &  .NET

“REST contributes ... the rationale behind the modern Web's software architecture...”

-- Roy Fielding

Page 16: REST  &  .NET

Resourcese.g. Person, Car, Post

Page 17: REST  &  .NET

Representations

<person> <name>Bill Gates</name> <gender>m</gender></person>

Page 18: REST  &  .NET
Page 19: REST  &  .NET

Addressabilityhttp://mysite.com/people/joe_citizen

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

Page 20: REST  &  .NET

Uniform Interface

Page 21: REST  &  .NET

GET

POST

HEAD

PUT

DELETE

OPTIONS

Su

pp

ort

Page 22: REST  &  .NET

GET

GET /people/joe_citizen HTTP/1.1Host: mysite.comAccept: application/xml

Page 23: REST  &  .NET

Response200 OKContent-Type: application/xmlLast-Modified: 2008-1-1 15:00..

<person> <name>Joe Citizen</name> <age>42</age> <gender>m</gender></person>

Page 24: REST  &  .NET

HEADHEAD /people/joe_citizen HTTP/1.1Host: mysite.com

200 OKContent-Type: application/xmlLast-Modified: 2008-1-1 15:00..ETag: a32daf15-b33da2a4d

Response

Page 25: REST  &  .NET

POSTPOST /people HTTP/1.1Content-Type: text/xmlHost: mysite.com....<person> <name>Tina Jones</name> <age>25</age> <gender>F</gender></person>

Page 26: REST  &  .NET

Response

201 CREATEDLocation: /people/tina_jones

(optionally with body)

Page 27: REST  &  .NET

PUTPUT /people/tina_jones HTTP/1.1Content-Type: text/xmlHost: mysite.com....<person> <name>Tina Jones</name> <age>24</age> <gender>F</gender></person>

Page 28: REST  &  .NET

Response

200 OKLocation: /people/tina_jones

(optionally with body)

Page 29: REST  &  .NET

DELETEDELETE /people/joe_citizen HTTP/1.1Host: mysite.com

200 OKContent-Type: application/xml<admin:personDeleted> joe_citizen</admin:personDeleted>

Response

Page 30: REST  &  .NET

OPTIONS

OPTIONS /people HTTP/1.1Host: mysite.com

200 OKAllowed: GET,HEAD,POST

Response

Page 31: REST  &  .NET

Status Codes

Page 32: REST  &  .NET

2xx = All good200 – OK201 – Created

Page 33: REST  &  .NET

3xx = Redirect301 – Moved permanently307 – Temporary redirect304 – Not modified (ie, see cache)

Page 34: REST  &  .NET

4xx = Client error400 – Bad request401 – Unauthorized403 – Forbidden405 – Method not allowed409 – Conflict

Page 35: REST  &  .NET

5xx = Server error500 – Internal server error

Page 36: REST  &  .NET

Headers

Page 37: REST  &  .NET

Content-Type:text/htmlapplication/xmlimage/jpeg....

Page 38: REST  &  .NET

WWW-Authenticate:

Authorization:

Page 39: REST  &  .NET

Last-Modified:If-Modified-Since:

ETag:If-None-Match:

Page 40: REST  &  .NET

Location:

Page 41: REST  &  .NET

Describe your services?

Page 42: REST  &  .NET

URI Templateshttp://s3.amazon.com/{bucket-name}/{object-name}

http://mysite.com/users/{user-name}/photos

http://google.com/search?q={search-query}

Page 43: REST  &  .NET

Start URL + Links

https://bank.com/accounts/345095

<account> <name>Tina Jones</name> <link rel="history" href="/tjones/account_history"> <link rel="close" verb="delete" href="/tjones"></account>

Page 44: REST  &  .NET

Microformats<account> <name>Tina Jones</name> <link rel="transaction-search" href="account_history?

from={date-from}&to={date-to}">

</account>

Page 45: REST  &  .NET

State Management

http://google.com

Search

google.com/search?q=rest

google.com/search?q=rest&start=10

Next Page

Searching

Searched

More Results

Page 46: REST  &  .NET

Data Format?

Page 47: REST  &  .NET

XHTML<h1>Accounts</h1> <ul> <li> <a rel="account_details" href="/tjones">Tina Jones</a> </li>

<li>.....

Page 48: REST  &  .NET

JSON

{ "account_name": "Tina Jones", "links":

{"history": "/tjones/history"}}

Page 49: REST  &  .NET

<XML></XML>

Page 50: REST  &  .NET

Framework

Page 51: REST  &  .NET

Ordering Pizza

Page 52: REST  &  .NET

http://epizza.com

GET / HTTP/1.1Host: epizza.comContent-Type: application/xml

Page 53: REST  &  .NET

Response

200 OKLocation: http://epizza.comContent-Type: application/xmlContent-Length: ...

<epizza xmlns="/schema"> <message>Welcome!</message> <link rel="menu" href="/menu" /> <link rel="place-order" href="/orders" /><epizza>

Page 54: REST  &  .NET

Let's see the menu!

GET /menu HTTP/1.1Host: epizza.comContent-Type: application/xml

Page 55: REST  &  .NET

200 OKLocation: ...

<epizza xmlns="/schema"> <menu> <pizza name="pepperoni"> <ingredients>...</ingredients> </pizza> ... <menu> <link rel="place-order" href="/orders" /></epizza>

Menu Response

Page 56: REST  &  .NET

Ordering time!POST /orders HTTP/1.1Host: epizza.comContent-Type: application/xmlContent-Length: ...

<order xmlns="/schema"> <pizza name="pepperoni" /></order>

Page 57: REST  &  .NET

201 CreatedLocation: http://epizza.com/orders/413Content-Type: ...

<epizza xmlns="/schema"> <order> <pizza name="pepperoni" /> </order> <link rel="destination" href="/orders/413/address"></epizza>

Place Order Response

Page 58: REST  &  .NET

Beer with that?

Page 59: REST  &  .NET

OPTIONS /orders/413 HTTP/1.1Host: epizza.com

200 OKAllowed: GET, HEAD, POST, PUT

Page 60: REST  &  .NET

Add BeerPOST /orders/413 HTTP/1.1Host: epizza.comContent-Type: application/xmlContent-Length: ...

<order xmlns="/schema"> <drink name="corona" /></order>

Page 61: REST  &  .NET

200 OKLocation: http://epizza.com/orders/413Content-Type: ...

<epizza xmlns="/schema"> <order> <pizza name="pepperoni" /> <drink name="corona" /> </order> <link rel="destination" href="/orders/413/destination"></epizza>

Response

Page 62: REST  &  .NET

Destination AddressPUT /orders/413/address HTTP/1.1

Host: epizza.comContent-Type: application/xmlContent-Length: ...

<address xmlns="/schema"> <line1>35 Rue Rd</line1> <suburb>Potts Point</suburb></address>

Page 63: REST  &  .NET

Destination Response200 OKHost: epizza.comContent-Type: application/xmlContent-Length: ...

<address xmlns="/schema"> <line1>35 Rue Rd</line1> <suburb>Potts Point</suburb></address>

Page 64: REST  &  .NET

200 OKLocation: http://epizza.com/orders/413Content-Type: ...

<epizza xmlns="/schema"> <order> <pizza name="pepperoni" /> <drink name="corona" /> </order></epizza>

GET /orders/413

Page 65: REST  &  .NET
Page 66: REST  &  .NET

Can we?

OPTIONS /orders/413 HTTP/1.1Host: epizza.com

200 OKAllowed: GET, HEAD

Page 67: REST  &  .NET

Too late!

Page 68: REST  &  .NET
Page 69: REST  &  .NET

Back at theePizza

Kitchen..

Page 70: REST  &  .NET

GET /orders

Page 71: REST  &  .NET

Implementation

Page 72: REST  &  .NET

Clientvar request =

(HttpWebRequest) WebRequest.Create(URL);

request.Methodrequest.ContentTyperequest.GetRequestStream()

var response = (HttpWebResponse)request.GetResponse();

response.StatusCoderesponse.Headersresponse.GetResponseStream()

Page 73: REST  &  .NET
Page 74: REST  &  .NET

Server

Page 75: REST  &  .NET

IHttpHandlerpublic class RestHandler : IHttpHandler{ public void ProcessRequest(HttpContext context) { context.Response.ContentType =

"application/xml"; if (context.Request.HttpMethod == "GET") { context.Response.Write("<xml>...</xml>"); }}

Page 76: REST  &  .NET

WCF REST

[ServiceContract]public interface IPizzaService{

[WebGet(UriTemplate="/orders/{orderId}")][OperationContract]Order GetOrder(int orderId);

}

Page 77: REST  &  .NET

Status codes?

Headers?

Links?

Microformats?

Page 78: REST  &  .NET

ASP.NET MVC

Page 79: REST  &  .NET

Routing Table

Controller Method

Model View

Page 80: REST  &  .NET

RoutesIn GlobalApplication : HttpApplication

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

... }

Page 81: REST  &  .NET

Controllerpublic 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); }}

Page 82: REST  &  .NET

Model

public class Order{ public MenuItem[] Items { get { ... }; }

public Address DeliveryAddress { get; set; }}

Page 83: REST  &  .NET

View

<%@ Page Language="C#" ...>

<epizza xmlns="/schema"> <order> <% foreach (var item in Order.Items) { %> <pizza name= "<%= item.Name %>" /> <% } %> </order> <% if (Order.DeliveryAddress == null) { %> <link rel="destination" href= ...</epizza>

Page 84: REST  &  .NET

MVC REST

Wrinkles

Page 85: REST  &  .NET
Page 86: REST  &  .NET

See also...Slides & Code

http://www.jamescrisp.org MVC http://www.asp.net/mvc/

RESTful Web Services

Page 87: REST  &  .NET

http://flickr.com/photos/misserion/2190827125/ - webhttp://flickr.com/photos/hyougushi/412600118/ - bridgehttp://en.wikipedia.org/wiki/Image:Internet_map_1024.jpg - webhttp://flickr.com/photos/gradin/3361527/ - defibrillatorhttp://flickr.com/photos/keoki/1418303458/ - padlockshttp://roadmap.cbdiforum.com/reports/protocols/ - ws* stackhttp://flickr.com/photos/chainsawpanda/1528894/ - tunnelhttp://flickr.com/photos/psd/421186578/ - fieldinghttp://flickr.com/photos/esparta/187132368/ - bill gateshttp://flickr.com/photos/acme_explosives/117276632/ - network plugshttp://flickr.com/photos/walterhertman/101489766/ - speedometershttp://flickr.com/photos/sis/5908199/ - Petco explosionhttp://flickr.com/photos/pleasewait/476776136/ - letterhttp://flickr.com/photos/bigpinkcookie/108895725/ - Eiffel towerhttp://flickr.com/photos/bala_/2077047513/ - pizza restauranthttp://flickr.com/photos/57231735@N00/201482385/ - pepperoni pizzahttp://flickr.com/photos/sheeshoo/10321250/ - coronahttp://flickr.com/photos/freddy/39340695/ - saladhttp://flickr.com/photos/huthfamily/1304336305/ - 42 pizza boxeshttp://www.crummy.com/writing/RESTful-Web-Services/cover.png – REST book

Image References

Page 88: REST  &  .NET

Questions?