An introduction to consuming remote APIs<br />Pacific Northwest Drupal Summit<br />10/16/2011<br />Joshua Kopel<br />Partn...
An introduction to consuming remote APIs<br />General overview and terms<br />A simple example<br />More complex considera...
Today<br />We are talking specifically about RESTful interaction berween Drupal (7) and remote web services over HTTP usin...
RESTful?<br />REpresentational State Transfer<br />Client / Server<br />Stateless<br />HTTP methods<br />There is also SOA...
Remote APIs as:<br />Information sources<br />Wunderground<br />Twitter<br />Media sources<br />Flickr<br />YouTube<br />S...
The Drupal way<br />Pro<br />Write minimal code!<br />Feeds, Rules, Entities<br />Easily modified<br />Fully integrated wi...
The coder way<br />Pro<br />Write fast code<br />Build a useful API for other modules<br />Manage complex interactions in ...
Be flexible<br />Look for easy wins<br />Use modules where possible <br />Investigate the tradeoffs<br />
CommonCraft<br />
CommonCraft<br />Recurly for recurring subscription payments<br />Wistia as video CDN<br />
Basic flow<br />Format a request URI that references a resource and provides parameters.<br /> Send request using http met...
Simple API example<br />GET https://api.wistia.com/v1/projects.json<br />Returns a list of all projects<br />
Fomatted URL<br />GET https://user:password@api.wistia.com/v1/ projects.json?sort_by=created&sort_direction=0<br />https:/...
Basic code<br />$query = array('sort_by' => 'created', 'sort_direction' => 0);<br />$options = array('query' => $query, 'h...
Now what?<br />Store<br />Entity<br />Table<br />Display<br />As part of node or page<br />Dynamically with js<br />
Building blocks<br />Consuming code<br />(module hooks)<br />Admin configuration<br />API methods<br />Security info in va...
Building blocks<br />Consuming code<br />(module hooks)<br />Admin configuration<br />API methods<br />Security info in va...
Security<br />$url = url('https://api:H7gd7n2yV9opM@ api.wistia.com/…<br />SSL<br />http basic authorization<br />Domain k...
Building blocks<br />Consuming code<br />(module hooks)<br />Admin configuration<br />API methods<br />Security info in va...
Low level communication<br />Standard functions to:<br />Format URL(s)<br />Format headers & http method<br />Format query...
Sending data<br />Usually uses the POST method<br />Build an array of encoded parameters<br />Serialize binary data - base...
Sending LOTS of data<br />Files can be sent and retrieved using stream wrappers.<br />Wraps a remote API and creates an ad...
Building blocks<br />Consuming code<br />(module hooks)<br />Admin configuration<br />API methods<br />Security info in va...
API methods<br />Map the remote API to what your module needs.<br />Parse decoded data and return only what is needed.<br ...
Building blocks<br />Consuming code<br />(module hooks)<br />Admin configuration<br />API methods<br />Security info in va...
Consuming Code<br />AKA “business logic”<br />Use hooks to trigger calls to the remote.<br />Format results for display or...
When to act<br />On demand<br />hook_nnnnn_presave()<br />hook_nnnnn_load()<br />hook_nnnnn_view()<br />Ajax (menu) callba...
Things go wrong<br />drupal_http_request is synchronous<br />Use reasonable timeout values<br />Cache if you can<br />Remo...
Play nice<br />Don’t pound their servers<br />Cache if you can<br />Follow the rules<br />Find the communities and join in...
Useful tools<br />Debugger<br />Xdebug<br />http headers “sniffer”<br />Live http headers extension (FF)<br />HTTP Headers...
Thanks!<br />http://www.number10webcompany.com<br />Joshua Kopel<br />Partner, Number 10 Web Company<br />CTO/Co-founder, ...
Upcoming SlideShare
Loading in...5
×

An introduction to consuming remote APIs with Drupal 7

15,575

Published on

Your client says "Can we make the site work with snarkly.com? I hear they have a great API".
What do you do?
Don't panic!

Working with remote services can be an incredibly powerful way to leverage the hard work of other developers and to build the capabilities of Drupal. Cool web services offering useful data and functions are proliferating like rabbits, and many have an API that they promote as "well documented and easy to use". Sometimes this is even true. We will look at two such services and review the basic building blocks of modules to interact with them.

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

No Downloads
Views
Total Views
15,575
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
40
Comments
0
Likes
10
Embeds 0
No embeds

No notes for slide
  • They will make you smarterThey will get your work done for youThey will bring you beer
  • An introduction to consuming remote APIs with Drupal 7

    1. 1. An introduction to consuming remote APIs<br />Pacific Northwest Drupal Summit<br />10/16/2011<br />Joshua Kopel<br />Partner, Number 10 Web Company<br />CTO/Co-founder, Squigaloo LLC<br />Twitter:jkopel<br />d.o:jkopel<br />
    2. 2. An introduction to consuming remote APIs<br />General overview and terms<br />A simple example<br />More complex considerations<br />Do’s and Don’ts<br />Tools and resources<br />
    3. 3. Today<br />We are talking specifically about RESTful interaction berween Drupal (7) and remote web services over HTTP using JSON, XML, or a variety of other languages.<br />
    4. 4. RESTful?<br />REpresentational State Transfer<br />Client / Server<br />Stateless<br />HTTP methods<br />There is also SOAP which is useful for transactional or sensitive information.<br />
    5. 5. Remote APIs as:<br />Information sources<br />Wunderground<br />Twitter<br />Media sources<br />Flickr<br />YouTube<br />Services<br />Google Charts<br />SalesForce<br />Storage<br />Amazon S3<br />Google Cloud Storage<br />
    6. 6. The Drupal way<br />Pro<br />Write minimal code!<br />Feeds, Rules, Entities<br />Easily modified<br />Fully integrated with the rest of Drupal<br />Con<br />Difficult to manage complex interactions<br />Implementation scattered across modules<br />Performance challenges<br />
    7. 7. The coder way<br />Pro<br />Write fast code<br />Build a useful API for other modules<br />Manage complex interactions in code<br />Con<br />Must expose data in order to integrate<br />Overhead, maintenance, security, updates<br />
    8. 8. Be flexible<br />Look for easy wins<br />Use modules where possible <br />Investigate the tradeoffs<br />
    9. 9. CommonCraft<br />
    10. 10. CommonCraft<br />Recurly for recurring subscription payments<br />Wistia as video CDN<br />
    11. 11. Basic flow<br />Format a request URI that references a resource and provides parameters.<br /> Send request using http method (POST, GET, DELETE, etc).<br />Decode response and pass to appropriate consumer.<br />Remote API<br />Process request, format a response, and define a status.<br />Check authentication and accept request.<br />
    12. 12. Simple API example<br />GET https://api.wistia.com/v1/projects.json<br />Returns a list of all projects<br />
    13. 13. Fomatted URL<br />GET https://user:password@api.wistia.com/v1/ projects.json?sort_by=created&sort_direction=0<br />https://user:password@api.wistia.com/v1<br />secured URL<br />projects<br />resource<br />.json<br />language<br />?sort_by=created&sort_direction=0 <br />Url encoded list of parameters<br />Returns a list of all projects sorted by creation date in descending order<br />
    14. 14. Basic code<br />$query = array('sort_by' => 'created', 'sort_direction' => 0);<br />$options = array('query' => $query, 'https' => TRUE);<br />$url = url('https://usr:pass@api.wistia.com/v1/projects.json', $options);<br />$options = array('method'=>'GET');<br />$result = drupal_http_request($url, $options);<br />$json = json_decode($result->data);<br />-or-<br />$xml = simplexml_load_string ($result);<br />
    15. 15. Now what?<br />Store<br />Entity<br />Table<br />Display<br />As part of node or page<br />Dynamically with js<br />
    16. 16. Building blocks<br />Consuming code<br />(module hooks)<br />Admin configuration<br />API methods<br />Security info in variables<br />Low level comm.<br />Remote API<br />
    17. 17. Building blocks<br />Consuming code<br />(module hooks)<br />Admin configuration<br />API methods<br />Security info in variables<br />Low level comm.<br />Remote API<br />
    18. 18. Security<br />$url = url('https://api:H7gd7n2yV9opM@ api.wistia.com/…<br />SSL<br />http basic authorization<br />Domain keys/subdomains<br />OAuth<br />
    19. 19. Building blocks<br />Consuming code<br />(module hooks)<br />Admin configuration<br />API methods<br />Security info in variables<br />Low level comm.<br />Remote API<br />
    20. 20. Low level communication<br />Standard functions to:<br />Format URL(s)<br />Format headers & http method<br />Format query strings<br />Request data<br />Read http status<br />Perform error checking on inbound data<br />
    21. 21. Sending data<br />Usually uses the POST method<br />Build an array of encoded parameters<br />Serialize binary data - base64_encode()<br />$headers = array('Content-Type' => 'application/x-www-form-urlencoded')<br />$query = drupal_http_build_query(array('name' => $name));<br />$options = array(‘headers'=>$headers, 'method'=>‘POST', 'data'=>$query);<br />$result = drupal_http_request($url, $options);<br />
    22. 22. Sending LOTS of data<br />Files can be sent and retrieved using stream wrappers.<br />Wraps a remote API and creates an addressable protocol (i.e. youtube://)<br />It is a whole other session.<br />
    23. 23. Building blocks<br />Consuming code<br />(module hooks)<br />Admin configuration<br />API methods<br />Security info in variables<br />Low level comm.<br />Remote API<br />
    24. 24. API methods<br />Map the remote API to what your module needs.<br />Parse decoded data and return only what is needed.<br />Format data for transfer to remote API.<br />Return meaningful error messages based upon context.<br />
    25. 25. Building blocks<br />Consuming code<br />(module hooks)<br />Admin configuration<br />API methods<br />Security info in variables<br />Low level comm.<br />Remote API<br />
    26. 26. Consuming Code<br />AKA “business logic”<br />Use hooks to trigger calls to the remote.<br />Format results for display or storage.<br />Caching<br />Permissions<br />
    27. 27. When to act<br />On demand<br />hook_nnnnn_presave()<br />hook_nnnnn_load()<br />hook_nnnnn_view()<br />Ajax (menu) callback<br />Rule action<br />Scheduled<br />hook_cron()<br />
    28. 28. Things go wrong<br />drupal_http_request is synchronous<br />Use reasonable timeout values<br />Cache if you can<br />Remote API<br />
    29. 29. Play nice<br />Don’t pound their servers<br />Cache if you can<br />Follow the rules<br />Find the communities and join in<br />
    30. 30. Useful tools<br />Debugger<br />Xdebug<br />http headers “sniffer”<br />Live http headers extension (FF)<br />HTTP Headers (chrome)<br />A reverse proxy / http monitor<br />Charles (http://www.charlesproxy.com/)<br />HttpFox (FF)<br />
    31. 31. Thanks!<br />http://www.number10webcompany.com<br />Joshua Kopel<br />Partner, Number 10 Web Company<br />CTO/Co-founder, Squigaloo LLC<br />Twitter:jkopel<br />d.o:jkopel<br />
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×