Top Banner
Drupal 8 REST WTF? miro.michalicka Miro Michalicka
31

Drupal8 REST WTF?

Apr 07, 2017

Download

Technology

Miro Michalicka
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: Drupal8 REST WTF?

Drupal 8 REST WTF?

miro.michalickaMiro Michalicka

Page 2: Drupal8 REST WTF?

Drupal enthusiast @Cheppers5+ years experience with web development

whoami

Page 3: Drupal8 REST WTF?

CONTENTMy story with headless Drupal

API best practises

Decoupling options in Drupal 8

Page 4: Drupal8 REST WTF?

Decoupling

Page 5: Drupal8 REST WTF?
Page 6: Drupal8 REST WTF?

WHAT IS IT?

Page 7: Drupal8 REST WTF?

DECOUPLING

PROS

flexible front-end

lack of Drupal specialists

multivendor back-end

strengths of Drupal back-end

and back office

CONS

loose some Drupal capabilities

multiple requests for resources

growth of teams

Page 8: Drupal8 REST WTF?

API BEST PRACTISES

DOCUMENTATION

stability and consistency

flexibility

security

ease of adoption

Source: http://www.toptal.com/api-developers/5-golden-rules-for-designing-a-great-web-api

Page 9: Drupal8 REST WTF?

DOCUMENTATION

Page 10: Drupal8 REST WTF?

DOCUMENTATION

Self Documenting REST API

Page 11: Drupal8 REST WTF?

API BEST PRACTISES

documentation

SCALABILITY AND CONSISTENCY

flexibility

security

ease of adoption

Source: http://www.toptal.com/api-developers/5-golden-rules-for-designing-a-great-web-api

Page 12: Drupal8 REST WTF?

GET http://mysite.com/entity/node/1

{ “title”: “My first node”, “body”: “Lorem ipsum…” }

SCALABILITY AND

CONSISTENCY

Page 13: Drupal8 REST WTF?

GET http://mysite.com/article/1

{ “title”: “My first node”, “body”: “Lorem ipsum…” }

SCALABILITY AND

CONSISTENCY

Page 14: Drupal8 REST WTF?

GET http://mysite.com/article/1

{ “title”: “My first node”, “body”: “Lorem ipsum…”, “tags”: [{ “blog”, “just trying” }] }SCALABILITY

AND CONSISTENCY

Page 15: Drupal8 REST WTF?

GET http://mysite.com/api/v2/article/1

{ “title”: “My first node”, “body”: “Lorem ipsum…”, “tags”: [{ “blog”, “just trying” }] }

SCALABILITY AND

CONSISTENCY

GET http://mysite.com/api/v1/article/1

{ “title”: “My first node”, “body”: “Lorem ipsum…” }

Page 16: Drupal8 REST WTF?

GET http://mysite.com/api/v1/article/1

{ “title”: “My first node”, “body”: “Lorem ipsum…” }

SCALABILITY AND

CONSISTENCY

GET http://mysite.com/api/blog/2?_version=1

{ “title”: “My first node”, “body”: “Lorem ipsum…” }

Page 17: Drupal8 REST WTF?

API BEST PRACTISES

documentation

scalability and consistency

FLEXIBILITY

security

ease of adoption

Source: http://www.toptal.com/api-developers/5-golden-rules-for-designing-a-great-web-api

Page 18: Drupal8 REST WTF?

FLEXIBILITY

Page 19: Drupal8 REST WTF?

API BEST PRACTISES

documentation

stability and consistency

flexibility

SECURITY

ease of adoption

Source: http://www.toptal.com/api-developers/5-golden-rules-for-designing-a-great-web-api

Page 20: Drupal8 REST WTF?

SECURITY

cookiesbasic authown authentication provider

Page 21: Drupal8 REST WTF?

OWN AUTHENTICATION PROVIDER

<?php/** * @file * Contains \Drupal\pin_auth\Authentication\Provider\PinAuth. */namespace Drupal\pin_auth\Authentication\Provider; use Drupal\Core\Authentication\AuthenticationProviderInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; /** * HTTP Basic authentication provider. */class PinAuth implements AuthenticationProviderInterface { /** * The entity type manager. * * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ protected $entityTypeManager; /** * Constructs a HTTP basic authentication provider object. * * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity manager service. */ public function __construct(EntityTypeManagerInterface $entity_type_manager) { $this->entityTypeManager = $entity_type_manager; }

}

SECURITY

Page 22: Drupal8 REST WTF?

public function applies(Request $request) { if (!empty($request->headers->get('pin')) && !empty($request->headers->get(‘number'))) { return TRUE; } return FALSE; } public function authenticate(Request $request) { $pin = $request->headers->get('pin'); $number = $request->headers->get('number'); $user = NULL; $user = $this->entityTypeManager->getStorage('user') ->getQuery() ->condition('field_phone_number', $number) ->condition('field_pin',$pin) ->range(0,1) ->execute(); if (!empty($user)) { return $user; } else { throw new AccessDeniedHttpException(); } }

OWN AUTHENTICATION PROVIDER

SECURITY

Page 23: Drupal8 REST WTF?

Solve using RouteSubscriber

https://docs.google.com/presentation/d/1wN7zICkTXcQp8d8UKMQz6oaMM_C2b58AC4oN_sywRCU

SECURITY

OWN REST END-POINTSViews

Page 24: Drupal8 REST WTF?

https://drupal.org/node/2228141

ViewsOWN REST END-POINTS

SECURITY

Page 25: Drupal8 REST WTF?

API BEST PRACTISES

documentation

stability and consistency

flexibility

security

EASE OF ADOPTION

Source: http://www.toptal.com/api-developers/5-golden-rules-for-designing-a-great-web-api

Page 26: Drupal8 REST WTF?

EASE OF ADOPTION

Page 27: Drupal8 REST WTF?

DECOUPLING OPTIONSIN DRUPAL 8

REST in core

RELAXed

Services

Page 28: Drupal8 REST WTF?

DECOUPLING OPTIONSIN DRUPAL 8

GraphQL

JSON API

Page 29: Drupal8 REST WTF?

DECOUPLING OPTIONSIN DRUPAL 8

Page 30: Drupal8 REST WTF?

THANK YOUQUESTIONS?

Page 31: Drupal8 REST WTF?