Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Eprel Api Client Bundle Laravel Package

asm/eprel-api-client-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. 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],
    
  2. 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)%'
    
  3. 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());
    }
    

Implementation Patterns

Dependency Injection

  • 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);
    

Caching Strategies

  • 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);
    

Error Handling

  • 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'
    

Batch Processing

  • 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++;
    }
    

Gotchas and Tips

Configuration Quirks

  • API Key Mandatory: While optional in config, omitting it will fail API calls. Always set %env(EPREL_API_KEY)%.
  • URI Overrides: Custom uri in config must include the full path (e.g., https://eprel.ec.europa.eu/api/v1).
  • Cache Service: Ensure the cache_service ID matches a valid PSR-6 cache pool (e.g., cache.app or a custom one).

Debugging

  • HTTP Client: Use a custom http_client_service (e.g., http_client.debug) to inspect requests/responses:
    asm_eprel_api_client:
        http_client_service: 'http_client.debug'
    
  • Logging: Enable debug mode for the logger to trace API interactions:
    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]]
    

Extension Points

  • Custom Responses: Extend the EprelClient to handle non-standard API responses:
    class CustomEprelClient extends EprelClient {
        public function getCustomData(string $endpoint) {
            return $this->request('GET', $endpoint);
        }
    }
    
  • Middleware: Add request/response middleware via Symfony’s http_client configuration:
    framework:
        http_client:
            middleware:
                - 'some.custom.middleware'
    

Performance Tips

  • Batch Requests: Minimize API calls by fetching multiple products in one request where possible.
  • Cache Tags: Use cache tags for related data (e.g., eprel_product_*) to invalidate groups of entries:
    $this->container->get('cache.eprel')->deleteItems(['tag' => 'eprel_product']);
    

Common Pitfalls

  • Rate Limiting: EPREL may throttle requests. Implement exponential backoff in custom clients:
    use Symfony\Contracts\HttpClient\Exception\RateLimited;
    try {
        $response = $client->request(...);
    } catch (RateLimited $e) {
        sleep($e->getRetryAfter());
        retry();
    }
    
  • Deprecated Endpoints: Monitor API version changes (e.g., version: 'latest'). Pin to a specific version if needed:
    asm_eprel_api_client:
        version: 'v1.2'
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours