pierrerolland/guzzle-config-operations-bundle

This bundle allows Symfony projects to add Guzzle operations to their configuration

v3.2.1 2019-04-17 10:02 UTC

README

Build Status Scrutinizer Code Quality Total Downloads

This bundle allows Symfony projects to add Guzzle operations to their configuration. It also uses Symfony's or JMS' serializer to directly deserialize responses into objects. All you have to do is define your calls in Yaml, and your model classes to welcome the responses, and you're done !

Installation

composer require pierrerolland/guzzle-config-operations-bundle

In your app/AppKernel.php if you're using Symfony 2/3

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            // ...
            new Guzzle\ConfigOperationsBundle\GuzzleConfigOperationsBundle()
        ];

        return $bundles;
    }

In config/bundles.php with Symfony 4

<?php

return [
    // ...
    Guzzle\ConfigOperationsBundle\GuzzleConfigOperationsBundle::class => ['all' => true],
];

Activate Symfony serializer in app/config.yml

framework:
    serializer: ~

or JMSSerializer

https://github.com/schmittjoh/JMSSerializerBundle http://jmsyst.com/bundles/JMSSerializerBundle

Usage

1. Define your client

# services.yml

services:
    app.client.foo:
        class: GuzzleHttp\Client
        tags:
            - { name: guzzle.client, alias: foo }
        arguments:
            $config:
                baseUrl: "http://foo"
                operations:
                    readBar:
                        httpMethod: "GET"
                        uri: "/bar/{barId}"
                        responseClass: AppBundle\Model\Bar # The model used to deserialize the response
                        parameters:
                            barId:
                                type: "string"
                                location: "uri"
                                required: true
                    # other operations here    
      

The tag line is important, and requires both the name: guzzle.client and alias parts.

2. Use the client

A new service will appear, called guzzle_client.[the alias you used]. You can call the operations directly.

   // @var AppBundle\Model\Bar $bar
   $bar = $this->get('guzzle_client.foo')->readBar(['barId' => 1]);

Client configuration

This documentation is extracted from the Guzzle Services package.

Operation

From https://github.com/guzzle/guzzle-services/blob/master/src/Operation.php#L23

Parameter

From https://github.com/guzzle/guzzle-services/blob/master/src/Parameter.php#L84

Objects normalization

This bundle provides a recursive normalizer. Use the Type annotation to make the normalizer know which object should be recursively populated (suffixed by [] for arrays).

<?php
// Article.php

namespace AppBundle\Model;

use Guzzle\ConfigOperationsBundle\Normalizer\Annotation as Normalizer;

class Article
{
    /**
     * @var Tag[]
     *
     * @Normalizer\Type(name="AppBundle\Model\Tag[]")
     */
    private $tags;

    /**
     * @var Category
     *
     * @Normalizer\Type(name="AppBundle\Model\Category")
     */
    private $category;

    // ...