Example #1
0
 /**
  * Tests Configuration::setDebug
  */
 public function testDebug()
 {
     $conf = new Configuration();
     $conf->setFilename('/tmp/foo.xml');
     $this->assertEquals(false, $conf->getDebug());
     $conf->setDebug(true);
     $this->assertEquals(true, $conf->getDebug());
 }
Example #2
0
 /**
  * Make the HTTP call (Sync)
  *
  * @param string $resourcePath path to method endpoint
  * @param string $method       method to call
  * @param array  $queryParams  parameters to be place in query URL
  * @param array  $postData     parameters to be placed in POST body
  * @param array  $headerParams parameters to be place in request header
  * @param string $responseType expected response type of the endpoint
  * @param string $endpointPath path to method endpoint before expanding parameters
  *
  * @throws \Squiggle\ApiException on a non 2xx response
  * @return mixed
  */
 public function callApi($resourcePath, $method, $queryParams, $postData, $headerParams, $responseType = null, $endpointPath = null)
 {
     $headers = [];
     // construct the http header
     $headerParams = array_merge((array) $this->config->getDefaultHeaders(), (array) $headerParams);
     foreach ($headerParams as $key => $val) {
         $headers[] = "{$key}: {$val}";
     }
     // form data
     if ($postData and in_array('Content-Type: application/x-www-form-urlencoded', $headers, true)) {
         $postData = http_build_query($postData);
     } elseif ((is_object($postData) or is_array($postData)) and !in_array('Content-Type: multipart/form-data', $headers, true)) {
         // json model
         $postData = json_encode(\Squiggle\ObjectSerializer::sanitizeForSerialization($postData));
     }
     $url = $this->config->getHost() . $resourcePath;
     $curl = curl_init();
     // set timeout, if needed
     if ($this->config->getCurlTimeout() !== 0) {
         curl_setopt($curl, CURLOPT_TIMEOUT, $this->config->getCurlTimeout());
     }
     // return the result on success, rather than just true
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
     // disable SSL verification, if needed
     if ($this->config->getSSLVerification() === false) {
         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
     }
     if (!empty($queryParams)) {
         $url = $url . '?' . http_build_query($queryParams);
     }
     if ($method === self::$POST) {
         curl_setopt($curl, CURLOPT_POST, true);
         curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
     } elseif ($method === self::$HEAD) {
         curl_setopt($curl, CURLOPT_NOBODY, true);
     } elseif ($method === self::$OPTIONS) {
         curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "OPTIONS");
         curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
     } elseif ($method === self::$PATCH) {
         curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH");
         curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
     } elseif ($method === self::$PUT) {
         curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
         curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
     } elseif ($method === self::$DELETE) {
         curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
         curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
     } elseif ($method !== self::$GET) {
         throw new ApiException('Method ' . $method . ' is not recognized.');
     }
     curl_setopt($curl, CURLOPT_URL, $url);
     // Set user agent
     curl_setopt($curl, CURLOPT_USERAGENT, $this->config->getUserAgent());
     // debugging for curl
     if ($this->config->getDebug()) {
         error_log("[DEBUG] HTTP Request body  ~BEGIN~" . PHP_EOL . print_r($postData, true) . PHP_EOL . "~END~" . PHP_EOL, 3, $this->config->getDebugFile());
         curl_setopt($curl, CURLOPT_VERBOSE, 1);
         curl_setopt($curl, CURLOPT_STDERR, fopen($this->config->getDebugFile(), 'a'));
     } else {
         curl_setopt($curl, CURLOPT_VERBOSE, 0);
     }
     // obtain the HTTP response headers
     curl_setopt($curl, CURLOPT_HEADER, 1);
     // Make the request
     $response = curl_exec($curl);
     $http_header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
     $http_header = $this->httpParseHeaders(substr($response, 0, $http_header_size));
     $http_body = substr($response, $http_header_size);
     $response_info = curl_getinfo($curl);
     // debug HTTP response body
     if ($this->config->getDebug()) {
         error_log("[DEBUG] HTTP Response body ~BEGIN~" . PHP_EOL . print_r($http_body, true) . PHP_EOL . "~END~" . PHP_EOL, 3, $this->config->getDebugFile());
     }
     // Handle the response
     if ($response_info['http_code'] === 0) {
         $curl_error_message = curl_error($curl);
         // curl_exec can sometimes fail but still return a blank message from curl_error().
         if (!empty($curl_error_message)) {
             $error_message = "API call to {$url} failed: {$curl_error_message}";
         } else {
             $error_message = "API call to {$url} failed, but for an unknown reason. " . "This could happen if you are disconnected from the network.";
         }
         $exception = new ApiException($error_message, 0, null, null);
         $exception->setResponseObject($response_info);
         throw $exception;
     } elseif ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299) {
         // return raw body if response is a file
         if ($responseType === '\\SplFileObject' || $responseType === 'string') {
             return [$http_body, $response_info['http_code'], $http_header];
         }
         $data = json_decode($http_body);
         if (json_last_error() > 0) {
             // if response is a string
             $data = $http_body;
         }
     } else {
         $data = json_decode($http_body);
         if (json_last_error() > 0) {
             // if response is a string
             $data = $http_body;
         }
         throw new ApiException("[" . $response_info['http_code'] . "] Error connecting to the API ({$url})", $response_info['http_code'], $http_header, $data);
     }
     if (is_object($data) && property_exists($data, 'data')) {
         $data = $data->data;
     }
     return [$data, $response_info['http_code'], $http_header];
 }
 /**
  * Make the HTTP call (Sync)
  * @param string $resourcePath path to method endpoint
  * @param string $method       method to call
  * @param array  $queryParams  parameters to be place in query URL
  * @param array  $postData     parameters to be placed in POST body
  * @param array  $headerParams parameters to be place in request header
  * @param string $responseType expected response type of the endpoint
  * @throws \Swagger\Client\ApiException on a non 2xx response
  * @return mixed
  */
 public function callApi($resourcePath, $method, $queryParams, $postData, $headerParams, $responseType = null)
 {
     $headers = array();
     // construct the http header
     $headerParams = array_merge((array) $this->config->getDefaultHeaders(), (array) $headerParams);
     foreach ($headerParams as $key => $val) {
         $headers[] = "{$key}: {$val}";
     }
     // form data
     if ($postData and in_array('Content-Type: application/x-www-form-urlencoded', $headers)) {
         $postData = http_build_query($postData);
     } else {
         if ((is_object($postData) or is_array($postData)) and !in_array('Content-Type: multipart/form-data', $headers)) {
             // json model
             $postData = json_encode($this->serializer->sanitizeForSerialization($postData));
         }
     }
     $url = $this->config->getHost() . $resourcePath;
     $curl = curl_init();
     // set timeout, if needed
     if ($this->config->getCurlTimeout() != 0) {
         curl_setopt($curl, CURLOPT_TIMEOUT, $this->config->getCurlTimeout());
     }
     // return the result on success, rather than just TRUE
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
     if (!empty($queryParams)) {
         $url = $url . '?' . http_build_query($queryParams);
     }
     if ($method == self::$POST) {
         curl_setopt($curl, CURLOPT_POST, true);
         curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
     } else {
         if ($method == self::$HEAD) {
             curl_setopt($curl, CURLOPT_NOBODY, true);
         } else {
             if ($method == self::$OPTIONS) {
                 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "OPTIONS");
                 curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
             } else {
                 if ($method == self::$PATCH) {
                     curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH");
                     curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
                 } else {
                     if ($method == self::$PUT) {
                         curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
                         curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
                     } else {
                         if ($method == self::$DELETE) {
                             curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
                             curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
                         } else {
                             if ($method != self::$GET) {
                                 throw new ApiException('Method ' . $method . ' is not recognized.');
                             }
                         }
                     }
                 }
             }
         }
     }
     curl_setopt($curl, CURLOPT_URL, $url);
     // Set user agent
     curl_setopt($curl, CURLOPT_USERAGENT, $this->config->getUserAgent());
     // debugging for curl
     if ($this->config->getDebug()) {
         error_log("[DEBUG] HTTP Request body  ~BEGIN~\n" . print_r($postData, true) . "\n~END~\n", 3, $this->config->getDebugFile());
         curl_setopt($curl, CURLOPT_VERBOSE, 1);
         curl_setopt($curl, CURLOPT_STDERR, fopen($this->config->getDebugFile(), 'a'));
     } else {
         curl_setopt($curl, CURLOPT_VERBOSE, 0);
     }
     // obtain the HTTP response headers
     curl_setopt($curl, CURLOPT_HEADER, 1);
     // Make the request
     $response = curl_exec($curl);
     $http_header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
     $http_header = substr($response, 0, $http_header_size);
     $http_body = substr($response, $http_header_size);
     $response_info = curl_getinfo($curl);
     // debug HTTP response body
     if ($this->config->getDebug()) {
         error_log("[DEBUG] HTTP Response body ~BEGIN~\n" . print_r($http_body, true) . "\n~END~\n", 3, $this->config->getDebugFile());
     }
     // Handle the response
     if ($response_info['http_code'] == 0) {
         throw new ApiException("API call to {$url} timed out: " . serialize($response_info), 0, null, null);
     } else {
         if ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299) {
             // return raw body if response is a file
             if ($responseType == '\\SplFileObject') {
                 return array($http_body, $http_header);
             }
             $data = json_decode($http_body);
             if (json_last_error() > 0) {
                 // if response is a string
                 $data = $http_body;
             }
         } else {
             throw new ApiException("[" . $response_info['http_code'] . "] Error connecting to the API ({$url})", $response_info['http_code'], $http_header, $http_body);
         }
     }
     return array($data, $http_header);
 }