Exemplo n.º 1
0
 protected function setUp()
 {
     $cloudId = '123456789';
     $account = new Account('abcdefgh', 'ijklmnop', 'api.pandastream.com');
     $this->signer = PandaSigner::getInstance($cloudId, $account);
 }
Exemplo n.º 2
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);
 }