pierrerolland / guzzle-config-operations-bundle
This bundle allows Symfony projects to add Guzzle operations to their configuration
Installs: 30 460
Dependents: 1
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 3
Open Issues: 0
Type:symfony-bundle
Requires
- php: >=7.1
- doctrine/annotations: >=1.2
- guzzlehttp/guzzle-services: >=1.0
- symfony/framework-bundle: ~4.0
- symfony/property-access: ~4.0
Requires (Dev)
- phpspec/phpspec: >=2.5.5
- symfony/property-info: ~4.0
- symfony/serializer: ~4.0
Suggests
- symfony/serializer: ~4.0
README
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; // ...