mirror of
https://github.com/valitydev/openapi-generator.git
synced 2024-11-08 19:33:55 +00:00
remove static fields in Configuration and make variable case more consistent
This commit is contained in:
parent
caa1b7f411
commit
b9ca19168a
@ -29,40 +29,46 @@ class ApiClient {
|
||||
public static $PUT = "PUT";
|
||||
public static $DELETE = "DELETE";
|
||||
|
||||
private static $default_header = array();
|
||||
protected $defaultHeaders = array();
|
||||
|
||||
/*
|
||||
* @var string timeout (second) of the HTTP request, by default set to 0, no timeout
|
||||
*/
|
||||
protected $curl_timeout = 0;
|
||||
protected $curlTimeout = 0;
|
||||
|
||||
/*
|
||||
* @var string user agent of the HTTP request, set to "PHP-Swagger" by default
|
||||
*/
|
||||
protected $user_agent = "PHP-Swagger";
|
||||
protected $userAgent = "PHP-Swagger";
|
||||
|
||||
/**
|
||||
* @param string $host Base url of the API server (optional)
|
||||
* @var ApiConfiguration
|
||||
*/
|
||||
function __construct($host = null) {
|
||||
if ($host === null) {
|
||||
$this->host = '{{basePath}}';
|
||||
} else {
|
||||
$this->host = $host;
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* @param ApiConfiguration $config config for this ApiClient
|
||||
*/
|
||||
function __construct(ApiConfiguration $config = null) {
|
||||
if ($config == null) {
|
||||
$config = ApiConfiguration::getDefaultConfiguration();
|
||||
}
|
||||
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* add default header
|
||||
*
|
||||
* @param string $header_name header name (e.g. Token)
|
||||
* @param string $header_value header value (e.g. 1z8wp3)
|
||||
* @param string $headerName header name (e.g. Token)
|
||||
* @param string $headerValue header value (e.g. 1z8wp3)
|
||||
*/
|
||||
public function addDefaultHeader($header_name, $header_value) {
|
||||
if (!is_string($header_name))
|
||||
public function addDefaultHeader($headerName, $headerValue) {
|
||||
if (!is_string($headerName)) {
|
||||
throw new \InvalidArgumentException('Header name must be a string.');
|
||||
}
|
||||
|
||||
self::$default_header[$header_name] = $header_value;
|
||||
$this->defaultHeaders[$headerName] = $headerValue;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -70,29 +76,29 @@ class ApiClient {
|
||||
*
|
||||
* @return array default header
|
||||
*/
|
||||
public function getDefaultHeader() {
|
||||
return self::$default_header;
|
||||
public function getDefaultHeaders() {
|
||||
return $this->defaultHeaders;
|
||||
}
|
||||
|
||||
/**
|
||||
* delete the default header based on header name
|
||||
*
|
||||
* @param string $header_name header name (e.g. Token)
|
||||
* @param string $headerName header name (e.g. Token)
|
||||
*/
|
||||
public function deleteDefaultHeader($header_name) {
|
||||
unset(self::$default_header[$header_name]);
|
||||
public function deleteDefaultHeader($headerName) {
|
||||
unset($this->defaultHeaders[$headerName]);
|
||||
}
|
||||
|
||||
/**
|
||||
* set the user agent of the api client
|
||||
*
|
||||
* @param string $user_agent the user agent of the api client
|
||||
* @param string $userAgent the user agent of the api client
|
||||
*/
|
||||
public function setUserAgent($user_agent) {
|
||||
if (!is_string($user_agent))
|
||||
public function setUserAgent($userAgent) {
|
||||
if (!is_string($userAgent))
|
||||
throw new \InvalidArgumentException('User-agent must be a string.');
|
||||
|
||||
$this->user_agent= $user_agent;
|
||||
$this->userAgent = $userAgent;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -100,8 +106,8 @@ class ApiClient {
|
||||
*
|
||||
* @return string user agent
|
||||
*/
|
||||
public function getUserAgent($user_agent) {
|
||||
return $this->user_agent;
|
||||
public function getUserAgent() {
|
||||
return $this->userAgent;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -113,7 +119,7 @@ class ApiClient {
|
||||
if (!is_numeric($seconds) || $seconds < 0)
|
||||
throw new \InvalidArgumentException('Timeout value must be numeric and a non-negative number.');
|
||||
|
||||
$this->curl_timeout = $seconds;
|
||||
$this->curlTimeout = $seconds;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -122,27 +128,34 @@ class ApiClient {
|
||||
* @return string HTTP timeout value
|
||||
*/
|
||||
public function getTimeout() {
|
||||
return $this->curl_timeout;
|
||||
return $this->curlTimeout;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get API key (with prefix if set)
|
||||
* @param string key name
|
||||
* @param string $apiKey name of apikey
|
||||
* @return string API key with the prefix
|
||||
*/
|
||||
public function getApiKeyWithPrefix($apiKey) {
|
||||
if (isset(Configuration::$apiKeyPrefix[$apiKey])) {
|
||||
return Configuration::$apiKeyPrefix[$apiKey]." ".Configuration::$apiKey[$apiKey];
|
||||
} else if (isset(Configuration::$apiKey[$apiKey])) {
|
||||
return Configuration::$apiKey[$apiKey];
|
||||
} else {
|
||||
return;
|
||||
$prefix = $this->config->getApiKeyPrefix($apiKey);
|
||||
$apiKey = $this->config->getApiKey($apiKey);
|
||||
|
||||
if (!isset($apiKey)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (isset($prefix)) {
|
||||
$keyWithPrefix = $prefix." ".$apiKey;
|
||||
} else {
|
||||
$keyWithPrefix = $apiKey;
|
||||
}
|
||||
|
||||
return $keyWithPrefix;
|
||||
}
|
||||
|
||||
/**
|
||||
* update hearder and query param based on authentication setting
|
||||
* update header and query param based on authentication setting
|
||||
*
|
||||
* @param array $headerParams header parameters (by ref)
|
||||
* @param array $queryParams query parameters (by ref)
|
||||
@ -159,7 +172,7 @@ class ApiClient {
|
||||
switch($auth) {
|
||||
{{#authMethods}}
|
||||
case '{{name}}':
|
||||
{{#isApiKey}}{{#isKeyInHeader}}$headerParams['{{keyParamName}}'] = $this->getApiKeyWithPrefix('{{keyParamName}}');{{/isKeyInHeader}}{{#isKeyInQuery}}$queryParams['{{keyParamName}}'] = $this->getApiKeyWithPrefix('{{keyParamName}}');{{/isKeyInQuery}}{{/isApiKey}}{{#isBasic}}$headerParams['Authorization'] = 'Basic '.base64_encode(Configuration::$username.":".Configuration::$password);{{/isBasic}}
|
||||
{{#isApiKey}}{{#isKeyInHeader}}$headerParams['{{keyParamName}}'] = $this->getApiKeyWithPrefix('{{keyParamName}}');{{/isKeyInHeader}}{{#isKeyInQuery}}$queryParams['{{keyParamName}}'] = $this->getApiKeyWithPrefix('{{keyParamName}}');{{/isKeyInQuery}}{{/isApiKey}}{{#isBasic}}$headerParams['Authorization'] = 'Basic '.base64_encode($this->config->getUsername().":".$this->config->getPassword());{{/isBasic}}
|
||||
{{#isOAuth}}//TODO support oauth{{/isOAuth}}
|
||||
break;
|
||||
{{/authMethods}}
|
||||
@ -175,6 +188,8 @@ class ApiClient {
|
||||
* @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 array $authSettings parameters for authentication
|
||||
* @throws \{{invokerNamespace}}\ApiException on a non 2xx response
|
||||
* @return mixed
|
||||
*/
|
||||
public function callApi($resourcePath, $method, $queryParams, $postData,
|
||||
@ -186,7 +201,7 @@ class ApiClient {
|
||||
$this->updateParamsForAuth($headerParams, $queryParams, $authSettings);
|
||||
|
||||
# construct the http header
|
||||
$headerParams = array_merge((array)self::$default_header, (array)$headerParams);
|
||||
$headerParams = array_merge((array)$this->defaultHeaders, (array)$headerParams);
|
||||
|
||||
foreach ($headerParams as $key => $val) {
|
||||
$headers[] = "$key: $val";
|
||||
@ -200,12 +215,12 @@ class ApiClient {
|
||||
$postData = json_encode($this->sanitizeForSerialization($postData));
|
||||
}
|
||||
|
||||
$url = $this->host . $resourcePath;
|
||||
$url = $this->config->getHost() . $resourcePath;
|
||||
|
||||
$curl = curl_init();
|
||||
// set timeout, if needed
|
||||
if ($this->curl_timeout != 0) {
|
||||
curl_setopt($curl, CURLOPT_TIMEOUT, $this->curl_timeout);
|
||||
if ($this->curlTimeout != 0) {
|
||||
curl_setopt($curl, CURLOPT_TIMEOUT, $this->curlTimeout);
|
||||
}
|
||||
// return the result on success, rather than just TRUE
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
||||
@ -234,14 +249,14 @@ class ApiClient {
|
||||
curl_setopt($curl, CURLOPT_URL, $url);
|
||||
|
||||
// Set user agent
|
||||
curl_setopt($curl, CURLOPT_USERAGENT, $this->user_agent);
|
||||
curl_setopt($curl, CURLOPT_USERAGENT, $this->userAgent);
|
||||
|
||||
// debugging for curl
|
||||
if (Configuration::$debug) {
|
||||
error_log("[DEBUG] HTTP Request body ~BEGIN~\n".print_r($postData, true)."\n~END~\n", 3, Configuration::$debug_file);
|
||||
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(Configuration::$debug_file, 'a'));
|
||||
curl_setopt($curl, CURLOPT_STDERR, fopen($this->config->getDebugFile(), 'a'));
|
||||
} else {
|
||||
curl_setopt($curl, CURLOPT_VERBOSE, 0);
|
||||
}
|
||||
@ -257,8 +272,8 @@ class ApiClient {
|
||||
$response_info = curl_getinfo($curl);
|
||||
|
||||
// debug HTTP response body
|
||||
if (Configuration::$debug) {
|
||||
error_log("[DEBUG] HTTP Response body ~BEGIN~\n".print_r($http_body, true)."\n~END~\n", 3, Configuration::$debug_file);
|
||||
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
|
||||
@ -278,9 +293,10 @@ class ApiClient {
|
||||
|
||||
/**
|
||||
* Build a JSON POST object
|
||||
* @param mixed $data the data to serialize
|
||||
* @return string serialized form of $data
|
||||
*/
|
||||
protected function sanitizeForSerialization($data)
|
||||
{
|
||||
protected function sanitizeForSerialization($data) {
|
||||
if (is_scalar($data) || null === $data) {
|
||||
$sanitized = $data;
|
||||
} else if ($data instanceof \DateTime) {
|
||||
@ -372,7 +388,7 @@ class ApiClient {
|
||||
/**
|
||||
* Deserialize a JSON string into an object
|
||||
*
|
||||
* @param object $object object or primitive to be deserialized
|
||||
* @param object $data object or primitive to be deserialized
|
||||
* @param string $class class name is passed as a string
|
||||
* @return object an instance of $class
|
||||
*/
|
||||
@ -419,7 +435,7 @@ class ApiClient {
|
||||
/*
|
||||
* return the header 'Accept' based on an array of Accept provided
|
||||
*
|
||||
* @param array[string] $accept Array of header
|
||||
* @param string[] $accept Array of header
|
||||
* @return string Accept (e.g. application/json)
|
||||
*/
|
||||
public static function selectHeaderAccept($accept) {
|
||||
|
@ -37,16 +37,11 @@ class {{classname}} {
|
||||
private $apiClient;
|
||||
|
||||
function __construct($apiClient = null) {
|
||||
if (null === $apiClient) {
|
||||
if (Configuration::$apiClient === null) {
|
||||
Configuration::$apiClient = new ApiClient(); // create a new API client if not present
|
||||
$this->apiClient = Configuration::$apiClient;
|
||||
}
|
||||
else
|
||||
$this->apiClient = Configuration::$apiClient; // use the default one
|
||||
} else {
|
||||
$this->apiClient = $apiClient; // use the one provided by the user
|
||||
if ($apiClient == null) {
|
||||
$apiClient = new ApiClient();
|
||||
}
|
||||
|
||||
$this->apiClient = $apiClient;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -88,28 +83,29 @@ class {{classname}} {
|
||||
$queryParams = array();
|
||||
$headerParams = array();
|
||||
$formParams = array();
|
||||
$_header_accept = $this->apiClient->selectHeaderAccept(array({{#produces}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/produces}}));
|
||||
$_header_accept = ApiClient::selectHeaderAccept(array({{#produces}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/produces}}));
|
||||
if (!is_null($_header_accept)) {
|
||||
$headerParams['Accept'] = $_header_accept;
|
||||
}
|
||||
$headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array({{#consumes}}'{{mediaType}}'{{#hasMore}},{{/hasMore}}{{/consumes}}));
|
||||
$headerParams['Content-Type'] = ApiClient::selectHeaderContentType(array({{#consumes}}'{{mediaType}}'{{#hasMore}},{{/hasMore}}{{/consumes}}));
|
||||
|
||||
{{#queryParams}}// query params
|
||||
if(${{paramName}} !== null) {
|
||||
$queryParams['{{baseName}}'] = $this->apiClient->toQueryValue(${{paramName}});
|
||||
$queryParams['{{baseName}}'] = ApiClient::toQueryValue(${{paramName}});
|
||||
}{{/queryParams}}
|
||||
{{#headerParams}}// header params
|
||||
if(${{paramName}} !== null) {
|
||||
$headerParams['{{baseName}}'] = $this->apiClient->toHeaderValue(${{paramName}});
|
||||
$headerParams['{{baseName}}'] = ApiClient::toHeaderValue(${{paramName}});
|
||||
}{{/headerParams}}
|
||||
{{#pathParams}}// path params
|
||||
if(${{paramName}} !== null) {
|
||||
$resourcePath = str_replace("{" . "{{baseName}}" . "}",
|
||||
$this->apiClient->toPathValue(${{paramName}}), $resourcePath);
|
||||
ApiClient::toPathValue(${{paramName}}),
|
||||
$resourcePath);
|
||||
}{{/pathParams}}
|
||||
{{#formParams}}// form params
|
||||
if (${{paramName}} !== null) {
|
||||
$formParams['{{baseName}}'] = {{#isFile}}'@' . {{/isFile}}$this->apiClient->toFormValue(${{paramName}});
|
||||
$formParams['{{baseName}}'] = {{#isFile}}'@' . {{/isFile}}ApiClient::toFormValue(${{paramName}});
|
||||
}{{/formParams}}
|
||||
{{#bodyParams}}// body params
|
||||
$_tempBody = null;
|
||||
@ -138,7 +134,7 @@ class {{classname}} {
|
||||
|
||||
switch ($e->getCode()) { {{#responses}}{{#dataType}}
|
||||
case {{code}}:
|
||||
$data = $this->apiClient->deserialize($e->getResponseBody(), '{{dataType}}');
|
||||
$data = ApiClient::deserialize($e->getResponseBody(), '{{dataType}}');
|
||||
$throw = new ApiException("{{message}}", $e->getCode(), $e->getResponseHeaders(), $data);
|
||||
break;{{/dataType}}{{/responses}}
|
||||
}
|
||||
@ -150,7 +146,7 @@ class {{classname}} {
|
||||
return null;
|
||||
}
|
||||
|
||||
$responseObject = $this->apiClient->deserialize($response,'{{returnType}}');
|
||||
$responseObject = ApiClient::deserialize($response,'{{returnType}}');
|
||||
return $responseObject;
|
||||
{{/returnType}}
|
||||
}
|
||||
|
@ -17,50 +17,173 @@
|
||||
|
||||
namespace {{invokerNamespace}};
|
||||
|
||||
class Configuration {
|
||||
class ApiConfiguration {
|
||||
|
||||
private static $defaultConfiguration = null;
|
||||
|
||||
/**
|
||||
* The host (from basePath)
|
||||
*/
|
||||
protected $host = '{{basePath}}';
|
||||
|
||||
/**
|
||||
* Associate array to store API key(s)
|
||||
*/
|
||||
public static $apiKey = array();
|
||||
protected $apiKeys = array();
|
||||
|
||||
/**
|
||||
* Associate array to store API prefix (e.g. Bearer)
|
||||
*/
|
||||
public static $apiKeyPrefix = array();
|
||||
protected $apiKeyPrefixes = array();
|
||||
|
||||
/**
|
||||
* Username for HTTP basic authentication
|
||||
*/
|
||||
public static $username = '';
|
||||
protected $username = '';
|
||||
|
||||
/**
|
||||
* Password for HTTP basic authentication
|
||||
*/
|
||||
public static $password = '';
|
||||
|
||||
/**
|
||||
* The default instance of ApiClient
|
||||
*/
|
||||
public static $apiClient;
|
||||
protected $password = '';
|
||||
|
||||
/**
|
||||
* Debug switch (default set to false)
|
||||
*/
|
||||
public static $debug = false;
|
||||
protected $debug = false;
|
||||
|
||||
/**
|
||||
* Debug file location (log to STDOUT by default)
|
||||
*/
|
||||
public static $debug_file = 'php://output';
|
||||
protected $debugFile = 'php://output';
|
||||
|
||||
/*
|
||||
* manually initalize ApiClient
|
||||
/**
|
||||
* @param string $host
|
||||
* @return ApiConfiguration
|
||||
*/
|
||||
public static function init() {
|
||||
if (self::$apiClient === null)
|
||||
self::$apiClient = new ApiClient();
|
||||
public function setHost($host) {
|
||||
$this->host = $host;
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getHost() {
|
||||
return $this->host;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param string $value
|
||||
* @return ApiConfiguration
|
||||
*/
|
||||
public function setApiKey($key, $value) {
|
||||
$this->apiKeys[$key] = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @return string
|
||||
*/
|
||||
public function getApiKey($key) {
|
||||
return isset($this->apiKeys[$key]) ? $this->apiKeys[$key] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param string $value
|
||||
* @return ApiConfiguration
|
||||
*/
|
||||
public function setApiKeyPrefix($key, $value) {
|
||||
$this->apiKeyPrefixes[$key] = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @return string
|
||||
*/
|
||||
public function getApiKeyPrefix($key) {
|
||||
return isset($this->apiKeyPrefixes[$key]) ? $this->apiKeyPrefixes[$key] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $username
|
||||
* @return ApiConfiguration
|
||||
*/
|
||||
public function setUsername($username) {
|
||||
$this->username = $username;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getUsername() {
|
||||
return $this->username;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $password
|
||||
* @return ApiConfiguration
|
||||
*/
|
||||
public function setPassword($password) {
|
||||
$this->password = $password;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getPassword() {
|
||||
return $this->password;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $debug
|
||||
* @return ApiConfiguration
|
||||
*/
|
||||
public function setDebug($debug) {
|
||||
$this->debug = $debug;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getDebug() {
|
||||
return $this->debug;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $debugFile
|
||||
* @return ApiConfiguration
|
||||
*/
|
||||
public function setDebugFile($debugFile) {
|
||||
$this->debugFile = $debugFile;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getDebugFile() {
|
||||
return $this->debugFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ApiConfiguration
|
||||
*/
|
||||
public static function getDefaultConfiguration() {
|
||||
if (self::$defaultConfiguration == null) {
|
||||
return new ApiConfiguration();
|
||||
}
|
||||
|
||||
return self::$defaultConfiguration;
|
||||
}
|
||||
|
||||
public static function setDefaultConfiguration(ApiConfiguration $config) {
|
||||
self::$defaultConfiguration = $config;
|
||||
}
|
||||
}
|
||||
|
@ -44,7 +44,7 @@ class {{classname}} implements ArrayAccess {
|
||||
* {{{description}}}{{/description}}
|
||||
* @var {{{datatype}}}
|
||||
*/
|
||||
public ${{name}};
|
||||
protected ${{name}};
|
||||
{{/vars}}
|
||||
public function __construct(array $data = null) {
|
||||
if ($data != null) {
|
||||
|
Loading…
Reference in New Issue
Block a user