talking to web services
DESCRIPTION
Web services are a treasure trove of tools, content and data. I'll be exploring how we can use Drupal's frameworks to tap into these services. From strategy and selecting the right approach, to triggering, encoding and sending HTTP messages, I'll walk through how you might go about writing a custom integration that puts your Drupal build into a conversation with the outside world. I'll follow up with real world examples I've built to interact with NASA's ECHO Earth science data service (http://earthdata.nasa.gov/echo) and the Agile Zen project management tool (http://agilezen.com). Sandboxes for the code demoed in this session are available at: * ECHO - http://drupal.org/sandbox/dbassendine/1829568 * AgileZen - http://drupal.org/sandbox/dbassendine/1828082 Presented by David Bassendine on 10/27/2012 at Drupalcamp Atlanta (https://www.drupalcampatlanta.com/session/talking-web-services).TRANSCRIPT
Talking to Web ServicesThe outside world from Drupal
David BassendineSenior Developer
Me
Day: Mediacurrent
Night: Science & Sustainability
@dbassendine
linkedin.com/in/dbassendine
"Web Services"
open data: science, gov. & development
business tools: customers, pm, seo
social: the big conversation
... (very) big data (and growing)
compfight.com
monitter.com
http://goo.gl/bCkJf
http://goo.gl/gkuDS
agilezen.com
campaignmonitor.com image:http://goo.gl/qlQQ8
The task ahead ...
Creative Commons (CC BY 2.0) by Flickr user mariachily http://goo.gl/iU8aI
http://reverb.echo.nasa.gov/
agilezen.com
Is there a module for that?
Step 0
HTTP and REST
Step 1
1: HTTP Comm.s
1: HTTP Comm.s
1: REST architectures
REST SOAP (XMLRPC)
Resources URIs Custom (data packet)
Methods HTTP verbs Custom (data packet)
Errors HTTP codes Custom (data packet)
Data encoding JSON / XML XML
Consistent*, chatty Flexible but inconsistent
REST for your wife: http://tomayko.com/writings/rest-to-my-wife
REST hypermedia: http://martinfowler.com/articles/richardsonMaturityModel.html
REST vs/ SOAP: http://en.wikipedia.org/wiki/Representational_state_transfer
API Documentation
Step 2
URLs and path structure
Authentication
Resources
Data formats
2: API Documentation
Agile Zen ECHO
URL Structure RESTful RESTful
Authentication Required: Header or Parameter token
Not required for search
Resources Stories, Projects ... Datasets, Granules...
Data formats JSON XML JSON/XML (GET) XML AQL (POST)
dev.agilezen.com http://goo.gl/b38dJ
Test in the browser
Step 3
GET test (simple URL) – eg. http://goo.gl/XUFtn
POSTer test (browser plugin) – Chrome: XHR POSTer – Firefox: Poster
Plan the approach
Step 4
4: Approaches
Drupal core HTTP http://goo.gl/pnML8
REST API Query API drupal.org/project/
rest_api_query
Services Clientdrupal.org/project/
services_client
Feeds (GET only)drupal.org/project/feeds
4: Approaches
Now start to code ...
Step 5
Drupal core HTTP
Drupal 7
Demo: NASA ECHO API
Reason for approach: REST but requires XML
Step 5a
Code: http://drupal.org/sandbox/dbassendine/1829568
Drupal core HTTP
Pros
Flexible
Cons
More code (169 lines)
Encoding / decoding not inc.
Less structural consistency
Step 5a
Encoding / decoding not inc.Well structuredrequest/response
5a: ECHO API
Demo: ECHO API
REST API Query API
Drupal 6 (Open Atrium)
Service: Agile Zen API
Reason for approach: REST and supports JSON
Step 5b
Code: http://drupal.org/sandbox/dbassendine/1828082
REST API Query API
Pros
Less code (114L)
Cons
No XML support
Encoding / decoding inc. Assumptions
More structural consistency
Step 5b
5b: Agile Zen API
Demo: Agile Zen API
Creative Commons (CC BY 2.0) by Flickr user Paxson Woelber http://goo.gl/MYPeM
Where next?
Rules
Logging
Job Scheduler
Views w/ Data module
Queue
Triggers Internal Export
Core hooks
Skills & resources
SimpleXML http://goo.gl/IvaKX (php.net)
HTTP protocol http://www.jmarshall.com/easy/http
REST http://rest.elkstein.org
json_encode/decode http://goo.gl/lKHZ (php.net)
Drupal core HTTP http://goo.gl/pnML8 (d.o)
Drupal REST API Query API drupal.org/project/rest_api_query