Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
namespace Renoir\AggregationBundle;
/**
* Abstract client to use for each Client
*
* Example location: src/Renoir/AggregationBundle/AbstractClient.php
**/
use Guzzle\Service\Inspector;
use Guzzle\Service\Client;
use Symfony\Component\Config\FileLocator;
use Guzzle\Service\Description\ServiceDescription;
/**
* Abstract Guzzle Client from Guzzle ServiceBuilder
*/
abstract class AbstractClient extends Client
{
/**
* Use to load from current bundle Resources/config/ directory
**/
protected function setServiceDescriptionUsingLocalXml($fileName)
{
$configs = array();
$configDirectories = array(__DIR__.'/Resources/config');
$locator = new FileLocator($configDirectories);
$commandDescriptionFiles = $locator->locate($fileName, null, false);
//throw new \Exception('Test-1:'.print_r($commandDescriptionFiles,1));
$descriptions = ServiceDescription::factory($commandDescriptionFiles[0]);
$this->setDescription($descriptions);
}
}
#
# file: src/Renoir/AggregationBundle/Resources/config/config.yml
#
# ... do not forget to add to imports in app/config/config.yml
# - { resource: @RenoirAggregationBundle/Resources/config/config.yml }
#
parameters:
guzzle.service_builder.class: Guzzle\Service\Builder\ServiceBuilder
#guzzle.service_builder.configuration_file:
guzzle_guzzle:
service_builder: ~
services:
#guzzle.service_builder:
# class: %guzzle.service_builder.class%
# factory_method: factory
# factory_class: %guzzle.service_builder.class%
# arguments:
# - { guzzle.service_builder.configuration_file: %guzzle.service_builder.configuration_file% }
<?php
namespace Renoir\SiteBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
/**
* In any controller
*/
class DefaultController extends Controller
{
/**
* @Route("/lifestream/", name="lifestream")
* @Template()
*/
public function lifestreamAction()
{
$serviceBuilder = $this->get('guzzle.service_builder');
$client = $serviceBuilder->get('twitter');
//$command = $client->getCommand('pingtest');
$command = $client->getCommand('search', array(
'screen_name'=>'renoirb',
'size'=>'original'
));
$execution = $client->execute($command);
$test = $command->getRequest()->getUrl();
throw new \Exception('The full URL to request would be '.print_r($test,1));
return array(
'twitter'=> array()
);
}
// ...
}
<?xml version="1.0" ?>
<!-- app/guzzleclients.xml -->
<guzzle>
<clients>
<client name="twitter" class="Renoir\AggregationBundle\TwitterClient" />
</clients>
</guzzle>
<?xml version="1.0" ?>
<!-- src/Renoir/AggregationBundle/Resources/config/twitter.commands.xml -->
<client>
<commands>
<command name="pingtestprout" method="GET" uri="/help/test.json">
<doc>Test if service is online</doc>
</command>
<command name="search" method="GET" uri="/1/users/profile_image?screen_name={{screen_name}}&amp;size={{size}}">
<doc>Get User image (https://dev.twitter.com/docs/api/1/get/users/profile_image/%3Ascreen_name)</doc>
<param name="screen_name" type="string" required="true" doc="User screen name e.g. twitterapi" />
<param name="size" type="string" required="true" doc="Size: bigger,normal,mini,original" />
</command>
</commands>
</client>
<?php
namespace Renoir\AggregationBundle;
/**
* Guzzle implementattion specific
*
* Example location: src/Renoir/AggregationBundle/TwitterClient.php
**/
use Guzzle\Service\Inspector;
/**
* Twitter Client
*/
class TwitterClient extends AbstractClient
{
/**
* Factory method to create a new TwitterClient
*
* @param array|Collection $config Configuration data. Array keys:
* base_url - Base URL of the web service
* scheme - API scheme (aka. http/https), defaults: "https"
* sub - API subdomain endpoint, defaults: "api"
* version - API version declared in endpoint url, defaults: 1
*
* @return TwitterClient
*/
public static function factory($config = array())
{
$default = array(
'base_url' => '{scheme}://{sub}.twitter.com/{version}/',
'scheme' => 'https',
'sub' => 'api',
'version' => 1
);
$required = array('scheme', 'version', 'base_url', 'sub');
$config = Inspector::prepareConfig($config, $default, $required);
//throw new \Exception('Should be a Collection:'.var_export($config,1));
$client = new self(
$config->get('base_url'),
$config->get('scheme'),
$config->get('sub'),
$config->get('version')
);
$client->setConfig($config);
return $client;
}
/**
* Client constructor
*
* @param string $baseUrl Base URL of the web service
* @param string $scheme API scheme (aka. http/https), defaults: "https"
* @param string $sub API subdomain endpoint, defaults: "api"
* @param string $version API version declared in endpoint url, defaults: 1
*/
public function __construct($baseUrl, $scheme, $sub, $version)
{
parent::__construct($baseUrl);
$this->scheme = $scheme;
$this->sub = $sub;
$this->version = $version;
$this->setServiceDescriptionUsingLocalXml('twitter.commands.xml');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment