Top Banner
Web APIs in Drupal 8 Larry Garfield, Senior Architect, Palantir.net Kyle Browning, Technical Consultant, Acquia
39

Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

Jan 21, 2017

Download

Technology

Acquia
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

Web APIs in Drupal 8

Larry Garfield, Senior Architect, Palantir.netKyle Browning, Technical Consultant, Acquia

Page 2: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

Larry GarfieldSenior Architect, Palantir.netDrupal Web Services Lead

Who are we?

Page 3: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

Kyle BrowningTechnical Consultant, AcquiaServices, and Drupal iOS SDK maintainer

Who are we?

Page 4: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

What are Web Services(tm)?

Page 5: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

The Internet (TCP/IP)

The Web (HTTP)

Web Services (non-HTML)

REST (REpresentational State Transfer)

Page 6: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

Collection of ResourcesResource identified by URIResource represented in a formatManipulate through Verbs/MethodsKnown hyperlinks to other Resources

REST (Hypermedia)

Page 7: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

So what's wrong with Drupal 7?

Page 8: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

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

Page 9: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

Drupal 7function example_page_delivery_callback_alter(&$delivery_callback) { if (...) { $delivery_callback = 'example_deliver_page'; }}

Happens after the page callback, so mostly useless.

Page 10: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

Drupal 7→And oh yeah, globals

▪$_GET▪drupal_add_http_header()▪print

Page 11: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

Drupal 7

Everything other than a full HTML page is an after-thought.

Page 12: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

So what's better in Drupal 8?

Page 13: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

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.

Page 14: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

Drupal 8

Arbitrary custom logic goes here

Page 15: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

Drupal 8Theming and page layout happen here

Page 16: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

Drupal 8

Everything's an HTTP Response. Sometimes that's a page.

Page 17: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

Drupal 8→You can serve any type of response to a request→Wire directly to the routing system.→No duplicating routing anymore!

Page 18: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

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)!

Page 19: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

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

Page 20: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

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')); }}

Page 21: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

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!

Page 22: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

Drupal 8 - Content NegotiationReverse proxies suck

+Browsers suck even more

=Accept-based negotiation is broken 90% of the time :-(

Page 23: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

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)

Page 24: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

Drupal 8REST UI

Page 25: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

Drupal 8REST UI

Larry Garfield
Whatever you did to the previous image, please do here, too. :-) And tell me how for future reference.
Alex Schurr
I just used the "shapes" tool and drew a box around the image, and then changed the "fill" (little button that has a paint can in it) to be transparent!
Larry Garfield
Oh, so it's not a border, just a co-located box? That's really the best Google can do?Why do people use this tool again? Sheesh!
Alex Schurr
Yeah, as far as I know I dont know how to add a border, but I haven't played around with it much.
Page 26: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

Drupal 8 - Hypermedia LinksComing soon!

(8.1? As soon as someone works on it.)

https://www.drupal.org/node/2113345

Page 27: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

What does Services module do?

Page 28: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

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!

Page 29: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

Drupal 8 - ServicesDefinition→ Defines the ‘resource’→ Protein of Services in

D8→ Route Access→ Request method

Page 30: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

Drupal 8 - ServicesDefinition

Page 31: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

Still respects core

Page 32: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

Config Entities!

Page 33: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

Lets grab a block

Page 34: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

Create a block

Page 35: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

Update a block

Page 36: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

And since everything is an entity.

Page 37: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

Drupal 8 - Views

Page 38: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

Respects Permissions

Page 39: Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core

Thank You