ijodkor/quick-http

There is no license information available for the latest version (v1.0.1) of this package.

PHP Http methods collection to use easy

v1.0.1 2025-04-16 11:52 UTC

This package is not auto-updated.

Last update: 2025-04-16 11:54:38 UTC


README

Example

/**
 * Middleware to refresh token
 * @return Closure
 */
class EDMRequestService extends HttpBearerRequestService {

    public function __construct(readonly EDMAuthService $authService) {
        parent::__construct();

        $url = config('integration.edm_api_url');
        $this->setUrl("$url/document");
        $this->setCredentials($authService->getToken());
    }

    protected function middleware(): Closure {
        return function (callable $handler) {
            return function (RequestInterface $request, array $options) use ($handler) {
                $promise = $handler($request, $options);
                return $promise->then(function (ResponseInterface $response) use ($handler, $request, $options) {
                    /* @var Promise $promise */

                    if (in_array($response->getStatusCode(), [401, 403])) {
                        $token = $this->authService->login();
                        $request = $request->withHeader('Authorization', "Bearer $token");

                        // Retry request after refreshing token
                        // $promise->wait();
                        return $handler($request, $options);
                    }

                    return $response;
                });
            };
        };
    }
}

References