itkg/consumer

Itkg consume library

2.0.0 2016-08-09 11:24 UTC

README

Build Status Scrutinizer Code Quality Code Coverage SensioLabsInsight

features

  • Rest & Soap consumer library
  • Webservice caching
  • Logging
  • OAuth management

Installation

Installation by Composer

If you use composer, add library as a dependency to the composer.json of your application

    "require": {
        "itkg/consumer": "dev-master"
    }

Usage

Simple example of Rest WS


$service =  new \Itkg\Consumer\Service\Service(
    $eventDispatcher,
    new Itkg\Consumer\Client\RestClient(array(
        'timeout' => 10
    )),
    array(
        'identifier' => 'my test'
    )
);

$response = $service
    ->sendRequest(\Symfony\Component\HttpFoundation\Request::create('http://URL/OF/MY/WEBSERVICE.json'))
    ->getResponse();

Configuration

Logs

  • Create a new logger instance

$service =  new \Itkg\Consumer\Service\Service(
    $eventDispatcher,
    new Itkg\Consumer\Client\RestClient(array(
        'timeout' => 10
    )),
    array(
        'identifier' => 'my test',
        'logger'     => new \Monolog\Logger('my_logger', array(new \Monolog\Handler\StreamHandler('/tmp/test'))),
    )
);
  • Add logger listener to your event dispatcher

$eventDispatcher->addSubscriber(new \Itkg\Consumer\Listener\LoggerListener());

Serialization

  • Add Deseralizer listener to your event dispatcher (Create serializer with JMS serializer builder)

    $eventDispatcher->addSubscriber(
        new \Itkg\Consumer\Listener\DeserializerListener(JMS\Serializer\SerializerBuilder::create()->build())
    );
  • Define response_type & response_format

$service =  new \Itkg\Consumer\Service\Service(
    $eventDispatcher,
    new Itkg\Consumer\Client\RestClient(array(
        'timeout' => 10
    )),
    array(
        'identifier' => 'my test',
        'reponse_format' => 'xml,
        'response_type'  => 'My\Class
    )
);
  • Get deserialized content like this

$object = $service->getResponse()->getDeserializedContent();

Cache

  • Add Cache listener to your event dispatcher

    $eventDispatcher->addSubscriber(
        new \Itkg\Consumer\Listener\CacheListener($eventDispatcher)
    );

$service =  new \Itkg\Consumer\Service\Service(
    $eventDispatcher,
    new Itkg\Consumer\Client\RestClient(array(
        'timeout' => 10
    )),
    array(
        'identifier' => 'my test',
        'cache_adapter' => new \Itkg\Core\Cache\Adapter\Registry(),
        'cache_ttl => 10000
    )
);
  • You can define serialize & deserialize method by defining 'cache_serializer' & 'cache_deserializer' options