$30 off During Our Annual Pro Sale. View Details »

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

    View Slide

  2. Jachim Coudenys

    View Slide

  3. View Slide

  4. View Slide

  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

    View Slide

  6. file_get_contents()

    View Slide

  7. php 01/*.php

    View Slide

  8. guzzlehttp/guzzle

    View Slide

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

    View Slide

  10. MOAR LOGIC

    View Slide

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

    View Slide

  12. MIDDLEWARE

    View Slide

  13. HTTP message interfaces
    PSR-7

    View Slide

  14. View Slide

  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);
    }

    View Slide

  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);
    };
    };
    }

    View Slide

  17. STACKPHP

    View Slide

  18. HTTP Server Request Handlers
    PSR-15

    View Slide

  19. GUZZLE HANDLERS AND MIDDLEWARE

    View Slide

  20. php 06/*.php

    View Slide

  21. GuzzleHttp\Middleware

    View Slide

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

    View Slide

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

    View Slide

  24. \GuzzleHttp\Middleware
    ::mapResponse
    Add mapRespons middleware (so caching works as
    expected)
    200 -> 404
    403
    500 -> 404
    etc...

    View Slide

  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

    View Slide

  26. \GuzzleHttp\Middleware
    ::mapResponse
    Add mapRespons middleware (so retry works as
    expected)
    500 -> 429
    500 -> 503
    etc...

    View Slide

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

    View Slide

  28. @coudenysj
    [email protected]
    THANK YOU
    https://joind.in/talk/45af2

    View Slide