GuzzleHttpMiddleware

Functions used to create and wrap handlers with handler middleware.

Defined (1)

The class is defined in the following location(s).

/lib/Azure/GuzzleHttp/Middleware.php  
  1. final class Middleware 
  2. /** 
  3. * Middleware that adds cookies to requests. 
  4. * The options array must be set to a CookieJarInterface in order to use 
  5. * cookies. This is typically handled for you by a client. 
  6. * @return callable Returns a function that accepts the next handler. 
  7. */ 
  8. public static function cookies() 
  9. return function (callable $handler) { 
  10. return function ($request, array $options) use ($handler) { 
  11. if (empty($options['cookies'])) { 
  12. return $handler($request, $options); 
  13. } elseif (!($options['cookies'] instanceof CookieJarInterface)) { 
  14. throw new \InvalidArgumentException('cookies must be an instance of GuzzleHttp\Cookie\CookieJarInterface'); 
  15. $cookieJar = $options['cookies']; 
  16. $request = $cookieJar->withCookieHeader($request); 
  17. return $handler($request, $options) 
  18. ->then(function ($response) use ($cookieJar, $request) { 
  19. $cookieJar->extractCookies($request, $response); 
  20. return $response; 
  21. ); 
  22. }; 
  23. }; 
  24.  
  25. /** 
  26. * Middleware that throws exceptions for 4xx or 5xx responses when the 
  27. * "http_error" request option is set to true. 
  28. * @return callable Returns a function that accepts the next handler. 
  29. */ 
  30. public static function httpErrors() 
  31. return function (callable $handler) { 
  32. return function ($request, array $options) use ($handler) { 
  33. if (empty($options['http_errors'])) { 
  34. return $handler($request, $options); 
  35. return $handler($request, $options)->then( 
  36. function (ResponseInterface $response) use ($request, $handler) { 
  37. $code = $response->getStatusCode(); 
  38. if ($code < 400) { 
  39. return $response; 
  40. throw RequestException::create($request, $response); 
  41. ); 
  42. }; 
  43. }; 
  44.  
  45. /** 
  46. * Middleware that pushes history data to an ArrayAccess container. 
  47. * @param array $container Container to hold the history (by reference). 
  48. * @return callable Returns a function that accepts the next handler. 
  49. * @throws \InvalidArgumentException if container is not an array or ArrayAccess. 
  50. */ 
  51. public static function history(&$container) 
  52. if (!is_array($container) && !$container instanceof \ArrayAccess) { 
  53. throw new \InvalidArgumentException('history container must be an array or object implementing ArrayAccess'); 
  54.  
  55. return function (callable $handler) use (&$container) { 
  56. return function ($request, array $options) use ($handler, &$container) { 
  57. return $handler($request, $options)->then( 
  58. function ($value) use ($request, &$container, $options) { 
  59. $container[] = [ 
  60. 'request' => $request,  
  61. 'response' => $value,  
  62. 'error' => null,  
  63. 'options' => $options 
  64. ]; 
  65. return $value; 
  66. },  
  67. function ($reason) use ($request, &$container, $options) { 
  68. $container[] = [ 
  69. 'request' => $request,  
  70. 'response' => null,  
  71. 'error' => $reason,  
  72. 'options' => $options 
  73. ]; 
  74. return new RejectedPromise($reason); 
  75. ); 
  76. }; 
  77. }; 
  78.  
  79. /** 
  80. * Middleware that invokes a callback before and after sending a request. 
  81. * The provided listener cannot modify or alter the response. It simply 
  82. * "taps" into the chain to be notified before returning the promise. The 
  83. * before listener accepts a request and options array, and the after 
  84. * listener accepts a request, options array, and response promise. 
  85. * @param callable $before Function to invoke before forwarding the request. 
  86. * @param callable $after Function invoked after forwarding. 
  87. * @return callable Returns a function that accepts the next handler. 
  88. */ 
  89. public static function tap(callable $before = null, callable $after = null) 
  90. return function (callable $handler) use ($before, $after) { 
  91. return function ($request, array $options) use ($handler, $before, $after) { 
  92. if ($before) { 
  93. $before($request, $options); 
  94. $response = $handler($request, $options); 
  95. if ($after) { 
  96. $after($request, $options, $response); 
  97. return $response; 
  98. }; 
  99. }; 
  100.  
  101. /** 
  102. * Middleware that handles request redirects. 
  103. * @return callable Returns a function that accepts the next handler. 
  104. */ 
  105. public static function redirect() 
  106. return function (callable $handler) { 
  107. return new RedirectMiddleware($handler); 
  108. }; 
  109.  
  110. /** 
  111. * Middleware that retries requests based on the boolean result of 
  112. * invoking the provided "decider" function. 
  113. * If no delay function is provided, a simple implementation of exponential 
  114. * backoff will be utilized. 
  115. * @param callable $decider Function that accepts the number of retries,  
  116. * a request, [response], and [exception] and 
  117. * returns true if the request is to be retried. 
  118. * @param callable $delay Function that accepts the number of retries and 
  119. * returns the number of milliseconds to delay. 
  120. * @return callable Returns a function that accepts the next handler. 
  121. */ 
  122. public static function retry(callable $decider, callable $delay = null) 
  123. return function (callable $handler) use ($decider, $delay) { 
  124. return new RetryMiddleware($decider, $handler, $delay); 
  125. }; 
  126.  
  127. /** 
  128. * Middleware that logs requests, responses, and errors using a message 
  129. * formatter. 
  130. * @param LoggerInterface $logger Logs messages. 
  131. * @param MessageFormatter $formatter Formatter used to create message strings. 
  132. * @param string $logLevel Level at which to log requests. 
  133. * @return callable Returns a function that accepts the next handler. 
  134. */ 
  135. public static function log(LoggerInterface $logger, MessageFormatter $formatter, $logLevel = LogLevel::INFO) 
  136. return function (callable $handler) use ($logger, $formatter, $logLevel) { 
  137. return function ($request, array $options) use ($handler, $logger, $formatter, $logLevel) { 
  138. return $handler($request, $options)->then( 
  139. function ($response) use ($logger, $request, $formatter, $logLevel) { 
  140. $message = $formatter->format($request, $response); 
  141. $logger->log($logLevel, $message); 
  142. return $response; 
  143. },  
  144. function ($reason) use ($logger, $request, $formatter) { 
  145. $response = $reason instanceof RequestException 
  146. ? $reason->getResponse() 
  147. : null; 
  148. $message = $formatter->format($request, $response, $reason); 
  149. $logger->notice($message); 
  150. return \GuzzleHttp\Promise\rejection_for($reason); 
  151. ); 
  152. }; 
  153. }; 
  154.  
  155. /** 
  156. * This middleware adds a default content-type if possible, a default 
  157. * content-length or transfer-encoding header, and the expect header. 
  158. * @return callable 
  159. */ 
  160. public static function prepareBody() 
  161. return function (callable $handler) { 
  162. return new PrepareBodyMiddleware($handler); 
  163. }; 
  164.  
  165. /** 
  166. * Middleware that applies a map function to the request before passing to 
  167. * the next handler. 
  168. * @param callable $fn Function that accepts a RequestInterface and returns 
  169. * a RequestInterface. 
  170. * @return callable 
  171. */ 
  172. public static function mapRequest(callable $fn) 
  173. return function (callable $handler) use ($fn) { 
  174. return function ($request, array $options) use ($handler, $fn) { 
  175. return $handler($fn($request), $options); 
  176. }; 
  177. }; 
  178.  
  179. /** 
  180. * Middleware that applies a map function to the resolved promise's 
  181. * response. 
  182. * @param callable $fn Function that accepts a ResponseInterface and 
  183. * returns a ResponseInterface. 
  184. * @return callable 
  185. */ 
  186. public static function mapResponse(callable $fn) 
  187. return function (callable $handler) use ($fn) { 
  188. return function ($request, array $options) use ($handler, $fn) { 
  189. return $handler($request, $options)->then($fn); 
  190. }; 
  191. };