29
RESTful development in Drupal 7/8 Johannes Schmidt twitter: _johannez drupal: user/670988 github: johannez

PNWDS 2013- Restful development in Drupal 7/8

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: PNWDS 2013- Restful development in Drupal 7/8

RESTful development in Drupal 7/8

Johannes Schmidttwitter: _johannezdrupal: user/670988github: johannez

Page 3: PNWDS 2013- Restful development in Drupal 7/8

What is REST?

• REpresentational State Transfer

• Resources

• Collections

Page 4: PNWDS 2013- Restful development in Drupal 7/8

Formats

JSON

XML

Page 5: PNWDS 2013- Restful development in Drupal 7/8

Formats

HAL+JSON

http://stateless.co/hal_specification.html

Page 6: PNWDS 2013- Restful development in Drupal 7/8

Verbs

• GET

• POST

• PUT / PATCH

• DELETE

Page 7: PNWDS 2013- Restful development in Drupal 7/8

Status codes

• 200 OK

• 201 Created

• 204 No content

• 400 Bad Request

• 403 Access Denied

• 404 Not Found

Page 8: PNWDS 2013- Restful development in Drupal 7/8

Headers

• Accept

• Content Type

• Authorization (Basic, OAuth)

Page 9: PNWDS 2013- Restful development in Drupal 7/8

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

Page 10: PNWDS 2013- Restful development in Drupal 7/8

Implement a REST client

• Test the 3rd party REST API

• Make a request

• Handle responses

• Write tests!

Page 12: PNWDS 2013- Restful development in Drupal 7/8

Test the APICommand line

Browser Tools

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

Page 14: PNWDS 2013- Restful development in Drupal 7/8

Make a request

• drupal_http_request()

• curl()

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

Page 15: PNWDS 2013- Restful development in Drupal 7/8

Guzzle PHP example

Page 16: PNWDS 2013- Restful development in Drupal 7/8

Handle responses

• Make sure exceptions are caught and logged

• Convert the raw JSON data into resource objects

Page 17: PNWDS 2013- Restful development in Drupal 7/8
Page 18: PNWDS 2013- Restful development in Drupal 7/8

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

Page 20: PNWDS 2013- Restful development in Drupal 7/8

Drupal as RESTful service

REST + = ?

Page 21: PNWDS 2013- Restful development in Drupal 7/8

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

Page 22: PNWDS 2013- Restful development in Drupal 7/8

Drupal 7 - Services Entity API

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

• Supports all entities through Entity API

• Configurable endpoints

Page 23: PNWDS 2013- Restful development in Drupal 7/8

Drupal 7 - RESTWS

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

• Uses Entity API

• No endpoints

• No configuration

• Cookie and Basic Auth

• Best candidate for RESTful service

Page 25: PNWDS 2013- Restful development in Drupal 7/8

Drupal 8

• Proper RESTful service in core

• HAL + JSON

• Cookie, Basic Auth (OAuth)

• Still in development.

Page 26: PNWDS 2013- Restful development in Drupal 7/8

Setup• Enable RESTful service and HAL module

• Define settings through YAML

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

Page 28: PNWDS 2013- Restful development in Drupal 7/8

Questions?

Page 29: PNWDS 2013- Restful development in Drupal 7/8

Thank you.

Let’s go and have a beer!