Создание api, которое полюбят разработчики. Глубокое...

29
Crafting WEB API Design that Developers Love. Deep dive Roman Bugaev, Senior developer at Adform

Upload: sqalab

Post on 15-Jan-2015

4.017 views

Category:

Education


1 download

DESCRIPTION

Доклад Романа Бугаева на конференции Application Developer Days-4. г.Минск 13 декабря 2013

TRANSCRIPT

Page 1: Создание API, которое полюбят разработчики. Глубокое погружение

Crafting WEB API Design that Developers Love. Deep dive

Roman Bugaev, Senior developer at Adform

Page 2: Создание API, которое полюбят разработчики. Глубокое погружение

About us -

• 400+ high performance servers

• High availability load balancing and failover

• 200 000 requests per second

• Peta Bytes of data on 50+ servers

• Up to 20 releases per day

• Integrations• Facebook, Google, Adobe• Large e-shops and CMS platform• TV ads recognition • Adform Marketplace

Page 3: Создание API, которое полюбят разработчики. Глубокое погружение

Why?

~70% API – REST

"there is no 'official' standard for RESTful web

Interviews

Page 4: Создание API, которое полюбят разработчики. Глубокое погружение

Pragmatic REST

• REST is an architectural style and not a strict standard, it allows for a lot of flexibly

• The primary design principle when crafting your API should be to maximize developer productivity and success.

• Pragmatic REST is a design problem

• Keep simple things simple

Page 5: Создание API, которое полюбят разработчики. Глубокое погружение

Keep base URL simple & intuitive

• Nouns are good; verbs are bad

• 2 base URLs per resource• /users /users/1234

• Keep verbs out of your base URLs

• Use HTTP verbs to operate on the collections and elements.

• POST create GET read PUT update DELETE delete

Page 6: Создание API, которое полюбят разработчики. Глубокое погружение

How to pick the nouns for URLs.

• Plural rather than singular nouns• Foursquare /checkins

• Dropbox /files

• Facebook /me/friends

• Concrete rather than abstract names

• /items vs. /blogs, /news

Page 7: Создание API, которое полюбят разработчики. Глубокое погружение

Simplify associations

• GET /folders/5678/files • Get all files belonging to a specific folder

• POST /folders/5678/files • Create new file for that folder

Page 8: Создание API, которое полюбят разработчики. Глубокое погружение

Sweep complexity behind the ‘?’

• Attributes GET /cars?color=red&state=new&location=minsk

• Paging GET /cars?limit=25&offset=50

• Global Search GET /search?q=lamb

• Scoped Search GET /owners/5678/cars?q=lamb

Page 9: Создание API, которое полюбят разработчики. Глубокое погружение

Handling errors

• Developers learn to write code through errors

• Developers depend on well-designed errors at the critical times

Page 10: Создание API, которое полюбят разработчики. Глубокое погружение

Handling errors - Facebook

HTTP Status Code 200

{error:

{message: “Malformed access token <token>”,type: “OAuthException”,code: “190”

}}

Page 11: Создание API, которое полюбят разработчики. Глубокое погружение

Handling errors - Twilio

HTTP Status Code: 401

{status: "401",message: "Authenticate",code: 20003,info: "http://www.twilio.com/docs/errors/20003"

}

Page 12: Создание API, которое полюбят разработчики. Глубокое погружение

A couple of best practices

• Use HTTP status codes• Make messages returned in the payload as verbose as

possible• Code• Developer message• User message• More Info

Start by using the following 3 codes: • 200 – OK (success)• 400 - Bad Request (client error)• 500 - Internal Server Error (server error)

I ❤BEST PRACTICES

Page 13: Создание API, которое полюбят разработчики. Глубокое погружение

Tips for versioning

• salesforce.com/services/data/v20.0/sobjects/Account

• Facebook ?v=1.0

• The version is mandatory.

• Accept header for entity versioning

• Specify the version with a 'v' prefix.

• Use a simple ordinal number.

• Create an alias for current version

Page 14: Создание API, которое полюбят разработчики. Глубокое погружение

Actions

• Use verbs not nouns:• /convert?from=EUR&to=CNY&amount=100

• Make it clear in your API documentation that these “non-resource” scenarios are different.

Page 15: Создание API, которое полюбят разработчики. Глубокое погружение

Probe Web Resources Efficiently with OPTIONS in REST

< HTTP/1.1 200 OK

< Allow: GET, HEAD, POST, OPTIONS, TRACE

< Content-Type: text/html; charset=UTF-8

< Date: Wed, 13 December 2013 10:24:43 GMT

< Content-Length: 0

Page 16: Создание API, которое полюбят разработчики. Глубокое погружение

Probe Web Resources Efficiently with HEAD in REST

< HTTP/1.1 200 OK

< Accept-Ranges: bytes

< Content-Type: text/html; charset=UTF-8

< Date: Wed, 08 May 2013 10:12:29 GMT

< ETag: "780602-4f6-4db31b2978ec0"

< Last-Modified: Thu, 25 Apr 2013 16:13:23 GMT

< Content-Length: 1270

Page 17: Создание API, которое полюбят разработчики. Глубокое погружение

Scale

• Think about scale sooner that later

• Rate limits

• Extra servers

• Partitioning

• Caching • Between application and database

• In the application itself

• Using an API proxy

• CDN for large static content

Page 18: Создание API, которое полюбят разработчики. Глубокое погружение

Supporting multiple formats

• To get the JSON format from a collection or specific element: • dogs.json• /dogs/1234.json

Accept header for entity versioning also applicable

Default format: JSON

Follow JavaScript conventions

Page 19: Создание API, которое полюбят разработчики. Глубокое погружение

Chatty APIs

Imagine how developers will use your API• You can design a RESTful API and still mitigate the

chattiness.

Be complete and RESTful and provide shortcuts

Take advantage of the partial response syntax

• /owners/5678?fields=name, dogs.name

Page 20: Создание API, которое полюбят разработчики. Глубокое погружение

Some useful tools

Page 21: Создание API, которое полюбят разработчики. Глубокое погружение

Apiary.io

Page 22: Создание API, которое полюбят разработчики. Глубокое погружение

Apigee

Page 23: Создание API, которое полюбят разработчики. Глубокое погружение

Runscope, hurl.it, apichangelog

Page 24: Создание API, которое полюбят разработчики. Глубокое погружение

Mashape

Page 25: Создание API, которое полюбят разработчики. Глубокое погружение

Security

• Use something established

• API keys for non-sensitive data only

• Username/password auth for site based APIs

• OAuth for server-to-server APIs

• SSL for EVERYTHING sensitive

Page 26: Создание API, которое полюбят разработчики. Глубокое погружение

OAuth

1. An OAuth token gives one app access to one API on behalf of one user.

2. App developers do not want responsibility of holding a user’s secret information (password).

3. there are three entities (legs) – user/server/client

Page 27: Создание API, которое полюбят разработчики. Глубокое погружение

Why is OAuth important?

• What if client is hacked and someone steals all the passwords?• OAuth allows the API provider to revoke tokens for an

individual user and for an entire app

• On the other hand, if user decides to change his password, the token will be the same.

• Developers can use an OAuth library in their language

Page 28: Создание API, которое полюбят разработчики. Глубокое погружение

Types of OAuth 2.0

• BEARER TOKEN • SSL and big numbers

• MAC TOKEN • Uses signature instead of SSL

• SAML• if you and your potential API developers don’t

understand SAML or know what it is, that’s a signal to stay away

Page 29: Создание API, которое полюбят разработчики. Глубокое погружение

Thank you! Questions?

[email protected]

bugaev_roman

http://twitter.com/rbugaev

http://facebook.com/rbugaev