+ All Categories
Home > Technology > Best practices for RESTful web service design

Best practices for RESTful web service design

Date post: 09-May-2015
Category:
Upload: ramin-orujov
View: 17,074 times
Download: 2 times
Share this document with a friend
Description:
This is my presentation from Caucasus Web Conference. caucasuswebcon.com/speakers.html
33
Best practices for RESTful web service design Ramin Orujov Azercell Telecom LLC
Transcript
Page 1: Best practices for RESTful web service design

Best practices for RESTful web service design

Ramin OrujovAzercell Telecom LLC

Page 2: Best practices for RESTful web service design

#whoami Senior Software developer Internal Applications Team Head

@Azercell Telecom Part time teacher @Qafqaz University

(Java OOP, Java web, Android) AZERJUG founder and manager Trainer, mentor, speaker, consultant,

etc…

Page 3: Best practices for RESTful web service design

#agenda REST fundamentals Resource naming Representation HTTP methods Error handling Version management Paging Search and filtering Security

Page 4: Best practices for RESTful web service design

#rest fundamentalsREpresentational State Transfer (REST) is a style of software architecture for distributed systems such as the WWW.It was introduced and defined in 2000 by Roy Fielding in his doctoral dissertation. Source: http://en.wikipedia.org/wiki/REST

Page 5: Best practices for RESTful web service design
Page 6: Best practices for RESTful web service design

#rest constraints Client-server Stateless Cacheable Layered system Layered system Code on demand (optional) Uniform interface

Page 7: Best practices for RESTful web service design

#resource naming

Follow KISS principle

Use nouns for resource names

Use verbs actions

Page 8: Best practices for RESTful web service design

#resource naming

2 base urls:Collection (plural)

/employeesResource

/employees/itramin

Page 9: Best practices for RESTful web service design

#resource naming

Use nouns for non-resource actions:convertcalculate

Page 10: Best practices for RESTful web service design

#representation Request header: Content-type

application/json application/xml

Extension /employees.json /employees.xml /employees/itramin.json /employees/itramin.xml

Query param /employees/type=json /employees/type=xml

Page 11: Best practices for RESTful web service design

#http methods

Create Read Update Delete

POST GET PUT DELETE

Page 12: Best practices for RESTful web service design

#http methodsResource POST

CreateGETRead

PUTUpdate

DELETEDelete

/employees Create new emp

Return list of employees

Bulk update or Error

Delete all employeesError

/employees/itramin

Not usedError

Return employee itramin

Update employee itramin

Delete emp itramin

Page 13: Best practices for RESTful web service design

#error handling Error handling is very important for

reliable and stable API. Use appropriate HTTP status codes Document success and error cases

Page 14: Best practices for RESTful web service design

#http status codesGroup Comment

1xx Informational(100,101)

2xx Success(200,201)

3xx Redirect (301, 304)

4xx Client error(401, 404, 405)

5xx Server error(500, 503)

http://en.wikipedia.org/wiki/List_of_HTTP_status_codes

Page 15: Best practices for RESTful web service design

#error messagesConsider the following: For user For developer Unique error code Link to documentation

Page 16: Best practices for RESTful web service design

#error messages{userMessage:”Show this error to user”,devMessage:”Detailed error for developer”,“errorCode”: 12345,“doc”:”http://api.azercell.com/azimus/docs/errors/12345”}

Page 17: Best practices for RESTful web service design

#version management Consider versioning during design, no

later Consider support for multiple client

3 general practice: Part of url Request param Request header

Page 18: Best practices for RESTful web service design

#version management Part of url

api.azercell.com/azimus/v1/employees api.azercell.com/azimus/20130501/

employees Request header

Content-Type: application/json;v=1 Query param – overrides header

?v=1

Page 19: Best practices for RESTful web service design

#paging It is bad idea to return all resources in

database, so use paging

Query parameter Request/response headers

Page 20: Best practices for RESTful web service design

#pagingQuery parameter: Facebook - limit, offset Twitter – page, rpp(records per page) LinkedIn – start, count Consider reasonable default values such

as limit=20, offset=0

Page 21: Best practices for RESTful web service design

#paging Request header

Range: items=0-19 Response header

Content-Range: items 0-19/152 Content-Range: items 0-19/*

Page 22: Best practices for RESTful web service design

#search&filter&sort Search /employees?q=ramin – (default json) /employees.xml?q=ramin

Filter /employees?filter=“name::ramin|department=ICT” /employees?name=ramin&department=ICT

Sort/employees?sort=name|surname/employees?sort=-salary|name

Page 23: Best practices for RESTful web service design

#partial responseFacebook:/employees/?fields=name,surname,title

LinkedIn:http://api.linkedin.com/v1/people/~/connections:(id,first-name,last-name,positions:(title))

https://developer.linkedin.com/documents/field-selectors

Page 24: Best practices for RESTful web service design

#partial responseRequest a feed of a user's uploaded videos that only contains the title, number of comments (and comment URL), and viewing statistics for each video:

https://gdata.youtube.com/feeds/api/users/default/uploads?fields=entry(title,gd:comments,yt:statistics)

Page 25: Best practices for RESTful web service design

#partial response@GET@Path("/{userId}")@Produces("application/json")public String getUser(@PathParam("userId") Long userId, @DefaultValue("userId,fullname,title") @QueryParam("fields") String fields) {

http://stackoverflow.com/questions/9314735/how-to-return-a-partial-json-response-using-java

Page 26: Best practices for RESTful web service design

#security Authentication

HTTP Basic authentication + SSL API token,key Custom authentication mechanism

Authorization Oauth 1.0 Oauth 2.0 Custom mechanism

Amazon AWS Identity and Access Management PayPal Permissions Service Your own homegrown api

Page 27: Best practices for RESTful web service design

#security HTTP Basic authentication + SSL

GET /employees/ HTTP/1.1Host: www.example.orgAuthorization: Basic cGhvdG9hcHAuMDAxOmJhc2ljYXV0aA==

Page 28: Best practices for RESTful web service design

#cache&scalability Carefully implement caching

HTTP/1.1 200 OKDate: Sat, 06 Jul 2013 09:56:14 GMTLast-Modified: Sat, 06 Jul 2013 09:56:14 GMTExpires: Sun, 06 Jul 2013 10:56:14 GMTCache-Control: max-age=3600,must-revalidateContent-Type: application/xml; charset=UTF-8

Page 29: Best practices for RESTful web service design

#best rest

Sample application implementing best practices for RESTful web service.

https://github.com/raminorujov/best-rest/

Page 31: Best practices for RESTful web service design

#references Web API Design, Brian Mulloy, Apigee RESTful Java with JAX-RS, Bilk Burke, O’Reilly,

2010 REST API Design Rulebook, Mark Masse,

O’Reilly, 2012 RESful Web Services Cookbook, Subhu

Allamaraju, O’Reilly, Yahoo Press, 2010 REST in Practice, Hypermedia and Systems

Architecture, Jim Webber, Savas Parastatidis, Ian Robinson, O’Reilly, 2010

Page 32: Best practices for RESTful web service design

#contacts

https://www.twitter.com/raminorujov https://www.linkedin.com/in/raminorujov https://www.facebook.com/ramin.orucov http://raminorucov.wordpress.com

Page 33: Best practices for RESTful web service design

?


Recommended