PNWDS 2013- Restful development in Drupal 7/8

Post on 27-Jan-2015

107 views 4 download

Tags:

description

http://2013.pnwdrupalsummit.org/sessions/restful-development-drupal78

Transcript of PNWDS 2013- Restful development in Drupal 7/8

RESTful development in Drupal 7/8

Johannes Schmidttwitter: _johannezdrupal: user/670988github: johannez

What is REST?

• REpresentational State Transfer

• Resources

• Collections

Formats

JSON

XML

Formats

HAL+JSON

http://stateless.co/hal_specification.html

Verbs

• GET

• POST

• PUT / PATCH

• DELETE

Status codes

• 200 OK

• 201 Created

• 204 No content

• 400 Bad Request

• 403 Access Denied

• 404 Not Found

Headers

• Accept

• Content Type

• Authorization (Basic, OAuth)

Do you want to learn more?

Lorna Mitchell - REST for Web Developershttps://portland2013.drupal.org/node/1358

List of all Status Codeshttp://en.wikipedia.org/wiki/List_of_HTTP_status_codes

Implement a REST client

• Test the 3rd party REST API

• Make a request

• Handle responses

• Write tests!

Test the APICommand line

Browser Tools

curl -u api-key:x https://subdomain.chargify.com/customers.xml

Make a request

• drupal_http_request()

• curl()

• Guzzle PHP (http://guzzlephp.org)

Guzzle PHP example

Handle responses

• Make sure exceptions are caught and logged

• Convert the raw JSON data into resource objects

Write Tests!

• Drupal 7: DrupalWebTestCase

• Drupal 8: PHP Unit test framework

• One test for each function/request

• Test Driven Development (TDD)https://github.com/daylerees/test-driven-development-example

Drupal as RESTful service

REST + = ?

Drupal 7 - Services

• http://drupal.org/project/services

• Well established, lots of support modules

• Uses endpoints instead of resource paths

• Supports RPC and SOAP

• Only supports Drupal core entities

• Complex configuration options

Drupal 7 - Services Entity API

• http://drupal.org/project/services_entity

• Supports all entities through Entity API

• Configurable endpoints

Drupal 7 - RESTWS

• http://drupal.org/project/restws

• Uses Entity API

• No endpoints

• No configuration

• Cookie and Basic Auth

• Best candidate for RESTful service

Drupal 8

• Proper RESTful service in core

• HAL + JSON

• Cookie, Basic Auth (OAuth)

• Still in development.

Setup• Enable RESTful service and HAL module

• Define settings through YAML

https://drupal.org/documentation/modules/rest

Questions?

Thank you.

Let’s go and have a beer!