decoupled drupal 8 and iot
TRANSCRIPT
Decoupled Drupal 8 and IoT
cheppers.com Miro MichalickaDrupal developer
DrupalCamp London 2016
Drupal enthusiast @Cheppers5+ years experience with web developmentBoard member of Slovak Drupal Association
whoami
CONTENTAPI best practises
Decoupling
Drupal 7 and Drupal 8decoupling options
Drupal 8 & IoT
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
Decoupling
WHAT IS IT?
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
DON’T MAKEFULL DECOUPLING
DRUPAL 7
RESTful Web Services
Services
RESTful
RESTful WEB SERVICES
PROS
partially OOP
CONS
versionable API
base for Drupal 8 REST module
SERVICES
PROS
active Drupal 8 branchversionable API
CONS
procedural code
most common solution
RESTful
PROS
OOP
versionable API
CONS
none found yet
DRUPAL 8RESTIN CORE
BUT…
DRUPAL 8
PROS
REST in core
CONS
non-versionable
~60 issues in backlog
DRUPAL 8
REST module
REST output from Views
RESTful FOR DRUPAL 8
IoT
INTERNET OFTHINGS
PICK-UPKIOSK
CHALLENGES
AUTHENTICATION DRUPAL REST HARDWARE
CHALLENGE #1
AUTHENTICATIONAUTHENTICATION
SOLUTION #1
OWN AUTHENTICATION PROVIDER
phone number and PIN
CHALLENGE #1
AUTHENTICATIONAUTHENTICATION
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; }
}
SOLUTION #1
CHALLENGE #1
AUTHENTICATIONAUTHENTICATION
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
SOLUTION #1
CHALLENGE #2
DRUPAL REST
SOLUTION #2
OWN REST END-POINTS
Views REST outputcustom-coded end-points
CHALLENGE #2
Views
DRUPAL REST
OWN REST END-POINTS
SOLUTION #2
CHALLENGE #2
DRUPAL REST
Solve using RouteSubscriber
https://docs.google.com/presentation/d/1wN7zICkTXcQp8d8UKMQz6oaMM_C2b58AC4oN_sywRCU
ViewsOWN REST END-POINTS
SOLUTION #2
CHALLENGE #2
DRUPAL REST
Views UI needs some workhttps://drupal.org/node/2228141
ViewsOWN REST END-POINTS
SOLUTION #2
CHALLENGE #2
DRUPAL REST
use Drupal Console
OWN REST END-POINTSCustom end-points
SOLUTION #2
CHALLENGE #2
DRUPAL REST
enable via REST UI or rest.settings.yml
OWN REST END-POINTSCustom end-points
SOLUTION #2
CHALLENGE #3
easiest one
HARDWARE
HARDWARE
SOLUTION #3
LESSONSLEARNED
Decoupling with IoT is viable concept
Drupal 8 REST needs improvements