PNWDS 2013- Restful development in Drupal 7/8
-
Upload
johannes-schmidt -
Category
Technology
-
view
107 -
download
4
description
Transcript of PNWDS 2013- Restful development in Drupal 7/8
RESTful development in Drupal 7/8
Johannes Schmidttwitter: _johannezdrupal: user/670988github: johannez
Implement a REST client
Drupal as a RESTful service
REST
In
Peace
mugley via Compfight CC
What is REST?
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!
Example: Chargify API
• Chargify.com
• Module: https://drupal.org/project/chargify_api
• Package: https://github.com/johannez/chargify
Test the APICommand line
Browser Tools
curl -u api-key:x https://subdomain.chargify.com/customers.xml
https://www.youtube.com/watch?v=ZqlYsTiqqQQ
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
Keep in mind...
• Don’t reinvent the wheel (packagist.org, github)
• Use Composer (http://getcomposer.org)
• PSR-0 Standardhttps://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md
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
https://www.youtube.com/watch?v=DJQnDZCbUHk
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
https://www.youtube.com/watch?v=LJQQ9g7_GfA
Questions?
Thank you.
Let’s go and have a beer!