Exemple #1
0
 /**
  * Returns a Signing instance for a Cloud.
  *
  * @param string  $cloudId The cloud id
  * @param Account $account The authorization details
  *
  * @return PandaSigner The generated Signing instance
  */
 public static function getInstance($cloudId, Account $account)
 {
     $signer = new PandaSigner();
     $signer->setCloudId($cloudId);
     $signer->setAccount($account);
     return $signer;
 }
 public function testSignatureWithoutAccessData()
 {
     $params = array('timestamp' => '2011-03-01T15:39:10.260762Z');
     $signature = $this->signer->signature('GET', '/videos.json', $params);
     $this->assertEquals('kVnZs/NX13ldKPdhFYoVnoclr8075DwiZF0TGgIbMsc=', $signature);
 }
Exemple #3
0
 /**
  * Send signed HTTP requests to the API server.
  *
  * @param string $method HTTP method (GET, POST, PUT or DELETE)
  * @param string $path   Request path
  * @param array  $params Additional request parameters
  *
  * @return string The API server's response
  *
  * @throws ApiException  if an API error occurs
  * @throws HttpException if the request fails
  */
 private function request($method, $path, array $params)
 {
     // sign the request parameters
     $signer = PandaSigner::getInstance($this->cloudId, $this->account);
     $params = $signer->signParams($method, $path, $params);
     // ensure to use relative paths
     if (0 === strpos($path, '/')) {
         $path = substr($path, 1);
     }
     // append request parameters to the URL
     if ('GET' === $method || 'DELETE' === $method) {
         $path .= '?' . http_build_query($params);
     }
     // prepare the request
     $request = null;
     switch ($method) {
         case 'GET':
             $request = $this->guzzleClient->get($path);
             break;
         case 'DELETE':
             $request = $this->guzzleClient->delete($path);
             break;
         case 'PUT':
             $request = $this->guzzleClient->put($path, null, $params);
             break;
         case 'POST':
             $request = $this->guzzleClient->post($path, null, $params);
             break;
     }
     // and execute it
     try {
         $response = $request->send();
     } catch (\Exception $e) {
         // throw an exception if the http request failed
         throw new HttpException($e->getMessage(), $e->getCode());
     }
     // throw an API exception if the API response is not valid
     if ($response->getStatusCode() < 200 || $response->getStatusCode() > 207) {
         $decodedResponse = json_decode($response->getBody(true));
         $message = $decodedResponse->error . ': ' . $decodedResponse->message;
         throw new ApiException($message, $response->getStatusCode());
     }
     return $response->getBody(true);
 }