Skip to content

Commit

Permalink
Merge pull request #7 from exonet/tsi-delete-return
Browse files Browse the repository at this point in the history
Return true for delete requests
  • Loading branch information
robbinjanssen committed Sep 20, 2019
2 parents 382f43c + 734492a commit 2046bbd
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 29 deletions.
15 changes: 10 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@ 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.0.0 - Unreleased](https://github.com/exonet/exonet-api-php/compare/v2.0.0...master)
[Compare v2.1.1 - Unreleased](https://github.com/exonet/exonet-api-php/compare/v2.1.1...master)

## [v2.1.1](https://github.com/exonet/exonet-api-php/releases/tag/v2.1.1) - 2019-09-19
[Compare v2.1.0 - v2.1.1](https://github.com/exonet/exonet-api-php/compare/v2.1.0...v2.1.1)
### Changed
- `DELETE` requests now return `true` when successful. If something went wrong, an exception is still thrown.

## [v2.1.0](https://github.com/exonet/exonet-api-php/releases/tag/v2.1.0) - 2019-09-06
[Compare v2.1.0 - v2.0.0](https://github.com/exonet/backend/compare/v2.0.0...v2.1.0)
[Compare v2.1.0 - v2.0.0](https://github.com/exonet/exonet-api-php/compare/v2.0.0...v2.1.0)
### Added
- Support for patching resources and relationships.
- Exceptions thrown by the package are extended with the `status` as the exception code, the `code` as detailed code and an array containing the returned variables.

## [v2.0.0](https://github.com/exonet/exonet-api-php/releases/tag/v2.0.0) - 2019-07-02
[Compare v2.0.0 - v1.0.0](https://github.com/exonet/backend/compare/v1.0.0...v2.0.0)
[Compare v2.0.0 - v1.0.0](https://github.com/exonet/exonet-api-php/compare/v1.0.0...v2.0.0)
## Breaking
- The Client has been refactored to keep consistency between packages in different programming languages. See the updated documentation and examples.

Expand All @@ -24,15 +29,15 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
- Making DELETE request to remove a resource.

## [v1.0.0](https://github.com/exonet/exonet-api-php/releases/tag/v1.0.0) - 2019-04-29
[Compare v0.2.0 - v1.0.0](https://github.com/exonet/backend/compare/v0.2.0...v1.0.0)
[Compare v0.2.0 - v1.0.0](https://github.com/exonet/exonet-api-php/compare/v0.2.0...v1.0.0)
### Breaking
- The public property `type` in the `ApiResource` class has been renamed to `resourceType` in order not to conflict with the DNS record resource, which has a `type` attribute.

### Added
- Two examples for DNS zones and records.

## [v0.2.0](https://github.com/exonet/exonet-api-php/releases/tag/v0.2.0) - 2018-07-09
[Compare v0.1.0 - v0.2.0](https://github.com/exonet/backend/compare/v0.1.0...v0.2.0)
[Compare v0.1.0 - v0.2.0](https://github.com/exonet/exonet-api-php/compare/v0.1.0...v0.2.0)
### Added
- Ready to use examples to get ticket details.
- The ApiResourceSet now supports ArrayAccess.
Expand Down
13 changes: 9 additions & 4 deletions src/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,13 @@ public function post(string $urlPath, array $data)
}

/**
* Convert the data to JSON and patch it to a URL.
* Convert the data to JSON and patch it to a URL. Will return 'true' when successful. If not successful an exception
* is thrown.
*
* @param string $urlPath The URL to patch to.
* @param array $data An array with data to patch to the API.
*
* @return bool True when the patch succeeded.
* @return true When the patch succeeded.
*/
public function patch(string $urlPath, array $data) : bool
{
Expand All @@ -117,12 +118,14 @@ public function patch(string $urlPath, array $data) : bool
}

/**
* Make a DELETE call to the API.
* Make a DELETE call to the API. Will return 'true' when successful. If not successful an exception is thrown.
*
* @param string $urlPath The url to make the DELETE request to.
* @param array $data (Optional) The data to send along with the DELETE request.
*
* @return true When the delete was successful.
*/
public function delete(string $urlPath, array $data = []) : void
public function delete(string $urlPath, array $data = []) : bool
{
$apiUrl = $this->apiClient()->getApiUrl().$urlPath;
$this->apiClient()->log()->debug('Sending [DELETE] request', ['url' => $apiUrl]);
Expand All @@ -135,6 +138,8 @@ public function delete(string $urlPath, array $data = []) : void
);

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

return true;
}

/**
Expand Down
13 changes: 8 additions & 5 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,13 @@ public function post(array $payload, string $appendUrl = null)
}

/**
* Patch data to the API.
* Patch data to the API. Will return 'true' when successful. If not successful an exception
* is thrown.
*
* @param string $id The ID of the resource to patch.
* @param array $payload The payload to post to the API.
*
* @return bool True when succeeded.
* @return true When the patch succeeded.
*/
public function patch(string $id, array $payload) : bool
{
Expand All @@ -103,14 +104,16 @@ public function patch(string $id, array $payload) : bool
}

/**
* Delete a resource.
* Delete a resource. Will return 'true' when successful. If not successful an exception is thrown.
*
* @param string $id The ID of the resource to delete.
* @param array $data (Optional) Data to send along with the request.
*
* @return true When the delete was successful.
*/
public function delete(string $id, array $data = []) : void
public function delete(string $id, array $data = []) : bool
{
$this->connector->delete(
return $this->connector->delete(
trim($this->resource, '/').'/'.$id,
$data
);
Expand Down
5 changes: 3 additions & 2 deletions src/Structures/ApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,10 @@ public function post()
}

/**
* Patch this resource to the API. For now this must be done in multiple calls if also relations are changed.
* Patch this resource to the API. For now this must be done in multiple calls if also relations are changed. If not
* successful an exception is thrown.
*
* @return bool True when the patch has succeeded.
* @return true When the patch succeeded.
*/
public function patch() : bool
{
Expand Down
12 changes: 7 additions & 5 deletions src/Structures/ApiResourceIdentifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ public function get()
}

/**
* Delete this resource from the API.
* Delete this resource from the API. Will return 'true' when successful. If not successful an exception is thrown.
*
* @return true When the delete was successful.
*/
public function delete() : void
public function delete() : bool
{
// If there are no changed relationships, perform a 'normal' delete.
if (empty($this->changedRelationships)) {
$this->request->delete($this->id());

return;
return $this->request->delete($this->id());
}

// If there are changed relationships, transform them to JSON and send a DELETE to the relationship endpoint.
Expand All @@ -106,6 +106,8 @@ public function delete() : void

$this->request->delete($this->id().'/relationships/'.$relationship, ['data' => $relationData]);
}

return true;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion tests/ConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,9 @@ public function testDelete()

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

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

$this->assertTrue($result);
$this->assertCount(1, $apiCalls);
$request = $apiCalls[0]['request'];

Expand Down
4 changes: 2 additions & 2 deletions tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ public function testDeleteRequest()
->shouldReceive('delete')
->withArgs(['test/id999', []])
->once()
->andReturnNull();
->andReturnTrue();

$request = new Request('/test', $connectorMock);

$this->assertNull($request->delete('id999'));
$this->assertTrue($request->delete('id999'));
}
}
10 changes: 5 additions & 5 deletions tests/Structures/ApiResourceIdentifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ public function testDeleteResource()
$requestMock->shouldReceive('delete')
->once()
->withArgs(['xV42'])
->andReturnNull();
->andReturnTrue();

$resourceIdentifier = new ApiResourceIdentifier('unitTest', 'xV42', $requestMock);

$this->assertNull($resourceIdentifier->delete());
$this->assertTrue($resourceIdentifier->delete());
}

public function testDeleteRelation()
Expand All @@ -51,17 +51,17 @@ public function testDeleteRelation()
$requestMock->shouldReceive('delete')
->once()
->withArgs(['xV42/relationships/test', ['data' => ['type' => 'testRelation', 'id' => 'testId']]])
->andReturnNull();
->andReturnTrue();

$requestMock->shouldReceive('delete')
->once()
->withArgs(['xV42/relationships/test2', ['data' => [['type' => 'testRelation2', 'id' => 'testId2']]]])
->andReturnNull();
->andReturnTrue();

$resourceIdentifier = new ApiResourceIdentifier('unitTest', 'xV42', $requestMock);
$resourceIdentifier->relationship('test', new ApiResourceIdentifier('testRelation', 'testId'));
$resourceIdentifier->relationship('test2', [new ApiResourceIdentifier('testRelation2', 'testId2')]);

$this->assertNull($resourceIdentifier->delete());
$this->assertTrue($resourceIdentifier->delete());
}
}

0 comments on commit 2046bbd

Please sign in to comment.