asm/eprel-api-client-bundle
Install the Bundle
composer require asm/eprel-api-client-bundle
Enable the bundle in config/bundles.php if not using Symfony Flex:
Asm\EprelApiClientBundle\AsmEprelApiClientBundle::class => ['all' => true],
Configure API Key
Add your EPREL API key to .env:
EPREL_API_KEY=your_api_key_here
Reference it in config/packages/asm_eprel_api_client.yaml:
asm_eprel_api_client:
api_key: '%env(EPREL_API_KEY)%'
First Use Case
Inject EprelClient into a controller or service and fetch a product by registration number:
use Asm\EprelApiClient\EprelClient;
public function show(EprelClient $client, string $registrationNumber)
{
$product = $client->getProduct($registrationNumber);
return $this->json($product->toArray());
}
Service Integration: Inject EprelClient into services for reusable API logic (e.g., a ProductService).
public function __construct(private EprelClient $client) {}
Command Bus: Use Symfony’s CommandBus to decouple API calls from business logic:
$command = new FetchProductCommand($registrationNumber);
$result = $this->commandBus->handle($command);
Default Cache: Leverages Symfony’s cache.app (TTL: 3600s by default). Override via config:
asm_eprel_api_client:
cache_service: 'cache.eprel'
cache_ttl: 7200 # 2 hours
Cache Invalidation: Manually clear cache for dynamic updates:
$this->container->get('cache.eprel')->delete('eprel_product_'.$registrationNumber);
Exception Handling: Catch EprelApiException for API-specific errors:
try {
$product = $client->getProduct($registrationNumber);
} catch (EprelApiException $e) {
$this->addFlash('error', $e->getMessage());
}
Logging: Use the configured logger (logger service by default) to track API calls:
asm_eprel_api_client:
logger_service: 'logger.eprel'
Bulk Operations: Use getProducts() for multiple registration numbers:
$products = $client->getProducts(['RN123', 'RN456']);
Pagination: Handle paginated responses (if supported by the API) with custom logic:
$products = [];
$page = 1;
while (true) {
$pageData = $client->getProducts([], ['page' => $page]);
if (empty($pageData)) break;
$products = array_merge($products, $pageData);
$page++;
}
%env(EPREL_API_KEY)%.uri in config must include the full path (e.g., https://eprel.ec.europa.eu/api/v1).cache_service ID matches a valid PSR-6 cache pool (e.g., cache.app or a custom one).http_client_service (e.g., http_client.debug) to inspect requests/responses:
asm_eprel_api_client:
http_client_service: 'http_client.debug'
asm_eprel_api_client:
logger_service: 'logger.eprel'
In config/services.yaml:
services:
logger.eprel:
class: Symfony\Bridge\Monolog\Logger
arguments: ['eprel']
calls:
- [setLevel, [debug]]
EprelClient to handle non-standard API responses:
class CustomEprelClient extends EprelClient {
public function getCustomData(string $endpoint) {
return $this->request('GET', $endpoint);
}
}
http_client configuration:
framework:
http_client:
middleware:
- 'some.custom.middleware'
eprel_product_*) to invalidate groups of entries:
$this->container->get('cache.eprel')->deleteItems(['tag' => 'eprel_product']);
use Symfony\Contracts\HttpClient\Exception\RateLimited;
try {
$response = $client->request(...);
} catch (RateLimited $e) {
sleep($e->getRetryAfter());
retry();
}
version: 'latest'). Pin to a specific version if needed:
asm_eprel_api_client:
version: 'v1.2'
How can I help you explore Laravel packages today?