Example #1
0
 /**
  * {@inheritdoc}
  * @see \Scalr\Service\Azure\Client\ClientInterface::prepareRequest()
  */
 public function prepareRequest($path, $method, $apiVersion, $baseUrl = Azure::URL_MANAGEMENT_WINDOWS, $queryData = [], $postFields = [], $headers = [])
 {
     if (!in_array($method, static::$httpMethods)) {
         throw new InvalidArgumentException(sprintf('Http method %s not supported or not exists.', $method));
     }
     try {
         $url = $baseUrl . $path;
         $parts = parse_url($url);
         if (isset($parts['query'])) {
             parse_str($parts['query'], $query);
         }
         if (!isset($query['api-version'])) {
             $queryData['api-version'] = $apiVersion;
         }
         $request = new Request($method, $url);
         $proxySettings = $this->azure->getProxy();
         if ($proxySettings !== false) {
             $request->setOptions(['proxyhost' => $proxySettings['host'], 'proxyport' => $proxySettings['port'], 'proxytype' => $proxySettings['type']]);
             if ($proxySettings['user']) {
                 $request->setOptions(['proxyauth' => "{$proxySettings['user']}:{$proxySettings['pass']}", 'proxyauthtype' => $proxySettings['authtype']]);
             }
         }
         $request->addQuery($queryData);
         if ($baseUrl === Azure::URL_MANAGEMENT_WINDOWS) {
             $headers['Content-Type'] = 'application/json';
         }
         if (empty($headers['Authorization']) && $baseUrl === Azure::URL_MANAGEMENT_WINDOWS) {
             $headers['Authorization'] = 'Bearer ' . $this->azure->getClientToken(Azure::URL_MANAGEMENT_WINDOWS)->token;
         }
         if (count($postFields)) {
             $request->append(json_encode($postFields));
         } else {
             if ($method == 'POST' && !isset($headers['Content-Length'])) {
                 // pecl_http does not include Content-Length for empty posts what breaks integration with Azure.
                 $headers['Content-Length'] = 1;
                 $request->append(" ");
             } else {
                 if (in_array($method, ['PUT', 'PATCH']) && !isset($headers['Content-Length'])) {
                     // pecl_http does not include Content-Length for empty posts what breaks integration with Azure.
                     $headers['Content-Length'] = 0;
                 }
             }
         }
         if (count($headers)) {
             $request->addHeaders($headers);
         }
     } catch (Exception $e) {
         throw new AzureException($e->getMessage());
     }
     return $request;
 }
Example #2
0
 /**
  * Makes api request
  *
  * @param   string     $qid     The id of the query.
  * @param   array      $options optional Query options for the request.
  * @param   string     $path    optional Uri path for the request (/user by default)
  * @param   string     $method  optional Http method (GET by default)
  * @return  object     Returns object that is an response data.
  * @throws  CloudynException
  */
 public function call($qid, array $options = array(), $path = '/user', $method = 'GET')
 {
     $options['qid'] = (string) $qid;
     $options['out'] = self::OUT_JSON;
     if (!isset($options['rqid'])) {
         $options['rqid'] = $this->getRequestId();
     }
     if (!isset($options['apiversion'])) {
         $options['apiversion'] = '0.4';
     }
     $this->request = $this->createNewRequest();
     $this->request->setRequestUrl($this->getUrl() . $path);
     $this->request->setRequestMethod($method);
     $this->request->setOptions(array('redirect' => 10, 'cookiesession' => true));
     $this->request->addQuery($options);
     $this->response = $this->tryCall($this->request);
     $json = $this->response->getBody()->toString();
     $json = preg_replace('#^[^\\{\\[]+|[^\\}\\]]+$#', '', trim($json));
     $obj = json_decode($json);
     if (isset($obj->status) && $obj->status != 'ok' && isset($obj->message)) {
         throw new CloudynException('Cloudyn error. ' . $obj->message);
     }
     return $obj;
 }
Example #3
0
 /**
  * Sends HTTP request with specified params.
  *
  * @param   string   $path       optional Path URL part
  * @param   string   $body       optional Raw request POST content
  * @param   array    $queryData  optional Associative array of query-string params
  * @param   array    $headers    optional Associative array of request headers
  * @param   string   $method     optional Request method
  * @param   array    $options    optional Request options
  * @param   array    $postFields optional Associative array of post-fields
  * @return  \http\Client\Response
  */
 protected function sendRequest($path = '/', $body = '', $queryData = [], $headers = [], $method = "POST", $options = [], $postFields = [])
 {
     $request = new Request($method, $this->scheme . '://' . $this->host . ':' . $this->port . $path);
     $request->setOptions(array_merge(['timeout' => $this->timeout, 'protocol' => '1.1'], $options));
     if (!empty($queryData)) {
         $request->addQuery($queryData);
     }
     if (!empty($body)) {
         $request->append($body);
     } else {
         if (!empty($postFields)) {
             $request->append($postFields);
         }
     }
     if (!empty($headers)) {
         $request->addHeaders(array_merge(["Accept" => "*/*", "Content-Type" => "application/json; charset=utf-8", "Host" => $this->host . ':' . $this->port, "Connection" => "close"], $headers));
     }
     return \Scalr::getContainer()->http->sendRequest($request);
 }