Api pain points

48
API PAIN-POINTS GETTING THINGS WRONG FOR FUN AND PROFIT @PHILSTURGEON #PHPJOBURG14

description

I've been building APIs for a long time now and it is becoming ever more common for server-side developer thanks to the rise of front-end JavaScript frameworks, iPhone applications and generally API-centric architectures. On one hand you're just grabbing stuff from a data source and shoving it out as JSON, but surviving changes in business logic, database schema updates, new or deprecated etc gets super difficult. This talk will outline the common pitfalls developers get trapped in when building APIs and outline methods to avoid them, including naming stuff badly then having to rename everything, when and how to use POST/PUT/PATCH, data structures, DDoSing yourself because pagination, picking your authentication system and all sorts of other stuff.

Transcript of Api pain points

Page 1: Api pain points

API PAIN-POINTSGETTING THINGS WRONG FOR FUN AND PROFIT

@PHILSTURGEON #PHPJOBURG14

Page 2: Api pain points
Page 3: Api pain points
Page 4: Api pain points
Page 5: Api pain points
Page 6: Api pain points
Page 7: Api pain points
Page 8: Api pain points
Page 9: Api pain points
Page 10: Api pain points
Page 11: Api pain points
Page 12: Api pain points

http://girlsgotsole.com/blog/thankful-thursday-rest-days/

Page 13: Api pain points

DATABASE SEEDINGLEAVE YOUR CUSTOMERS ALONE

Page 14: Api pain points

ENDPOINT THEORYNAMING THINGS IS HARD

Page 15: Api pain points

PLURAL V SINGULAR?CONSISTENCY IS KING

/user/23

/user

s

Page 16: Api pain points

PLURAL V SINGULAR?CONSISTENCY IS KING

/opportunity/

43

/opportunitie

s

Page 17: Api pain points

PLURAL V SINGULAR?CONSISTENCY IS KING

/places/places/12/places/12/checkins/places/12/checkins/34/checkins/34

Page 18: Api pain points

NO NEED FOR SEOQUERY STRINGS ARE FINE

/users/active/true

/users?active=true

Page 19: Api pain points

AUTO-INCREMENT = BADCTRL + S YOUR WEBSITE

/checkins/

1/

checkins/2

/checkins/2369

/checkins/

3

Page 20: Api pain points

AUTO-INCREMENT = BADCTRL + S YOUR WEBSITE

https://github.com/zackkitzmiller/tiny-php

https://github.com/ramsey/uuid

Page 21: Api pain points

WHICH METHODSVERB SOUP

List GET /users Create POST /usersRead GET /users/XUpdate PUT /users/XDelete DELETE /users/XImage PUT /users/X/imageImage POST /users/X/imagesFavorites GET /users/X/favoritesCheckins GET /users/X/checkins

Page 22: Api pain points

FORM PAYLOADSJUST SEND JSON

foo=something&bar[baz]=thing&bar[stuff]=junk&bar=true

22

Page 23: Api pain points

HACKY PAYLOADSNOT LIKE THAT

Page 24: Api pain points

REAL JSON PAYLOADSTHNX!

Page 25: Api pain points

200 = OK

Page 26: Api pain points

2xx is all about success3xx is all about

redirection4xx is all about client

errors5xx is all about service

errors

Page 27: Api pain points

200 - Generic everything is OK

201 - Created something OK

202 - Accepted but is being processed async

400 - Bad Request (Validation?)

401 - Unauthorized

403 - Current user is forbidden

404 - That URL is not a valid route

410 - Data has been deleted, deactivated, suspended, etc

405 - Method Not Allowed

500 - Something unexpected happened and it is the APIs fault

503 - API is not here right now, please try again later

Page 28: Api pain points

SUPPLEMENT HTTP CODESWHAT HAPPENED

{"error": {

"type": "OAuthException", "message": "Session has expired at unix

time 1385243766. The current unix time is 1385848532"

}}

Page 29: Api pain points

SUPPLEMENT HTTP CODESWHAT HAPPENED

{"error": {

"type": "OAuthException","code": “ERR-1012“,

"message": "Session has expired at unix time 1385243766. The current unix time is 1385848532"

}}

Page 30: Api pain points

AUTHENTICATION STRATEGYHOW MUCH DO YOU CARE

HTTP Basic

HTTP Digest

OAuth 1.0a

OAuth 2.0

Page 32: Api pain points

USE SSL

Page 33: Api pain points

OAUTH 2 CAN DO A LOTPASSWORDS, IMPLICIT, SOCIAL LOGINS…

Page 34: Api pain points
Page 35: Api pain points

TRANSFORMERS… ASSEMBLE!

Page 36: Api pain points

FLEXIBLE RESPONSESSTOP YOUR IPHONE DEV COMPLAINING

GET /checkins/dsfXte ?

include=place,user,activity

Page 37: Api pain points

PAGINATEDATA GROWS FAST

{"data": [

...],"cursors": { "after": "MTI=", "next_url": "https://api.example.com/

places?cursor=MTI%3&number=12"

}}

Page 38: Api pain points

DEFINE A MAXIMUMPAGINATION DDOS

if ($limit > 100) {

$limit = 100;}

Page 39: Api pain points
Page 40: Api pain points

PHPUNIT + BEHAT

http://www.bil-jac.com/bestfriendsclub.php

Page 41: Api pain points

AUTOMATE TESTINGIF YOU LOVE YOUR JOB

Page 42: Api pain points

Scenario: Find a merchant When I request "GET /moments/1" Then I get a "200" response And scope into the "data" property And the properties exist: """ id … created_at """

Page 43: Api pain points

Scenario: Try to find an invalid checkin

When I request "GET /checkins/nope"

Then I get a "404" response

Page 44: Api pain points

Scenario:Wrong Arguments for user follow

Given I have the payload: """ {"is_following": "foo"} """

When I request "PUT /users/1”

Then I get a "400" response

Page 45: Api pain points

apiblueprint.org

Page 46: Api pain points
Page 47: Api pain points

ARCHITECTUREOLD SCHOOL

Page 48: Api pain points