2017 © Pedro Peláez
 

library http-transport

Library to fulfill http resquest

image

innmind/http-transport

Library to fulfill http resquest

  • Saturday, April 14, 2018
  • by Baptouuuu
  • Repository
  • 1 Watchers
  • 0 Stars
  • 898 Installations
  • PHP
  • 6 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 10 Versions
  • 20 % Grown

The README.md

HttpTransport

Build Status codecov Type Coverage, (*1)

This library allows you to send http request., (*2)

Important: to use this library correctly you must use vimeo/psalm., (*3)

Installation

composer require innmind/http-transport

Usage

Send a request:, (*4)

use Innmind\HttpTransport\Curl;
use Innmind\TimeContinuum\Earth\Clock;
use Innmind\Http\Request;

$fulfill = Curl::of(new Clock);

$either = $fulfill(
    Request::of(/* initialize your request */),
);

2xx responses will be on the right side of $either, all errors and other kinds of responses will be on the left side., (*5)

Important: you must call match to the returned Either otherwise the request will not be sent, but you can still call other methods on the Either before calling match., (*6)

Concurrency

By default there is no limit of concurrency for the Curl transport. But if you call many requests before unwrapping the results you may want to configure the max concurrency like below., (*7)

use Innmind\HttpTransport\Curl;
use Innmind\Http\{
    Request,
    Response,
    Method,
    ProtocolVersion,
};
use Innmind\Url\Url;
use Innmind\Immutable\Sequence;

$fulfill = Curl::of(new Clock)->maxConcurrency(5);
$responses = Sequence::of(
    'https://github.com/user/repo-a',
    'https://github.com/user/repo-b',
    'https://github.com/user/repo-c',
    // etc...
)
    ->map(static fn($url) => Request::of(
        Url::of($url),
        Method::get,
        ProtocolVersion::v20,
    ))
    ->map($fulfill)
    ->flatMap(static fn($either) => $either->match(
        static fn($success) => Sequence::of($success->response()),
        static fn() => Sequence::of(), // discard errors
    ))
    ->toList();
$responses; // list<Response>

Let's say you have 100 urls to fetch, there will never be more than 5 requests being done in parallel., (*8)

Log the request

You can easily log all your requests like so:, (*9)

use Innmind\HttpTransport\Logger
use Psr\Log\LoggerInterface;

$fulfill = Logger::psr(/* an instance of Transport */, /* an instance of LoggerInterface */)

$fulfill(/* your request */);

Here a message is logged before the request is sent and another one once it's sent., (*10)

Exponential Backoff

Sometimes when calling an external API it may not be available due to heavy load, in such case you could retry the http call after a certain amount of time leaving time for the API to recover. You can apply this pattern like so:, (*11)

use Innmind\HttpTransport\ExponentialBackoff;
use Innmind\TimeWarp\Halt\Usleep;

$fulfill = ExponentialBackoff::of(
    /* an instance of Transport */,
    new Usleep,
);

$fulfill(/* your request */);

By default it will retry 5 times the request if the server is unavailable, following the given periods (in milliseconds) between each call: 100, 271, 738, 2008 and 5459., (*12)

Circuit breaker

When a call to a certain domain fails you may want to all further calls to that domain to fail immediately as you know it means the host is down. Such pattern is called a circuit breaker., (*13)

use Innmind\HttpTransport\CircuitBreaker;
use Innmind\TimeContinuum\Earth\{
    Clock,
    Period\Minute,
};

$fulfill = CircuitBreaker::of(
    /* an instance of CircuitBreaker */,
    new Clock,
    new Minute(10),
);

$fulfill(/* your request */);

This code will open the circuit for a given domain for 10 minutes in case a call results in a server error, after this delay the transport will let new request through as if nothing happened., (*14)

Follow redirections

By default the transports do not follow redirections to give you full control on what to do. But you can wrap your transport with FollowRedirections like this:, (*15)

use Innmind\HttpTransport\FollowRedirections;

$fulfill = FollowRedirections::of(/* an instance of Transport */);

$fulfill(/* your request */);

To avoid infinite loops it will follow up to 5 consecutive redirections., (*16)

Important: as defined in the rfc, requests with methods other than GET and HEAD that results in redirection with the codes 301, 302, 307 and 308 will NOT be redirected. It will be up to you to implement the redirection as you need to make sure such redirection is safe., (*17)

The Versions

14/04 2018
14/04 2018
23/09 2017

3.0.0

3.0.0.0 http://github.com/Innmind/HttpTransport

Library to fulfill http resquest

  Sources   Download

MIT

The Requires

 

The Development Requires

http client

11/07 2017

2.2.0

2.2.0.0 http://github.com/Innmind/HttpTransport

Library to fulfill http resquest

  Sources   Download

MIT

The Requires

 

The Development Requires

http client

10/07 2017

2.1.0

2.1.0.0 http://github.com/Innmind/HttpTransport

Library to fulfill http resquest

  Sources   Download

MIT

The Requires

 

The Development Requires

http client

14/02 2017

2.0.0

2.0.0.0 http://github.com/Innmind/HttpTransport

Library to fulfill http resquest

  Sources   Download

MIT

The Requires

 

The Development Requires

http client

15/11 2016