[geektalk#2] takaaki mizuno - api url design

15

Click here to load reader

Upload: innovatube

Post on 15-Apr-2017

90 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: [GeekTalk#2] Takaaki Mizuno - Api Url Design

DESIGING URLS OF WEB APITAKAAKI MIZUNO

Page 2: [GeekTalk#2] Takaaki Mizuno - Api Url Design

DESIGNING URLS OF WEB API

I hesitate to use “REST” APIs today,

so just say “JSON over HTTP”

Page 3: [GeekTalk#2] Takaaki Mizuno - Api Url Design

DESIGNING URLS OF WEB API

BECAUSE I VIOLATE MANY REST PRINCIPLES WHEN I DESIGN URL OF WEB API ;(

▸ Because we cannot follow REST architecture strictly sometimes.

▸ We can use many REST concept, but sometimes choose more practical solutions.

▸ REST is really misconceived concept

▸ Roy Fielding created this word in 2000

▸ REST is the architecture that described in his article

▸ Roy Fielding also said that “I am getting frustrated by the number of people calling any HTTP-based interface a REST API.” in 2008

Page 4: [GeekTalk#2] Takaaki Mizuno - Api Url Design

DESIGING URLS OF WEB APITAKAAKI MIZUNO

PRACTICAL

Page 5: [GeekTalk#2] Takaaki Mizuno - Api Url Design

テキスト

SUMMARY

I will explain some basic URL design rules

Page 6: [GeekTalk#2] Takaaki Mizuno - Api Url Design

DESIGNING URLS OF WEB API

NEVER INCLUDE SERVER SIDE ARCHITECTURE

▸ Good

▸ http://api.example.com/users/100

▸ Bad

▸ http://api.example.com/cgi-bin/get_user.php?user=100

Page 7: [GeekTalk#2] Takaaki Mizuno - Api Url Design

DESIGNING URLS OF WEB API

YOU USE NOUN ( PLURAL FORM ) FOR URLS

▸ Good

▸ https://api.example.com/v1/companies/12345

▸ Bad

▸ https://api.example.com/v1/company/12345

▸ https://api.example.com/v1/get-companies/12345

Page 8: [GeekTalk#2] Takaaki Mizuno - Api Url Design

DESIGNING URLS OF WEB API

YOU USE NOUN ( PLURAL FORM ) FOR URLS

▸ URLs “basically” points “Resource”

▸ Set of resources

▸ https://api.example.com/v1/companies

▸ Individual resource

▸ https://api.example.com/v1/companies/123

Page 9: [GeekTalk#2] Takaaki Mizuno - Api Url Design

DESIGNING URLS OF WEB API

USE HTTP METHODS AS “VERBS”

GET Get data ( don’t change data )

POST Create new data & change the state of data

PUT Update data

PATCH Update a part of data

DELETE Delete data

Page 10: [GeekTalk#2] Takaaki Mizuno - Api Url Design

DESIGNING URLS OF WEB API

USE ALIES LIKE “ME” OR “SELF” FOR AUTHENTICATED USER RESOURCE

▸ Use me/self for the resource for user himself/herself

▸ https://api.exmaple.com/v1/users/me

▸ https://api.exmaple.com/v1/users/1234

Page 11: [GeekTalk#2] Takaaki Mizuno - Api Url Design

DESIGNING URLS OF WEB API

VERSIONING

▸ Embed Major version of “semantic versioning” into URLs.

▸ https://api.example.com/v1/users/12345

▸ Violate REST concept, but practical

▸ Other Ways

▸ Use query params

▸ https://api.example.com/users/12345?v=20161010

▸ Behavior is not clear if client calls without params

▸ Use HTTP Request headers

▸ Accept: application/vnd.example.v2+json

▸ It’s most beautiful and following HTTP concept, but not common

Page 12: [GeekTalk#2] Takaaki Mizuno - Api Url Design

テキスト

VERSIONING

▸ Semantic Versioning

▸ http://semver.org/

Ver. 1. 5. 7Major Minor Patch

NEED TO HAVE BACKWARD

COMPATIBILITY

BRAKING CHANGE

Page 13: [GeekTalk#2] Takaaki Mizuno - Api Url Design

DESIGNING URLS OF WEB API

SPECIFYING DATA FORMAT

▸ Use query params

▸ https://api.example.com/v1/users?format=xml

▸ Practical, easy to understand, and format is not a resource

▸ Use extension

▸ https://api.example.com/v1/users.xml

▸ Okay, but format is not a resource

▸ Use HTTP Request header

▸ Accept: application/json

▸ I like most, and it’s okay for mobile apps.

▸ But not recommend to use for public APIs.

Page 14: [GeekTalk#2] Takaaki Mizuno - Api Url Design

DESIGNING URLS OF WEB API

API Design is reviewed by other developers

Page 15: [GeekTalk#2] Takaaki Mizuno - Api Url Design

DESIGNING URLS OF WEB API

Cool URLs make your service more cool