diff --git a/src/ApiClient/Client.php b/src/ApiClient/Client.php index e9aa63b..a82e23a 100644 --- a/src/ApiClient/Client.php +++ b/src/ApiClient/Client.php @@ -78,14 +78,12 @@ public function setHttpClient(HttpClient $httpClient): void /** * @throws BokbasenApiClientException */ - protected function call(string $method, string $path, $body = null, array $headers = [], bool $authenticate = true): ResponseInterface + protected function call(string $method, string $path, array $headers = [], $body = null, bool $authenticate = true): ResponseInterface { $headers = $authenticate ? $this->addAuthenticationHeaders($headers) : $headers; $url = $this->prependBaseUrl($path); - if ($this->logger) { - $this->logger->debug(sprintf('Executing HTTP %s request to %s with data %s.', $method, $url, $body)); - } + $this->logRequest($method, $url, $body); return $this->getCaller()->request($method, $url, $headers, $body); } @@ -107,8 +105,8 @@ public function post(string $path, $body, ?array $headers = [], bool $authentica return $this->call( HttpRequestOptions::HTTP_METHOD_POST, $path, - $body, $headers, + $body, $authenticate ); } @@ -130,8 +128,8 @@ public function put(string $path, $body, array $headers = [], bool $authenticate return $this->call( HttpRequestOptions::HTTP_METHOD_PUT, $path, - $body, $headers, + $body, $authenticate ); } @@ -153,8 +151,8 @@ public function get(string $path, array $headers = [], $authenticate = true): Re return $this->call( HttpRequestOptions::HTTP_METHOD_GET, $path, - null, $headers, + null, $authenticate ); } @@ -176,8 +174,8 @@ public function patch(string $path, $body, array $headers = [], bool $authentica return $this->call( HttpRequestOptions::HTTP_METHOD_PATCH, $path, - $body, $headers, + $body, $authenticate ); } @@ -185,23 +183,19 @@ public function patch(string $path, $body, array $headers = [], bool $authentica /** * Special endpoint for posting json, sets correct content type header and encodes data as json * - * @param string $path - * @param array|\stdClass $body + * @param string $path + * @param array $body * * @return ResponseInterface * * @throws BokbasenApiClientException */ - public function postJson(string $path, $body): ResponseInterface + public function postJson(string $path, array $body): ResponseInterface { - if (!is_array($body) && !$body instanceof \stdClass) { - throw new BokbasenApiClientException('Data must be array or stdClass'); - } - $body = json_encode($body); if ($body === false) { - throw new BokbasenApiClientException('Not able to convert data to json'); + throw new BokbasenApiClientException('Unable to convert data to json'); } return $this->post( @@ -230,4 +224,15 @@ protected function addAuthenticationHeaders(array $existingHeaders = []): array { return array_merge($this->login->getAuthHeadersAsArray(), $existingHeaders); } + + protected function logRequest(string $method, string $url, $body = null): void + { + if ($this->logger) { + $message = sprintf('Executing HTTP %s request to %s', $method, $url); + if (!empty($body)) { + $message .= sprintf(' with data %s', $body); + } + $this->logger->debug($message); + } + } } \ No newline at end of file diff --git a/tests/ApiClient/ClientTest.php b/tests/ApiClient/ClientTest.php index 29e14bf..44f96c0 100644 --- a/tests/ApiClient/ClientTest.php +++ b/tests/ApiClient/ClientTest.php @@ -44,11 +44,11 @@ public function testSetLogger() $logger = new Logger('first', [$handler = new TestHandler()]); $client->setLogger($logger); - $client->get('/path'); + $client->post('/path', json_encode(["data" => "data"])); list($record) = $handler->getRecords(); $this->assertEquals( - 'Executing HTTP GET request to http://client.test/path with data .', + 'Executing HTTP POST request to http://client.test/path with data {"data":"data"}', $record['message'] ); } @@ -104,18 +104,10 @@ public function testPostJson() $response = $client->postJson('/path', ['my' => 'body']); $this->assertInstanceOf(ResponseInterface::class, $response); - $response = $client->postJson('/path', (object) ['my' => 'body'], ["Content-Type" => "application/json"],false); + $response = $client->postJson('/path', ['my' => 'body'], ["Content-Type" => "application/json"],false); $this->assertInstanceOf(ResponseInterface::class, $response); } - public function testPostJsonInvalidArgument() - { - $this->expectException(BokbasenApiClientException::class); - - $client = $this->getClient(); - $client->postJson('/path', "invalid body"); - } - public function testClientException() { $this->expectException(BokbasenApiClientException::class);