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

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

Jachim Coudenys

June 12, 2018
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 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 [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. 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
  3. 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); }
  4. 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); }; }; }
  5. 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