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