diff --git a/.gitignore b/.gitignore index 94d6d75..85d49cb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /vendor/ -/composer.lock \ No newline at end of file +/composer.lock +.idea \ No newline at end of file diff --git a/composer.json b/composer.json index 5786df4..0db65ab 100644 --- a/composer.json +++ b/composer.json @@ -10,11 +10,12 @@ } ], "require": { - "php": ">=5.4.0", - "guzzlehttp/guzzle": ">=5.3 <7.0" + "php": "^7.2|^8.0", + "guzzlehttp/guzzle": "~7.0", + "guzzlehttp/uri-template": "0.2.0" }, "require-dev": { - "phpunit/phpunit": "4.8.*" + "phpunit/phpunit": "^9.0" }, "autoload": { "psr-4": { @@ -26,4 +27,4 @@ "HapiClient\\Tests\\": "tests/" } } -} \ No newline at end of file +} diff --git a/src/Http/HapiClient.php b/src/Http/HapiClient.php index 6f54567..e73102c 100644 --- a/src/Http/HapiClient.php +++ b/src/Http/HapiClient.php @@ -1,6 +1,7 @@ entryPointUrl = trim($entryPointUrl); $this->profile = trim($profile); $this->authenticationMethod = $authenticationMethod; - + if ($this->apiUrl) { $baseUrl = rtrim($this->apiUrl, '/') . '/'; - + if (Misc::isGuzzle6()) { $this->client = new Client(['base_uri' => $baseUrl]); } else { @@ -55,7 +54,7 @@ public function __construct( $this->client = new Client(); } } - + /** * {@inheritDoc} */ @@ -63,7 +62,7 @@ public function getApiUrl() { return $this->apiUrl; } - + /** * {@inheritDoc} */ @@ -71,7 +70,7 @@ public function getEntryPointUrl() { return $this->entryPointUrl; } - + /** * {@inheritDoc} */ @@ -79,7 +78,7 @@ public function getProfile() { return $this->profile; } - + /** * {@inheritDoc} */ @@ -87,7 +86,7 @@ public function getAuthenticationMethod() { return $this->authenticationMethod; } - + /** * {@inheritDoc} */ @@ -95,14 +94,14 @@ public function &getClient() { return $this->client; } - + /** * The magic setter is overridden to insure immutability. */ final public function __set($name, $value) { } - + /** * {@inheritDoc} */ @@ -112,34 +111,34 @@ public function sendRequest(RequestInterface $request) $options = []; if (Misc::isGuzzle6()) { $options['exceptions'] = false; - + if (($verify = Misc::verify($request->getUrl(), __DIR__ . '/../CA/')) !== null) { $options['verify'] = $verify; } } - + // Create the HTTP request $httpRequest = $this->createHttpRequest($request); - + // Send the request $httpResponse = $this->client->send($httpRequest, $options); - + // If Unauthorized, maybe the authorization just timed out. // Try it again to be sure. if ($httpResponse->getStatusCode() == 401 && $this->authenticationMethod != null) { // Create the HTTP request (and Authorize again) $httpRequest = $this->createHttpRequest($request); - + // Execute again $httpResponse = $this->client->send($httpRequest, $options); } - + // Check the status code (must be 2xx) $statusCode = $httpResponse->getStatusCode(); if ($statusCode >= 200 && $statusCode < 300) { return Resource::fromJson((string) $httpResponse->getBody()); } - + // Exception depending on status code for 3xx, 4xx and 5xx if ($statusCode >= 300 && $statusCode < 400) { throw new Exception\HttpRedirectionException($httpRequest, $httpResponse); @@ -151,7 +150,7 @@ public function sendRequest(RequestInterface $request) throw new Exception\HttpException($httpRequest, $httpResponse); } } - + /** * {@inheritDoc} */ @@ -160,11 +159,11 @@ public function sendFollow($follow, ResourceInterface $resource = null) if (!$resource) { $resource = $this->getEntryPointResource(); } - + if (!is_array($follow)) { $follow = array($follow); } - + foreach ($follow as $hop) { $resource = $this->sendRequest(new Request( $hop->getUrl($resource), @@ -174,7 +173,7 @@ public function sendFollow($follow, ResourceInterface $resource = null) $hop->getHeaders() )); } - + return $resource; } @@ -186,10 +185,10 @@ public function getEntryPointResource() if ($this->entryPointResource) { return $this->entryPointResource; } - + return $this->entryPointResource = $this->sendRequest(new Request($this->entryPointUrl)); } - + /** * {@inheritDoc} */ @@ -202,7 +201,7 @@ public function refresh($resource) return $resource; } } - + /** * Instantiates the HttpRequest depending on the * configuration from the given Request. @@ -215,19 +214,19 @@ private function createHttpRequest(RequestInterface $request) if ($this->authenticationMethod) { $request = $this->authenticationMethod->authorizeRequest($this, $request); } - + // The URL $url = ltrim(trim($request->getUrl()), '/'); - + // Handle templated URLs if ($urlVariables = $request->getUrlVariables()) { $url = (new UriTemplate())->expand($url, $urlVariables); } - + // Headers $headers = []; $headersToAdd = $request->getHeaders(); - + // The message body $body = null; if ($messageBody = $request->getMessageBody()) { @@ -235,14 +234,14 @@ private function createHttpRequest(RequestInterface $request) $headers['Content-Length'] = $messageBody->getContentLength(); $body = $messageBody->getContent(); } - + // Accept hal+json response if ($this->profile) { $headers['Accept'] = 'application/hal+json; profile="' . $this->profile . '"'; } else { $headers['Accept'] = 'application/json'; } - + // Prepare the Guzzle request if (Misc::isGuzzle6()) { // Guzzle 6 @@ -255,25 +254,25 @@ private function createHttpRequest(RequestInterface $request) } else { // Guzzle 5.3 $httpRequest = $this->client->createRequest($request->getMethod(), $url, ['exceptions' => false]); - + // verify option for HTTPS requests if needed if (($verify = Misc::verify($url, __DIR__ . '/../CA/')) !== null) { $httpRequest->getConfig()->set('verify', $verify); } - + foreach ($headers as $key => $value) { $httpRequest->setHeader($key, $value); } - + foreach ($headersToAdd as $key => $value) { $httpRequest->setHeader($key, $value); } - + if ($body) { $httpRequest->setBody(\GuzzleHttp\Stream\Stream::factory($body)); } } - + return $httpRequest; } } diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 37ad244..fec2cc4 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -1,12 +1,13 @@