cognetif / ez-api
This package is abandoned and no longer maintained.
No replacement package was suggested.
EZ API - Starting point for API service calls
v1.3.0
2021-09-16 16:38 UTC
Requires
- php: ^8.0
- guzzlehttp/guzzle: ^7.0.1
Requires (Dev)
- phpunit/phpunit: ^9
README
This library is useful to help abstract API calls, removing the need re-implement authentication logic and pagination logic into API calls.
Installation
Install via composer with:
$ composer require cognetif/ez-api
Usage
- Create a new class that extends
Cognetf\EzApi\ApiClient
. - Implement your header or authorization logic in your class constructor after the call to
parent::__construct(Client $client)
- Use the [GET, POST, DELETE, PUT, PATCH] methods to retrieve api results and perform calls such as:
use GuzzleHttp\Client; use Cognetif\EzApi\ApiClient;
class MyClient extends ApiClient {
public function __construct(Client $client) {
parent::__construct($client);
$this->setBaseUrl('https://mydomain.com/api/')
->addHeader('Authorization', 'Bearer apkikey_hash')
->addHeader('Content-Type', 'application/json');
}
}
$myClient = new MyClient(new Client); $results = $myClient->get('/api/endpoint', ['param' => 'value']);
## Result Return
results are returned as an array with the following contract:
$results = [
'data' => [
//..Api Request Body
],
'error' => false
];
## Custom return logic
You can override how you class handles the returned data from the response by overriding the following function :
`ApiClient::handleResponse(ResponseInterface $response): array`
## Error message
If the api call returns a non-successful response code, the reason phrase will be available within the result array:
$results = [
'data' => [
//..Api Request Body
],
'error' => true,
'message' => '403 Forbidden'
];
## Running Tests
You can run tests via PhpUnit with the following command: `$ ./phpunit`.
There is a test report built to visualize the results and code coverage in `/Tests/Report`. Simply open the `index.html` or `dashboard.html` file in your local browser.