2013-10-24 102 views
1

我正在使用服務描述創建一個Guzzle客戶端。服務描述中的每個操作都包含一個URI。我正在訪問的REST端點需要一個授權標頭,它通過將公鑰和端點的uri粘在一起,然後從結果字符串中創建一個md5。這用作授權值。如何在實例化客戶端時訪問Guzzle服務描述中的值?

我不知道如何在實例化客戶端後從服務描述中獲取uri值。

我創建了狂飲客戶是這樣的:

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?

回答

3

客戶端有一個包含操作的服務描述對象。這些操作包含各種屬性的getter方法以及用作輸入的每個參數。

例如:

$client->getDescription()->getOperation('GetFranchiseList')->getUri(); 
相關問題