REST and Web API

18
REST and Web API I Roman Kalita [email protected] @rkregfor Skype: kalita_roman

description

Why we need REST? Why speak about it? Roman Kalita

Transcript of REST and Web API

Page 1: REST and Web API

REST and Web APIIRoman [email protected]@rkregforSkype: kalita_roman

Page 2: REST and Web API

Why we need REST?Why speak about it?I

Page 3: REST and Web API
Page 4: REST and Web API

From web sites to web ApisFrom mobile to data

Page 5: REST and Web API

Representational State Transfer

Architectural style based on HTTP protocol usage and principles of The Web

Tightly coupled to HTTP protocol

Lightweight

Stateless

Simple caching “from the box”

Easy to consume (e.g. mobile devices, JavaScript etc.) Goal: scaling, loose coupling and

compose functionality across service boundaries

Page 6: REST and Web API

Level3: Hypermedia controls

Level2: HTTP Verbs

Level1: Resources

Level0: POX, Single URI

Richardson Maturity Model I

Page 7: REST and Web API

Level0POX, Single URI, Transport

The systems focus are service end point URI and one HTTP verb (likely POST verb) for communication.

ClientServi

ce

AppointmentService

GetOpenTimeSlot

MakeAppointment

TimeSlotsList

ReservationResult

Page 8: REST and Web API

Level1Resources

Introduces resources, URIs per resource, but one HTTP verb. Handling complexity by using divide and conquer,

breaking a large endpoint down into multiple resources.

Client

Doctors

http://klinik.com/

doctors/eberwein

slots/15092012

TimeSlotsList

ReservationResultSlots

POST

POST

Page 9: REST and Web API

Level2HTTP Verbs

The system relies on more HTTP verbs and HTTP response codes on each resource.

Client

Doctors

http://klinik.com/doctors/eberwein?date=…&open=1

slots/15092012

200 OK TimeSlotsList

204 CREATED ReservationResultSlots

GET

POST

Page 10: REST and Web API

Level3Hypermedia

Introduces discoverability, providing a way of making a protocol more self-documenting.

Client

Doctors

http://klinik.com/doctors/eberwein?date=…&open=1

slots/15092012

200 OK TimeSlotsList<link rel = "/linkrels/slot/book"

uri = "/slots/5678"/>

204 CREATED ReservationResult

Slots

GET

POST

Page 11: REST and Web API

Cooking REST on .NET – Web API

URL Routing to produce clean URLs

Content Negotiation based on Accept headers for request and response serialization

Support for a host of supported output formats including JSON, XML, ATOM

Extensible (easy to add new formats, custom handlers, routing, error handlers)

Self-hostable in non-Web applications

Testable using testing concepts similar to MVC

Page 12: REST and Web API

Web API Routing

RouteTable.Routes.MapHttpRoute( name: "UploadDownlaodOnlyTrhougthTransport", routeTemplate: "api/transports/{controller}/{id}", defaults: new { id = RouteParameter.Optional }, constraints: new { controller = "uploads|downloads" } );

RouteTable.Routes.MapHttpRoute( name: "AllExceptUploadaDownload", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional }, constraints: new { controller = @"^((?!(downloads|uploads)).)*$" } );

Routing “à la” ASP.NET MVC Register your routs from Application_Start in Global.asax.cs

Page 13: REST and Web API

Web API Controller

public class ProductsController : ApiController { public IEnumerable<Product> GetAllProducts(){} public Product GetProduct(int id) {} public HttpResponseMessage PostProduct(Product item) {} public void PutProduct(int id, Product contact) {} [AcceptVerbs("GET", "HEAD")] public Product FindProduct(id) {} [HttpGet] public Product FindProduct(id) {}}

Derived from ApiController Actions are mapped to URIs

via naming convention or attributes Deep support for more advanced HTTP features

via HttpResponseMessage and HttpRequestMessage.

Page 14: REST and Web API

OData support Support “in the box” for OData $top, $skip, $filter, $orderby,

advanced support in pre-release Install-Package Microsoft.AspNet.WebApi.OData -Pre Till release - as alternative use of WCF Data Services

Queryable attribute to enable OData queries

http://localhost:38856/api/products?$filter=category+eq+'Toys'

[Queryable] public IQueryable<Product> GetAllProducts() { return repository.GetAll().AsQueryable(); }

Page 15: REST and Web API

Web API extensiblity Message handlers

to handle request and responses

Action filters for custom logic before and after action execution

Custom formatters to add custom media type formats

Dependency resolver to enable dependency injection for controllers

Page 16: REST and Web API

Things that helpHTTP debugging proxies Fiddler

fiddler2.com Curl, for cmd-line fans

curl.haxx.se Httpie, for cmd-line fans

github.com/jkbr/httpie

Debugging Wireshark Remote debug

Service things REST service help page Test web client

Page 17: REST and Web API

Read more and references RFCs ietf.org/rfc/... URI …/rfc3986.txt,

URL …/rfc1738.txt HTTP, …/rfc2616.txt

Web API bloggers webapibloggers.com

ASP.NET Web API asp.net/webapi aspnetwebstack.codeplex.com

Alternatives to Web API servicestack.net

Martin Fowler’s article about Richardson Maturity Model martinfowler.com/articles/

richardsonMaturityModel.html

Page 18: REST and Web API

http://www.flickr.com/photos/f-oxymoron/5005673112