Skip to content

Commit

Permalink
Merge pull request #6 from programmatordev/1.x
Browse files Browse the repository at this point in the history
v1.0.0
  • Loading branch information
andrepimpao authored May 28, 2024
2 parents ef7821f + 1f8358a commit fbe1bb0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 23 deletions.
35 changes: 20 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ A library for creating SDKs in PHP with support for:
- Event listeners;
- ...and more.

All methods are public for full end user hackability 🔥.
All methods are public for full hackability 🔥.

## Requirements

Expand Down Expand Up @@ -144,7 +144,7 @@ class YourApi extends Api

By default, this method will return a `string` as it will be the response of the request as is.
If you want to change how the response is handled in all requests (for example, decode a JSON string into an array),
check the [`addResponseContentsHandler`](#addresponsecontentshandler) method in the [Event Listeners](#event-listeners) section.
check the [`addResponseContentsListener`](#addresponsecontentslistener) method in the [Event Listeners](#event-listeners) section.

#### `buildPath`

Expand Down Expand Up @@ -472,7 +472,7 @@ The `addResponseContentsListener` method is used to manipulate the response that
This event listener will be applied to every API request.

```php
$this->addResponseContentsListener(callable $handler, int $priority = 0): self;
$this->addResponseContentsListener(callable $listener, int $priority = 0): self;
```

For example, if the API responses are JSON strings, you can use this event listener to decode them into arrays:
Expand Down Expand Up @@ -527,7 +527,7 @@ Event listeners are then executed from the highest priority to the lowest:

```php
use ProgrammatorDev\Api\Api;
use ProgrammatorDev\Api\Event\PostRequestEvent;
use ProgrammatorDev\Api\Event\ResponseContentsEvent;

class YourApi extends Api
{
Expand All @@ -538,13 +538,13 @@ class YourApi extends Api

// executed last (lower priority)
$this->addResponseContentsListener(
listener: function(PostRequestEvent $event) { ... },
listener: function(ResponseContentsEvent $event) { ... },
priority: 0
);

// executed first (higher priority)
$this->addResponseContentsListener(
listener: function(PostRequestEvent $event) { ... },
listener: function(ResponseContentsEvent $event) { ... },
priority: 10
);
}
Expand All @@ -558,21 +558,21 @@ For that, you can use the `stopPropagation()` method:

```php
use ProgrammatorDev\Api\Api;
use ProgrammatorDev\Api\Event\PostRequestEvent;
use ProgrammatorDev\Api\Event\ResponseContentsEvent;

class YourApi extends Api
{
public function __construct()
{
$this->addResponseContentsListener(function(PostRequestEvent $event) {
$this->addResponseContentsListener(function(ResponseContentsEvent $event) {
// stop propagation so future listeners of this event will not be called
$event->stopPropagation();
});

// this listener will not be called
$this->addResponseContentsListener(function(PostRequestEvent $event) {
$this->addResponseContentsListener(function(ResponseContentsEvent $event) {
// ...
});
});
}
}
```
Expand Down Expand Up @@ -849,11 +849,11 @@ class YourApi extends Api
{
parent::__construct();

$this->configureOptions($options);
$this->options = $this->configureOptions($options);
$this->configureApi();
}

private function configureOptions(array $options): void
private function configureOptions(array $options): array
{
// set defaults values, if none were provided
$this->optionsResolver->setDefault('timezone', 'UTC');
Expand All @@ -867,8 +867,8 @@ class YourApi extends Api
$this->optionsResolver->setAllowedValues('timezone', \DateTimeZone::listIdentifiers());
$this->optionsResolver->setAllowedValues('language', ['en', 'pt']);

// resolve and set to options property
$this->options = $this->optionsResolver->resolve($options);
// return resolved options
return $this->optionsResolver->resolve($options);
}

private function configureApi(): void
Expand Down Expand Up @@ -906,7 +906,12 @@ $api = new YourApi([
$posts = $api->getPosts();
```

For all available methods, check the official page documentation [here](https://symfony.com/doc/current/components/options_resolver.html).
For all available methods, check the official page [documentation](https://symfony.com/doc/current/components/options_resolver.html).

## Libraries using PHP API SDK

- [programmatordev/openweathermap-php-api](https://github.com/programmatordev/openweathermap-php-api)
- [programmatordev/sportmonksfootball-php-api](https://github.com/programmatordev/sportmonksfootball-php-api)

## Contributing

Expand Down
22 changes: 14 additions & 8 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,22 @@ public function request(
private function configurePlugins(): void
{
// https://docs.php-http.org/en/latest/plugins/content-type.html
$this->clientBuilder->addPlugin(new ContentTypePlugin(), 40);
$this->clientBuilder->addPlugin(
plugin: new ContentTypePlugin(),
priority: 40
);

// https://docs.php-http.org/en/latest/plugins/content-length.html
$this->clientBuilder->addPlugin(new ContentLengthPlugin(), 32);
$this->clientBuilder->addPlugin(
plugin: new ContentLengthPlugin(),
priority: 32
);

// https://docs.php-http.org/en/latest/message/authentication.html
if ($this->authentication) {
$this->clientBuilder->addPlugin(
new AuthenticationPlugin($this->authentication),
24
plugin: new AuthenticationPlugin($this->authentication),
priority: 24
);
}

Expand All @@ -129,23 +135,23 @@ private function configurePlugins(): void
}

$this->clientBuilder->addPlugin(
new CachePlugin(
plugin: new CachePlugin(
$this->cacheBuilder->getPool(),
$this->clientBuilder->getStreamFactory(),
$cacheOptions
),
16
priority: 16
);
}

// https://docs.php-http.org/en/latest/plugins/logger.html
if ($this->loggerBuilder) {
$this->clientBuilder->addPlugin(
new LoggerPlugin(
plugin: new LoggerPlugin(
$this->loggerBuilder->getLogger(),
$this->loggerBuilder->getFormatter()
),
8
priority: 8
);
}
}
Expand Down

0 comments on commit fbe1bb0

Please sign in to comment.