Skip to content

Commit

Permalink
Fix typecast in argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Harald Tollefsen committed Oct 19, 2018
1 parent 58fe0f1 commit 56dc2fc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 27 deletions.
37 changes: 21 additions & 16 deletions src/ApiClient/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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
);
}
Expand All @@ -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
);
}
Expand All @@ -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
);
}
Expand All @@ -176,32 +174,28 @@ public function patch(string $path, $body, array $headers = [], bool $authentica
return $this->call(
HttpRequestOptions::HTTP_METHOD_PATCH,
$path,
$body,
$headers,
$body,
$authenticate
);
}

/**
* 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(
Expand Down Expand Up @@ -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);
}
}
}
14 changes: 3 additions & 11 deletions tests/ApiClient/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']
);
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 56dc2fc

Please sign in to comment.