Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 32... Línea 32...
32
                } elseif (!($options['cookies'] instanceof CookieJarInterface)) {
32
                } elseif (!($options['cookies'] instanceof CookieJarInterface)) {
33
                    throw new \InvalidArgumentException('cookies must be an instance of GuzzleHttp\Cookie\CookieJarInterface');
33
                    throw new \InvalidArgumentException('cookies must be an instance of GuzzleHttp\Cookie\CookieJarInterface');
34
                }
34
                }
35
                $cookieJar = $options['cookies'];
35
                $cookieJar = $options['cookies'];
36
                $request = $cookieJar->withCookieHeader($request);
36
                $request = $cookieJar->withCookieHeader($request);
-
 
37
 
37
                return $handler($request, $options)
38
                return $handler($request, $options)
38
                    ->then(
39
                    ->then(
39
                        static function (ResponseInterface $response) use ($cookieJar, $request): ResponseInterface {
40
                        static function (ResponseInterface $response) use ($cookieJar, $request): ResponseInterface {
40
                            $cookieJar->extractCookies($request, $response);
41
                            $cookieJar->extractCookies($request, $response);
-
 
42
 
41
                            return $response;
43
                            return $response;
42
                        }
44
                        }
43
                    );
45
                    );
44
            };
46
            };
45
        };
47
        };
Línea 51... Línea 53...
51
     *
53
     *
52
     * @param BodySummarizerInterface|null $bodySummarizer The body summarizer to use in exception messages.
54
     * @param BodySummarizerInterface|null $bodySummarizer The body summarizer to use in exception messages.
53
     *
55
     *
54
     * @return callable(callable): callable Returns a function that accepts the next handler.
56
     * @return callable(callable): callable Returns a function that accepts the next handler.
55
     */
57
     */
56
    public static function httpErrors(BodySummarizerInterface $bodySummarizer = null): callable
58
    public static function httpErrors(?BodySummarizerInterface $bodySummarizer = null): callable
57
    {
59
    {
58
        return static function (callable $handler) use ($bodySummarizer): callable {
60
        return static function (callable $handler) use ($bodySummarizer): callable {
59
            return static function ($request, array $options) use ($handler, $bodySummarizer) {
61
            return static function ($request, array $options) use ($handler, $bodySummarizer) {
60
                if (empty($options['http_errors'])) {
62
                if (empty($options['http_errors'])) {
61
                    return $handler($request, $options);
63
                    return $handler($request, $options);
62
                }
64
                }
-
 
65
 
63
                return $handler($request, $options)->then(
66
                return $handler($request, $options)->then(
64
                    static function (ResponseInterface $response) use ($request, $bodySummarizer) {
67
                    static function (ResponseInterface $response) use ($request, $bodySummarizer) {
65
                        $code = $response->getStatusCode();
68
                        $code = $response->getStatusCode();
66
                        if ($code < 400) {
69
                        if ($code < 400) {
67
                            return $response;
70
                            return $response;
Línea 91... Línea 94...
91
        return static function (callable $handler) use (&$container): callable {
94
        return static function (callable $handler) use (&$container): callable {
92
            return static function (RequestInterface $request, array $options) use ($handler, &$container) {
95
            return static function (RequestInterface $request, array $options) use ($handler, &$container) {
93
                return $handler($request, $options)->then(
96
                return $handler($request, $options)->then(
94
                    static function ($value) use ($request, &$container, $options) {
97
                    static function ($value) use ($request, &$container, $options) {
95
                        $container[] = [
98
                        $container[] = [
96
                            'request'  => $request,
99
                            'request' => $request,
97
                            'response' => $value,
100
                            'response' => $value,
98
                            'error'    => null,
101
                            'error' => null,
99
                            'options'  => $options
102
                            'options' => $options,
100
                        ];
103
                        ];
-
 
104
 
101
                        return $value;
105
                        return $value;
102
                    },
106
                    },
103
                    static function ($reason) use ($request, &$container, $options) {
107
                    static function ($reason) use ($request, &$container, $options) {
104
                        $container[] = [
108
                        $container[] = [
105
                            'request'  => $request,
109
                            'request' => $request,
106
                            'response' => null,
110
                            'response' => null,
107
                            'error'    => $reason,
111
                            'error' => $reason,
108
                            'options'  => $options
112
                            'options' => $options,
109
                        ];
113
                        ];
-
 
114
 
110
                        return P\Create::rejectionFor($reason);
115
                        return P\Create::rejectionFor($reason);
111
                    }
116
                    }
112
                );
117
                );
113
            };
118
            };
114
        };
119
        };
Línea 125... Línea 130...
125
     * @param callable $before Function to invoke before forwarding the request.
130
     * @param callable $before Function to invoke before forwarding the request.
126
     * @param callable $after  Function invoked after forwarding.
131
     * @param callable $after  Function invoked after forwarding.
127
     *
132
     *
128
     * @return callable Returns a function that accepts the next handler.
133
     * @return callable Returns a function that accepts the next handler.
129
     */
134
     */
130
    public static function tap(callable $before = null, callable $after = null): callable
135
    public static function tap(?callable $before = null, ?callable $after = null): callable
131
    {
136
    {
132
        return static function (callable $handler) use ($before, $after): callable {
137
        return static function (callable $handler) use ($before, $after): callable {
133
            return static function (RequestInterface $request, array $options) use ($handler, $before, $after) {
138
            return static function (RequestInterface $request, array $options) use ($handler, $before, $after) {
134
                if ($before) {
139
                if ($before) {
135
                    $before($request, $options);
140
                    $before($request, $options);
136
                }
141
                }
137
                $response = $handler($request, $options);
142
                $response = $handler($request, $options);
138
                if ($after) {
143
                if ($after) {
139
                    $after($request, $options, $response);
144
                    $after($request, $options, $response);
140
                }
145
                }
-
 
146
 
141
                return $response;
147
                return $response;
142
            };
148
            };
143
        };
149
        };
144
    }
150
    }
Línea 168... Línea 174...
168
     * @param callable $delay   Function that accepts the number of retries and
174
     * @param callable $delay   Function that accepts the number of retries and
169
     *                          returns the number of milliseconds to delay.
175
     *                          returns the number of milliseconds to delay.
170
     *
176
     *
171
     * @return callable Returns a function that accepts the next handler.
177
     * @return callable Returns a function that accepts the next handler.
172
     */
178
     */
173
    public static function retry(callable $decider, callable $delay = null): callable
179
    public static function retry(callable $decider, ?callable $delay = null): callable
174
    {
180
    {
175
        return static function (callable $handler) use ($decider, $delay): RetryMiddleware {
181
        return static function (callable $handler) use ($decider, $delay): RetryMiddleware {
176
            return new RetryMiddleware($decider, $handler, $delay);
182
            return new RetryMiddleware($decider, $handler, $delay);
177
        };
183
        };
178
    }
184
    }
Línea 200... Línea 206...
200
            return static function (RequestInterface $request, array $options = []) use ($handler, $logger, $formatter, $logLevel) {
206
            return static function (RequestInterface $request, array $options = []) use ($handler, $logger, $formatter, $logLevel) {
201
                return $handler($request, $options)->then(
207
                return $handler($request, $options)->then(
202
                    static function ($response) use ($logger, $request, $formatter, $logLevel): ResponseInterface {
208
                    static function ($response) use ($logger, $request, $formatter, $logLevel): ResponseInterface {
203
                        $message = $formatter->format($request, $response);
209
                        $message = $formatter->format($request, $response);
204
                        $logger->log($logLevel, $message);
210
                        $logger->log($logLevel, $message);
-
 
211
 
205
                        return $response;
212
                        return $response;
206
                    },
213
                    },
207
                    static function ($reason) use ($logger, $request, $formatter): PromiseInterface {
214
                    static function ($reason) use ($logger, $request, $formatter): PromiseInterface {
208
                        $response = $reason instanceof RequestException ? $reason->getResponse() : null;
215
                        $response = $reason instanceof RequestException ? $reason->getResponse() : null;
209
                        $message = $formatter->format($request, $response, P\Create::exceptionFor($reason));
216
                        $message = $formatter->format($request, $response, P\Create::exceptionFor($reason));
210
                        $logger->error($message);
217
                        $logger->error($message);
-
 
218
 
211
                        return P\Create::rejectionFor($reason);
219
                        return P\Create::rejectionFor($reason);
212
                    }
220
                    }
213
                );
221
                );
214
            };
222
            };
215
        };
223
        };