Skip to content

Commit a561c4d

Browse files
committed
docs: added event listeners documentation
1 parent 280365f commit a561c4d

1 file changed

Lines changed: 86 additions & 6 deletions

File tree

README.md

Lines changed: 86 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ A library for creating SDKs in PHP with support for:
1010
- [PSR-6 caches](https://www.php-fig.org/psr/psr-6);
1111
- [PSR-3 logs](https://www.php-fig.org/psr/psr-3);
1212
- Authentication;
13-
- Events;
13+
- Event Listeners;
1414
- ...and more.
1515

1616
## Requirements
@@ -284,7 +284,7 @@ Available authentication methods:
284284

285285
You can also [implement your own](https://docs.php-http.org/en/latest/message/authentication.html#implement-your-own) authentication method.
286286

287-
For example, if you have an API that is authenticated through a query parameter:
287+
For example, if you have an API that is authenticated with a query parameter:
288288

289289
```php
290290
use ProgrammatorDev\Api\Api;
@@ -297,14 +297,94 @@ class YourApi extends Api
297297
parent::__construct();
298298

299299
$this->setBaseUrl('https://api.example.com/v1');
300-
$this->setAuthentication(
301-
new QueryParam(['api_key' => $applicationKey])
302-
);
300+
$this->setAuthentication(new QueryParam(['api_token' => $applicationKey]));
303301
}
304302

305303
public function getPosts(): string
306304
{
307-
// GET https://api.example.com/v1/posts?api_key=cd982h3diwh98dd23d32j
305+
// GET https://api.example.com/v1/posts?api_token=cd982h3diwh98dd23d32j
306+
return $this->request(
307+
method: 'GET',
308+
path: '/posts'
309+
);
310+
}
311+
}
312+
```
313+
314+
### Events Listeners
315+
316+
Currently, there are two event listeners available:
317+
318+
The `addPostRequestHandler` method is used to add a handler function that is executed after a request has been made.
319+
This handler function can be used to inspect the request and response data that was sent to, and received from, the API.
320+
This event listener will be applied to every API request.
321+
322+
```php
323+
$this->addPostRequestHandler(callable $handler, int $priority = 0): self;
324+
```
325+
326+
For example, you can use this event listener to handle API errors:
327+
328+
```php
329+
use ProgrammatorDev\Api\Api;
330+
use ProgrammatorDev\Api\Event\PostRequestEvent;
331+
332+
class YourApi extends Api
333+
{
334+
public function __construct()
335+
{
336+
// ...
337+
338+
$this->addPostRequestHandler(function(PostRequestEvent $event) {
339+
$response = $event->getResponse();
340+
$statusCode = $response->getStatusCode();
341+
342+
// if there was a response with an error status code
343+
if ($statusCode >= 400) {
344+
// throw an exception
345+
match ($statusCode) {
346+
400 => throw new BadRequestException(),
347+
404 => throw new NotFoundException(),
348+
default => throw new UnexpectedErrorException()
349+
};
350+
}
351+
});
352+
}
353+
354+
// ...
355+
}
356+
```
357+
358+
On the other hand, the `addResponseContentsHandler` method is used to manipulate the response that was received from the API.
359+
This event listener will be applied to every API request.
360+
361+
```php
362+
$this->addResponseContentsHandler(callable $handler, int $priority = 0): self;
363+
```
364+
365+
For example, if the API responses are JSON strings, you can use this event listener to decode them into arrays:
366+
367+
```php
368+
use ProgrammatorDev\Api\Api;
369+
use ProgrammatorDev\Api\Event\ResponseContentsEvent;
370+
371+
class YourApi extends Api
372+
{
373+
public function __construct()
374+
{
375+
// ...
376+
377+
$this->addResponseContentsHandler(function(ResponseContentsEvent $event) {
378+
// get response contents and decode json string into an array
379+
$contents = json_decode($event->getContents(), true);
380+
// set new handled contents
381+
$event->setContents($contents);
382+
});
383+
}
384+
385+
public function getPosts(): array
386+
{
387+
// will return an array
308388
return $this->request(
309389
method: 'GET',
310390
path: '/posts'

0 commit comments

Comments
 (0)