1
|
|
|
<?php declare (strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace OpenCloud\Common\Transport; |
4
|
|
|
|
5
|
|
|
use function GuzzleHttp\Psr7\modify_request; |
6
|
|
|
use GuzzleHttp\MessageFormatter; |
7
|
|
|
use GuzzleHttp\Middleware as GuzzleMiddleware; |
8
|
|
|
use OpenCloud\Common\Auth\AuthHandler; |
9
|
|
|
use OpenCloud\Common\Auth\Token; |
10
|
|
|
use OpenCloud\Common\Error\Builder; |
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
12
|
|
|
use Psr\Log\LoggerInterface; |
13
|
|
|
use Psr\Log\LogLevel; |
14
|
|
|
|
15
|
|
|
final class Middleware |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
9 |
|
* @return callable |
19
|
|
|
*/ |
20
|
|
|
public static function httpErrors(): callable |
21
|
|
|
{ |
22
|
2 |
|
return function (callable $handler) { |
23
|
|
|
return function ($request, array $options) use ($handler) { |
24
|
2 |
|
return $handler($request, $options)->then( |
25
|
1 |
|
function (ResponseInterface $response) use ($request, $handler) { |
26
|
|
|
if ($response->getStatusCode() < 400) { |
27
|
1 |
|
return $response; |
28
|
|
|
} |
29
|
2 |
|
throw (new Builder())->httpError($request, $response); |
30
|
2 |
|
} |
31
|
9 |
|
); |
32
|
|
|
}; |
33
|
|
|
}; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param callable $tokenGenerator |
38
|
|
|
* @param Token $token |
39
|
|
|
* |
40
|
|
|
* @return callable |
41
|
|
|
*/ |
42
|
3 |
|
public static function authHandler(callable $tokenGenerator, Token $token = null): callable |
43
|
1 |
|
{ |
44
|
3 |
|
return function (callable $handler) use ($tokenGenerator, $token) { |
45
|
|
|
return new AuthHandler($handler, $tokenGenerator, $token); |
46
|
|
|
}; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @codeCoverageIgnore |
51
|
|
|
*/ |
52
|
|
|
public static function history(array &$container): callable |
53
|
|
|
{ |
54
|
|
|
return GuzzleMiddleware::history($container); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @codeCoverageIgnore |
59
|
|
|
*/ |
60
|
|
|
public static function retry(callable $decider, callable $delay = null): callable |
61
|
|
|
{ |
62
|
|
|
return GuzzleMiddleware::retry($decider, $delay); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @codeCoverageIgnore |
67
|
|
|
*/ |
68
|
|
|
public static function log(LoggerInterface $logger, MessageFormatter $formatter, $logLevel = LogLevel::INFO): callable |
69
|
|
|
{ |
70
|
|
|
return GuzzleMiddleware::log($logger, $formatter, $logLevel); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @codeCoverageIgnore |
75
|
|
|
*/ |
76
|
|
|
public static function prepareBody(): callable |
77
|
|
|
{ |
78
|
|
|
return GuzzleMiddleware::prepareBody(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @codeCoverageIgnore |
83
|
|
|
*/ |
84
|
|
|
public static function mapRequest(callable $fn): callable |
85
|
|
|
{ |
86
|
|
|
return GuzzleMiddleware::mapRequest($fn); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @codeCoverageIgnore |
91
|
|
|
*/ |
92
|
|
|
public static function mapResponse(callable $fn): callable |
93
|
|
|
{ |
94
|
|
|
return GuzzleMiddleware::mapResponse($fn); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|