cos / rest-client-bundle
Symfony rest client bundle - Work in progress
Installs: 182
Dependents: 0
Suggesters: 0
Security: 0
Stars: 10
Watchers: 2
Forks: 1
Open Issues: 1
Type:symfony-bundle
Requires
- php: ^5.5 || ^7.0
- doctrine/annotations: ^1.3
- guzzlehttp/guzzle: ^6.2
- ocramius/proxy-manager: 1.0.2
- symfony/framework-bundle: ^2.6 || ^3.0
Requires (Dev)
- mockery/mockery: >=0.9.0
- phpunit/phpunit: >4.8.0
This package is not auto-updated.
Last update: 2025-03-30 05:28:12 UTC
README
Symfony Rest Client Bundle using GuzzleHttp and Ocramius/ProxyManager
Installation
To install CosRestClientBundle with Composer execute the following command:
$ composer require "cos/rest-client-bundle": "dev-master"
Now, Composer will automatically download all required files, and install them
for you. All that is left to do is to update your AppKernel.php
file, and
register the new bundle:
<?php
// in AppKernel::registerBundles()
$bundles = array(
// ...
new Cos\RestClientBundle\CosRestClientBundle(),
// ...
);
The bundle is now installed. Lets start configure with the bundle.
Configuration reference
# config.yml
cos_rest_client:
annotation_reader: annotation_reader # annotation reader service id
# http clients and base URLs
clients:
default: { baseUri: 'https://jsonplaceholder.typicode.com' }
Usage
Define a rest resource interface
<?php
namespace AppBundle\Rest;
use Cos\RestClientBundle\Annotation\Client;
use Cos\RestClientBundle\Annotation\Endpoint;
use Cos\RestClientBundle\Annotation\Form;
use Cos\RestClientBundle\Annotation\Json;
use Cos\RestClientBundle\Annotation\Path;
use Cos\RestClientBundle\Annotation\Query;
/**
* Client configuration
* @Client(name="default")
*/
interface Posts
{
/**
* @Path(name="id", paramName="idParam")
* @Endpoint(uri="/posts/{id}", method="get")
*/
public function get($idParam);
/**
* @Query(name="userId")
* @Endpoint(uri="/posts")
*/
public function getWithQuery($userId);
/**
* @Form(name="formData")
* @Endpoint(uri="/posts", method="POST")
*/
public function form(array $formData);
/**
* @Json(name="data")
* @Endpoint(uri="/posts", method="POST")
*/
public function json(array $data);
}
Create proxy for Posts
<?php
//in controller
$proxyFactory = $this->get('cos_rest_client.proxy_factory');
$proxy = $proxyFactory->create(Posts::class);
Call proxy methods defined in interface
<?php
$proxy->get(1); //request for client base uri + /posts/1
$proxy->getWithQuery(1)->getBody()->getContents(); //request for /posts?userId=1
$data = ['foo' => 'bar']
$proxy->form($data) //post request where $data is sent as application/x-www-form-urlencoded
$proxy->json($data) // send data as json
Response
Every method call from a proxy return a Psr\Http\Message\ResponseInterface
Full example
https://github.com/cosminseceleanu/RestClientBundleSample
Events
RequestEvent: dispatched before a request is executed
ResponseEvent: dispatched when response is received