Skip to content

Commit

Permalink
Merge pull request #12 from exonet/delete-validation
Browse files Browse the repository at this point in the history
Dont parse response for Delete and patch requests
  • Loading branch information
trizz authored Feb 1, 2021
2 parents 84f862d + 2a44f47 commit 9e0e818
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ All notable changes to `exonet-api-php` will be documented in this file.
Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles.

## Unreleased
[Compare v2.4.0 - Unreleased](https://github.com/exonet/exonet-api-php/compare/v2.4.0...master)
[Compare v2.4.1 - Unreleased](https://github.com/exonet/exonet-api-php/compare/v2.4.1...master)

## [v2.4.1](https://github.com/exonet/exonet-api-php/releases/tag/v2.4.1) - 2021-01-29
[Compare v2.4.0 - v2.4.1](https://github.com/exonet/exonet-api-php/compare/v2.4.0...v2.4.1)
### Fixed
- Parsing the response of a `DELETE` and `PATCH` requests will only validate the response.

## [v2.4.0](https://github.com/exonet/exonet-api-php/releases/tag/v2.4.0) - 2021-01-20
[Compare v2.3.0 - v2.4.0](https://github.com/exonet/exonet-api-php/compare/v2.3.0...v2.4.0)
Expand Down
2 changes: 1 addition & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Client implements LoggerAwareInterface
/**
* The version of this package. Used in the user-agent header.
*/
public const CLIENT_VERSION = 'v2.3.0';
public const CLIENT_VERSION = 'v2.4.1';

/**
* The API base URL.
Expand Down
20 changes: 15 additions & 5 deletions src/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function patch(string $urlPath, array $data): bool

$response = self::httpClient()->send($request);

$this->parseResponse($response);
$this->validateResponse($response);

return true;
}
Expand All @@ -155,7 +155,7 @@ public function delete(string $urlPath, array $data = []): bool

$response = self::httpClient()->send($request);

$this->parseResponse($response);
$this->validateResponse($response);

return true;
}
Expand All @@ -174,9 +174,7 @@ private function parseResponse(PsrResponse $response)
{
$this->apiClient()->log()->debug('Request completed', ['statusCode' => $response->getStatusCode()]);

if ($response->getStatusCode() >= 300) {
(new ResponseExceptionHandler($response))->handle();
}
$this->validateResponse($response);

$contents = $response->getBody()->getContents();

Expand All @@ -195,6 +193,18 @@ private function parseResponse(PsrResponse $response)
return new ApiResourceIdentifier($decodedContent->data->type, $decodedContent->data->id);
}

/**
* Validate a response. Check for validation errors.
*
* @param PsrResponse $response The call response.
*/
private function validateResponse(PsrResponse $response)
{
if ($response->getStatusCode() >= 300) {
(new ResponseExceptionHandler($response))->handle();
}
}

/**
* Get or create an HTTP client based on the configured handler stack. Implement the singleton pattern so the HTTP
* client is shared.
Expand Down
6 changes: 2 additions & 4 deletions tests/ConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public function testPatch()
public function testDelete()
{
$apiCalls = [];
$mock = new MockHandler([new Response(201, [], json_encode($this->singleResource))]);
$mock = new MockHandler([new Response(204)]);

$history = Middleware::history($apiCalls);
$handler = HandlerStack::create($mock);
Expand All @@ -197,9 +197,7 @@ public function testDelete()
new Client(new PersonalAccessToken('test-token'));
$connectorClass = new Connector($handler);

$payload = ['test' => 'demo'];

$result = $connectorClass->delete('url', $payload);
$result = $connectorClass->delete('url');

$this->assertTrue($result);
$this->assertCount(1, $apiCalls);
Expand Down

0 comments on commit 9e0e818

Please sign in to comment.