api pain points (phpne)
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
![Page 1: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/1.jpg)
A P I PA I N - P O I N T SGE TT I NG T H INGS W R O NG F O R F U N A ND PR O F I T
@P H ILST U R GE O N 2014
![Page 2: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/2.jpg)
![Page 3: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/3.jpg)
![Page 4: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/4.jpg)
A RC H I T E C T U R EO LD SCH O O L
![Page 5: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/5.jpg)
![Page 6: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/6.jpg)
http://girlsgotsole.com/blog/thankful-thursday-rest-days/
![Page 7: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/7.jpg)
DATA B A S E S E E D I N GLE AV E YO U R CU ST O ME R S A LO NE
![Page 8: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/8.jpg)
E N D P O I N T T H E O RYNA MING T H I NGS I S H A R D
![Page 9: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/9.jpg)
P LU RA L V S I N G U L A R ?CO NS IST E NCY I S K ING
/user/23
/users
![Page 10: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/10.jpg)
P LU RA L V S I N G U L A R ?CO NS IST E NCY I S K ING
/opportunity/43
/opportunities
![Page 11: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/11.jpg)
P LU RA L V S I N G U L A R ?CO NS IST E NCY I S K ING
/person/dave
/people
![Page 12: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/12.jpg)
P LU RA L V S I N G U L A R ?CO NS IST E NCY I S K ING
/places/places/12/places/12/checkins/places/12/checkins/34/checkins/34
![Page 13: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/13.jpg)
N O N E E D F O R S E OQ U E RY ST R I NGS A R E F I NE
/users/active/true
/users?active=true
![Page 14: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/14.jpg)
A U T O - I N C R E M E N T = B A D
CT R L + S YO U R W E BS IT E
/checkins/1/checkins/2
/checkins/2369
…/checkins/3
![Page 15: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/15.jpg)
A U T O - I N C R E M E N T = B A D
CT R L + S YO U R W E BS IT E
github.com/zackkitzmiller/tiny-php
$tiny = new \ZackKitzmiller\Tiny('lDpuU74QNH6B');
echo $tiny->to(5);// E
echo $tiny->from('E');// 5
![Page 16: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/16.jpg)
A U T O - I N C R E M E N T = B A D
CT R L + S YO U R W E BS IT E
use Rhumsaa\Uuid\Uuid;use Rhumsaa\Uuid\Exceptio
$uuid4 = Uuid::uuid4();
echo $uuid4;// 25769c6c-d34d-4bfe-ba98-e0ee856f3e7a
github.com/ramsey/uuid
![Page 17: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/17.jpg)
H TT P V E R BS M ATT E RH O NE ST LY
Dont be @jamiehannaford. That sounds like a bad day.
![Page 18: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/18.jpg)
F O R M PAY LOA D SJ U ST SE ND J SO N
foo=something&bar[baz]=thing&bar[stuff]=junk&bar=true
![Page 19: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/19.jpg)
H AC KY PAY LOA D SNO T L IKE T H AT
![Page 20: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/20.jpg)
R E A L J S O N PAY LOA D ST H NX!
![Page 21: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/21.jpg)
R E A D I N G R E A L DATA I S E A S Y
T H E H TT P WAY
json_decode($_POST['stupid-json']);
json_decode(file_get_contents(‘php://input'));
Input::get(‘foo’);
![Page 22: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/22.jpg)
![Page 23: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/23.jpg)
200 I S NOT THE ON LY S UC C ES S
KNO W YO U R CO DE S
if ($statusCode != 200) {throw new Exception('AAGHH!!');
}
![Page 24: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/24.jpg)
2xx is all about success3xx is all about redirection
4xx is all about client errors5xx is all about service errors
![Page 25: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/25.jpg)
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
405 - Method Not Allowed
410 - Data has been deleted, deactivated, suspended, etc
500 - Something unexpected happened and it is the APIs fault
503 - API is not here right now, please try again later
![Page 26: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/26.jpg)
418 - I am a Teapothttp://httpstatus.es/418
![Page 27: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/27.jpg)
C L E A R , H U M A N E R R O R S
W H AT H A PPE NE D
{ "error": { "errors": [ { "domain": "youtube.parameter", "reason": "missingRequiredParameter", "message": "No filter selected.", "locationType": "parameter", "location": "" } ], "code": 400, "message": "No filter selected." }}
![Page 28: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/28.jpg)
E R R OR S S H OUL D MA K E S E N S E
W H AT H A PPE NE D
&mine=true
"reason": "missingRequiredParameter", "message": "No filter selected.",
…
WTF
![Page 29: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/29.jpg)
S U PPL E M E N T H TT P C O D E S
W H AT H A PPE NE D
{"error": {
"type": "OAuthException", "message": "Session has expired at unix time 1385243766. The current unix time is 1385848532"}
}
![Page 30: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/30.jpg)
S U PPL E M E N T H TT P C O D E S
W H AT H A PPE NE D
{ "error": { "message": "(#210) Subject must be a page.", "type": "OAuthException", "code": 210 }}
![Page 31: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/31.jpg)
S U PPL E M E N T H TT P C O D E S
W H AT H A PPE NE D
{ "error": { "message": "(#210) Subject must be a page.", "type": "OAuthException", "code": 210, "url": “http://developers.facebook.com/errors#210“ }}
![Page 32: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/32.jpg)
OA U T H 2 . 0
thephpleague.com
github.com/thephpleague/oauth2-server
![Page 33: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/33.jpg)
OA U T H 2 C AN D O A LO T
PA SSW O R DS , I MPL IC I T , SO C IA L LO G INS…
![Page 34: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/34.jpg)
U S E S S L
![Page 35: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/35.jpg)
LO LE XCE PT F O R …
![Page 36: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/36.jpg)
FAC E BOOK … YOU B#% @*DS ! ! !
SE R IO U SLY
Refresh Tokens?
Lol
![Page 37: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/37.jpg)
YO U T U B E … YO U S E M I -B #% @ * D S ! ! !
ST I LL SE R IO U SLY
Refresh Tokens?
Kinda
![Page 38: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/38.jpg)
P R E S E N TAT I O N L AY E RDO NT LE T U SE R S BE H IND T H E CU RTA IN
![Page 39: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/39.jpg)
return Places::all();
P R E S E N TAT I O N L AY E RDO NT LE T U SE R S BE H IND T H E CU RTA IN
![Page 40: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/40.jpg)
![Page 41: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/41.jpg)
![Page 42: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/42.jpg)
T RA N SFORM E RS… A SSE M B L E !
public function transform(Book $book) { return [ 'id' => (int) $book->id, 'title' => $book->title, 'year' => $book->yr, ‘created' => (string) $book->created_at, ]; }
fractal.thephpleague.com
![Page 43: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/43.jpg)
F L E X I B L E R E S P O N S E SST O P YO U R I P H O NE DE V CO MP L A IN I NG
GET /checkins/dsfXte ?include=place,user,activity
![Page 44: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/44.jpg)
PAG I N AT EDATA GR O W S FA ST
{"data": [
...],"cursors": { "after": "MTI=", "next_url": "https://api.example.com/places
?cursor=MTI%3"}
}
![Page 45: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/45.jpg)
D E F I N E A L I M I T RA N G EPAG INAT IO N DDO S
if ($limit < 1 || $limit > 100) {$limit = 100;
}
![Page 46: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/46.jpg)
A U T O M AT E T E S T I N GIF YOU LOVE YOUR JOB
http://www.engineersgotblued.com/
![Page 47: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/47.jpg)
P H P U N I T + B E H AT
http://www.bil-jac.com/bestfriendsclub.php
![Page 48: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/48.jpg)
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
title
year
created
"""
![Page 49: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/49.jpg)
Scenario: Try to find an ` checkinWhen I request "GET /checkins/nope"Then I get a "404" response
![Page 50: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/50.jpg)
Scenario:Wrong Arguments for user followGiven I have the payload:
""" {"is_following": "foo"} """
When I request "PUT /users/1”Then I get a "400" response
Not a boolean
![Page 52: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/52.jpg)
![Page 53: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/53.jpg)
V E R S I O N I N G/ V 1 /DO E SNT CO U NT
https://api.example.com/v1/places
![Page 54: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/54.jpg)
V E R S I O N I N G/ V 1 /DO E SNT CO U NT
https://api-v1.example.com/places
![Page 55: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/55.jpg)
V E R S I O N I N G/ V 1 /DO E SNT CO U NT
Accept: application/vnd.example+json; version=1
Accept: application/vnd.example+json; version=2
![Page 56: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/56.jpg)
V E R S I O N I N G/ V 1 /DO E SNT CO U NT
Accept: application/vnd.example.user+json; version=1
Accept: application/vnd.example.user+json; version=2
![Page 57: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/57.jpg)
V E R S I O N I N G/ V 1 /DO E SNT CO U NT
Copy Facebook
Maybe?
THIS ONE TIME!
Facebook ruined the one good thing they ever did
![Page 58: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/58.jpg)
E V E RY T H I N G I S W R O N GDO NT BE T H AT GU Y
troyhunt.com/2014/02/your-api-versioning-is-wrong-which-is.html
![Page 59: API Pain Points (PHPNE)](https://reader035.vdocuments.us/reader035/viewer/2022081518/554dba6fb4c905c2488b47d5/html5/thumbnails/59.jpg)
leanpub.com/build-apis-you-wont-hate/c/TOONARMY