forked from loafle/openapi-generator-original
feature(PHP QA) add initial PHP client template tweaks to improve emitted code quality
This commit is contained in:
@@ -31,13 +31,13 @@ namespace {{invokerPackage}};
|
||||
*/
|
||||
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";
|
||||
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
|
||||
@@ -58,7 +58,7 @@ class ApiClient
|
||||
*
|
||||
* @param Configuration $config config for this ApiClient
|
||||
*/
|
||||
public function __construct(\{{invokerPackage}}\Configuration $config = null)
|
||||
public function __construct(Configuration $config = null)
|
||||
{
|
||||
if ($config === null) {
|
||||
$config = Configuration::getDefaultConfiguration();
|
||||
@@ -91,7 +91,7 @@ class ApiClient
|
||||
/**
|
||||
* Get API key (with prefix if set)
|
||||
*
|
||||
* @param string $apiKeyIdentifier name of apikey
|
||||
* @param string $apiKeyIdentifier name of API key
|
||||
*
|
||||
* @return string API key with the prefix
|
||||
*/
|
||||
@@ -100,14 +100,13 @@ class ApiClient
|
||||
$prefix = $this->config->getApiKeyPrefix($apiKeyIdentifier);
|
||||
$apiKey = $this->config->getApiKey($apiKeyIdentifier);
|
||||
|
||||
if (!isset($apiKey)) {
|
||||
if ($apiKey === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (isset($prefix)) {
|
||||
$keyWithPrefix = $prefix." ".$apiKey;
|
||||
} else {
|
||||
$keyWithPrefix = $apiKey;
|
||||
$keyWithPrefix = $apiKey;
|
||||
if ($prefix !== null) {
|
||||
$keyWithPrefix = $prefix.' '.$apiKey;
|
||||
}
|
||||
|
||||
return $keyWithPrefix;
|
||||
@@ -116,19 +115,24 @@ class ApiClient
|
||||
/**
|
||||
* 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
|
||||
* @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|string $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 \{{invokerPackage}}\ApiException on a non 2xx response
|
||||
* @throws ApiException on a non 2xx response
|
||||
* @return mixed
|
||||
*/
|
||||
public function callApi($resourcePath, $method, $queryParams, $postData, $headerParams, $responseType = null, $endpointPath = null)
|
||||
{
|
||||
public function callApi(
|
||||
$resourcePath,
|
||||
$method,
|
||||
array $queryParams,
|
||||
$postData,
|
||||
array $headerParams,
|
||||
$responseType = null
|
||||
) {
|
||||
$headers = [];
|
||||
|
||||
// construct the http header
|
||||
@@ -142,10 +146,10 @@ class ApiClient
|
||||
}
|
||||
|
||||
// form data
|
||||
if ($postData and in_array('Content-Type: application/x-www-form-urlencoded', $headers, true)) {
|
||||
if (is_array($postData) && 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));
|
||||
} elseif ((is_object($postData) || is_array($postData)) && !in_array('Content-Type: multipart/form-data', $headers, true)) { // json model
|
||||
$postData = json_encode(ObjectSerializer::sanitizeForSerialization($postData));
|
||||
}
|
||||
|
||||
$url = $this->config->getHost() . $resourcePath;
|
||||
@@ -161,12 +165,12 @@ class ApiClient
|
||||
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
|
||||
|
||||
// disable SSL verification, if needed
|
||||
if ($this->config->getSSLVerification() === false) {
|
||||
if ($this->config->isSSLVerification() === false) {
|
||||
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
|
||||
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
|
||||
}
|
||||
|
||||
if (!empty($queryParams)) {
|
||||
if ($queryParams !== null) {
|
||||
$url = ($url . '?' . http_build_query($queryParams));
|
||||
}
|
||||
|
||||
@@ -176,16 +180,16 @@ class ApiClient
|
||||
} elseif ($method === self::$HEAD) {
|
||||
curl_setopt($curl, CURLOPT_NOBODY, true);
|
||||
} elseif ($method === self::$OPTIONS) {
|
||||
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "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_CUSTOMREQUEST, 'PATCH');
|
||||
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
|
||||
} elseif ($method === self::$PUT) {
|
||||
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "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_CUSTOMREQUEST, 'DELETE');
|
||||
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
|
||||
} elseif ($method !== self::$GET) {
|
||||
throw new ApiException('Method ' . $method . ' is not recognized.');
|
||||
@@ -196,8 +200,12 @@ class ApiClient
|
||||
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());
|
||||
if ($this->config->isDebug()) {
|
||||
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'));
|
||||
@@ -210,55 +218,59 @@ class ApiClient
|
||||
|
||||
// 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);
|
||||
$httpHeaderSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
|
||||
$httpHeader = $this->httpParseHeaders(substr($response, 0, $httpHeaderSize));
|
||||
$httpBody = substr($response, $httpHeaderSize);
|
||||
$responseInfo = 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());
|
||||
if ($this->config->isDebug()) {
|
||||
error_log(
|
||||
'[DEBUG] HTTP Response body ~BEGIN~'.PHP_EOL.print_r($httpBody, 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);
|
||||
if ($responseInfo['http_code'] === 0) {
|
||||
$curlErrorMessage = 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";
|
||||
if ($curlErrorMessage !== '') {
|
||||
$errorMessage = 'API call to '.$url.' failed: '.$curlErrorMessage;
|
||||
} else {
|
||||
$error_message = "API call to $url failed, but for an unknown reason. " .
|
||||
"This could happen if you are disconnected from the network.";
|
||||
$errorMessage = '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);
|
||||
$exception = new ApiException($errorMessage, 0, null, null);
|
||||
$exception->setResponseObject($responseInfo);
|
||||
throw $exception;
|
||||
} elseif ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299) {
|
||||
} elseif ($responseInfo['http_code'] >= 200 && $responseInfo['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];
|
||||
return [$httpBody, $responseInfo['http_code'], $httpHeader];
|
||||
}
|
||||
|
||||
$data = json_decode($http_body);
|
||||
$data = json_decode($httpBody);
|
||||
if (json_last_error() > 0) { // if response is a string
|
||||
$data = $http_body;
|
||||
$data = $httpBody;
|
||||
}
|
||||
} else {
|
||||
$data = json_decode($http_body);
|
||||
$data = json_decode($httpBody);
|
||||
if (json_last_error() > 0) { // if response is a string
|
||||
$data = $http_body;
|
||||
$data = $httpBody;
|
||||
}
|
||||
|
||||
throw new ApiException(
|
||||
"[".$response_info['http_code']."] Error connecting to the API ($url)",
|
||||
$response_info['http_code'],
|
||||
$http_header,
|
||||
'['.$responseInfo['http_code'].'] Error connecting to the API ('.$url.')',
|
||||
$responseInfo['http_code'],
|
||||
$httpHeader,
|
||||
$data
|
||||
);
|
||||
}
|
||||
return [$data, $response_info['http_code'], $http_header];
|
||||
return [$data, $responseInfo['http_code'], $httpHeader];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -270,9 +282,9 @@ class ApiClient
|
||||
*/
|
||||
public function selectHeaderAccept($accept)
|
||||
{
|
||||
if (count($accept) === 0 or (count($accept) === 1 and $accept[0] === '')) {
|
||||
if (count($accept) === 0 || (count($accept) === 1 && $accept[0] === '')) {
|
||||
return null;
|
||||
} elseif (preg_grep("/application\/json/i", $accept)) {
|
||||
} elseif (preg_grep('/application\\/json/i', $accept)) {
|
||||
return 'application/json';
|
||||
} else {
|
||||
return implode(',', $accept);
|
||||
@@ -282,35 +294,35 @@ class ApiClient
|
||||
/**
|
||||
* Return the content type based on an array of content-type provided
|
||||
*
|
||||
* @param string[] $content_type Array fo content-type
|
||||
* @param string[] $contentType Array for Content-type
|
||||
*
|
||||
* @return string Content-Type (e.g. application/json)
|
||||
*/
|
||||
public function selectHeaderContentType($content_type)
|
||||
public function selectHeaderContentType($contentType)
|
||||
{
|
||||
if (count($content_type) === 0 or (count($content_type) === 1 and $content_type[0] === '')) {
|
||||
if (count($contentType) === 0 || (count($contentType) === 1 && $contentType[0] === '')) {
|
||||
return 'application/json';
|
||||
} elseif (preg_grep("/application\/json/i", $content_type)) {
|
||||
} elseif (preg_grep('/application\\/json/i', $contentType)) {
|
||||
return 'application/json';
|
||||
} else {
|
||||
return implode(',', $content_type);
|
||||
return implode(',', $contentType);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of HTTP response headers
|
||||
*
|
||||
* @param string $raw_headers A string of raw HTTP response headers
|
||||
* @param string $rawHeaders A string of raw HTTP response headers
|
||||
*
|
||||
* @return string[] Array of HTTP response heaers
|
||||
* @return string[] Array of HTTP response headers
|
||||
*/
|
||||
protected function httpParseHeaders($raw_headers)
|
||||
protected function httpParseHeaders($rawHeaders)
|
||||
{
|
||||
// ref/credit: http://php.net/manual/en/function.http-parse-headers.php#112986
|
||||
$headers = [];
|
||||
$key = '';
|
||||
|
||||
foreach (explode("\n", $raw_headers) as $h) {
|
||||
foreach (explode("\n", $rawHeaders) as $h) {
|
||||
$h = explode(':', $h, 2);
|
||||
|
||||
if (isset($h[1])) {
|
||||
@@ -324,7 +336,7 @@ class ApiClient
|
||||
|
||||
$key = $h[0];
|
||||
} else {
|
||||
if (substr($h[0], 0, 1) === "\t") {
|
||||
if (strpos($h[0], "\t") === 0) {
|
||||
$headers[$key] .= "\r\n\t".trim($h[0]);
|
||||
} elseif (!$key) {
|
||||
$headers[0] = trim($h[0]);
|
||||
|
||||
Reference in New Issue
Block a user