Skip to content

Instantly share code, notes, and snippets.

@jojobyte
Forked from renoirb/AbstractClient.php
Created April 12, 2013 11:57

Revisions

  1. @renoirb renoirb revised this gist Jul 10, 2012. 3 changed files with 28 additions and 3 deletions.
    20 changes: 20 additions & 0 deletions config.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    #
    # 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% }
    7 changes: 7 additions & 0 deletions guzzleclients.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    <?xml version="1.0" ?>
    <!-- app/guzzleclients.xml -->
    <guzzle>
    <clients>
    <client name="twitter" class="Renoir\AggregationBundle\TwitterClient" />
    </clients>
    </guzzle>
    4 changes: 1 addition & 3 deletions twitter.commands.xml
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,5 @@
    <!--
    example location: src/Renoir/AggregationBundle/Resources/config/twitter.commands.xml
    -->
    <?xml version="1.0" ?>
    <!-- src/Renoir/AggregationBundle/Resources/config/twitter.commands.xml -->
    <client>
    <commands>
    <command name="pingtestprout" method="GET" uri="/help/test.json">
  2. @renoirb renoirb renamed this gist Jul 9, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @renoirb renoirb created this gist Jul 9, 2012.
    38 changes: 38 additions & 0 deletions AbstractClient.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    <?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);
    }
    }
    42 changes: 42 additions & 0 deletions DefaultController.com
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    <?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()
    );
    }

    // ...
    }
    73 changes: 73 additions & 0 deletions TwitterClient.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,73 @@
    <?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');
    }
    }
    16 changes: 16 additions & 0 deletions twitter.commands.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    <!--
    example location: src/Renoir/AggregationBundle/Resources/config/twitter.commands.xml
    -->
    <?xml version="1.0" ?>
    <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>