An introduction to consuming remote APIsPacific Northwest Drupal Summit10/16/2011Joshua KopelPartner, Number 10 Web CompanyCTO/Co-founder, Squigaloo LLCTwitter:jkopeld.o:jkopel
An introduction to consuming remote APIsGeneral overview and termsA simple exampleMore complex considerationsDo’s and Don’tsTools and resources
TodayWe are talking specifically about RESTful interaction berween Drupal (7) and remote web services over HTTP using JSON, XML, or a variety of other languages.
RESTful?REpresentational State TransferClient / ServerStatelessHTTP methodsThere is also SOAP which is useful for transactional or sensitive information.
Remote APIs as:Information sourcesWundergroundTwitterMedia sourcesFlickrYouTubeServicesGoogle ChartsSalesForceStorageAmazon S3Google Cloud Storage
The Drupal wayProWrite minimal code!Feeds, Rules, EntitiesEasily modifiedFully integrated with the rest of DrupalConDifficult to manage complex interactionsImplementation scattered across modulesPerformance challenges
The coder wayProWrite fast codeBuild a useful API for other modulesManage complex interactions in codeConMust expose data in order to integrateOverhead, maintenance, security, updates
Be flexibleLook for easy winsUse modules where possible Investigate the tradeoffs
CommonCraft
CommonCraftRecurly for recurring subscription paymentsWistia as video CDN
Basic flowFormat a request URI that references a resource and provides parameters. Send request using http method (POST, GET, DELETE, etc).Decode response and pass to appropriate consumer.Remote APIProcess request, format a response, and define a status.Check authentication and accept request.
Simple API exampleGET https://api.wistia.com/v1/projects.jsonReturns a list of all projects
Fomatted URLGET https://user:password@api.wistia.com/v1/ projects.json?sort_by=created&sort_direction=0https://user:password@api.wistia.com/v1secured URLprojectsresource.jsonlanguage?sort_by=created&sort_direction=0 Url encoded list of parametersReturns a list of all projects sorted by creation date in descending order
Basic code$query = array('sort_by' => 'created', 'sort_direction' => 0);$options = array('query' => $query, 'https' => TRUE);$url = url('https://usr:pass@api.wistia.com/v1/projects.json', $options);$options = array('method'=>'GET');$result = drupal_http_request($url, $options);$json = json_decode($result->data);-or-$xml = simplexml_load_string ($result);
Now what?StoreEntityTableDisplayAs part of node or pageDynamically with js
Building blocksConsuming code(module hooks)Admin configurationAPI methodsSecurity info in variablesLow level comm.Remote API
Building blocksConsuming code(module hooks)Admin configurationAPI methodsSecurity info in variablesLow level comm.Remote API
Security$url = url('https://api:H7gd7n2yV9opM@ api.wistia.com/…SSLhttp basic authorizationDomain keys/subdomainsOAuth
Building blocksConsuming code(module hooks)Admin configurationAPI methodsSecurity info in variablesLow level comm.Remote API
Low level communicationStandard functions to:Format URL(s)Format headers & http methodFormat query stringsRequest dataRead http statusPerform error checking on inbound data
Sending dataUsually uses the POST methodBuild an array of encoded parametersSerialize binary data - base64_encode()$headers = array('Content-Type' => 'application/x-www-form-urlencoded')$query = drupal_http_build_query(array('name' => $name));$options = array(‘headers'=>$headers, 'method'=>‘POST', 'data'=>$query);$result = drupal_http_request($url, $options);
Sending LOTS of dataFiles can be sent and retrieved using stream wrappers.Wraps a remote API and creates an addressable protocol (i.e. youtube://)It is a whole other session.
Building blocksConsuming code(module hooks)Admin configurationAPI methodsSecurity info in variablesLow level comm.Remote API
API methodsMap the remote API to what your module needs.Parse decoded data and return only what is needed.Format data for transfer to remote API.Return meaningful error messages based upon context.
Building blocksConsuming code(module hooks)Admin configurationAPI methodsSecurity info in variablesLow level comm.Remote API
Consuming CodeAKA “business logic”Use hooks to trigger calls to the remote.Format results for display or storage.CachingPermissions
When to actOn demandhook_nnnnn_presave()hook_nnnnn_load()hook_nnnnn_view()Ajax (menu) callbackRule actionScheduledhook_cron()
Things go wrongdrupal_http_request is synchronousUse reasonable timeout valuesCache if you canRemote API
Play niceDon’t pound their serversCache if you canFollow the rulesFind the communities and join in
Useful toolsDebuggerXdebughttp headers “sniffer”Live http headers extension (FF)HTTP Headers (chrome)A reverse proxy / http monitorCharles (http://www.charlesproxy.com/)HttpFox (FF)
Thanks!http://www.number10webcompany.comJoshua KopelPartner, Number 10 Web CompanyCTO/Co-founder, Squigaloo LLCTwitter:jkopeld.o:jkopel

An introduction to consuming remote APIs with Drupal 7

  • 1.
    An introduction toconsuming remote APIsPacific Northwest Drupal Summit10/16/2011Joshua KopelPartner, Number 10 Web CompanyCTO/Co-founder, Squigaloo LLCTwitter:jkopeld.o:jkopel
  • 2.
    An introduction toconsuming remote APIsGeneral overview and termsA simple exampleMore complex considerationsDo’s and Don’tsTools and resources
  • 3.
    TodayWe are talkingspecifically about RESTful interaction berween Drupal (7) and remote web services over HTTP using JSON, XML, or a variety of other languages.
  • 4.
    RESTful?REpresentational State TransferClient/ ServerStatelessHTTP methodsThere is also SOAP which is useful for transactional or sensitive information.
  • 5.
    Remote APIs as:InformationsourcesWundergroundTwitterMedia sourcesFlickrYouTubeServicesGoogle ChartsSalesForceStorageAmazon S3Google Cloud Storage
  • 6.
    The Drupal wayProWriteminimal code!Feeds, Rules, EntitiesEasily modifiedFully integrated with the rest of DrupalConDifficult to manage complex interactionsImplementation scattered across modulesPerformance challenges
  • 7.
    The coder wayProWritefast codeBuild a useful API for other modulesManage complex interactions in codeConMust expose data in order to integrateOverhead, maintenance, security, updates
  • 8.
    Be flexibleLook foreasy winsUse modules where possible Investigate the tradeoffs
  • 9.
  • 10.
    CommonCraftRecurly for recurringsubscription paymentsWistia as video CDN
  • 11.
    Basic flowFormat arequest URI that references a resource and provides parameters. Send request using http method (POST, GET, DELETE, etc).Decode response and pass to appropriate consumer.Remote APIProcess request, format a response, and define a status.Check authentication and accept request.
  • 12.
    Simple API exampleGEThttps://api.wistia.com/v1/projects.jsonReturns a list of all projects
  • 13.
    Fomatted URLGET https://user:password@api.wistia.com/v1/projects.json?sort_by=created&sort_direction=0https://user:password@api.wistia.com/v1secured URLprojectsresource.jsonlanguage?sort_by=created&sort_direction=0 Url encoded list of parametersReturns a list of all projects sorted by creation date in descending order
  • 14.
    Basic code$query =array('sort_by' => 'created', 'sort_direction' => 0);$options = array('query' => $query, 'https' => TRUE);$url = url('https://usr:pass@api.wistia.com/v1/projects.json', $options);$options = array('method'=>'GET');$result = drupal_http_request($url, $options);$json = json_decode($result->data);-or-$xml = simplexml_load_string ($result);
  • 15.
    Now what?StoreEntityTableDisplayAs partof node or pageDynamically with js
  • 16.
    Building blocksConsuming code(modulehooks)Admin configurationAPI methodsSecurity info in variablesLow level comm.Remote API
  • 17.
    Building blocksConsuming code(modulehooks)Admin configurationAPI methodsSecurity info in variablesLow level comm.Remote API
  • 18.
    Security$url = url('https://api:H7gd7n2yV9opM@api.wistia.com/…SSLhttp basic authorizationDomain keys/subdomainsOAuth
  • 19.
    Building blocksConsuming code(modulehooks)Admin configurationAPI methodsSecurity info in variablesLow level comm.Remote API
  • 20.
    Low level communicationStandardfunctions to:Format URL(s)Format headers & http methodFormat query stringsRequest dataRead http statusPerform error checking on inbound data
  • 21.
    Sending dataUsually usesthe POST methodBuild an array of encoded parametersSerialize binary data - base64_encode()$headers = array('Content-Type' => 'application/x-www-form-urlencoded')$query = drupal_http_build_query(array('name' => $name));$options = array(‘headers'=>$headers, 'method'=>‘POST', 'data'=>$query);$result = drupal_http_request($url, $options);
  • 22.
    Sending LOTS ofdataFiles can be sent and retrieved using stream wrappers.Wraps a remote API and creates an addressable protocol (i.e. youtube://)It is a whole other session.
  • 23.
    Building blocksConsuming code(modulehooks)Admin configurationAPI methodsSecurity info in variablesLow level comm.Remote API
  • 24.
    API methodsMap theremote API to what your module needs.Parse decoded data and return only what is needed.Format data for transfer to remote API.Return meaningful error messages based upon context.
  • 25.
    Building blocksConsuming code(modulehooks)Admin configurationAPI methodsSecurity info in variablesLow level comm.Remote API
  • 26.
    Consuming CodeAKA “businesslogic”Use hooks to trigger calls to the remote.Format results for display or storage.CachingPermissions
  • 27.
    When to actOndemandhook_nnnnn_presave()hook_nnnnn_load()hook_nnnnn_view()Ajax (menu) callbackRule actionScheduledhook_cron()
  • 28.
    Things go wrongdrupal_http_requestis synchronousUse reasonable timeout valuesCache if you canRemote API
  • 29.
    Play niceDon’t poundtheir serversCache if you canFollow the rulesFind the communities and join in
  • 30.
    Useful toolsDebuggerXdebughttp headers“sniffer”Live http headers extension (FF)HTTP Headers (chrome)A reverse proxy / http monitorCharles (http://www.charlesproxy.com/)HttpFox (FF)
  • 31.
    Thanks!http://www.number10webcompany.comJoshua KopelPartner, Number10 Web CompanyCTO/Co-founder, Squigaloo LLCTwitter:jkopeld.o:jkopel

Editor's Notes

  • #3 They will make you smarterThey will get your work done for youThey will bring you beer