SlideShare una empresa de Scribd logo
1 de 31
An introduction to consuming remote APIs Pacific Northwest Drupal Summit 10/16/2011 Joshua Kopel Partner, Number 10 Web Company CTO/Co-founder, Squigaloo LLC Twitter:jkopel d.o:jkopel
An introduction to consuming remote APIs General overview and terms A simple example More complex considerations Do’s and Don’ts Tools and resources
Today 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.
RESTful? REpresentational State Transfer Client / Server Stateless HTTP methods There is also SOAP which is useful for transactional or sensitive information.
Remote APIs as: Information sources Wunderground Twitter Media sources Flickr YouTube Services Google Charts SalesForce Storage Amazon S3 Google Cloud Storage
The Drupal way Pro Write minimal code! Feeds, Rules, Entities Easily modified Fully integrated with the rest of Drupal Con Difficult to manage complex interactions Implementation scattered across modules Performance challenges
The coder way Pro Write fast code Build a useful API for other modules Manage complex interactions in code Con Must expose data in order to integrate Overhead, maintenance, security, updates
Be flexible Look for easy wins Use modules where possible  Investigate the tradeoffs
CommonCraft
CommonCraft Recurly for recurring subscription payments Wistia as video CDN
Basic flow Format 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 API Process request, format a response, and define a status. Check authentication and accept request.
Simple API example GET https://api.wistia.com/v1/projects.json Returns a list of all projects
Fomatted URL GET https://user:password@api.wistia.com/v1/ projects.json?sort_by=created&sort_direction=0 https://user:password@api.wistia.com/v1 secured URL projects resource .json language ?sort_by=created&sort_direction=0  Url encoded list of parameters Returns 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? Store Entity Table Display As part of node or page Dynamically with js
Building blocks Consuming code (module hooks) Admin configuration API methods Security info in variables Low level comm. Remote API
Building blocks Consuming code (module hooks) Admin configuration API methods Security info in variables Low level comm. Remote API
Security $url = url('https://api:H7gd7n2yV9opM@ api.wistia.com/… SSL http basic authorization Domain keys/subdomains OAuth
Building blocks Consuming code (module hooks) Admin configuration API methods Security info in variables Low level comm. Remote API
Low level communication Standard functions to: Format URL(s) Format headers & http method Format query strings Request data Read http status Perform error checking on inbound data
Sending data Usually uses the POST method Build an array of encoded parameters Serialize 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 data Files 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 blocks Consuming code (module hooks) Admin configuration API methods Security info in variables Low level comm. Remote API
API methods Map 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 blocks Consuming code (module hooks) Admin configuration API methods Security info in variables Low level comm. Remote API
Consuming Code AKA “business logic” Use hooks to trigger calls to the remote. Format results for display or storage. Caching Permissions
When to act On demand hook_nnnnn_presave() hook_nnnnn_load() hook_nnnnn_view() Ajax (menu) callback Rule action Scheduled hook_cron()
Things go wrong drupal_http_request is synchronous Use reasonable timeout values Cache if you can Remote API
Play nice Don’t pound their servers Cache if you can Follow the rules Find the communities and join in
Useful tools Debugger Xdebug http headers “sniffer” Live http headers extension (FF) HTTP Headers (chrome) A reverse proxy / http monitor Charles (http://www.charlesproxy.com/) HttpFox (FF)
Thanks! http://www.number10webcompany.com Joshua Kopel Partner, Number 10 Web Company CTO/Co-founder, Squigaloo LLC Twitter:jkopel d.o:jkopel

Más contenido relacionado

La actualidad más candente

ASP.NET Web API
ASP.NET Web APIASP.NET Web API
ASP.NET Web APIhabib_786
 
Very Brief Intro to Catalyst
Very Brief Intro to CatalystVery Brief Intro to Catalyst
Very Brief Intro to CatalystZachary Blair
 
Introduction to REST and the Restlet Framework
Introduction to REST and the Restlet FrameworkIntroduction to REST and the Restlet Framework
Introduction to REST and the Restlet FrameworkPhilip Johnson
 
From Open Source to Open API with Restlet
From Open Source to Open API with RestletFrom Open Source to Open API with Restlet
From Open Source to Open API with RestletRestlet
 
Calling database with groovy in mule
Calling database with groovy in muleCalling database with groovy in mule
Calling database with groovy in muleAnirban Sen Chowdhary
 
ASP.NET Mvc 4 web api
ASP.NET Mvc 4 web apiASP.NET Mvc 4 web api
ASP.NET Mvc 4 web apiTiago Knoch
 
SPFx Webinar Loading SharePoint data in a SPFx Webpart
SPFx Webinar Loading SharePoint data in a SPFx WebpartSPFx Webinar Loading SharePoint data in a SPFx Webpart
SPFx Webinar Loading SharePoint data in a SPFx WebpartJenkins NS
 
RESTful Web Services and Drupal
RESTful Web Services and DrupalRESTful Web Services and Drupal
RESTful Web Services and DrupalGreg Hines
 
RESTFul Web Services - Intro
RESTFul Web Services - IntroRESTFul Web Services - Intro
RESTFul Web Services - IntroManuel Correa
 
CakeFest 2013 - A-Z REST APIs
CakeFest 2013 - A-Z REST APIsCakeFest 2013 - A-Z REST APIs
CakeFest 2013 - A-Z REST APIsanthony_putignano
 
Running ms sql stored procedures in mule
Running ms sql stored procedures in muleRunning ms sql stored procedures in mule
Running ms sql stored procedures in muleAnilKumar Etagowni
 
The Full Power of ASP.NET Web API
The Full Power of ASP.NET Web APIThe Full Power of ASP.NET Web API
The Full Power of ASP.NET Web APIEyal Vardi
 
Building RESTfull Data Services with WebAPI
Building RESTfull Data Services with WebAPIBuilding RESTfull Data Services with WebAPI
Building RESTfull Data Services with WebAPIGert Drapers
 

La actualidad más candente (20)

RESTEasy
RESTEasyRESTEasy
RESTEasy
 
ASP.NET Web API
ASP.NET Web APIASP.NET Web API
ASP.NET Web API
 
Very Brief Intro to Catalyst
Very Brief Intro to CatalystVery Brief Intro to Catalyst
Very Brief Intro to Catalyst
 
Introduction to REST and the Restlet Framework
Introduction to REST and the Restlet FrameworkIntroduction to REST and the Restlet Framework
Introduction to REST and the Restlet Framework
 
ASP.NET WEB API
ASP.NET WEB APIASP.NET WEB API
ASP.NET WEB API
 
Rest web services
Rest web servicesRest web services
Rest web services
 
From Open Source to Open API with Restlet
From Open Source to Open API with RestletFrom Open Source to Open API with Restlet
From Open Source to Open API with Restlet
 
Calling database with groovy in mule
Calling database with groovy in muleCalling database with groovy in mule
Calling database with groovy in mule
 
ASP.NET Mvc 4 web api
ASP.NET Mvc 4 web apiASP.NET Mvc 4 web api
ASP.NET Mvc 4 web api
 
SPFx Webinar Loading SharePoint data in a SPFx Webpart
SPFx Webinar Loading SharePoint data in a SPFx WebpartSPFx Webinar Loading SharePoint data in a SPFx Webpart
SPFx Webinar Loading SharePoint data in a SPFx Webpart
 
RESTful Web Services and Drupal
RESTful Web Services and DrupalRESTful Web Services and Drupal
RESTful Web Services and Drupal
 
CakePHP REST Plugin
CakePHP REST PluginCakePHP REST Plugin
CakePHP REST Plugin
 
REST & RESTful Web Services
REST & RESTful Web ServicesREST & RESTful Web Services
REST & RESTful Web Services
 
RESTFul Web Services - Intro
RESTFul Web Services - IntroRESTFul Web Services - Intro
RESTFul Web Services - Intro
 
CakeFest 2013 - A-Z REST APIs
CakeFest 2013 - A-Z REST APIsCakeFest 2013 - A-Z REST APIs
CakeFest 2013 - A-Z REST APIs
 
Running ms sql stored procedures in mule
Running ms sql stored procedures in muleRunning ms sql stored procedures in mule
Running ms sql stored procedures in mule
 
Excellent rest using asp.net web api
Excellent rest using asp.net web apiExcellent rest using asp.net web api
Excellent rest using asp.net web api
 
Captcha
CaptchaCaptcha
Captcha
 
The Full Power of ASP.NET Web API
The Full Power of ASP.NET Web APIThe Full Power of ASP.NET Web API
The Full Power of ASP.NET Web API
 
Building RESTfull Data Services with WebAPI
Building RESTfull Data Services with WebAPIBuilding RESTfull Data Services with WebAPI
Building RESTfull Data Services with WebAPI
 

Similar a An introduction to consuming remote APIs with Drupal 7

JavaOne 2009 - TS-5276 - RESTful Protocol Buffers
JavaOne 2009 - TS-5276 - RESTful  Protocol BuffersJavaOne 2009 - TS-5276 - RESTful  Protocol Buffers
JavaOne 2009 - TS-5276 - RESTful Protocol BuffersMatt O'Keefe
 
Seattle StrongLoop Node.js Workshop
Seattle StrongLoop Node.js WorkshopSeattle StrongLoop Node.js Workshop
Seattle StrongLoop Node.js WorkshopJimmy Guerrero
 
Advanced Web Development
Advanced Web DevelopmentAdvanced Web Development
Advanced Web DevelopmentRobert J. Stein
 
Understanding and Using Rest APIs (SocialDevCamp Chicago 2009)
Understanding and Using Rest APIs (SocialDevCamp Chicago 2009)Understanding and Using Rest APIs (SocialDevCamp Chicago 2009)
Understanding and Using Rest APIs (SocialDevCamp Chicago 2009)Pete Morano
 
Ipedo Company Overview
Ipedo Company OverviewIpedo Company Overview
Ipedo Company OverviewTim_Matthews
 
Document Databases & RavenDB
Document Databases & RavenDBDocument Databases & RavenDB
Document Databases & RavenDBBrian Ritchie
 
Play Framework: async I/O with Java and Scala
Play Framework: async I/O with Java and ScalaPlay Framework: async I/O with Java and Scala
Play Framework: async I/O with Java and ScalaYevgeniy Brikman
 
Building social and RESTful frameworks
Building social and RESTful frameworksBuilding social and RESTful frameworks
Building social and RESTful frameworksbrendonschwartz
 
Introducing SOA and Oracle SOA Suite 11g for Database Professionals
Introducing SOA and Oracle SOA Suite 11g for Database ProfessionalsIntroducing SOA and Oracle SOA Suite 11g for Database Professionals
Introducing SOA and Oracle SOA Suite 11g for Database ProfessionalsLucas Jellema
 
Dave Orchard - Offline Web Apps with HTML5
Dave Orchard - Offline Web Apps with HTML5Dave Orchard - Offline Web Apps with HTML5
Dave Orchard - Offline Web Apps with HTML5Web Directions
 
The Django Web Application Framework
The Django Web Application FrameworkThe Django Web Application Framework
The Django Web Application FrameworkSimon Willison
 
OWASP Portland - OWASP Top 10 For JavaScript Developers
OWASP Portland - OWASP Top 10 For JavaScript DevelopersOWASP Portland - OWASP Top 10 For JavaScript Developers
OWASP Portland - OWASP Top 10 For JavaScript DevelopersLewis Ardern
 
Web API or WCF - An Architectural Comparison
Web API or WCF - An Architectural ComparisonWeb API or WCF - An Architectural Comparison
Web API or WCF - An Architectural ComparisonAdnan Masood
 
Building Large Scale PHP Web Applications with Laravel 4
Building Large Scale PHP Web Applications with Laravel 4Building Large Scale PHP Web Applications with Laravel 4
Building Large Scale PHP Web Applications with Laravel 4Darwin Biler
 
jkljklj
jkljkljjkljklj
jkljkljhoefo
 

Similar a An introduction to consuming remote APIs with Drupal 7 (20)

JavaOne 2009 - TS-5276 - RESTful Protocol Buffers
JavaOne 2009 - TS-5276 - RESTful  Protocol BuffersJavaOne 2009 - TS-5276 - RESTful  Protocol Buffers
JavaOne 2009 - TS-5276 - RESTful Protocol Buffers
 
Seattle StrongLoop Node.js Workshop
Seattle StrongLoop Node.js WorkshopSeattle StrongLoop Node.js Workshop
Seattle StrongLoop Node.js Workshop
 
Advanced Web Development
Advanced Web DevelopmentAdvanced Web Development
Advanced Web Development
 
Unerstanding and Using RESTful APIs
Unerstanding and Using RESTful APIsUnerstanding and Using RESTful APIs
Unerstanding and Using RESTful APIs
 
Understanding and Using Rest APIs (SocialDevCamp Chicago 2009)
Understanding and Using Rest APIs (SocialDevCamp Chicago 2009)Understanding and Using Rest APIs (SocialDevCamp Chicago 2009)
Understanding and Using Rest APIs (SocialDevCamp Chicago 2009)
 
Ipedo Company Overview
Ipedo Company OverviewIpedo Company Overview
Ipedo Company Overview
 
Document Databases & RavenDB
Document Databases & RavenDBDocument Databases & RavenDB
Document Databases & RavenDB
 
Codeigniter
CodeigniterCodeigniter
Codeigniter
 
Play Framework: async I/O with Java and Scala
Play Framework: async I/O with Java and ScalaPlay Framework: async I/O with Java and Scala
Play Framework: async I/O with Java and Scala
 
Getting Started with API Management
Getting Started with API ManagementGetting Started with API Management
Getting Started with API Management
 
Woa. Reloaded
Woa. ReloadedWoa. Reloaded
Woa. Reloaded
 
Building social and RESTful frameworks
Building social and RESTful frameworksBuilding social and RESTful frameworks
Building social and RESTful frameworks
 
Introducing SOA and Oracle SOA Suite 11g for Database Professionals
Introducing SOA and Oracle SOA Suite 11g for Database ProfessionalsIntroducing SOA and Oracle SOA Suite 11g for Database Professionals
Introducing SOA and Oracle SOA Suite 11g for Database Professionals
 
Dave Orchard - Offline Web Apps with HTML5
Dave Orchard - Offline Web Apps with HTML5Dave Orchard - Offline Web Apps with HTML5
Dave Orchard - Offline Web Apps with HTML5
 
The Django Web Application Framework
The Django Web Application FrameworkThe Django Web Application Framework
The Django Web Application Framework
 
Switch to Backend 2023
Switch to Backend 2023Switch to Backend 2023
Switch to Backend 2023
 
OWASP Portland - OWASP Top 10 For JavaScript Developers
OWASP Portland - OWASP Top 10 For JavaScript DevelopersOWASP Portland - OWASP Top 10 For JavaScript Developers
OWASP Portland - OWASP Top 10 For JavaScript Developers
 
Web API or WCF - An Architectural Comparison
Web API or WCF - An Architectural ComparisonWeb API or WCF - An Architectural Comparison
Web API or WCF - An Architectural Comparison
 
Building Large Scale PHP Web Applications with Laravel 4
Building Large Scale PHP Web Applications with Laravel 4Building Large Scale PHP Web Applications with Laravel 4
Building Large Scale PHP Web Applications with Laravel 4
 
jkljklj
jkljkljjkljklj
jkljklj
 

Último

Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 

Último (20)

Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 

An introduction to consuming remote APIs with Drupal 7

  • 1. An introduction to consuming remote APIs Pacific Northwest Drupal Summit 10/16/2011 Joshua Kopel Partner, Number 10 Web Company CTO/Co-founder, Squigaloo LLC Twitter:jkopel d.o:jkopel
  • 2. An introduction to consuming remote APIs General overview and terms A simple example More complex considerations Do’s and Don’ts Tools and resources
  • 3. Today 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.
  • 4. RESTful? REpresentational State Transfer Client / Server Stateless HTTP methods There is also SOAP which is useful for transactional or sensitive information.
  • 5. Remote APIs as: Information sources Wunderground Twitter Media sources Flickr YouTube Services Google Charts SalesForce Storage Amazon S3 Google Cloud Storage
  • 6. The Drupal way Pro Write minimal code! Feeds, Rules, Entities Easily modified Fully integrated with the rest of Drupal Con Difficult to manage complex interactions Implementation scattered across modules Performance challenges
  • 7. The coder way Pro Write fast code Build a useful API for other modules Manage complex interactions in code Con Must expose data in order to integrate Overhead, maintenance, security, updates
  • 8. Be flexible Look for easy wins Use modules where possible Investigate the tradeoffs
  • 10. CommonCraft Recurly for recurring subscription payments Wistia as video CDN
  • 11. Basic flow Format 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 API Process request, format a response, and define a status. Check authentication and accept request.
  • 12. Simple API example GET https://api.wistia.com/v1/projects.json Returns a list of all projects
  • 13. Fomatted URL GET https://user:password@api.wistia.com/v1/ projects.json?sort_by=created&sort_direction=0 https://user:password@api.wistia.com/v1 secured URL projects resource .json language ?sort_by=created&sort_direction=0 Url encoded list of parameters Returns 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? Store Entity Table Display As part of node or page Dynamically with js
  • 16. Building blocks Consuming code (module hooks) Admin configuration API methods Security info in variables Low level comm. Remote API
  • 17. Building blocks Consuming code (module hooks) Admin configuration API methods Security info in variables Low level comm. Remote API
  • 18. Security $url = url('https://api:H7gd7n2yV9opM@ api.wistia.com/… SSL http basic authorization Domain keys/subdomains OAuth
  • 19. Building blocks Consuming code (module hooks) Admin configuration API methods Security info in variables Low level comm. Remote API
  • 20. Low level communication Standard functions to: Format URL(s) Format headers & http method Format query strings Request data Read http status Perform error checking on inbound data
  • 21. Sending data Usually uses the POST method Build an array of encoded parameters Serialize 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 of data Files 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 blocks Consuming code (module hooks) Admin configuration API methods Security info in variables Low level comm. Remote API
  • 24. API methods Map 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.
  • 25. Building blocks Consuming code (module hooks) Admin configuration API methods Security info in variables Low level comm. Remote API
  • 26. Consuming Code AKA “business logic” Use hooks to trigger calls to the remote. Format results for display or storage. Caching Permissions
  • 27. When to act On demand hook_nnnnn_presave() hook_nnnnn_load() hook_nnnnn_view() Ajax (menu) callback Rule action Scheduled hook_cron()
  • 28. Things go wrong drupal_http_request is synchronous Use reasonable timeout values Cache if you can Remote API
  • 29. Play nice Don’t pound their servers Cache if you can Follow the rules Find the communities and join in
  • 30. Useful tools Debugger Xdebug http headers “sniffer” Live http headers extension (FF) HTTP Headers (chrome) A reverse proxy / http monitor Charles (http://www.charlesproxy.com/) HttpFox (FF)
  • 31. Thanks! http://www.number10webcompany.com Joshua Kopel Partner, Number 10 Web Company CTO/Co-founder, Squigaloo LLC Twitter:jkopel d.o:jkopel

Notas del editor

  1. They will make you smarterThey will get your work done for youThey will bring you beer