[guzzle4] Add a parameter to all requests created with guzzle-services

160 views
Skip to first unread message

Simone Fumagalli

unread,
Aug 28, 2014, 6:55:52 AM8/28/14
to guz...@googlegroups.com
I'm trying to update my library to use `Guzzle4`. I load my client configuration with `\Guzzle\Description` as shown in the doc. The file is https://github.com/hpatoio/bitly-api/blob/master/src/Resources/bitly.json

As you can see there is an `access_token` parameter to all requests. In the `Guzzle3` version of my library `access_token` is added by a plugin https://github.com/hpatoio/bitly-api/blob/master/src/Plugin/TokenAuthPlugin.php

I'm now trying to replicate this behaviour with Guzzle4. I've create an `AccessTokenSubscriber` that listen for event `prepare` on '\Command\Guzzle\GuzzleClient' and add `access_token` parameter to the request. Here you can see a draft https://gist.github.com/hpatoio/d5d0c2a772b3fb5f8a80

I couldn't find a way to add the parameter "access_token" so I had to add a "addParam" method to "GuzzleHttp\Command" class. The method is dead simple:

    public function addParam($name, $value)
    {
        $this->data[$name] = $value;
    }

In this way the parameter is set in the request. Am I missing something or there is now other way to achive this ?

Thanks

--
Simone

Michael Crumm

unread,
Nov 23, 2014, 3:02:37 PM11/23/14
to guz...@googlegroups.com
I haven't attempted this in a Subscriber, but if you just need to get a parameter into the query string, you can do so at construction time:

If this doesn't work for your use case, hopefully it might lead you to a solution that does.  If so, please post it here!  I'd love to know how if this is possible in a Subscriber.

use GuzzleHttp\Client;
use GuzzleHttp\Command\Guzzle\Description;
use GuzzleHttp\Command\Guzzle\GuzzleClient;

$bitly = new GuzzleClient(new Client([
'defaults' => [
'query' => [
'access_token' => YOUR_ACCESS_TOKEN
        ]
]
]), new Description([ 'baseUrl' => 'https://api-ssl.bitly.com' ]));

J Cobb

unread,
Dec 22, 2014, 5:32:11 PM12/22/14
to guz...@googlegroups.com
Although, I think this probably should be done in a Subscriber I have seen a client library built on Guzzle 5 that does this. Check out lines 57-80 & 225 in https://github.com/ShopifyExtras/PHP-Shopify-API-Wrapper/blob/master/src/Client.php#L57-L80



On Thursday, August 28, 2014 3:55:52 AM UTC-7, Simone Fumagalli wrote:
Reply all
Reply to author
Forward
0 new messages