Understanding the Semantic Web and Drupal, DrupalCon Chicago 2011
JSON API Services with Advanced Web - DrupalCon Web... ...
-
Upload
nguyendang -
Category
Documents
-
view
251 -
download
4
Transcript of JSON API Services with Advanced Web - DrupalCon Web... ...
![Page 1: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/1.jpg)
Advanced Web Services with
JSON API
![Page 2: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/2.jpg)
HOWDY!I am MateuI am here because I am a decoupling nerdYou can find me at @e0ipso
![Page 3: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/3.jpg)
Outstanding problemsStill looking for solutions!
JSON APIWhat is it?Why use it?
You will learn about…
Drupal moduleWhat’s the status?What are the limitations?How does it relates to REST in core?
![Page 4: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/4.jpg)
{json:api} paints your bike shed
![Page 5: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/5.jpg)
Defines:- Transport- Interaction
![Page 6: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/6.jpg)
Creative Commons specification
Strongly driven by FE & UX experts
![Page 7: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/7.jpg)
Why this one?Since there are others, and a HAL implementation is already in core. And GraphQL in contrib.
![Page 8: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/8.jpg)
141 reposThat’s a lot of traction
Client & ServerTotal success!
18 languagesAnd a lot of range
![Page 9: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/9.jpg)
Place your screenshot here
![Page 10: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/10.jpg)
With a highlight on its flexibilityStays neutral on implementation details and gives you space. Also provides extension system.
![Page 11: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/11.jpg)
![Page 12: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/12.jpg)
HOW DID I GET HERE?
![Page 13: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/13.jpg)
Response to the typical problems
› Multiple round trip requests› Bloated responses› Content discovery
They all have known solid solutions!
![Page 14: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/14.jpg)
1.TRANSPORT
FORMATThe shape of the JSON object
![Page 15: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/15.jpg)
FORMAT
SupportingStructure
(GLUE)
Resource dataInfo(ID)
Attributes&
Relationships(DATA)
HATEOAS&
Metadata(HYPERMEDIA)
![Page 16: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/16.jpg)
{“data”: {
“type”: “articles”,“id”: “1”,“attributes”: {…},“relationships”: {…},
},“links”: {…},“meta”: {…}
} FORMAT
![Page 17: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/17.jpg)
{…
“attributes”: {“title”: “Drupal 8!”,“body”: “Lorem ipsum”…
},…
}FORMAT
![Page 18: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/18.jpg)
…
“relationships”: {
“links”: {
“self”: “articles/1/relationships”
},
“tags”: {
“data”: [{
“type”: “tags”,
“id”: “2”
}]
}
… FORMAT
![Page 19: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/19.jpg)
2.RESOURCE
INTERACTIONHow do we get and update data
![Page 20: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/20.jpg)
Uses RESTGET, POST, PUT, PATCH, DELETE, …
![Page 21: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/21.jpg)
Typical request
GET /articles HTTP/1.1Accept: application/vnd.api+json
RESPONSE/api/node/article?_format=api_json
![Page 22: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/22.jpg)
The typical solutions
› Multiple round trip requests› Resource embedding
› Bloated responses› Sparse fieldsets
› Content discovery› Collections and filters
![Page 23: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/23.jpg)
Place your screenshot here
EXTREMELY SIMPLEYour project will have way more stuff than this!
![Page 24: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/24.jpg)
› 1: GET articles/12› 2: GET articles/12 => tags/34› 3: GET articles/12 => tags/88› 4: GET articles/12 => users/88› 5: GET articles/12 => users/88 => images/5› 6: GET articles/12/comments› 7: GET articles/12 => comment/2› 8: GET articles/12 => comment/2 => user/8› 9: GET articles/12 => comment/2 =>
user/8 => image/9› 10: GET articles/12 => comment/7 […] › 11: GET articles/12 => comment/7 […]› 12: GET articles/12 => comment/7 […]› MORE!
![Page 25: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/25.jpg)
GET /articles/12?
include= author,author.pic, tags, comment,comment.author, comment.author.pic
Resource embedding
![Page 26: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/26.jpg)
GET /articles/12?
fields[articles]= title, created
Sparse fieldsets
![Page 27: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/27.jpg)
…“attributes”: { “title”: “My article”, “uuid”: “12345-1234-34”, “created”: “10-05-2012”, “status”: “1”, “body”: {…}, “langcode”: “en”}…
![Page 28: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/28.jpg)
“Give me the cover image and the publication year of all the albums of all the bands having one of the members
under 35 currently living in Murcia.
Oh! And while you're at it, output the name of the band and that member as
well.”
![Page 29: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/29.jpg)
GET /bands?
filter[members.city][value]=Murcia&
filter[members.age][value]=35&
filter[members.age][operator]=”<=”&
include=albums,albums.cover,members&
fields[bands]=name,albums,members&
fields[members]=name&
fields[albums]=publication&
fields[images]=uriCollections and filters
![Page 30: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/30.jpg)
Every API consumer requests the resource data it needs. It can be different every time.
WRITEURL QUERIES
![Page 31: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/31.jpg)
Every consumer has different data needs. The server (Drupal) cannot choose what those are.
![Page 32: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/32.jpg)
1. /bands/1234› GET, PUT, PATCH, DELETE
2. /bands› GET, POST
3. /bands/1234/albums› GET
4. /bands/1234/relationships/albums› GET, PATCH
Every resource 4 “endpoints”
![Page 33: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/33.jpg)
3.PERFORMANCE
How fast is the Drupal module?
![Page 34: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/34.jpg)
Benchmarking JSON API
› ab -v4 -k -c8 -n10 -A u:p› node:2100› include
› Author› Author image
› Tags (2 tags)
![Page 35: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/35.jpg)
Benchmarking core HAL JSON
› ab -v4 -k -c8 -n10 -A u:p› node:2100› user:1105› file:156 (slow path)
› tag:11› tag:18
![Page 36: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/36.jpg)
Results (core): anonymous
user:1105node:2100
file:156tag:11tag:18
~ 21 ms
Using Keep Alive
![Page 37: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/37.jpg)
Results (jsonapi): anonymous
user:1105
node:2100
file:156
tag:11
tag:18
~ 7 ms
node:2100 include:author,author.pic,tags
![Page 38: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/38.jpg)
Core (ms) {json:api} (ms)
Anonymous 21 7
Auth 320 115
Uncached 392 182
https://gist.github.com/e0ipso/4b1b346b296fbf0c918450fef5b0b3d7
![Page 39: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/39.jpg)
AVOIDBOOTSTRAPSAnd unnecessary HTTP round trips.
![Page 40: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/40.jpg)
4.DRUPAL MODULE
Our implementation of the standard.
![Page 41: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/41.jpg)
drupal.org/project/jsonapiThat was expected, wasn’t it?
![Page 42: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/42.jpg)
Drupal Integration
› Integrates with Authentication Providers› OAuth 2 Bearer Token (via simple_oauth)
› Full cacheability metadata support
![Page 43: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/43.jpg)
Oriented to entity bundles
› Each resource is a bundle› /api/node/page› Automatically enabled (can be disabled)› You can do any entity query as filter› Works with config entities!
![Page 44: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/44.jpg)
Automatic schema generation
› Uses type data to generate the schema› /schema/node/page› Automatically enabled (can be disabled)
![Page 45: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/45.jpg)
Schema usages?GENERATE DOCS
![Page 46: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/46.jpg)
Schema usages?GENERATE FORMS
![Page 47: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/47.jpg)
Schema usages?VALIDATE DATA
![Page 48: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/48.jpg)
Schema usages?GENERATE CODE
![Page 49: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/49.jpg)
Limitations
› Multilingual support is not great› File integration needs some work› Revision support› Extensible through code only› Limited to the entity system
![Page 50: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/50.jpg)
Open challenges
› Versioning content model in Drupal› Responsive images and image styles› Data pre-processing› Multiple-operation requests› Aggregated values
![Page 51: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/51.jpg)
Do you want to help?
› First Time Sprinter Workshop - 9:00-12:00 - Room Wicklow 2A› Mentored Core Sprint - 9:00-18:00 - Wicklow Hall 2B› General Sprints - 9:00 - 18:00 - Wicklow Hall 2A
Join us for contribution sprints!
![Page 52: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/52.jpg)
Credits
Special thanks to all the people who made and released these awesome resources for free:› Presentation template by SlidesCarnival› Photographs by Startupstockphotos› Creative Commons images
![Page 53: JSON API Services with Advanced Web - DrupalCon Web... ...](https://reader031.fdocuments.net/reader031/viewer/2022022506/5ac24bfb7f8b9a433f8de4f3/html5/thumbnails/53.jpg)
Evaluate this sessionevents.drupal.org/dublin2016/schedule
What did you think?
https://events.drupal.org/node/add/session-evaluation?field_eval_session=13193