实例化客户端时,如何访问 Guzzle 服务描述中的值


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

我正在使用服务描述创建一个Guzzle客户端。服务说明中的每个操作都包含一个 URI。我正在访问的 REST 端点需要一个授权标头,该标头是通过将公钥和端点的 uri 粘在一起,然后从生成的字符串创建 md5 来创建的。这用作授权值。

我不知道如何在实例化客户端后从服务描述中获取 uri 值。

我正在像这样创建 Guzzle 客户端:

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."
        }
    }
}

如何访问 GetFranchiseList 中的"uri"值,以便使用它来创建$authstring?

客户端具有包含操作的服务描述对象。这些操作包含各种属性的 getter 方法以及用作输入的每个参数。

例如:

$client->getDescription()->getOperation('GetFranchiseList')->getUri();