Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/vincentchalnot/apiclientbundle

Ease the creation of business API client with OAuth authentication, serialization and cache
https://github.com/vincentchalnot/apiclientbundle

Last synced: 4 days ago
JSON representation

Ease the creation of business API client with OAuth authentication, serialization and cache

Awesome Lists containing this project

README

        

Sidus/ApiClientBundle
=======================

## Introduction

This library provides basic blocks for building a business oriented OAuth API client very easily.

### Features

- Automatic token negotiation
- Serialization and deserialization of API requests and responses
- Cache layer for API responses
- Easy to extend and customize

## Basic usage

### Simple request:

```php
/** @var \Sidus\ApiClientBundle\ApiClient $apiClient */

$apiRequest = new \Sidus\ApiClientBundle\Model\Request\AuthenticatedApiRequest(
new \Sidus\ApiClientBundle\Model\Request\Component\HttpComponent(
baseUri: 'https://api.example.com',
path: '/api/v1/resource',
),
);

$response = $apiClient->query($apiRequest);

$response->getBody(); // Raw response content
```

### Using normalizations/denormalization:

```php
/** @var \Sidus\ApiClientBundle\ApiClient $apiClient */

$apiRequest = new \Sidus\ApiClientBundle\Model\Request\AuthenticatedApiRequest(
new \Sidus\ApiClientBundle\Model\Request\Component\HttpComponent(
baseUri: 'https://api.example.com',
path: '/api/v1/resource',
method: 'POST',
),
);

$apiRequest->setSerializationComponent(
new \Sidus\ApiClientBundle\Model\Request\Component\SerializationComponent(
content: new Resource(),
),
);
$apiRequest->setDeserializationComponent(
new \Sidus\ApiClientBundle\Model\Request\Component\DeserializationComponent(
className: ResourceResponse::class,
),
);

$response = $apiClient->query($apiRequest);

$responseResource = $response->getContent(); // Deserialized response content
```

### Using cache:

```php
/**
* @var \Sidus\ApiClientBundle\ApiClient $apiClient
* @var \Sidus\ApiClientBundle\Model\Request\ApiRequest $apiRequest
*/

$apiRequest->setCacheComponent(
new \Sidus\ApiClientBundle\Model\Request\Component\CacheComponent(
ttl: 3600,
),
);
```

## Installation

```bash
composer require sidus/api-client-bundle
```

## Configuration

You have two options to provide credentials for token negotiation:

Declare a Credentials service:

```yaml
services:
app.oauth_credentials:
class: Sidus\ApiClientBundle\Credentials
arguments:
$baseUrl: 'https://api.example.com'
$path: '/oauth/token' # Token negotiation endpoint
$authenticationParams: # What you send to the token negotiation endpoint
username: 'xxxx'
password: 'xxxx'
```

Each time you create an API request using the same base URL, the credentials will be automatically used.

Or you can provide the access token directly in the API request:

```php
$apiRequest = new \Sidus\ApiClientBundle\Model\Request\AuthenticatedApiRequest(
new \Sidus\ApiClientBundle\Model\Request\Component\HttpComponent(
baseUri: 'https://api.example.com',
path: '/api/v1/resource',
),
);
$apiRequest->setAuthorizationComponent(
new Sidus\ApiClientBundle\Model\Authorization\OAuthToken(
accessToken: 'xxxx',
),
);
```

Or any custom authorization component implementing the
`\Sidus\ApiClientBundle\Contracts\Request\Component\AuthorizationComponentInterface`.

When using custom headers, simply add the custom header in the API request:

```php
$apiRequest->addHeader('X-Custom-Header', 'value');
```

Or use an event listener to add the header automatically using the `\Sidus\ApiClientBundle\Model\Event\ApiRequestEvent`
event.