+1 What a fantastic answer mtdowling. Before I had an abstract command in my client's service description with the api hard coded in and was wondering if there was a way to dynamically insert the api key from my Symfony2 applications parameters.yml so I used your event dispatcher example. I insert the api key from the service container into my client's $config parameter as key "api-key" and then find that key again from the event object. Here is an example client class:
class ExampleClient extends Client
{
public static function factory($config = array())
{
$default = array(
);
$required = array('base_url', 'api-key');
$config = Collection::fromConfig($config, $default, $required);
$client = new self(
$config->get('base_url'),
$config
);
$client->setConfig($config);
$client->setDescription(ServiceDescription::factory(__DIR__ . DIRECTORY_SEPARATOR . 'Command/commands.json'));
$e['request']->getQuery()->set('api-key', $e['client']->getConfig()->get('api-key'));
});
return $client;
}
}
This will append a query parameter "api-key" to every request in the above client as long as you pass into the $config argument a key called "api-key" with a vlue of your choosing.