Web APIs in Drupal 8 Larry Garfield, Senior Architect, Palantir.net Kyle Browning, Technical Consultant, Acquia
Jan 21, 2017
Web APIs in Drupal 8
Larry Garfield, Senior Architect, Palantir.netKyle Browning, Technical Consultant, Acquia
Larry GarfieldSenior Architect, Palantir.netDrupal Web Services Lead
Who are we?
Kyle BrowningTechnical Consultant, AcquiaServices, and Drupal iOS SDK maintainer
Who are we?
What are Web Services(tm)?
The Internet (TCP/IP)
The Web (HTTP)
Web Services (non-HTML)
REST (REpresentational State Transfer)
Collection of ResourcesResource identified by URIResource represented in a formatManipulate through Verbs/MethodsKnown hyperlinks to other Resources
REST (Hypermedia)
So what's wrong with Drupal 7?
Drupal 7function example_menu() { $items['my/page'] = array( 'title' => 'My page', 'page callback' => 'my_page_function', 'access arguments' => array('access content'), ); return $items;}
Only map on path
Very basic access control
Drupal 7function example_page_delivery_callback_alter(&$delivery_callback) { if (...) { $delivery_callback = 'example_deliver_page'; }}
Happens after the page callback, so mostly useless.
Drupal 7→And oh yeah, globals
▪$_GET▪drupal_add_http_header()▪print
Drupal 7
Everything other than a full HTML page is an after-thought.
So what's better in Drupal 8?
Drupal 8example.route: path: /my/page defaults: _controller: '\Drupal\example\Controller\ExampleController::page' _title: 'Example page' requirements: _permission: 'access_examples' _day: 'Tuesday' _method: 'GET'
Multiple access checks
Map on path, method, domain, etc.
Drupal 8
Arbitrary custom logic goes here
Drupal 8Theming and page layout happen here
Drupal 8
Everything's an HTTP Response. Sometimes that's a page.
Drupal 8→You can serve any type of response to a request→Wire directly to the routing system.→No duplicating routing anymore!
Drupal 8 - Serialization module→Standard serialization/deserialization pipeline→Built on Symfony Serializer component
$json = $this->serializer->serialize($entity, 'json');
$xml = $this->serializer->serialize($entity, 'xml');
$entity = $this->serializer->deserialize($json, 'Node' 'json');
Standard universal serialized format (for internal structure)!
Drupal 8 - REST module→Core module for common pattern of REST usage→~RestWS→Uses Serialization
→Define "Resource" Plugins▪GET/get(), PUT/put(), DELETE/delete()▪Return ResourceResponse▪Drupal will serialize()/deserialize() for you
Drupal 8 - REST resourcesclass DBLogResource extends ResourceBase { public function get($id = NULL) { if ($id) { $record = db_query("SELECT * FROM {watchdog} WHERE wid = :wid", [':wid' => $id]) ->fetchAssoc(); if (!empty($record)) { return new ResourceResponse($record); } throw new NotFoundHttpException(t('Log entry with ID @id was not found', ['@id' => $id])); } throw new HttpException(t('No log entry ID was provided')); }}
Drupal 8 - REST resourcesAll Content Entities are supported out-of-the-box
● Uses same URL as HTML page● All supported formats, automatically● Opt-in configuration
Easily add your own resources, too→ Teach serializer about it (Normalizer)→ Create new Resource plugin→ Profit!!1!
Drupal 8 - Content NegotiationReverse proxies suck
+Browsers suck even more
=Accept-based negotiation is broken 90% of the time :-(
Drupal 8 - Content Negotiationhttp://example.com/node/5?&_format=hal_json
http://example.com/node/5?&_format=xml
http://example.com/node/5?&_format=html (default)
Drupal 8REST UI
Drupal 8REST UI
Drupal 8 - Hypermedia LinksComing soon!
(8.1? As soon as someone works on it.)
https://www.drupal.org/node/2113345
What does Services module do?
Drupal 8 - Services→Provide endpoint capabilities to put your API behind a
centralized URL→Standardize on an approach to building non-REST APIs→Accept Header content negotiation→Gives us regular json response instead of hal_json→Config entities too!
Drupal 8 - ServicesDefinition→ Defines the ‘resource’→ Protein of Services in
D8→ Route Access→ Request method
Drupal 8 - ServicesDefinition
Still respects core
Config Entities!
Lets grab a block
Create a block
Update a block
And since everything is an entity.
Drupal 8 - Views
Respects Permissions
Thank You