twentytwo-labs/api-service-bundle

Integrate API Service into Symfony

1.1.1 2024-09-23 18:29 UTC

This package is auto-updated.

Last update: 2024-10-23 18:40:17 UTC


README

This bundle integrate the API Service Component into Symfony.

Installation

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

composer require twentytwo-labs/api-service-bundle

Then, enable the bundle by adding the following line in the app/AppKernel.php file of your project:

<?php
// app/AppKernel.php
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new TwentytwoLabs\ApiServiceBundle\ApiServiceBundle(),
        );
        // ...
    }
}

Config

Full configuration provided by the bundle:

api_service:
  # (optional)
  default_services:
    client: httplug.client
    message_factory: httplug.message_factory
    uri_factory: httplug.uri_factory
  # (optional) cache schema files
  cache:
    # provide the Id of any PSR-6 cache service
    service: 'my_app.cache'
  # (optional) configure supported pagination providers
  pagination:
    # extract pagination from response headers
    header:
      page: X-Page
      perPage: X-Per-Page
      totalPages: X-Total-Pages
      totalItems: X-Total-Items
  # configure api services
  apis:
    my_service:
      # the schema describing your api
      schema: 'file://%kernel.root_dir%/config/schema/foo.yml'
      # (optional) use a specific http client implementation
      client: httplug.client.foo
      # (optional) fine tune your api service
      config:
        # provide a base url
        baseUri: https://bar.com
        # validate request
        validateRequest: true
        # validate response
        validateResponse: false
        # return a psr-7 response
        # by default it return a `Resource` class
        returnResponse: false

Dependencies

HTTP client

The Api Service component make use of the Http\Client\HttpClient interface provided by HttPlug to send requests.

You can use your own HTTP client services within Symfony, but we strongly advice you to use the HttplugBundle

composer require php-http/httplug-bundle

You can then choose one the many HTTP client adapter supported by HTTPlug. A list is available here

# for example, here we choose the Guzzle 6 adapter
composer require php-http/guzzle6-adapter

Cache

This bundle have the ability to cache schema files used by you API services.
It use the PSR-6: Caching Interface to do so.

For performance reasons, the cache SHOULD be enable in production.

From Symfony 4.0 and above

Symfony 3.1 provide a cache implementation of the PSR-6 Caching Interface. You don't need additional components to integrate caching capabilities to the framework.