24hoursmedia/tesla-client-bundle
Symfony bundle that provides:
Requests to a client are done through a subclass of SF's Request instances.
Via composer:
(1) add following lines to composer.json:
"require": {
(...)
"24hoursmedia/tesla-client-bundle": "master-dev"
(...)
}
(2) run composer update
php composer.phar update
(3) add bundle to AppKernel.php
$bundles = array(
(....)
new Tesla\Bundle\ClientBundle\TeslaClientBundle()
);
20131125 added possibility to directly inject additional curl options into client and proxy service
create client from a factory and do a request:
use Tesla\Bundle\ClientBundle\Client\HttpClientFactory;
use Tesla\Bundle\ClientBundle\Client\HttpClientInterface;
use Tesla\Bundle\ClientBundle\Client\TeslaRequest;
use Tesla\Bundle\ClientBundle\Client\TeslaResponse;
/* @var $factory HttpClientFactory */
$factory = $client->getContainer()->get('tesla_client.http_client_factory');
/* @var $client HttpClientInterface */
$http = $factory->get($baseUrl);
$request = $http->createRequest('http://www.example.com');
// (add headers and other request stuff here) //
$response = $http->execute($request);
$format = $response->getFormat(); // (json etc)
$content = $response->getContent();
$status = $response->getStatusCode();
configure an http client as a service which can be obtained through the container:
acme.http_client:
class: %tesla_client.http_client.class%
factory_service: tesla_client.http_client_factory
factory_method: get
arguments:
- http://www.example.com
create a proxy service that proxies requests from http://foo & http://bar/sub to http://www.example.com:
acme.http_proxy_example:
class: %tesla_client.http_proxy.class%
factory_service: tesla_client.httpproxy_factory
factory_method: get
arguments:
- http://www.example.com
calls:
- [addTranslationUrl, ["http://foo"]]
- [addTranslationUrl, ["http://bar/sub"]]
example of using a proxy defined as a service:
use Tesla\Bundle\ClientBundle\Proxy\HttpProxy;
/* @var $proxy HttpProxy */
$proxy = $client->getContainer()->get('tesla_client.http_proxy_example');
// foo host gets proxied to example.com
$responseContent = $proxy->execute($proxy->createRequest('http://foo'))->getContent();
$this->assertContains('example', $responseContent);
// foo bar/sub gets proxied to example.com
$responseContent = $proxy->execute($proxy->createRequest('http://bar/sub'))->getContent();
How can I help you explore Laravel packages today?