当前位置: 动力学知识库 > 问答 > 编程问答 >

php - How can I access values from a Guzzle service description when instantiating a client?

问题描述:

I'm creating a Guzzle client using a service description. Each operation in the service description contains a URI. The REST endpoint I'm accessing requires an authorization header that is made by sticking together a public key and the uri of the endpoint and then creating an md5 from the resulting string. This is used as the authorization value.

I don't know how to get the uri value from the service description after instantiating the client.

I'm creating the Guzzle client like this:

class RestClient extends Client

{

public static function factory($config = array())

{

// The following values are required when creating the client

$required = array(

'base_url',

'public_key',

'private_key'

);

$path = drupal_get_path('module', 'junkrest');

// Merge in default settings and validate the config

$config = Collection::fromConfig($config, $required);

// Create a new client

$client = new self($config->get('base_url'), $config);

// Set the service description

$client->setDescription(ServiceDescription::factory($path . '/config/services.json'));

$authstring = md5($public_key, 'the uri value from an operation in the services.json file');

$client->setDefaultHeaders(array(

'Authentication' => $authstring));

return $client;

}

}

The services.json file contains this:

{

"name": "TheName",

"apiVersion": "1",

"baseUrl": "https://apidev.example.com",

"description": "Custom REST API client",

"operations": {

"GetFranchiseList": {

"httpMethod": "GET",

"uri": "v1/franchise",

"summary": "Returns an array of franchises."

},

"GetReviews": {

"httpMethod": "GET",

"uri": "v1/review",

"summary": "Returns an array of reviews."

}

}

}

How can I access the 'uri' value in GetFranchiseList so that I can use it to create $authstring?

网友答案:

The client has a service description object that contains operations. The operations contain getter methods for the various properties as well as each parameter used as input.

For example:

$client->getDescription()->getOperation('GetFranchiseList')->getUri();
分享给朋友:
您可能感兴趣的文章:
随机阅读: