Restful php-web-services-sample-chapter-chapter-5-resource-oriented clients
Amsterdam php create a restful api
-
Upload
ceeram -
Category
Technology
-
view
4.760 -
download
0
Transcript of Amsterdam php create a restful api
![Page 1: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/1.jpg)
Create a RESTful apiNovember 15, 2012
AmsterdamPHP
AmsterdamPHP
![Page 2: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/2.jpg)
INTRODUCTION
AmsterdamPHP
Marc Ypes@Ceeram
CakePHP 4 yearsCore team 1.5 years
Undercover as programmer
![Page 3: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/3.jpg)
OVERVIEW
AmsterdamPHP
■ Content-types■ Interface■ Authentication■ Cache■ Errors
![Page 4: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/4.jpg)
INTRODUCTION TO REST
AmsterdamPHP
Representational state transfer
Set of architectural principles
- resource focussed- manipulation through representations- HTTP protocol?
![Page 5: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/5.jpg)
INTRODUCTION TO REST
AmsterdamPHP
Constraints
■ Client-server■ Stateless■ Uniform interface■ Cacheable■ Layered system
![Page 6: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/6.jpg)
INTRODUCTION TO REST
AmsterdamPHP
Client-server
separation of concerns
portability of UI across platforms, scalability
allowing components to evolve independently
![Page 7: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/7.jpg)
INTRODUCTION TO REST
AmsterdamPHP
Stateless
request includes required information
no stored context on server: Sessions
![Page 8: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/8.jpg)
INTRODUCTION TO REST
AmsterdamPHP
Uniform interface
information is transfered in standardized form
using nouns for resources
(http) protocol describes methods
![Page 9: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/9.jpg)
INTRODUCTION TO REST
AmsterdamPHP
Cacheable
reducing latency
lower serverload
![Page 10: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/10.jpg)
INTRODUCTION TO REST
AmsterdamPHP
Layered system
ServerClient
ProxyGatewaySecurityAnything
![Page 11: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/11.jpg)
INTRODUCTION TO REST
AmsterdamPHP
Uniform interface
■ resource
■ identification of the resource
■ manipulation through representation
■ self-descriptive
■ hypermedia as the engine of application state HATEOAS
![Page 12: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/12.jpg)
INTRODUCTION TO REST
AmsterdamPHP
Uniform Interface
Data element Exampleresource user, book etc. (users, books etc.)resource identifier URL, URN (/users/1234)representation
data TXT / HTML / XML /YAML,JSONmetadata content type, last-modified time
resource metadata source link, alternatecontrol data if-modified-since, cache-control, etag
http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm
![Page 13: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/13.jpg)
INTRODUCTION TO REST
AmsterdamPHP
Uniform Interface
/api/getUserProfile/1234
/api/users?action=vote&id=1234
/api/deleteUser?id=1
![Page 14: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/14.jpg)
INTRODUCTION TO REST
AmsterdamPHP
Uniform Interface
RPC style
Steep learning curveDocumentationNew functionality, BC
![Page 15: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/15.jpg)
INTRODUCTION TO REST
AmsterdamPHP
Uniform Interface
2 base urls
- /books- /books/1234
![Page 16: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/16.jpg)
INTRODUCTION TO REST
AmsterdamPHP
Uniform Interface
GET /users Get collectionPOST /users Add to collectionGET /users/1234 Get resourcePUT /users/1234 Update resourceDELETE /users/1234 Delete resource
Update is not replace?POST /users/1234
![Page 17: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/17.jpg)
INTRODUCTION TO REST
AmsterdamPHP
Uniform Interface
Typical request:
>GET /books/1849511926 HTTP/1.1>Host: api.amazin.com>Accept: application/json
>If-Modified-Since: Sat, 01 Sep 2012 10:22:36 GMT
![Page 18: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/18.jpg)
INTRODUCTION TO REST
AmsterdamPHP
Uniform Interface
Typical response:
< HTTP/1.1 200 OK< Date: Sat, 01 Sep 2012 11:45:12 GMT< Server: Apache/2.2.16 (Debian)< Last-Modified: Sat, 01 Sep 2012 11:25:31 GMT< Content-Length: 145< Content-Type: application/json{"book":{........"}}
![Page 19: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/19.jpg)
INTRODUCTION TO REST
AmsterdamPHP
Uniform Interface
Typical response:
< HTTP/1.1 304 Not Modified< Date: Sat, 01 Sep 2012 11:45:12 GMT< Server: Apache/2.2.16 (Debian)< Vary: Accept-Encoding
![Page 20: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/20.jpg)
INTRODUCTION TO REST
AmsterdamPHP
Uniform Interface
Safe methodsIdempotent methods
GET (HEAD) is safe (nullipotent)PUT, DELETE are idempotent
POSTPATCH?
![Page 21: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/21.jpg)
INTRODUCTION TO REST
AmsterdamPHP
Uniform Interface
Normalize the resources
GET /books/1849511926/votes
GET /votes?book=1849511926
![Page 22: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/22.jpg)
INTRODUCTION TO REST
AmsterdamPHP
Uniform Interface
Normalize the resources
POST /books/1849511926/votes
PUT /books/1849511926data contains votes subresource data
POST /votesdata is book=1849511926
![Page 23: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/23.jpg)
INTRODUCTION TO REST
AmsterdamPHP
Uniform Interface
PATCH
Edge Rails: PATCH is the new primary HTTP method for updates
http://weblog.rubyonrails.org/2012/2/25/edge-rails-patch-is-the-new-primary-http-method-for-updates/
![Page 24: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/24.jpg)
INTRODUCTION TO REST
AmsterdamPHP
Versioning
/rest/v1
content-type
![Page 25: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/25.jpg)
INTRODUCTION TO REST
AmsterdamPHP
Uniform Interface / HATEOAS
Level 3 of REST maturity model (RMM)
![Page 26: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/26.jpg)
INTRODUCTION TO REST
AmsterdamPHP
Uniform Interface / HATEOAS
Level 0
Single URI, single HTTP method
![Page 27: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/27.jpg)
INTRODUCTION TO REST
AmsterdamPHP
Uniform Interface / HATEOAS
Level 1
Many URI, single HTTP method
![Page 28: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/28.jpg)
INTRODUCTION TO REST
AmsterdamPHP
Uniform Interface / HATEOAS
Level 2
Many URI, different HTTP methods
![Page 29: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/29.jpg)
INTRODUCTION TO REST
AmsterdamPHP
Uniform Interface / HATEOAS
Level 3
Self descriptive■ Media types■ Links■ Other protocols
![Page 30: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/30.jpg)
INTRODUCTION TO REST
AmsterdamPHP
Uniform Interface / HATEOAS
HATEOAS
GET /comments?book=1849511926
Link to api.amazin.com/books/1849511926
Links to all comments
![Page 31: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/31.jpg)
INTRODUCTION TO REST
AmsterdamPHP
Uniform Interface / HATEOAS
>GET /comments/1<HTTP/1.1 200 Ok<Content-Type: text/xml<?xml version="1.0"><comment>
<foo>great book</foo><book>
<link href="/books/1849511926" title="wow" /></book>
</comment>
![Page 32: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/32.jpg)
INTRODUCTION TO REST
AmsterdamPHP
Uniform Interface / HATEOAS{
"foo":"great book","book":
{"links":[
{"href":"/books/1849511926", "title":"wow"
}]
}}
![Page 33: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/33.jpg)
INTRODUCTION TO REST
AmsterdamPHP
Uniform Interface / HATEOAS
Link header
Link: <https://api.github.com/user/repos?page=2&per_page=100>;rel="next", <https://api.github.com/user/repos?page=50&per_page=100>;rel="last"
github.com
![Page 34: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/34.jpg)
INTRODUCTION TO REST
AmsterdamPHP
Uniform Interface / Errors
HTTP Statuscodes
Human reads messageCode reads code
![Page 35: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/35.jpg)
INTRODUCTION TO REST
AmsterdamPHP
Uniform Interface / Errors
HTTP Statuscodes
200 OK201 Created204 No Content304 Not Modified400 Bad Request401 Unauthorized404 Not Found405 Method Not Allowed
![Page 36: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/36.jpg)
INTRODUCTION TO REST
AmsterdamPHP
Uniform Interface / Errors
Link to support page
Link: <http://api.amazin.com/errors/405>; rel="help"
![Page 37: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/37.jpg)
REST my Cake
CakeFest 2012 Manchester
Authentication
PublicApi-keyBasic AuthOAuth
![Page 38: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/38.jpg)
INTRODUCTION TO REST
AmsterdamPHP
Cacheable
HTTP Cache headers
Use them!
![Page 39: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/39.jpg)
INTRODUCTION TO REST
AmsterdamPHP
Cacheable
HTTP Cache headers
Cache-control- private- public- max-age / s-maxage- must-revalidate- nocache
![Page 40: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/40.jpg)
INTRODUCTION TO REST
AmsterdamPHP
Cacheable
HTTP Cache validation
Etag ResponseLast-Modified Response
If-Modified-Since Request (GET)If-None-Match Request (GET)
If-Match Request (PUT)
![Page 41: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/41.jpg)
REST my Cake
AmsterdamPHP
CakeResponse
header( $header = NULL, $value = NULL )send( )statusCode( $code = NULL )type( $contentType = NULL )
![Page 42: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/42.jpg)
REST my Cake
AmsterdamPHP
CakeResponse
cache( $since, $time = '+1 day' )checkNotModified( $request )disableCache( )etag( $tag = NULL, $weak = false )expires( $time = NULL )maxAge( $seconds = NULL )modified( $time = NULL )mustRevalidate( $enable = NULL )notModified( )sharable( $public = NULL, $time = NULL )sharedMaxAge( $seconds = NULL )
![Page 43: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/43.jpg)
REST my Cake
AmsterdamPHP
CakePHP 3.0
2013PHP 5.4composerarrays => objects
![Page 44: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/44.jpg)
INTRODUCTION TO REST
AmsterdamPHP
REST might not be what you are looking for
Questions?
![Page 45: Amsterdam php create a restful api](https://reader034.fdocuments.us/reader034/viewer/2022052410/554f6e0eb4c905bb178b4fc8/html5/thumbnails/45.jpg)
INTRODUCTION TO REST
AmsterdamPHP
THANKS
https://joind.in/event/view/1141