Top Banner
REST in Peace API DEVELOPMENT IN DRUPAL
59

REST in Peace

Jan 21, 2017

Download

Technology

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: REST in Peace

REST in PeaceAPI DEVELOPMENT IN DRUPAL

Page 2: REST in Peace

Kate Marshalkina

Konstantin Komelin

Drupal Consultant from Moscow who fell in love with Drupal in 2011. Interested in i18n, distributions and Drupal 8.Path Breadcrumbs co-maintainer.@kalabro

Drupal Consultant from Saint PetersburgCo-founder of local Drupal CommunityDrupal Trainer at MorningCurve@kkomelin

Page 3: REST in Peace

Let’s REST

Page 4: REST in Peace

Headless?!

Page 5: REST in Peace

What is API for?

Page 6: REST in Peace

Mobile Apps

API

Page 7: REST in Peace

Microservices

API

Page 8: REST in Peace

Frontend Apps

API

Page 9: REST in Peace

What is REST?

Page 10: REST in Peace

ResourceRepresentation

GET /items

POST /items

GET /items/1

PUT /items/1

DELETE /items/1

Methods

REpresentational State Transfer

Page 11: REST in Peace

RESTful or RESTless

Page 12: REST in Peace

REST in Drupal

Page 13: REST in Peace

Services RestWS RESTful Endpoint Drupal 8

Popularity

Documentation

Extensibility

Authentication

Performance

Auto API Docs

Versioning

Page 14: REST in Peace

Serviceshttps://www.drupal.org/project/services

“A standardized solution of integrating external applications with Drupal.”

37,085 sites use this module.

Popularity: ★★★★★

Page 15: REST in Peace

RESTful Web Serviceshttps://www.drupal.org/project/restws

“Builds upon the Entity API, to provide support for all entity types out of the box.”

4,746 sites use this module.

Popularity: ★★★

Page 16: REST in Peace

RESTfulhttps://www.drupal.org/project/restfulhttps://github.com/RESTful-Drupal/restful

“This module allows Drupal to be operated via RESTful HTTP requests, using best practices for security, performance, and usability.”

“Audience is developers and not site builders.”

395 sites use this module.

Popularity: ★★

Page 17: REST in Peace

Endpointhttps://www.drupal.org/project/endpoint

“Endpoint is really light, fast and flexible, that makes it a good solution for projects where Drupal role is mobile backend and single-page app backend.”

7 sites use this module.

Popularity: ★

REST-focused alternative to High-performance JavaScript callback handlerhttps://www.drupal.org/project/js

Page 18: REST in Peace

Drupal 8 RESTCore + https://www.drupal.org/project/restui

“In Drupal 8 core, interactions with content entities are supported via a REST interface. The REST module is extensible, and modules that wish to offer other services can implement Resource Plugins.”

Popularity: ★★

Page 19: REST in Peace

Services RestWS RESTful Endpoint Drupal 8

Popularity

Documentation

Extensibility

Authentication

Performance

Auto API Docs

Versioning

Page 20: REST in Peace
Page 21: REST in Peace

Services RestWS RESTful Endpoint Drupal 8

Popularity

Documentation

Extensibility

Authentication

Performance

Auto API Docs

Versioning

Page 22: REST in Peace

Project docs API docs (hooks) UI Examples Videos

Services ★★★ ★★★★ ★★★★ ★★★ ★★★★

RestWS ★★★ ★★★★ ★ ★★★★ ★

RESTful ★★★★★ ★★★★★ ★★ ★★★★★ ★★

Endpoint ★★ ★★ ★ ★ ★

Drupal 8 ★★★ ★★★ ★★★ ★★★ ★★★

Documentation & Quick Start

Page 23: REST in Peace

Services RestWS RESTful Endpoint Drupal 8

Popularity

Documentation

Extensibility

Authentication

Performance

Auto API Docs

Versioning

Page 24: REST in Peace
Page 25: REST in Peace

Extensibility & hooks

Page 26: REST in Peace

Services RestWS RESTful Endpoint Drupal 8

Popularity

Documentation

Extensibility

Authentication

Performance

Auto API Docs

Versioning

Page 27: REST in Peace

Total lines of PHP code Without comments, tests and whitespace Hooks

Services 15,000 6,000 18

RestWS 3,000 1,000 7

RESTful 18,000 6,000 1

Endpoint 300 300 -

Drupal 8 5,000 1 3

Code Statistics

Page 28: REST in Peace

ServicesCustom architecture, ~18 hooks (13 — alter)

To create a custom resource:

1. Implement hook_services_resources()

2. Write custom callbacks

Page 29: REST in Peace

RestWSEntity API + 7 hooks

To create a custom resource:

1. Implement hook_restws_resource_info()

2. Create controller class on top of RestWSResourceControllerInterface

Page 30: REST in Peace

RESTfulCtools plugins, Entity API, OOP

To create a custom resource:

1. Implement hook_ctools_plugin_directory ()

2. Create controller class on top of RestfulEntityBase / RestfulInterface

Page 31: REST in Peace

EndpointCustom routing function.

To create a custom resource:

1. Create /api.php with an array of endpoints.

2. Call endpoint_route() from that file.

Page 32: REST in Peace

Drupal 8 RESTPlugin Manager, Config Manager, Routes, Annotations etc.

To create a custom resource:

1. Create controller on top of ResourceBase / ResourceInterface.

2. Save it as src/Plugin/rest/resource/MyCustomResource.php inside your module.

To enable endpoint for existing resource:

3. Write/paste resource settings into rest.settings.yml.

4. Create config/install/rest.settings.yml inside your module.

Page 33: REST in Peace

Services RestWS RESTful Endpoint Drupal 8

Popularity

Documentation

Extensibility

Authentication

Performance

Auto API Docs

Versioning

Page 34: REST in Peace

Security & Authentication

Page 35: REST in Peace

Security & Authentication0. X-CSRF-Token

1. Cookie Auth

2. HTTP Basic Auth

3. Token Auth

4. OAuth

5. Oauth2

Page 36: REST in Peace

X-CSRF-TokenHTTP Header to prevent Cross-Site Request Forgery for session based authentication.

For writing methods: POST, PUT, PATCH, DELETE.

Services RestWS RESTful Endpoint Drupal 8

✔️ ✔️ ✔️ ✖️ ✔️

services/session/token

restws/session/token

api/session/token

rest/session/token

Page 37: REST in Peace

Cookie AuthDrupal build-in auth mechanism.

1. Client sends auth request (user / password).

2. Server returns session cookie in Set-Cookie header.

3. Client makes further requests with Cookie: SESSb7f18cc=pvOhLNLdNNs7BkwbX8… header.

Services RestWS RESTful Endpoint Drupal 8

✔️ ✔️ ✔️ ✔️ ✔️

Page 38: REST in Peace

HTTP Basic AuthUsername and password are sent on every request (base64):

Authorization: Basic aHR0cHdhdGNoOmY=

Services RestWS RESTful Endpoint Drupal 8

✔️ ✔️ ✔️ ✖️ ✔️

Page 39: REST in Peace

Token AuthServer returns token instead of Set-Cookie. { access_token: "7P1bwJtBTSKm-f_UHZFa6m2VWtyLNA8jHRiKUbhNwMQ", type: "Bearer", expires_in: 39584, refresh_token: "Ch9p0Q4KZjisw-vGDzjAQW583bj6He6eiRZOp1ovFLQ" }

(Example from Restful).

Solves some cookies problems with CDNs, session store, CSRF, CORS.

Services RestWS RESTful Endpoint Drupal 8

✖️ ✖️ ✔️ ✖️ ✖️

See #1494132

Page 40: REST in Peace

OAuth implementations in Drupal:

1. OAuth 1.0: https://www.drupal.org/project/oauth

2. OAuth 2.0: https://www.drupal.org/project/oauth2_server

OAuth & OAuth2

Services RestWS RESTful Endpoint Drupal 8

OAuth ✔️ ✖️ ✖️ ✖️ ✖️*

OAuth2 Server ✔️ ✖️ ✔️ ✖️ ✖️

Page 41: REST in Peace

Services RestWS RESTful Endpoint Drupal 8

Popularity

Documentation

Extensibility

Authentication

Performance

Auto API Docs

Versioning

Page 42: REST in Peace

Performance & Speed

Page 43: REST in Peace

How did we count?• Ubuntu 14.04, Nginx 1.8.0, Mariadb 10.0.20, PHP 5.5.9 with php5-fpm, 1GB RAM

• Minimal Drupal Profile

• Node with just Title and Body

• Disabled Drupal cache

• Anonymous requests

• HTTP POST to create entities

• Apache Benchmark (ab)

• Clean database after each ab run

Page 44: REST in Peace
Page 45: REST in Peace
Page 46: REST in Peace

Services RestWS RESTful Endpoint Drupal 8

Popularity

Documentation

Extensibility

Authentication

Performance

Auto API Docs

Versioning

Page 47: REST in Peace

From apiary.io

Page 49: REST in Peace

Self Documenting REST APIhttps://www.drupal.org/project/rest_api_doc (7.x)

Page 50: REST in Peace

RESTful OPTIONS Request

Page 51: REST in Peace

Self Documenting REST APIhttps://www.drupal.org/project/rest_api_doc (8.x)

Page 52: REST in Peace

Services RestWS RESTful Endpoint Drupal 8

Popularity

Documentation

Extensibility

Authentication

Performance

Auto API Docs

Versioning

Page 53: REST in Peace

1. Versioning API

2. Multiple endpoints: /api/v1, /api/v2/

Versioning in Services

Page 54: REST in Peace

Built-in resource versioning.

Versioning in RESTful

Page 55: REST in Peace

Services RestWS RESTful Endpoint Drupal 8

Popularity

Documentation

Extensibility

Authentication

Performance

Auto API Docs

Versioning

Page 56: REST in Peace

Better to make a difference together

than make it different alone

Page 57: REST in Peace

Leave feedback through Picbackhttp://promokids.github.io/[email protected]@kkomelin

[email protected]@kalabro

Page 58: REST in Peace
Page 59: REST in Peace

Bonus: Drupal as an API Client1. drupal_http_request()/ curl_exec()

2. RESTClient — Wrapper for 1.

3. Guzzle — PHP HTTP client

4. Feeds — for GET only

5. Clients — Pluggable client, supports Services endpoints

6. Remote Entity — Entity API + Clients

7. WSData — Alternative to Remote Entity

8. Integration with popular APIs: Twitter, Facebook, Dropbox etc.

9. Saucier — A Node.JS framework for Drupal API consumption.