a bird's eye view on api development

85

Upload: frederick-vanbrabant

Post on 06-Apr-2017

146 views

Category:

Software


0 download

TRANSCRIPT

Page 1: A bird's eye view on API development
Page 2: A bird's eye view on API development

Birds eye view on:API DEVELOPMENT

Page 3: A bird's eye view on API development

WHO ?

Frederick Vanbrabant Software engineermadewithlove

Page 4: A bird's eye view on API development
Page 6: A bird's eye view on API development

{ "id": 1, "title": "7 Things You Should NEVER Do to a potato", "subtitle": "you will never believe nr 4", "body": "Some body copy that will go on in great detail", "comments": [{ "id": 1, "body": "woah this changed my life" }, { "id": 2, "body": "I will never look at stuff in the same way" }]}

GET /getblogpost/1

Page 7: A bird's eye view on API development

Why is this not so great ?

Url is kinda structure less

What if we get loads of articles?

What if we get loads of comments?

Damn, we need to write documentation

Is this the best way to pinpoint a resource

Page 8: A bird's eye view on API development

GET /getblogposts/1

Let's look at that endpoint

HTTP verb

Endpoint

Page 9: A bird's eye view on API development

HTTP verbs

Page 10: A bird's eye view on API development

HTTP verbs

HTTP != CRUD

Page 11: A bird's eye view on API development

HTTP verbs

Verb Return HTTP Code

POST 201

Sends a payload to the server,Returns an empty body.

Page 12: A bird's eye view on API development

HTTP verbs

Verb Return HTTP Code

POST 201

POST /posts

{ "title": "Why cheese is better then real friends", "body": "People only stand in the way of you and your cheese!"}

Post body

Page 13: A bird's eye view on API development

HTTP verbs

Verb Return HTTP Code

GET 200

Does not send a payloadReturns a collection of or a single resource

Page 14: A bird's eye view on API development

HTTP verbs

Verb Return HTTP Code

GET 200

GET /posts/2

{ "title": "How to fake your own death", "body": "You can buy cheese with the insurance money!"}

Return body

Page 15: A bird's eye view on API development

HTTP verbs

Verb Return HTTP Code

PUT 204

Updates a resource (needs the entire object)Returns an empty body.

Page 16: A bird's eye view on API development

HTTP verbs

Verb Return HTTP Code

PUT 204

PUT /posts/2

{ "title": "Help I'm stuck in a keynote making factory", "body": "Send cheese!"}

Put body

Page 17: A bird's eye view on API development

HTTP verbs

Verb Return HTTP Code

PATCH 200

Accepts instructions to update the resourceReturns the resource

Page 18: A bird's eye view on API development

Wait what?

Accepts instructions to update the resourcereturns the resource

PATCH /artists/kanye/[ { "op": "replace", "path": "artists/kanye/cars", "value": 7 },]

Page 19: A bird's eye view on API development

Wait what?

Accepts instructions to update the resourcereturns the resource

PATCH /artists/kanye[ {"cars" : 7}]

Content-Type: application/partial-update-json

Page 20: A bird's eye view on API development

HTTP verbs

Verb Return HTTP Code

DELETE 204

Removed the resourceReturns an empty body.

Page 21: A bird's eye view on API development

HTTP verbs

Verb Return HTTP Code

DELETE 204

DELETE /posts/1

Page 22: A bird's eye view on API development

HTTP CODE

Page 23: A bird's eye view on API development
Page 24: A bird's eye view on API development

HTTP CODE

Range 1XX - Information

Code What it means

100 continue

101 Switching Protocols

102 Processing

Page 25: A bird's eye view on API development

HTTP CODE

Range 2XX - Success

Code What it means

200 Ok

201 Created

202 Accepted

Page 26: A bird's eye view on API development

HTTP CODE

Range 3XX - Redirection

Code What it means

301 Moved

300 Multiple Choices

Page 27: A bird's eye view on API development

HTTP CODE

Range 4XX - Client error

Code What it means

400 Bad Request

401 Unauthorized

404 Not found

Page 28: A bird's eye view on API development

HTTP CODE

Range 5XX - Server error

Code What it means

500 Internal Server Error

503 Service Unavailable

504 Gateway Timeout

Page 29: A bird's eye view on API development

HTTP HEADERS

Page 30: A bird's eye view on API development

HTTP HEADERS

Send on each request and response

Meta data to the call

Can define custom headers

Page 31: A bird's eye view on API development

HTTP HEADERS

GET / HTTP/1.1

Host: www.google.comConnection: keep-alivePragma: no-cacheCache-Control: no-cacheUpgrade-Insecure-Requests: 1User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/X-Chrome-UMA-Enabled: 1X-Client-Data: CIS2yQEIpbbJAQjEtskBCOCUygEI+pjKAQjznMoBAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8Accept-Encoding: gzip, deflate, sdchAccept-Language: nl,en;q=0.8Cookie: *Cookie data*

Request

Page 32: A bird's eye view on API development

HTTP HEADERS

Status: 200 (OK)

Date: Fri, 08 Jul 2016 13:59:05 GMTExpires: -1Cache-Control: private, max-age=0Content-Type: text/html; charset=ISO-8859-1X-Xss-Protection: 1; mode=blockX-Frame-Options: SAMEORIGINSet-Cookie: NID=81=x8aqWN0zsVHK2QcLEZca6603H8BYJxXP6bfjQl0cT82iM4j7fE3V9wH2qbpXW0DAAPL3G56n9ID-H_RCP-w9eVxA-mu9XV-oX-oE00qrVwRgzXV334c7XpsX2DDuTdcq; expires=Sat, 07-Jan-2017 13:59:05 GMT; path=/; domain=.google.be; HttpOnlyAccept-Ranges: noneVary: Accept-EncodingTransfer-Encoding: chunked

*HTML, JSON, XML OR WHATEVER*

Response

Page 33: A bird's eye view on API development

What about the endpoint

GET /getblogposts/1

Page 34: A bird's eye view on API development

What about the endpoint

GET /posts/1

Page 35: A bird's eye view on API development

What about the endpoint

GET /posts/1

Nouns are good, verbs are bad

Use plurals

Page 36: A bird's eye view on API development

What about the endpoint

GET /posts/1

GET /posts/1/comments

GET /posts/1/comments/15

OR

GET /comments/15

Page 37: A bird's eye view on API development

What about the endpoint

GET /posts

GET /posts/slugs-are-allowed

Page 38: A bird's eye view on API development

What about the endpoint

GET users/4b3403665fea6/posts/4b340550242239

Page 39: A bird's eye view on API development

What about the endpoint

Endpoint structure != database structure

Page 40: A bird's eye view on API development

We still 200 ?

Page 41: A bird's eye view on API development

Let's check out that body?

{ "id": 1, "title": "7 Things You Should NEVER Do to a potato", "subtitle": "you will never believe nr 4", "body": "Some body copy that will go on in great detail", "comments": [{ "id": 1, "body": "woah this changed my life" }, { "id": 2, "body": "I will never look at stuff in the same way" }]}

Page 42: A bird's eye view on API development

Let's use a standard

Battle tested and growing up to be the standard

Page 43: A bird's eye view on API development

{ "count": 87, "next": "http://swapi.co/api/people/?page=2", "previous": null, "results": [ { "name": "Luke Skywalker", "height": "172", "mass": "77", "hair_color": "blond", "skin_color": "fair", "eye_color": "blue", "birth_year": "19BBY", "gender": "male", "homeworld": "http://swapi.co/api/planets/1/", "films": [ "http://swapi.co/api/films/6/", "http://swapi.co/api/films/3/", "http://swapi.co/api/films/2/", "http://swapi.co/api/films/1/", "http://swapi.co/api/films/7/" ], "species": [ "http://swapi.co/api/species/1/" ], "vehicles": [ "http://swapi.co/api/vehicles/14/", "http://swapi.co/api/vehicles/30/" ], "starships": [ "http://swapi.co/api/starships/12/", "http://swapi.co/api/starships/22/" ], "created": "2014-12-09T13:50:51.644000Z", "edited": "2014-12-20T21:17:56.891000Z", "url": "http://swapi.co/api/people/1/"

Page 44: A bird's eye view on API development

{ "count": 87, "next": "http://swapi.co/api/people/?page=3", "previous": "http://swapi.co/api/people/?page=1", "results": [ .... ]}

Links ?

page-based

Page 45: A bird's eye view on API development

{ "count": 30, "next": "http://swapi.co/api/people/?offset=60&limit=30", "previous": "http://swapi.co/api/people/?offset=0&limit=30", "results": [ .... ]}

Links ?

offset-based

Page 46: A bird's eye view on API development

{ "count": 87, "next": "http://swapi.co/api/people/?cursor=1374004777531007833", "previous": "http://swapi.co/api/people/?cursor=2627305797328905258", "results": [ .... ]}

Links ?

cursor-based

Page 47: A bird's eye view on API development

{ "name": "Luke Skywalker", "height": "172", "mass": "77", "hair_color": "blond", "skin_color": "fair", "eye_color": "blue", "birth_year": "19BBY", "gender": "male", "homeworld": "http://swapi.co/api/planets/1/", "films": [ "http://swapi.co/api/films/6/", "http://swapi.co/api/films/3/", "http://swapi.co/api/films/2/", "http://swapi.co/api/films/1/", "http://swapi.co/api/films/7/" ], "species": [ "http://swapi.co/api/species/1/" ], "vehicles": [ "http://swapi.co/api/vehicles/14/", "http://swapi.co/api/vehicles/30/" ], "starships": [ "http://swapi.co/api/starships/12/", "http://swapi.co/api/starships/22/" ], "created": "2014-12-09T13:50:51.644000Z", "edited": "2014-12-20T21:17:56.891000Z", "url": "http://swapi.co/api/people/1/"}

Page 48: A bird's eye view on API development

Wait I can't have nested-resources?

Page 49: A bird's eye view on API development

Wait I can't have nested-resources?

This is not really HTTP now is it ?

Page 50: A bird's eye view on API development

Think about howyou surf the webThat's pretty nice right ?

Page 51: A bird's eye view on API development

HATEOASHypermedia as the Engine of Application State

Page 52: A bird's eye view on API development

HATEOAS Hypermedia as the Engine ofApplication State

{ "name": "Boba Fett", "height": "183", "mass": "78.2", "hair_color": "black", "skin_color": "fair", "eye_color": "brown", "birth_year": "31.5BBY", "gender": "male", "homeworld": "http://swapi.co/api/planets/10/", "films": [ "http://swapi.co/api/films/5/", "http://swapi.co/api/films/3/", "http://swapi.co/api/films/2/" ], "species": [ "http://swapi.co/api/species/1/" ], "vehicles": [], "starships": [ "http://swapi.co/api/starships/21/" ], "created": "2014-12-15T12:49:32.457000Z", "edited": "2014-12-20T21:17:50.349000Z", "url": "http://swapi.co/api/people/22/"

Page 53: A bird's eye view on API development

HATEOAS

"I'm not going to make 500 calls just to get somecomments"

Page 54: A bird's eye view on API development

HATEOAS

"I'm not going to make 500 calls just to get somecomments"

GET /films

GET /films/4,5,6

Page 55: A bird's eye view on API development

HATEOAS

"Can't you just add the resources to the url?"

GET /articles/1?include=comments,likes

Page 56: A bird's eye view on API development

QUERY PARAMETERS

Filter the response to what's valid on the query

GET /cheeses?type=brie,cheddar

GET /cheeses?type[]=brie&type[]=cheddar

Page 57: A bird's eye view on API development

AUTH

Page 58: A bird's eye view on API development

AUTH

Page 59: A bird's eye view on API development

AUTH

- HTTP Basic- JWT- OAuth 2.0

Page 60: A bird's eye view on API development

HTTP Basic

- Super easy- Not super safe

Page 61: A bird's eye view on API development

JWT (Json web tokens)

- No database hassle- Needs some knowledge

Page 62: A bird's eye view on API development

JWT (Json web tokens)

ServerClient

make new callswith the token

Return token

Send credentials

Page 63: A bird's eye view on API development

JWT (Json web tokens)

https://jwt.io/

Page 64: A bird's eye view on API development

OAuth2

- Very secure and very flexible- Can be daunting to setup

Page 65: A bird's eye view on API development

OAuth2

ServerClient

make new callswith the token

Return token +Refresh token

Send credentials

Page 66: A bird's eye view on API development

OAuth2

ServerClient

make new callswith the token

Return token +Refresh token

Send refresh token

Page 67: A bird's eye view on API development

API VERSIONING

Page 68: A bird's eye view on API development

API VERSIONING

YOU SHOULD NOTHAVE TO VERSION AN

API!

Page 69: A bird's eye view on API development

API VERSIONING

BUT YOU WILL HAVETO EVENTUALLY

Page 70: A bird's eye view on API development

API VERSIONING

GET /api/v1/artists/taylor-swift

Page 71: A bird's eye view on API development

API VERSIONING

GET /api/v1/artists/taylor-swift

The good The bad

Easy to implement Routes should not change

Easy to see what versionwe are on

Meta data should be in aheader

Page 72: A bird's eye view on API development

API VERSIONING

GET /api/artists/taylor-swiftx-api-version: 1

Page 73: A bird's eye view on API development

API VERSIONING

The good The bad

Meta data is in the header Custom header ... ?

Url does not change You need documentation

Url does not change

GET /api/artists/taylor-swiftx-api-version: 1

Page 74: A bird's eye view on API development

API VERSIONING

GET /api/artists/taylor-swiftAccept: application/vnd.website.v1+json

Page 75: A bird's eye view on API development

API VERSIONING

The good The bad

Meta data is in the header Harder to test

Standard http header You need documentation

GET /api/artists/taylor-swiftAccept: application/vnd.website.v1+json

Page 76: A bird's eye view on API development

Random TIPS

START WITH DOCUMENTATION

Page 77: A bird's eye view on API development

Random TIPS

LOOK AT THE USER FIRST, NOT THE TECH(A GOOD API DOES NOT NEED TO BE REST)

Page 78: A bird's eye view on API development

Random TIPS

TYPE CAST YOUR RETURN VALUES

Page 79: A bird's eye view on API development

Random TIPS

CONSISTENCY IS KING

Page 80: A bird's eye view on API development

Random TIPS

TEST YOUR ENDPOINTS

Page 81: A bird's eye view on API development

Random TIPS

NEVER TRUST YOUR USERS

Page 82: A bird's eye view on API development

Random TIPS

Use PHP?

Laravel: https://github.com/dingo/apiVanilla: https://thephpleague.com https://github.com/spatie

Use Python?

Django: http://www.django-rest-framework.org

Page 83: A bird's eye view on API development

Random TIPS

Want to know more?

This talks in text form:https://blog.madewithlove.be/post/birdseye-view-on-api

Phil Sturgeon's book:

https://leanpub.com/build-apis-you-wont-hate

Great O'Reily book RESTful Web APIs:

http://shop.oreilly.com/product/0636920028468.do

Page 84: A bird's eye view on API development

Questions!

Twitter: TheEdonian

Page 85: A bird's eye view on API development

THANKS !