forked from loafle/openapi-generator-original
Delete ApiClient (#7305)
This commit is contained in:
parent
68a4552741
commit
fa9fc4b45b
@ -1,361 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* ApiClient
|
|
||||||
*
|
|
||||||
* PHP version 5
|
|
||||||
*
|
|
||||||
* @category Class
|
|
||||||
* @package {{invokerPackage}}
|
|
||||||
* @author Swagger Codegen team
|
|
||||||
* @link https://github.com/swagger-api/swagger-codegen
|
|
||||||
*/
|
|
||||||
|
|
||||||
{{>partial_header}}
|
|
||||||
/**
|
|
||||||
* NOTE: This class is auto generated by the swagger code generator program.
|
|
||||||
* https://github.com/swagger-api/swagger-codegen
|
|
||||||
* Do not edit the class manually.
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace {{invokerPackage}};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* ApiClient Class Doc Comment
|
|
||||||
*
|
|
||||||
* @category Class
|
|
||||||
* @package {{invokerPackage}}
|
|
||||||
* @author Swagger Codegen team
|
|
||||||
* @link https://github.com/swagger-api/swagger-codegen
|
|
||||||
*/
|
|
||||||
class ApiClient
|
|
||||||
{
|
|
||||||
public static $PATCH = "PATCH";
|
|
||||||
public static $POST = "POST";
|
|
||||||
public static $GET = "GET";
|
|
||||||
public static $HEAD = "HEAD";
|
|
||||||
public static $OPTIONS = "OPTIONS";
|
|
||||||
public static $PUT = "PUT";
|
|
||||||
public static $DELETE = "DELETE";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Configuration
|
|
||||||
*
|
|
||||||
* @var Configuration
|
|
||||||
*/
|
|
||||||
protected $config;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Object Serializer
|
|
||||||
*
|
|
||||||
* @var ObjectSerializer
|
|
||||||
*/
|
|
||||||
protected $serializer;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor of the class
|
|
||||||
*
|
|
||||||
* @param Configuration $config config for this ApiClient
|
|
||||||
*/
|
|
||||||
public function __construct(\{{invokerPackage}}\Configuration $config = null)
|
|
||||||
{
|
|
||||||
if ($config === null) {
|
|
||||||
$config = Configuration::getDefaultConfiguration();
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->config = $config;
|
|
||||||
$this->serializer = new ObjectSerializer();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the config
|
|
||||||
*
|
|
||||||
* @return Configuration
|
|
||||||
*/
|
|
||||||
public function getConfig()
|
|
||||||
{
|
|
||||||
return $this->config;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the serializer
|
|
||||||
*
|
|
||||||
* @return ObjectSerializer
|
|
||||||
*/
|
|
||||||
public function getSerializer()
|
|
||||||
{
|
|
||||||
return $this->serializer;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get API key (with prefix if set)
|
|
||||||
*
|
|
||||||
* @param string $apiKeyIdentifier name of apikey
|
|
||||||
*
|
|
||||||
* @return string API key with the prefix
|
|
||||||
*/
|
|
||||||
public function getApiKeyWithPrefix($apiKeyIdentifier)
|
|
||||||
{
|
|
||||||
$prefix = $this->config->getApiKeyPrefix($apiKeyIdentifier);
|
|
||||||
$apiKey = $this->config->getApiKey($apiKeyIdentifier);
|
|
||||||
|
|
||||||
if (!isset($apiKey)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isset($prefix)) {
|
|
||||||
$keyWithPrefix = $prefix." ".$apiKey;
|
|
||||||
} else {
|
|
||||||
$keyWithPrefix = $apiKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $keyWithPrefix;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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 \{{invokerPackage}}\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(\{{invokerPackage}}\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());
|
|
||||||
}
|
|
||||||
// set connect timeout, if needed
|
|
||||||
if ($this->config->getCurlConnectTimeout() != 0) {
|
|
||||||
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $this->config->getCurlConnectTimeout());
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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 ($this->config->getCurlProxyHost()) {
|
|
||||||
curl_setopt($curl, CURLOPT_PROXY, $this->config->getCurlProxyHost());
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->config->getCurlProxyPort()) {
|
|
||||||
curl_setopt($curl, CURLOPT_PROXYPORT, $this->config->getCurlProxyPort());
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->config->getCurlProxyType()) {
|
|
||||||
curl_setopt($curl, CURLOPT_PROXYTYPE, $this->config->getCurlProxyType());
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->config->getCurlProxyUser()) {
|
|
||||||
curl_setopt($curl, CURLOPT_PROXYUSERPWD, $this->config->getCurlProxyUser() . ':' .$this->config->getCurlProxyPassword());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!empty($queryParams)) {
|
|
||||||
$url = ($url . '?' . http_build_query($queryParams));
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->config->getAllowEncoding()) {
|
|
||||||
curl_setopt($curl, CURLOPT_ENCODING, '');
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return [$data, $response_info['http_code'], $http_header];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the header 'Accept' based on an array of Accept provided
|
|
||||||
*
|
|
||||||
* @param string[] $accept Array of header
|
|
||||||
*
|
|
||||||
* @return string Accept (e.g. application/json)
|
|
||||||
*/
|
|
||||||
public function selectHeaderAccept($accept)
|
|
||||||
{
|
|
||||||
if (count($accept) === 0 or (count($accept) === 1 and $accept[0] === '')) {
|
|
||||||
return null;
|
|
||||||
} elseif (preg_grep("/application\/json/i", $accept)) {
|
|
||||||
return 'application/json';
|
|
||||||
} else {
|
|
||||||
return implode(',', $accept);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the content type based on an array of content-type provided
|
|
||||||
*
|
|
||||||
* @param string[] $content_type Array fo content-type
|
|
||||||
*
|
|
||||||
* @return string Content-Type (e.g. application/json)
|
|
||||||
*/
|
|
||||||
public function selectHeaderContentType($content_type)
|
|
||||||
{
|
|
||||||
if (count($content_type) === 0 or (count($content_type) === 1 and $content_type[0] === '')) {
|
|
||||||
return 'application/json';
|
|
||||||
} elseif (preg_grep("/application\/json/i", $content_type)) {
|
|
||||||
return 'application/json';
|
|
||||||
} else {
|
|
||||||
return implode(',', $content_type);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return an array of HTTP response headers
|
|
||||||
*
|
|
||||||
* @param string $raw_headers A string of raw HTTP response headers
|
|
||||||
*
|
|
||||||
* @return string[] Array of HTTP response heaers
|
|
||||||
*/
|
|
||||||
protected function httpParseHeaders($raw_headers)
|
|
||||||
{
|
|
||||||
// ref/credit: http://php.net/manual/en/function.http-parse-headers.php#112986
|
|
||||||
$headers = [];
|
|
||||||
$key = '';
|
|
||||||
|
|
||||||
foreach (explode("\n", $raw_headers) as $h) {
|
|
||||||
$h = explode(':', $h, 2);
|
|
||||||
|
|
||||||
if (isset($h[1])) {
|
|
||||||
if (!isset($headers[$h[0]])) {
|
|
||||||
$headers[$h[0]] = trim($h[1]);
|
|
||||||
} elseif (is_array($headers[$h[0]])) {
|
|
||||||
$headers[$h[0]] = array_merge($headers[$h[0]], [trim($h[1])]);
|
|
||||||
} else {
|
|
||||||
$headers[$h[0]] = array_merge([$headers[$h[0]]], [trim($h[1])]);
|
|
||||||
}
|
|
||||||
|
|
||||||
$key = $h[0];
|
|
||||||
} else {
|
|
||||||
if (substr($h[0], 0, 1) === "\t") {
|
|
||||||
$headers[$key] .= "\r\n\t".trim($h[0]);
|
|
||||||
} elseif (!$key) {
|
|
||||||
$headers[0] = trim($h[0]);
|
|
||||||
}
|
|
||||||
trim($h[0]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $headers;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,371 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* ApiClient
|
|
||||||
*
|
|
||||||
* PHP version 5
|
|
||||||
*
|
|
||||||
* @category Class
|
|
||||||
* @package Swagger\Client
|
|
||||||
* @author Swagger Codegen team
|
|
||||||
* @link https://github.com/swagger-api/swagger-codegen
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Swagger Petstore
|
|
||||||
*
|
|
||||||
* This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters.
|
|
||||||
*
|
|
||||||
* OpenAPI spec version: 1.0.0
|
|
||||||
* Contact: apiteam@swagger.io
|
|
||||||
* Generated by: https://github.com/swagger-api/swagger-codegen.git
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* NOTE: This class is auto generated by the swagger code generator program.
|
|
||||||
* https://github.com/swagger-api/swagger-codegen
|
|
||||||
* Do not edit the class manually.
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace Swagger\Client;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* ApiClient Class Doc Comment
|
|
||||||
*
|
|
||||||
* @category Class
|
|
||||||
* @package Swagger\Client
|
|
||||||
* @author Swagger Codegen team
|
|
||||||
* @link https://github.com/swagger-api/swagger-codegen
|
|
||||||
*/
|
|
||||||
class ApiClient
|
|
||||||
{
|
|
||||||
public static $PATCH = "PATCH";
|
|
||||||
public static $POST = "POST";
|
|
||||||
public static $GET = "GET";
|
|
||||||
public static $HEAD = "HEAD";
|
|
||||||
public static $OPTIONS = "OPTIONS";
|
|
||||||
public static $PUT = "PUT";
|
|
||||||
public static $DELETE = "DELETE";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Configuration
|
|
||||||
*
|
|
||||||
* @var Configuration
|
|
||||||
*/
|
|
||||||
protected $config;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Object Serializer
|
|
||||||
*
|
|
||||||
* @var ObjectSerializer
|
|
||||||
*/
|
|
||||||
protected $serializer;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor of the class
|
|
||||||
*
|
|
||||||
* @param Configuration $config config for this ApiClient
|
|
||||||
*/
|
|
||||||
public function __construct(\Swagger\Client\Configuration $config = null)
|
|
||||||
{
|
|
||||||
if ($config === null) {
|
|
||||||
$config = Configuration::getDefaultConfiguration();
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->config = $config;
|
|
||||||
$this->serializer = new ObjectSerializer();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the config
|
|
||||||
*
|
|
||||||
* @return Configuration
|
|
||||||
*/
|
|
||||||
public function getConfig()
|
|
||||||
{
|
|
||||||
return $this->config;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the serializer
|
|
||||||
*
|
|
||||||
* @return ObjectSerializer
|
|
||||||
*/
|
|
||||||
public function getSerializer()
|
|
||||||
{
|
|
||||||
return $this->serializer;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get API key (with prefix if set)
|
|
||||||
*
|
|
||||||
* @param string $apiKeyIdentifier name of apikey
|
|
||||||
*
|
|
||||||
* @return string API key with the prefix
|
|
||||||
*/
|
|
||||||
public function getApiKeyWithPrefix($apiKeyIdentifier)
|
|
||||||
{
|
|
||||||
$prefix = $this->config->getApiKeyPrefix($apiKeyIdentifier);
|
|
||||||
$apiKey = $this->config->getApiKey($apiKeyIdentifier);
|
|
||||||
|
|
||||||
if (!isset($apiKey)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isset($prefix)) {
|
|
||||||
$keyWithPrefix = $prefix." ".$apiKey;
|
|
||||||
} else {
|
|
||||||
$keyWithPrefix = $apiKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $keyWithPrefix;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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 \Swagger\Client\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(\Swagger\Client\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());
|
|
||||||
}
|
|
||||||
// set connect timeout, if needed
|
|
||||||
if ($this->config->getCurlConnectTimeout() != 0) {
|
|
||||||
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $this->config->getCurlConnectTimeout());
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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 ($this->config->getCurlProxyHost()) {
|
|
||||||
curl_setopt($curl, CURLOPT_PROXY, $this->config->getCurlProxyHost());
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->config->getCurlProxyPort()) {
|
|
||||||
curl_setopt($curl, CURLOPT_PROXYPORT, $this->config->getCurlProxyPort());
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->config->getCurlProxyType()) {
|
|
||||||
curl_setopt($curl, CURLOPT_PROXYTYPE, $this->config->getCurlProxyType());
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->config->getCurlProxyUser()) {
|
|
||||||
curl_setopt($curl, CURLOPT_PROXYUSERPWD, $this->config->getCurlProxyUser() . ':' .$this->config->getCurlProxyPassword());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!empty($queryParams)) {
|
|
||||||
$url = ($url . '?' . http_build_query($queryParams));
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->config->getAllowEncoding()) {
|
|
||||||
curl_setopt($curl, CURLOPT_ENCODING, '');
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return [$data, $response_info['http_code'], $http_header];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the header 'Accept' based on an array of Accept provided
|
|
||||||
*
|
|
||||||
* @param string[] $accept Array of header
|
|
||||||
*
|
|
||||||
* @return string Accept (e.g. application/json)
|
|
||||||
*/
|
|
||||||
public function selectHeaderAccept($accept)
|
|
||||||
{
|
|
||||||
if (count($accept) === 0 or (count($accept) === 1 and $accept[0] === '')) {
|
|
||||||
return null;
|
|
||||||
} elseif (preg_grep("/application\/json/i", $accept)) {
|
|
||||||
return 'application/json';
|
|
||||||
} else {
|
|
||||||
return implode(',', $accept);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the content type based on an array of content-type provided
|
|
||||||
*
|
|
||||||
* @param string[] $content_type Array fo content-type
|
|
||||||
*
|
|
||||||
* @return string Content-Type (e.g. application/json)
|
|
||||||
*/
|
|
||||||
public function selectHeaderContentType($content_type)
|
|
||||||
{
|
|
||||||
if (count($content_type) === 0 or (count($content_type) === 1 and $content_type[0] === '')) {
|
|
||||||
return 'application/json';
|
|
||||||
} elseif (preg_grep("/application\/json/i", $content_type)) {
|
|
||||||
return 'application/json';
|
|
||||||
} else {
|
|
||||||
return implode(',', $content_type);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return an array of HTTP response headers
|
|
||||||
*
|
|
||||||
* @param string $raw_headers A string of raw HTTP response headers
|
|
||||||
*
|
|
||||||
* @return string[] Array of HTTP response heaers
|
|
||||||
*/
|
|
||||||
protected function httpParseHeaders($raw_headers)
|
|
||||||
{
|
|
||||||
// ref/credit: http://php.net/manual/en/function.http-parse-headers.php#112986
|
|
||||||
$headers = [];
|
|
||||||
$key = '';
|
|
||||||
|
|
||||||
foreach (explode("\n", $raw_headers) as $h) {
|
|
||||||
$h = explode(':', $h, 2);
|
|
||||||
|
|
||||||
if (isset($h[1])) {
|
|
||||||
if (!isset($headers[$h[0]])) {
|
|
||||||
$headers[$h[0]] = trim($h[1]);
|
|
||||||
} elseif (is_array($headers[$h[0]])) {
|
|
||||||
$headers[$h[0]] = array_merge($headers[$h[0]], [trim($h[1])]);
|
|
||||||
} else {
|
|
||||||
$headers[$h[0]] = array_merge([$headers[$h[0]]], [trim($h[1])]);
|
|
||||||
}
|
|
||||||
|
|
||||||
$key = $h[0];
|
|
||||||
} else {
|
|
||||||
if (substr($h[0], 0, 1) === "\t") {
|
|
||||||
$headers[$key] .= "\r\n\t".trim($h[0]);
|
|
||||||
} elseif (!$key) {
|
|
||||||
$headers[0] = trim($h[0]);
|
|
||||||
}
|
|
||||||
trim($h[0]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $headers;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,126 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* ApiClientTest
|
|
||||||
* PHP version 5
|
|
||||||
*
|
|
||||||
* @category Class
|
|
||||||
* @package Swagger\Client
|
|
||||||
* @author http://github.com/swagger-api/swagger-codegen
|
|
||||||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License v2
|
|
||||||
* @link https://github.com/swagger-api/swagger-codegen
|
|
||||||
*/
|
|
||||||
/**
|
|
||||||
* Copyright 2016 SmartBear Software
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* NOTE: This class is auto generated by the swagger code generator program.
|
|
||||||
* https://github.com/swagger-api/swagger-codegen
|
|
||||||
* Please update the test case below to test the endpoint.
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace Swagger\Client;
|
|
||||||
|
|
||||||
use \Swagger\Client\Configuration;
|
|
||||||
use \Swagger\Client\ApiClient;
|
|
||||||
use \Swagger\Client\ApiException;
|
|
||||||
use \Swagger\Client\ObjectSerializer;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* ApiClientTest Class Doc Comment
|
|
||||||
*
|
|
||||||
* @category Class
|
|
||||||
* @package Swagger\Client
|
|
||||||
* @author http://github.com/swagger-api/swagger-codegen
|
|
||||||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License v2
|
|
||||||
* @link https://github.com/swagger-api/swagger-codegen
|
|
||||||
*/
|
|
||||||
class ApiClientTest extends \PHPUnit_Framework_TestCase
|
|
||||||
{
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Setup before running each test case
|
|
||||||
*/
|
|
||||||
public static function setUpBeforeClass()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Clean up after running each test case
|
|
||||||
*/
|
|
||||||
public static function tearDownAfterClass()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// test static functions defined in ApiClient
|
|
||||||
public function testApiClient()
|
|
||||||
{
|
|
||||||
// test selectHeaderAccept
|
|
||||||
$api_client = new ApiClient();
|
|
||||||
$this->assertSame('application/json', $api_client->selectHeaderAccept(array(
|
|
||||||
'application/xml',
|
|
||||||
'application/json'
|
|
||||||
)));
|
|
||||||
$this->assertSame(null, $api_client->selectHeaderAccept(array()));
|
|
||||||
$this->assertSame('application/yaml,application/xml', $api_client->selectHeaderAccept(array(
|
|
||||||
'application/yaml',
|
|
||||||
'application/xml'
|
|
||||||
)));
|
|
||||||
|
|
||||||
// test selectHeaderContentType
|
|
||||||
$this->assertSame('application/json', $api_client->selectHeaderContentType(array(
|
|
||||||
'application/xml',
|
|
||||||
'application/json'
|
|
||||||
)));
|
|
||||||
$this->assertSame('application/json', $api_client->selectHeaderContentType(array()));
|
|
||||||
$this->assertSame('application/yaml,application/xml', $api_client->selectHeaderContentType(array(
|
|
||||||
'application/yaml',
|
|
||||||
'application/xml'
|
|
||||||
)));
|
|
||||||
|
|
||||||
// test addDefaultHeader and getDefaultHeader
|
|
||||||
$api_client->getConfig()->addDefaultHeader('test1', 'value1');
|
|
||||||
$api_client->getConfig()->addDefaultHeader('test2', 200);
|
|
||||||
$defaultHeader = $api_client->getConfig()->getDefaultHeaders();
|
|
||||||
$this->assertSame('value1', $defaultHeader['test1']);
|
|
||||||
$this->assertSame(200, $defaultHeader['test2']);
|
|
||||||
|
|
||||||
// test deleteDefaultHeader
|
|
||||||
$api_client->getConfig()->deleteDefaultHeader('test2');
|
|
||||||
$defaultHeader = $api_client->getConfig()->getDefaultHeaders();
|
|
||||||
$this->assertFalse(isset($defaultHeader['test2']));
|
|
||||||
|
|
||||||
$pet_api2 = new Api\PetApi();
|
|
||||||
$config3 = new Configuration();
|
|
||||||
$apiClient3 = new ApiClient($config3);
|
|
||||||
$apiClient3->getConfig()->setUserAgent('api client 3');
|
|
||||||
$config4 = new Configuration();
|
|
||||||
$apiClient4 = new ApiClient($config4);
|
|
||||||
$apiClient4->getConfig()->setUserAgent('api client 4');
|
|
||||||
$pet_api3 = new Api\PetApi($apiClient3);
|
|
||||||
|
|
||||||
// 2 different api clients are not the same
|
|
||||||
$this->assertNotEquals($apiClient3, $apiClient4);
|
|
||||||
// customied pet api not using the old pet api's api client
|
|
||||||
$this->assertNotEquals($pet_api2->getApiClient(), $pet_api3->getApiClient());
|
|
||||||
|
|
||||||
// test access token
|
|
||||||
$api_client->getConfig()->setAccessToken("testing_only");
|
|
||||||
$this->assertSame('testing_only', $api_client->getConfig()->getAccessToken());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user