Skip to content

Commit ef7821f

Browse files
authored
Merge pull request #5 from programmatordev/1.x
Added PreRequestListener
2 parents a661c32 + 573237d commit ef7821f

File tree

6 files changed

+212
-88
lines changed

6 files changed

+212
-88
lines changed

Diff for: README.md

+91-25
Original file line numberDiff line numberDiff line change
@@ -359,19 +359,66 @@ class YourApi extends Api
359359

360360
### Event Listeners
361361

362-
- [`addPostRequestHandler`](#addpostrequesthandler)
363-
- [`addResponseContentsHandler`](#addresponsecontentshandler)
362+
- [`addPreRequestListener`](#addprerequestlistener)
363+
- [`addPostRequestListener`](#addpostrequestlistener)
364+
- [`addResponseContentsListener`](#addresponsecontentslistener)
364365
- [Event Priority](#event-priority)
365366
- [Event Propagation](#event-propagation)
366367

367-
#### `addPostRequestHandler`
368+
#### `addPreRequestListener`
368369

369-
The `addPostRequestHandler` method is used to add a handler function that is executed after a request has been made.
370-
This handler function can be used to inspect the request and response data that was sent to, and received from, the API.
370+
The `addPreRequestListener` method is used to add a function that is called before a request, and all handled data, has been made.
371371
This event listener will be applied to every API request.
372372

373373
```php
374-
$this->addPostRequestHandler(callable $handler, int $priority = 0): self;
374+
$this->addPreRequestListener(callable $listener, int $priority = 0): self;
375+
```
376+
377+
For example:
378+
379+
```php
380+
use ProgrammatorDev\Api\Api;
381+
use ProgrammatorDev\Api\Event\PreRequestEvent;
382+
383+
class YourApi extends Api
384+
{
385+
public function __construct()
386+
{
387+
// a PreRequestEvent is passed as an argument
388+
$this->addPreRequestListener(function(PreRequestEvent $event) {
389+
$request = $event->getRequest();
390+
391+
if ($request->getMethod() === 'POST') {
392+
// do something for all POST requests
393+
// ...
394+
}
395+
});
396+
}
397+
398+
// ...
399+
}
400+
```
401+
402+
Available event methods:
403+
404+
```php
405+
$this->addPreRequestListener(function(PreRequestEvent $event) {
406+
// get request data
407+
$request = $event->getRequest();
408+
// ...
409+
// set request data
410+
$event->setRequest($request);
411+
});
412+
```
413+
414+
#### `addPostRequestListener`
415+
416+
The `addPostRequestListener` method is used to add a function that is called after a request has been made.
417+
This function can be used to inspect the request and response data that was sent to, and received from, the API.
418+
This event listener will be applied to every API request.
419+
420+
```php
421+
$this->addPostRequestListener(callable $listener, int $priority = 0): self;
375422
```
376423

377424
For example, you can use this event listener to handle API errors:
@@ -384,13 +431,8 @@ class YourApi extends Api
384431
{
385432
public function __construct()
386433
{
387-
// ...
388-
389434
// a PostRequestEvent is passed as an argument
390-
$this->addPostRequestHandler(function(PostRequestEvent $event) {
391-
// request data is also available
392-
// $request = $event->getRequest();
393-
435+
$this->addPostRequestListener(function(PostRequestEvent $event) {
394436
$response = $event->getResponse();
395437
$statusCode = $response->getStatusCode();
396438

@@ -410,13 +452,27 @@ class YourApi extends Api
410452
}
411453
```
412454

413-
#### `addResponseContentsHandler`
455+
Available event methods:
456+
457+
```php
458+
$this->addPostRequestListener(function(PostRequestEvent $event) {
459+
// get request data
460+
$request = $event->getRequest();
461+
// get response data
462+
$response = $event->getResponse();
463+
// ...
464+
// set response data
465+
$event->setResponse($response);
466+
});
467+
```
468+
469+
#### `addResponseContentsListener`
414470

415-
The `addResponseContentsHandler` method is used to manipulate the response that was received from the API.
471+
The `addResponseContentsListener` method is used to manipulate the response that was received from the API.
416472
This event listener will be applied to every API request.
417473

418474
```php
419-
$this->addResponseContentsHandler(callable $handler, int $priority = 0): self;
475+
$this->addResponseContentsListener(callable $handler, int $priority = 0): self;
420476
```
421477

422478
For example, if the API responses are JSON strings, you can use this event listener to decode them into arrays:
@@ -429,10 +485,8 @@ class YourApi extends Api
429485
{
430486
public function __construct()
431487
{
432-
// ...
433-
434488
// a ResponseContentsEvent is passed as an argument
435-
$this->addResponseContentsHandler(function(ResponseContentsEvent $event) {
489+
$this->addResponseContentsListener(function(ResponseContentsEvent $event) {
436490
// get response contents and decode json string into an array
437491
$contents = $event->getContents();
438492
$contents = json_decode($contents, true);
@@ -453,6 +507,18 @@ class YourApi extends Api
453507
}
454508
```
455509

510+
Available event methods:
511+
512+
```php
513+
$this->addResponseContentsListener(function(ResponseContentsEvent $event) {
514+
// get response body contents data
515+
$contents = $event->getContents();
516+
// ...
517+
// set contents
518+
$event->setContents($contents);
519+
});
520+
```
521+
456522
#### Event Priority
457523

458524
It is possible to add multiple listeners for the same event and set the order in which they will be executed.
@@ -471,14 +537,14 @@ class YourApi extends Api
471537
// but the second is executed first (higher priority) even though it was added after
472538

473539
// executed last (lower priority)
474-
$this->addResponseContentsHandler(
475-
handler: function(PostRequestEvent $event) { ... },
540+
$this->addResponseContentsListener(
541+
listener: function(PostRequestEvent $event) { ... },
476542
priority: 0
477543
);
478544

479545
// executed first (higher priority)
480-
$this->addResponseContentsHandler(
481-
handler: function(PostRequestEvent $event) { ... },
546+
$this->addResponseContentsListener(
547+
listener: function(PostRequestEvent $event) { ... },
482548
priority: 10
483549
);
484550
}
@@ -498,13 +564,13 @@ class YourApi extends Api
498564
{
499565
public function __construct()
500566
{
501-
$this->addResponseContentsHandler(function(PostRequestEvent $event) {
567+
$this->addResponseContentsListener(function(PostRequestEvent $event) {
502568
// stop propagation so future listeners of this event will not be called
503569
$event->stopPropagation();
504570
});
505571

506572
// this listener will not be called
507-
$this->addResponseContentsHandler(function(PostRequestEvent $event) {
573+
$this->addResponseContentsListener(function(PostRequestEvent $event) {
508574
// ...
509575
});
510576
}
@@ -682,7 +748,7 @@ class YourApi extends Api
682748
$pool = new FilesystemAdapter();
683749

684750
// file-based cache adapter with a 1-hour default cache lifetime
685-
$this->setClientBuilder(
751+
$this->setCacheBuilder(
686752
new CacheBuilder(
687753
pool: $pool,
688754
ttl: 3600

Diff for: src/Api.php

+28-9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use ProgrammatorDev\Api\Builder\Listener\CacheLoggerListener;
1414
use ProgrammatorDev\Api\Builder\LoggerBuilder;
1515
use ProgrammatorDev\Api\Event\PostRequestEvent;
16+
use ProgrammatorDev\Api\Event\PreRequestEvent;
1617
use ProgrammatorDev\Api\Event\ResponseContentsEvent;
1718
use ProgrammatorDev\Api\Exception\ConfigException;
1819
use ProgrammatorDev\Api\Helper\StringHelperTrait;
@@ -67,6 +68,8 @@ public function request(
6768
throw new ConfigException('A base URL must be set.');
6869
}
6970

71+
$this->configurePlugins();
72+
7073
if (!empty($this->queryDefaults)) {
7174
$query = \array_merge($this->queryDefaults, $query);
7275
}
@@ -75,15 +78,24 @@ public function request(
7578
$headers = \array_merge($this->headerDefaults, $headers);
7679
}
7780

78-
$this->configurePlugins();
79-
8081
$uri = $this->buildUri($path, $query);
81-
$request = $this->buildRequest($method, $uri, $headers, $body);
82+
$request = $this->createRequest($method, $uri, $headers, $body);
83+
84+
// pre request listener
85+
$request = $this->eventDispatcher->dispatch(new PreRequestEvent($request))->getRequest();
86+
87+
// request
8288
$response = $this->clientBuilder->getClient()->sendRequest($request);
8389

84-
$this->eventDispatcher->dispatch(new PostRequestEvent($request, $response));
90+
// post request listener
91+
$response = $this->eventDispatcher->dispatch(new PostRequestEvent($request, $response))->getResponse();
8592

93+
// always rewind the body contents in case it was used in the PostRequestEvent
94+
// otherwise it would return an empty string
95+
$response->getBody()->rewind();
8696
$contents = $response->getBody()->getContents();
97+
98+
// response contents listener
8799
return $this->eventDispatcher->dispatch(new ResponseContentsEvent($contents))->getContents();
88100
}
89101

@@ -236,16 +248,23 @@ public function setAuthentication(?Authentication $authentication): self
236248
return $this;
237249
}
238250

239-
public function addPostRequestHandler(callable $handler, int $priority = 0): self
251+
public function addPreRequestListener(callable $listener, int $priority = 0): self
252+
{
253+
$this->eventDispatcher->addListener(PreRequestEvent::class, $listener, $priority);
254+
255+
return $this;
256+
}
257+
258+
public function addPostRequestListener(callable $listener, int $priority = 0): self
240259
{
241-
$this->eventDispatcher->addListener(PostRequestEvent::class, $handler, $priority);
260+
$this->eventDispatcher->addListener(PostRequestEvent::class, $listener, $priority);
242261

243262
return $this;
244263
}
245264

246-
public function addResponseContentsHandler(callable $handler, int $priority = 0): self
265+
public function addResponseContentsListener(callable $listener, int $priority = 0): self
247266
{
248-
$this->eventDispatcher->addListener(ResponseContentsEvent::class, $handler, $priority);
267+
$this->eventDispatcher->addListener(ResponseContentsEvent::class, $listener, $priority);
249268

250269
return $this;
251270
}
@@ -274,7 +293,7 @@ private function buildUri(string $path, array $query = []): string
274293
return $uri;
275294
}
276295

277-
private function buildRequest(
296+
private function createRequest(
278297
string $method,
279298
string $uri,
280299
array $headers = [],

Diff for: src/Event/PostRequestEvent.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class PostRequestEvent extends Event
1010
{
1111
public function __construct(
1212
private readonly RequestInterface $request,
13-
private readonly ResponseInterface $response
13+
private ResponseInterface $response
1414
) {}
1515

1616
public function getRequest(): RequestInterface
@@ -22,4 +22,9 @@ public function getResponse(): ResponseInterface
2222
{
2323
return $this->response;
2424
}
25+
26+
public function setResponse(ResponseInterface $response): void
27+
{
28+
$this->response = $response;
29+
}
2530
}

Diff for: src/Event/PreRequestEvent.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\Api\Event;
4+
5+
use Psr\Http\Message\RequestInterface;
6+
use Symfony\Contracts\EventDispatcher\Event;
7+
8+
class PreRequestEvent extends Event
9+
{
10+
public function __construct(
11+
private RequestInterface $request
12+
) {}
13+
14+
public function getRequest(): RequestInterface
15+
{
16+
return $this->request;
17+
}
18+
19+
public function setRequest(RequestInterface $request): void
20+
{
21+
$this->request = $request;
22+
}
23+
}

Diff for: src/Event/ResponseContentsEvent.php

+3-6
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@
66

77
class ResponseContentsEvent extends Event
88
{
9-
private mixed $contents;
10-
11-
public function __construct($contents)
12-
{
13-
$this->contents = $contents;
14-
}
9+
public function __construct(
10+
private mixed $contents
11+
) {}
1512

1613
public function getContents(): mixed
1714
{

0 commit comments

Comments
 (0)