Skip to content

Commit ffee2a6

Browse files
committed
docs: added client and factories sections
1 parent 092745c commit ffee2a6

2 files changed

Lines changed: 138 additions & 3 deletions

File tree

README.md

Lines changed: 136 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,6 @@ class YourApi extends Api
6363

6464
## Documentation
6565

66-
Methods available only to developers of the API SDK, for configuration and validation of data.
67-
End users have no access to these methods.
68-
6966
### Base URL
7067

7168
Getter and setter for the base URL.
@@ -443,6 +440,142 @@ class YourApi extends Api
443440
}
444441
```
445442

443+
### HTTP Client (PSR-18) and HTTP Factories (PSR-17)
444+
445+
> [!IMPORTANT]
446+
> The methods in this section are all public.
447+
> The purpose for that is to allow the end user to configure their own HTTP client, HTTP factories and plugins.
448+
449+
- [HTTP client and HTTP factories adapters](#http-client-and-http-factories-adapters)
450+
- [Plugin system](#plugin-system)
451+
452+
#### HTTP Client and HTTP Factories Adapters
453+
454+
By default, this library makes use of the [HTTPlug's Discovery](https://github.com/php-http/discovery) library.
455+
This means that it will automatically find and install a well-known PSR-18 client and PSR-17 factory implementation for you
456+
(if they were not found on your project):
457+
- [PSR-18 compatible implementations](https://packagist.org/providers/psr/http-client-implementation)
458+
- [PSR-17 compatible implementations](https://packagist.org/providers/psr/http-factory-implementation)
459+
460+
```php
461+
use ProgrammatorDev\Api\Builder\ClientBuilder;
462+
463+
new ClientBuilder(
464+
?ClientInterface $client = null,
465+
?RequestFactoryInterface $requestFactory = null,
466+
?StreamFactoryInterface $streamFactory = null
467+
);
468+
```
469+
470+
```php
471+
use ProgrammatorDev\Api\Builder\ClientBuilder;
472+
473+
$this->setClientBuilder(ClientBuilder $clientBuilder): ClientBuilder;
474+
```
475+
476+
```php
477+
use ProgrammatorDev\Api\Builder\ClientBuilder;
478+
479+
$this->getClientBuilder(): ClientBuilder;
480+
```
481+
482+
If you don't want to rely on the discovery of implementations, you can set the ones you want:
483+
484+
```php
485+
use ProgrammatorDev\Api\Api;
486+
use ProgrammatorDev\Api\Builder\ClientBuilder;
487+
use Symfony\Component\HttpClient\Psr18Client;
488+
use Nyholm\Psr7\Factory\Psr17Factory;
489+
490+
class YourApi extends Api
491+
{
492+
public function __construct()
493+
{
494+
// ...
495+
496+
$client = new Psr18Client();
497+
$requestFactory = $streamFactory = new Psr17Factory();
498+
499+
$this->setClientBuilder(new ClientBuilder($client, $requestFactory, $streamFactory));
500+
}
501+
}
502+
```
503+
504+
The same for the end user:
505+
506+
```php
507+
$api = new YourApi();
508+
509+
$client = new Psr18Client();
510+
$requestFactory = $streamFactory = new Psr17Factory();
511+
512+
$api->setClientBuilder(new ClientBuilder($client, $requestFactory, $streamFactory));
513+
```
514+
515+
#### Plugin System
516+
517+
This library enables attaching plugins to the HTTP client.
518+
A plugin modifies the behavior of the client by intercepting the request and response flow.
519+
520+
Since plugin order matters, a plugin is added with a priority level,
521+
and are executed in descending order from highest to lowest.
522+
523+
Check all the [available plugins](https://docs.php-http.org/en/latest/plugins/index.html) or [create your own](https://docs.php-http.org/en/latest/plugins/build-your-own.html).
524+
525+
```php
526+
use Http\Client\Common\Plugin;
527+
528+
$this->getClientBuilder()->addPlugin(Plugin $plugin, int $priority): self;
529+
```
530+
531+
> [!NOTE]
532+
> A `PluginException` will be thrown if there is a plugin with the same `priority` level.
533+
534+
It is important to know that this library already uses various plugins with different priorities.
535+
The following list has all the implemented plugins with the respective priority in descending order (remember that order matters):
536+
537+
- [`ContentTypePlugin`](https://docs.php-http.org/en/latest/plugins/content-type.html): `40`
538+
- [`ContentLengthPlugin`](https://docs.php-http.org/en/latest/plugins/content-length.html): `32`
539+
- [`AuthenticationPlugin`](https://docs.php-http.org/en/latest/plugins/authentication.html): `24` (only if authentication is enabled)
540+
- [`CachePlugin`](https://docs.php-http.org/en/latest/plugins/cache.html): `16` (only if cache is enabled)
541+
- [`LoggerPlugin`](https://docs.php-http.org/en/latest/plugins/logger.html): `8` (only if logger is enabled)
542+
543+
For example, if you wanted the client to automatically attempt to re-send a request that failed
544+
(due to unreliable connections and servers, for example)
545+
you can add the [RetryPlugin](https://docs.php-http.org/en/latest/plugins/retry.html);
546+
547+
```php
548+
use ProgrammatorDev\Api\Api;
549+
use Http\Client\Common\Plugin\RetryPlugin;
550+
551+
class YourApi extends Api
552+
{
553+
public function __construct()
554+
{
555+
// ...
556+
557+
// if a request fails, it will retry at least 3 times
558+
// priority is 12 to execute the plugin between the cache and logger plugins
559+
// (check the above plugin order for more information)
560+
$this->getClientBuilder()->addPlugin(
561+
plugin: new RetryPlugin(['retries' => 3])
562+
priority: 12
563+
);
564+
}
565+
}
566+
```
567+
568+
The same for the end user:
569+
570+
```php
571+
$api = new YourApi();
572+
573+
$api->getClientBuilder()->addPlugin(
574+
plugin: new RetryPlugin(['retries' => 3])
575+
priority: 12
576+
);
577+
```
578+
446579
## Contributing
447580

448581
Any form of contribution to improve this library (including requests) will be welcome and appreciated.

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
"php-http/message": "^1.16",
2121
"psr/cache": "^2.0 || ^3.0",
2222
"psr/http-client": "^1.0",
23+
"psr/http-client-implementation": "*",
2324
"psr/http-factory": "^1.0",
25+
"psr/http-factory-implementation": "*",
2426
"psr/log": "^2.0 || ^3.0",
2527
"symfony/event-dispatcher": "^6.4",
2628
"symfony/options-resolver": "^6.4"

0 commit comments

Comments
 (0)