Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Consuming APIs: reporting from the trenches (PHPBenelux Conference 2019)

Consuming APIs: reporting from the trenches (PHPBenelux Conference 2019)

Integrating multiple API endpoints into a single application can be challenging. In this talk I will go over a lot of problems and how the can be solved. Going from easy authentication to locally caching calls via middlewares and using webhooks (or callbacks) for notification, this talk covers it all (or at least tries to).

Jachim Coudenys

January 26, 2019
Tweet

More Decks by Jachim Coudenys

Other Decks in Technology

Transcript

  1. Consuming APIs Consuming APIs Consuming APIs Consuming APIs Consuming APIs

    Consuming APIs Consuming APIs Consuming APIs reporting from the trenches reporting from the trenches reporting from the trenches reporting from the trenches reporting from the trenches reporting from the trenches reporting from the trenches reporting from the trenches Jachim Coudenys Jachim Coudenys Jachim Coudenys Jachim Coudenys Jachim Coudenys Jachim Coudenys Jachim Coudenys Jachim Coudenys [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] @coudenysj @coudenysj @coudenysj @coudenysj @coudenysj @coudenysj @coudenysj @coudenysj
  2. {#3 +"id": 12568822 +"name": "PHP-WVL" +"status": "active" +"link": "https://www.meetup.com/php-wvl/" +"urlname":

    "php-wvl" +"description": "<p>A PHP Usergroup for all PHP devs based in West-Vlaanderen.</p>" +"created": 1390997367000 +"city": "Torhout" +"untranslated_city": "Torhout" +"country": "BE" +"localized_country_name": "Belgium" +"localized_location": "Torhout, Belgium" +"state": "" +"join_mode": "open" +"visibility": "public" +"lat": 51.07 +"lon": 3.1 +"members": 427 +"organizer": {#2 +"id": 84915262
  3. failed to open stream: HTTP request failed! HTTP/1.1 404 Not

    Found PHP Warning: file_get_contents(https://api.meetup.com/mqskjdfqsdjjfaoijqmodiffqsjdf): failed to open stream in 01/2_file_get_contents_exceptions.php on line 9 PHP Stack trace: PHP 1. {main}() 01/2_file_get_contents_exceptions.php:0 PHP 2. file_get_contents() 01/2_file_get_contents_exceptions.php:9 PHP Fatal error: Uncaught TypeError: json_decode() expects parameter 1 to be string, boolean given in 01/ Stack trace: #0 01/2_file_get_contents_exceptions.php(9): json_decode(false) #1 {main} thrown in 01/2_file_get_contents_exceptions.php on line 9
  4. allow_url_fopen This option enables the URL-aware fopen wrappers that enable

    accessing URL object like files. Default wrappers are provided for the access of remote files using the ftp or http protocol, some extensions like zlib may register additional wrappers.
  5. Consuming APIs Consuming APIs Consuming APIs Consuming APIs Consuming APIs

    Consuming APIs Consuming APIs Consuming APIs reporting from the trenches reporting from the trenches reporting from the trenches reporting from the trenches reporting from the trenches reporting from the trenches reporting from the trenches reporting from the trenches
  6. Guzzle is a PHP HTTP client that makes it easy

    to send HTTP requests and trivial to integrate with web services.
  7. Simple interface for building query strings, POST requests, streaming large

    uploads, streaming large downloads, using HTTP cookies, uploading JSON data, etc... Synchronous and asynchronous requests. Uses PSR-7 interfaces for requests, responses, and streams. This allows you to utilize other PSR- 7 compatible libraries with Guzzle. No hard dependency on cURL, PHP streams, sockets, or non-blocking event loops. Middleware system allows you to augment and compose client behavior.
  8. Authentication Every API has its own way of auth Most

    of them use Authorization header getToken() in subclasses
  9. Server use Tari\ServerMiddlewareInterface; use Tari\ServerFrameInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ResponseInterface; class

    Foo implements ServerMiddlewareInterface { public function handle( ServerRequestInterface $request, ServerFrameInterface $frame ): ResponseInterface { if ($this->isBadRequest($request)) { return $frame->factory() ->createResponse("Bad Request", 400); } return $frame->next($request);
  10. Server A/B Testing Debugging Caching CORS CSRF Protection HTTP Basic

    Auth OAuth 2.0 OpenID Rate Limiting Referrals IP Restriction
  11. Client function (callable $handler) { return function (RequestInterface $request, array

    $options) use ($handler) { return $handler($request, $options); }; }
  12. Automatically retries HTTP requests when a server responds with a

    429 or 503 status Sets a retry delay based on the Retry-After HTTP header, if it is sent, or automatically backs off exponentially if no Retry-After header is sent Optionally retries requests that time out (based on the connect_timeout or timeout options) Set an optional callback when a retry occurs (useful for logging/reporting) Specify a maximum number of retry attempts before giving up (default: 10) caseyamcl/ guzzle_retry_middleware