Consuming APIs: reporting from the trenches (PHP-WVL Meetup)

Consuming APIs: reporting from the trenches (PHP-WVL Meetup)

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).

4ce755c7f3ddf4e0c92e1aeaeea7677b?s=128

Jachim Coudenys

June 12, 2018
Tweet

Transcript

  1. CONSUMING APIS CONSUMING APIS CONSUMING APIS CONSUMING APIS CONSUMING APIS

    CONSUMING APIS CONSUMING APIS CONSUMING APIS CONSUMING APIS CONSUMING APIS CONSUMING APIS CONSUMING APIS CONSUMING APIS CONSUMING APIS CONSUMING APIS CONSUMING APIS REPORTING FROM THE TRENCHES Jachim Coudenys Jachim Coudenys Jachim Coudenys Jachim Coudenys Jachim Coudenys Jachim Coudenys Jachim Coudenys Jachim Coudenys jachim.coudenys@combell.group jachim.coudenys@combell.group jachim.coudenys@combell.group jachim.coudenys@combell.group jachim.coudenys@combell.group jachim.coudenys@combell.group jachim.coudenys@combell.group jachim.coudenys@combell.group @coudenysj @coudenysj @coudenysj @coudenysj @coudenysj @coudenysj @coudenysj @coudenysj
  2. Jachim Coudenys

  3. None
  4. None
  5. CONSUMING APIS CONSUMING APIS CONSUMING APIS CONSUMING APIS CONSUMING APIS

    CONSUMING APIS CONSUMING APIS CONSUMING APIS CONSUMING APIS CONSUMING APIS CONSUMING APIS CONSUMING APIS CONSUMING APIS CONSUMING APIS CONSUMING APIS REPORTING FROM THE TRENCHES
  6. file_get_contents()

  7. php 01/*.php

  8. guzzlehttp/guzzle

  9. php 02/*.php php 03/*.php

  10. MOAR LOGIC

  11. php 04/*.php php 05/*.php

  12. MIDDLEWARE

  13. HTTP message interfaces PSR-7

  14. None
  15. 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); }
  16. CLIENT public static function mapRequest(callable $fn) { return function (callable

    $handler) use ($fn) { return function ($request, array $options) use ($handler, $fn) { return $handler($fn($request), $options); }; }; }
  17. STACKPHP

  18. HTTP Server Request Handlers PSR-15

  19. GUZZLE HANDLERS AND MIDDLEWARE

  20. php 06/*.php

  21. GuzzleHttp\Middleware

  22. https://www.google.be/search? q=guzzle%20middleware

  23. LOCAL CACHING LAYER and ModerateCacheStrategy rfc7234 Cache Docs kevinrob/guzzle-cache-middleware PrivateCacheStrategy

    PublicCacheStrategy GreedyCacheStrategy
  24. \GuzzleHttp\Middleware ::mapResponse Add mapRespons middleware (so caching works as expected)

    200 -> 404 403 500 -> 404 etc...
  25. 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 (also configurable) 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
  26. \GuzzleHttp\Middleware ::mapResponse Add mapRespons middleware (so retry works as expected)

    500 -> 429 500 -> 503 etc...
  27. WEBHOOKS / CALLBACKS Don't poll Register callback url (https://app.local/callback/34dl2F4v) https://swagger.io/docs/specification/callbacks/

  28. @coudenysj jachim.coudenys@combell.group THANK YOU https://joind.in/talk/45af2