Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/vincentchalnot/apiclientbundle
- Owner: VincentChalnot
- License: mit
- Created: 2024-08-13T09:20:23.000Z (3 months ago)
- Default Branch: v1.0-dev
- Last Pushed: 2024-09-04T15:45:08.000Z (2 months ago)
- Last Synced: 2024-11-11T14:50:03.079Z (6 days ago)
- Language: PHP
- Size: 28.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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.