RESTful development in
Drupal 7/8
Johannes Schmidt
twitter: _johannez
drupal: user/670988
github: johannez
Implement a REST client
Drupal as a RESTful service
R
E
S
T
I
n
P
e
a
c
e
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 Developers
https://portland2013.drupal.org/node/1358
List of all ...
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/joha...
Test the API
Command 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...
Keep in mind...
• Don’t reinvent the wheel (packagist.org,
github)
• Use Composer (http://getcomposer.org)
• PSR-0 Standar...
Drupal as RESTful service
REST + = ?
Drupal 7 - Services
• http://drupal.org/project/services
• Well established, lots of support modules
• Uses endpoints inst...
Drupal 7 - Services Entity API
• http://drupal.org/project/services_entity
• Supports all entities through Entity API
• Co...
Drupal 7 - RESTWS
• http://drupal.org/project/restws
• Uses Entity API
• No endpoints
• No configuration
• Cookie and Basic...
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 throughYAML
https://drupal.org/documentation/modules/rest
https://www.youtube.com/watch?v=LJQQ9g7_GfA
Questions?
Thank you.
Let’s go and have a beer!
PNWDS 2013- Restful development in Drupal 7/8
Upcoming SlideShare
Loading in …5
×

PNWDS 2013- Restful development in Drupal 7/8

1,003 views
930 views

Published on

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

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,003
On SlideShare
0
From Embeds
0
Number of Embeds
145
Actions
Shares
0
Downloads
17
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

PNWDS 2013- Restful development in Drupal 7/8

  1. 1. RESTful development in Drupal 7/8 Johannes Schmidt twitter: _johannez drupal: user/670988 github: johannez
  2. 2. Implement a REST client Drupal as a RESTful service R E S T I n P e a c e mugley via Compfight CC What is REST?
  3. 3. What is REST? • REpresentational State Transfer • Resources • Collections
  4. 4. Formats JSON XML
  5. 5. Formats HAL+JSON http://stateless.co/hal_specification.html
  6. 6. Verbs • GET • POST • PUT / PATCH • DELETE
  7. 7. Status codes • 200 OK • 201 Created • 204 No content • 400 Bad Request • 403 Access Denied • 404 Not Found
  8. 8. Headers • Accept • Content Type • Authorization (Basic, OAuth)
  9. 9. Do you want to learn more? Lorna Mitchell - REST for Web Developers https://portland2013.drupal.org/node/1358 List of all Status Codes http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
  10. 10. Implement a REST client • Test the 3rd party REST API • Make a request • Handle responses • Write tests!
  11. 11. Example: Chargify API • Chargify.com • Module: https://drupal.org/project/ chargify_api • Package: https://github.com/johannez/ chargify
  12. 12. Test the API Command line Browser Tools curl -u api-key:x https://subdomain.chargify.com/customers.xml
  13. 13. https://www.youtube.com/watch?v=ZqlYsTiqqQQ
  14. 14. Make a request • drupal_http_request() • curl() • Guzzle PHP (http://guzzlephp.org)
  15. 15. Guzzle PHP example
  16. 16. Handle responses • Make sure exceptions are caught and logged • Convert the raw JSON data into resource objects
  17. 17. 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
  18. 18. Keep in mind... • Don’t reinvent the wheel (packagist.org, github) • Use Composer (http://getcomposer.org) • PSR-0 Standard https://github.com/php-fig/fig-standards/blob/ master/accepted/PSR-0.md
  19. 19. Drupal as RESTful service REST + = ?
  20. 20. 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
  21. 21. Drupal 7 - Services Entity API • http://drupal.org/project/services_entity • Supports all entities through Entity API • Configurable endpoints
  22. 22. Drupal 7 - RESTWS • http://drupal.org/project/restws • Uses Entity API • No endpoints • No configuration • Cookie and Basic Auth • Best candidate for RESTful service
  23. 23. https://www.youtube.com/watch?v=DJQnDZCbUHk
  24. 24. Drupal 8 • Proper RESTful service in core • HAL + JSON • Cookie, Basic Auth (OAuth) • Still in development.
  25. 25. Setup • Enable RESTful service and HAL module • Define settings throughYAML https://drupal.org/documentation/modules/rest
  26. 26. https://www.youtube.com/watch?v=LJQQ9g7_GfA
  27. 27. Questions?
  28. 28. Thank you. Let’s go and have a beer!

×