forked from loafle/openapi-generator-original
merged from #902, rebuilt client
This commit is contained in:
@@ -25,141 +25,60 @@ class ApiClient {
|
||||
public static $PUT = "PUT";
|
||||
public static $DELETE = "DELETE";
|
||||
|
||||
/** @var string[] Array of default headers where the key is the header name and the value is the header value */
|
||||
private $default_header = array();
|
||||
/** @var Configuration */
|
||||
protected $config;
|
||||
|
||||
/** @var string timeout (second) of the HTTP request, by default set to 0, no timeout */
|
||||
protected $curl_timeout = 0;
|
||||
|
||||
/** @var string user agent of the HTTP request, set to "PHP-Swagger" by default */
|
||||
protected $user_agent = "PHP-Swagger";
|
||||
/** @var ObjectSerializer */
|
||||
protected $serializer;
|
||||
|
||||
/**
|
||||
* @param string $host Base url of the API server (optional)
|
||||
* @param Configuration $config config for this ApiClient
|
||||
*/
|
||||
function __construct($host = null) {
|
||||
if ($host === null) {
|
||||
$this->host = '{{basePath}}';
|
||||
} else {
|
||||
$this->host = $host;
|
||||
function __construct(Configuration $config = null) {
|
||||
if ($config == null) {
|
||||
$config = Configuration::getDefaultConfiguration();
|
||||
}
|
||||
|
||||
$this->config = $config;
|
||||
$this->serializer = new ObjectSerializer();
|
||||
}
|
||||
|
||||
/**
|
||||
* add default header
|
||||
*
|
||||
* @param string $header_name header name (e.g. Token)
|
||||
* @param string $header_value header value (e.g. 1z8wp3)
|
||||
* get the config
|
||||
* @return Configuration
|
||||
*/
|
||||
public function addDefaultHeader($header_name, $header_value) {
|
||||
if (!is_string($header_name))
|
||||
throw new \InvalidArgumentException('Header name must be a string.');
|
||||
|
||||
$this->default_header[$header_name] = $header_value;
|
||||
public function getConfig() {
|
||||
return $this->config;
|
||||
}
|
||||
|
||||
/**
|
||||
* get the default header
|
||||
*
|
||||
* @return array default header
|
||||
* get the serializer
|
||||
* @return ObjectSerializer
|
||||
*/
|
||||
public function getDefaultHeader() {
|
||||
return $this->default_header;
|
||||
public function getSerializer() {
|
||||
return $this->serializer;
|
||||
}
|
||||
|
||||
/**
|
||||
* delete the default header based on header name
|
||||
*
|
||||
* @param string $header_name header name (e.g. Token)
|
||||
*/
|
||||
public function deleteDefaultHeader($header_name) {
|
||||
unset($this->default_header[$header_name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* set the user agent of the api client
|
||||
*
|
||||
* @param string $user_agent the user agent of the api client
|
||||
*/
|
||||
public function setUserAgent($user_agent) {
|
||||
if (!is_string($user_agent))
|
||||
throw new \InvalidArgumentException('User-agent must be a string.');
|
||||
|
||||
$this->user_agent= $user_agent;
|
||||
}
|
||||
|
||||
/**
|
||||
* get the user agent of the api client
|
||||
*
|
||||
* @return string user agent
|
||||
*/
|
||||
public function getUserAgent($user_agent) {
|
||||
return $this->user_agent;
|
||||
}
|
||||
|
||||
/**
|
||||
* set the HTTP timeout value
|
||||
*
|
||||
* @param integer $seconds Number of seconds before timing out [set to 0 for no timeout]
|
||||
*/
|
||||
public function setTimeout($seconds) {
|
||||
if (!is_numeric($seconds) || $seconds < 0)
|
||||
throw new \InvalidArgumentException('Timeout value must be numeric and a non-negative number.');
|
||||
|
||||
$this->curl_timeout = $seconds;
|
||||
}
|
||||
|
||||
/**
|
||||
* get the HTTP timeout value
|
||||
*
|
||||
* @return string HTTP timeout value
|
||||
*/
|
||||
public function getTimeout() {
|
||||
return $this->curl_timeout;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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];
|
||||
$prefix = $this->config->getApiKeyPrefix($apiKey);
|
||||
$apiKey = $this->config->getApiKey($apiKey);
|
||||
|
||||
if (!isset($apiKey)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (isset($prefix)) {
|
||||
$keyWithPrefix = $prefix." ".$apiKey;
|
||||
} else {
|
||||
return;
|
||||
$keyWithPrefix = $apiKey;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* update hearder and query param based on authentication setting
|
||||
*
|
||||
* @param array $headerParams header parameters (by ref)
|
||||
* @param array $queryParams query parameters (by ref)
|
||||
* @param array $authSettings array of authentication scheme (e.g ['api_key'])
|
||||
*/
|
||||
public function updateParamsForAuth(&$headerParams, &$queryParams, $authSettings)
|
||||
{
|
||||
if (count($authSettings) == 0)
|
||||
return;
|
||||
|
||||
// one endpoint can have more than 1 auth settings
|
||||
foreach($authSettings as $auth) {
|
||||
// determine which one to use
|
||||
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}}
|
||||
{{#isOAuth}}//TODO support oauth{{/isOAuth}}
|
||||
break;
|
||||
{{/authMethods}}
|
||||
default:
|
||||
//TODO show warning about security definition not found
|
||||
}
|
||||
}
|
||||
return $keyWithPrefix;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -168,18 +87,15 @@ 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
|
||||
* @throws \{{invokerPackage}}\ApiException on a non 2xx response
|
||||
* @return mixed
|
||||
*/
|
||||
public function callApi($resourcePath, $method, $queryParams, $postData,
|
||||
$headerParams, $authSettings) {
|
||||
public function callApi($resourcePath, $method, $queryParams, $postData, $headerParams) {
|
||||
|
||||
$headers = array();
|
||||
|
||||
# determine authentication setting
|
||||
$this->updateParamsForAuth($headerParams, $queryParams, $authSettings);
|
||||
|
||||
# construct the http header
|
||||
$headerParams = array_merge((array)$this->default_header, (array)$headerParams);
|
||||
$headerParams = array_merge((array)$this->config->getDefaultHeaders(), (array)$headerParams);
|
||||
|
||||
foreach ($headerParams as $key => $val) {
|
||||
$headers[] = "$key: $val";
|
||||
@@ -190,15 +106,15 @@ class ApiClient {
|
||||
$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->sanitizeForSerialization($postData));
|
||||
$postData = json_encode($this->serializer->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->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);
|
||||
@@ -227,14 +143,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->config->getUserAgent());
|
||||
|
||||
// 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);
|
||||
}
|
||||
@@ -250,8 +166,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
|
||||
@@ -269,153 +185,13 @@ class ApiClient {
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a JSON POST object
|
||||
*/
|
||||
protected function sanitizeForSerialization($data)
|
||||
{
|
||||
if (is_scalar($data) || null === $data) {
|
||||
$sanitized = $data;
|
||||
} else if ($data instanceof \DateTime) {
|
||||
$sanitized = $data->format(\DateTime::ISO8601);
|
||||
} else if (is_array($data)) {
|
||||
foreach ($data as $property => $value) {
|
||||
$data[$property] = $this->sanitizeForSerialization($value);
|
||||
}
|
||||
$sanitized = $data;
|
||||
} else if (is_object($data)) {
|
||||
$values = array();
|
||||
foreach (array_keys($data::$swaggerTypes) as $property) {
|
||||
if ($data->$property !== null) {
|
||||
$values[$data::$attributeMap[$property]] = $this->sanitizeForSerialization($data->$property);
|
||||
}
|
||||
}
|
||||
$sanitized = $values;
|
||||
} else {
|
||||
$sanitized = (string)$data;
|
||||
}
|
||||
|
||||
return $sanitized;
|
||||
}
|
||||
|
||||
/**
|
||||
* Take value and turn it into a string suitable for inclusion in
|
||||
* the path, by url-encoding.
|
||||
* @param string $value a string which will be part of the path
|
||||
* @return string the serialized object
|
||||
*/
|
||||
public function toPathValue($value) {
|
||||
return rawurlencode($this->toString($value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Take value and turn it into a string suitable for inclusion in
|
||||
* the query, by imploding comma-separated if it's an object.
|
||||
* If it's a string, pass through unchanged. It will be url-encoded
|
||||
* later.
|
||||
* @param object $object an object to be serialized to a string
|
||||
* @return string the serialized object
|
||||
*/
|
||||
public function toQueryValue($object) {
|
||||
if (is_array($object)) {
|
||||
return implode(',', $object);
|
||||
} else {
|
||||
return $this->toString($object);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Take value and turn it into a string suitable for inclusion in
|
||||
* the header. If it's a string, pass through unchanged
|
||||
* If it's a datetime object, format it in ISO8601
|
||||
* @param string $value a string which will be part of the header
|
||||
* @return string the header string
|
||||
*/
|
||||
public function toHeaderValue($value) {
|
||||
return $this->toString($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Take value and turn it into a string suitable for inclusion in
|
||||
* the http body (form parameter). If it's a string, pass through unchanged
|
||||
* If it's a datetime object, format it in ISO8601
|
||||
* @param string $value the value of the form parameter
|
||||
* @return string the form string
|
||||
*/
|
||||
public function toFormValue($value) {
|
||||
return $this->toString($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Take value and turn it into a string suitable for inclusion in
|
||||
* the parameter. If it's a string, pass through unchanged
|
||||
* If it's a datetime object, format it in ISO8601
|
||||
* @param string $value the value of the parameter
|
||||
* @return string the header string
|
||||
*/
|
||||
public function toString($value) {
|
||||
if ($value instanceof \DateTime) { // datetime in ISO8601 format
|
||||
return $value->format(\DateTime::ISO8601);
|
||||
}
|
||||
else {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserialize a JSON string into an object
|
||||
*
|
||||
* @param object $object object or primitive to be deserialized
|
||||
* @param string $class class name is passed as a string
|
||||
* @return object an instance of $class
|
||||
*/
|
||||
public function deserialize($data, $class)
|
||||
{
|
||||
if (null === $data) {
|
||||
$deserialized = null;
|
||||
} elseif (substr($class, 0, 4) == 'map[') { # for associative array e.g. map[string,int]
|
||||
$inner = substr($class, 4, -1);
|
||||
$deserialized = array();
|
||||
if(strrpos($inner, ",") !== false) {
|
||||
$subClass_array = explode(',', $inner, 2);
|
||||
$subClass = $subClass_array[1];
|
||||
foreach ($data as $key => $value) {
|
||||
$deserialized[$key] = $this->deserialize($value, $subClass);
|
||||
}
|
||||
}
|
||||
} elseif (strcasecmp(substr($class, -2),'[]') == 0) {
|
||||
$subClass = substr($class, 0, -2);
|
||||
$values = array();
|
||||
foreach ($data as $key => $value) {
|
||||
$values[] = $this->deserialize($value, $subClass);
|
||||
}
|
||||
$deserialized = $values;
|
||||
} elseif ($class == 'DateTime') {
|
||||
$deserialized = new \DateTime($data);
|
||||
} elseif (in_array($class, array('string', 'int', 'float', 'double', 'bool', 'object'))) {
|
||||
settype($data, $class);
|
||||
$deserialized = $data;
|
||||
} else {
|
||||
$instance = new $class();
|
||||
foreach ($instance::$swaggerTypes as $property => $type) {
|
||||
$original_property_name = $instance::$attributeMap[$property];
|
||||
if (isset($original_property_name) && isset($data->$original_property_name)) {
|
||||
$instance->$property = $this->deserialize($data->$original_property_name, $type);
|
||||
}
|
||||
}
|
||||
$deserialized = $instance;
|
||||
}
|
||||
|
||||
return $deserialized;
|
||||
}
|
||||
|
||||
/*
|
||||
* 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 function selectHeaderAccept($accept) {
|
||||
public static function selectHeaderAccept($accept) {
|
||||
if (count($accept) === 0 or (count($accept) === 1 and $accept[0] === '')) {
|
||||
return NULL;
|
||||
} elseif (preg_grep("/application\/json/i", $accept)) {
|
||||
@@ -428,10 +204,10 @@ class ApiClient {
|
||||
/*
|
||||
* return the content type based on an array of content-type provided
|
||||
*
|
||||
* @param array[string] content_type_array Array fo content-type
|
||||
* @param string[] content_type_array Array fo content-type
|
||||
* @return string Content-Type (e.g. application/json)
|
||||
*/
|
||||
public function selectHeaderContentType($content_type) {
|
||||
public static 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)) {
|
||||
@@ -440,6 +216,4 @@ class ApiClient {
|
||||
return implode(',', $content_type);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -22,15 +22,20 @@ use \Exception;
|
||||
class ApiException extends Exception {
|
||||
|
||||
/** @var string The HTTP body of the server response. */
|
||||
protected $response_body;
|
||||
protected $responseBody;
|
||||
|
||||
/** @var string[] The HTTP header of the server response. */
|
||||
protected $response_headers;
|
||||
protected $responseHeaders;
|
||||
|
||||
/**
|
||||
* The deserialized response object
|
||||
*/
|
||||
protected $responseObject;
|
||||
|
||||
public function __construct($message="", $code=0, $responseHeaders=null, $responseBody=null) {
|
||||
parent::__construct($message, $code);
|
||||
$this->response_headers = $responseHeaders;
|
||||
$this->response_body = $responseBody;
|
||||
$this->responseHeaders = $responseHeaders;
|
||||
$this->responseBody = $responseBody;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -39,7 +44,7 @@ class ApiException extends Exception {
|
||||
* @return string HTTP response header
|
||||
*/
|
||||
public function getResponseHeaders() {
|
||||
return $this->response_headers;
|
||||
return $this->responseHeaders;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -48,7 +53,18 @@ class ApiException extends Exception {
|
||||
* @return string HTTP response body
|
||||
*/
|
||||
public function getResponseBody() {
|
||||
return $this->response_body;
|
||||
return $this->responseBody;
|
||||
}
|
||||
|
||||
/**
|
||||
* sets the deseralized response object (during deserialization)
|
||||
* @param mixed $obj
|
||||
*/
|
||||
public function setResponseObject($obj) {
|
||||
$this->responseObject = $obj;
|
||||
}
|
||||
|
||||
public function getResponseObject() {
|
||||
return $this->responseObject;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,151 @@
|
||||
<?php
|
||||
namespace {{invokerPackage}};
|
||||
|
||||
class ObjectSerializer {
|
||||
|
||||
/**
|
||||
* Build a JSON POST object
|
||||
* @param mixed $data the data to serialize
|
||||
* @return string serialized form of $data
|
||||
*/
|
||||
public function sanitizeForSerialization($data) {
|
||||
if (is_scalar($data) || null === $data) {
|
||||
$sanitized = $data;
|
||||
} else if ($data instanceof \DateTime) {
|
||||
$sanitized = $data->format(\DateTime::ISO8601);
|
||||
} else if (is_array($data)) {
|
||||
foreach ($data as $property => $value) {
|
||||
$data[$property] = $this->sanitizeForSerialization($value);
|
||||
}
|
||||
$sanitized = $data;
|
||||
} else if (is_object($data)) {
|
||||
$values = array();
|
||||
foreach (array_keys($data::$swaggerTypes) as $property) {
|
||||
$getter = $data::$getters[$property];
|
||||
if ($data->$getter() !== null) {
|
||||
$values[$data::$attributeMap[$property]] = $this->sanitizeForSerialization($data->$getter());
|
||||
}
|
||||
}
|
||||
$sanitized = $values;
|
||||
} else {
|
||||
$sanitized = (string)$data;
|
||||
}
|
||||
|
||||
return $sanitized;
|
||||
}
|
||||
|
||||
/**
|
||||
* Take value and turn it into a string suitable for inclusion in
|
||||
* the path, by url-encoding.
|
||||
* @param string $value a string which will be part of the path
|
||||
* @return string the serialized object
|
||||
*/
|
||||
public function toPathValue($value) {
|
||||
return rawurlencode($this->toString($value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Take value and turn it into a string suitable for inclusion in
|
||||
* the query, by imploding comma-separated if it's an object.
|
||||
* If it's a string, pass through unchanged. It will be url-encoded
|
||||
* later.
|
||||
* @param object $object an object to be serialized to a string
|
||||
* @return string the serialized object
|
||||
*/
|
||||
public function toQueryValue($object) {
|
||||
if (is_array($object)) {
|
||||
return implode(',', $object);
|
||||
} else {
|
||||
return $this->toString($object);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Take value and turn it into a string suitable for inclusion in
|
||||
* the header. If it's a string, pass through unchanged
|
||||
* If it's a datetime object, format it in ISO8601
|
||||
* @param string $value a string which will be part of the header
|
||||
* @return string the header string
|
||||
*/
|
||||
public function toHeaderValue($value) {
|
||||
return $this->toString($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Take value and turn it into a string suitable for inclusion in
|
||||
* the http body (form parameter). If it's a string, pass through unchanged
|
||||
* If it's a datetime object, format it in ISO8601
|
||||
* @param string $value the value of the form parameter
|
||||
* @return string the form string
|
||||
*/
|
||||
public function toFormValue($value) {
|
||||
return $this->toString($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Take value and turn it into a string suitable for inclusion in
|
||||
* the parameter. If it's a string, pass through unchanged
|
||||
* If it's a datetime object, format it in ISO8601
|
||||
* @param string $value the value of the parameter
|
||||
* @return string the header string
|
||||
*/
|
||||
public function toString($value) {
|
||||
if ($value instanceof \DateTime) { // datetime in ISO8601 format
|
||||
return $value->format(\DateTime::ISO8601);
|
||||
} else {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserialize a JSON string into an object
|
||||
*
|
||||
* @param mixed $data object or primitive to be deserialized
|
||||
* @param string $class class name is passed as a string
|
||||
* @return object an instance of $class
|
||||
*/
|
||||
public function deserialize($data, $class) {
|
||||
if (null === $data) {
|
||||
$deserialized = null;
|
||||
} elseif (substr($class, 0, 4) == 'map[') { # for associative array e.g. map[string,int]
|
||||
$inner = substr($class, 4, -1);
|
||||
$deserialized = array();
|
||||
if(strrpos($inner, ",") !== false) {
|
||||
$subClass_array = explode(',', $inner, 2);
|
||||
$subClass = $subClass_array[1];
|
||||
foreach ($data as $key => $value) {
|
||||
$deserialized[$key] = $this->deserialize($value, $subClass);
|
||||
}
|
||||
}
|
||||
} elseif (strcasecmp(substr($class, -2),'[]') == 0) {
|
||||
$subClass = substr($class, 0, -2);
|
||||
$values = array();
|
||||
foreach ($data as $key => $value) {
|
||||
$values[] = $this->deserialize($value, $subClass);
|
||||
}
|
||||
$deserialized = $values;
|
||||
} elseif ($class == 'DateTime') {
|
||||
$deserialized = new \DateTime($data);
|
||||
} elseif (in_array($class, array('string', 'int', 'float', 'double', 'bool', 'object'))) {
|
||||
settype($data, $class);
|
||||
$deserialized = $data;
|
||||
} else {
|
||||
$instance = new $class();
|
||||
foreach ($instance::$swaggerTypes as $property => $type) {
|
||||
$propertySetter = $instance::$setters[$property];
|
||||
|
||||
if (!isset($propertySetter) || !isset($data->{$instance::$attributeMap[$property]})) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$propertyValue = $data->{$instance::$attributeMap[$property]};
|
||||
if (isset($propertyValue)) {
|
||||
$instance->$propertySetter($this->deserialize($propertyValue, $type));
|
||||
}
|
||||
}
|
||||
$deserialized = $instance;
|
||||
}
|
||||
|
||||
return $deserialized;
|
||||
}
|
||||
}
|
||||
@@ -22,31 +22,29 @@
|
||||
|
||||
namespace {{apiPackage}};
|
||||
|
||||
use \{{invokerPackage}}\ApiClient;
|
||||
use \{{invokerPackage}}\Configuration;
|
||||
use \{{invokerPackage}}\ApiClient;
|
||||
use \{{invokerPackage}}\ApiException;
|
||||
use \{{invokerPackage}}\ObjectSerializer;
|
||||
|
||||
{{#operations}}
|
||||
class {{classname}} {
|
||||
|
||||
/**
|
||||
* @param \{{invokerPackage}}\ApiClient|null $apiClient The api client to use. Defaults to getting it from Configuration
|
||||
*/
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
/** @var \{{invokerPackage}}\ApiClient instance of the ApiClient */
|
||||
private $apiClient;
|
||||
|
||||
/**
|
||||
* @param \{{invokerPackage}}\ApiClient|null $apiClient The api client to use
|
||||
*/
|
||||
function __construct($apiClient = null) {
|
||||
if ($apiClient == null) {
|
||||
$apiClient = new ApiClient();
|
||||
$apiClient->getConfig()->setHost('{{basePath}}');
|
||||
}
|
||||
|
||||
$this->apiClient = $apiClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \{{invokerPackage}}\ApiClient get the API client
|
||||
*/
|
||||
@@ -55,10 +53,12 @@ class {{classname}} {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \{{invokerPackage}} $apiClient set the API client
|
||||
* @param \{{invokerPackage}}\ApiClient $apiClient set the API client
|
||||
* @return {{classname}}
|
||||
*/
|
||||
public function setApiClient($apiClient) {
|
||||
public function setApiClient(ApiClient $apiClient) {
|
||||
$this->apiClient = $apiClient;
|
||||
return $this;
|
||||
}
|
||||
|
||||
{{#operation}}
|
||||
@@ -69,6 +69,7 @@ class {{classname}} {
|
||||
*
|
||||
{{#allParams}} * @param {{dataType}} ${{paramName}} {{description}} {{^optional}}(required){{/optional}}{{#optional}}(optional){{/optional}}
|
||||
{{/allParams}} * @return {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}
|
||||
* @throws \{{invokerPackage}}\ApiException on non-2xx response
|
||||
*/
|
||||
public function {{nickname}}({{#allParams}}${{paramName}}{{#optional}}=null{{/optional}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) {
|
||||
{{#allParams}}{{#required}}
|
||||
@@ -86,28 +87,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}}'] = $this->apiClient->getSerializer()->toQueryValue(${{paramName}});
|
||||
}{{/queryParams}}
|
||||
{{#headerParams}}// header params
|
||||
if(${{paramName}} !== null) {
|
||||
$headerParams['{{baseName}}'] = $this->apiClient->toHeaderValue(${{paramName}});
|
||||
$headerParams['{{baseName}}'] = $this->apiClient->getSerializer()->toHeaderValue(${{paramName}});
|
||||
}{{/headerParams}}
|
||||
{{#pathParams}}// path params
|
||||
if(${{paramName}} !== null) {
|
||||
$resourcePath = str_replace("{" . "{{baseName}}" . "}",
|
||||
$this->apiClient->toPathValue(${{paramName}}), $resourcePath);
|
||||
$this->apiClient->getSerializer()->toPathValue(${{paramName}}),
|
||||
$resourcePath);
|
||||
}{{/pathParams}}
|
||||
{{#formParams}}// form params
|
||||
if (${{paramName}} !== null) {
|
||||
$formParams['{{baseName}}'] = {{#isFile}}'@'.{{/isFile}}$this->apiClient->toFormValue(${{paramName}});
|
||||
$formParams['{{baseName}}'] = {{#isFile}}'@' . {{/isFile}}$this->apiClient->getSerializer()->toFormValue(${{paramName}});
|
||||
}{{/formParams}}
|
||||
{{#bodyParams}}// body params
|
||||
$_tempBody = null;
|
||||
@@ -122,22 +124,38 @@ class {{classname}} {
|
||||
// for HTTP post (form)
|
||||
$httpBody = $formParams;
|
||||
}
|
||||
|
||||
// authentication setting, if any
|
||||
$authSettings = array({{#authMethods}}'{{name}}'{{#hasMore}}, {{/hasMore}}{{/authMethods}});
|
||||
|
||||
{{#authMethods}}{{#isApiKey}}
|
||||
$apiKey = $this->apiClient->getApiKeyWithPrefix('{{keyParamName}}');
|
||||
if (isset($apiKey)) {
|
||||
{{#isKeyInHeader}}$headerParams['{{keyParamName}}'] = $apiKey;{{/isKeyInHeader}}{{#isKeyInQuery}}$queryParams['{{keyParamName}}'] = $apiKey;{{/isKeyInQuery}}
|
||||
}{{/isApiKey}}
|
||||
{{#isBasic}}$headerParams['Authorization'] = 'Basic '.base64_encode($this->apiClient->getConfig()->getUsername().":".$this->apiClient->getConfig()->getPassword());{{/isBasic}}
|
||||
{{#isOAuth}}//TODO support oauth{{/isOAuth}}
|
||||
{{/authMethods}}
|
||||
// make the API Call
|
||||
$response = $this->apiClient->callAPI($resourcePath, $method,
|
||||
$queryParams, $httpBody,
|
||||
$headerParams, $authSettings);
|
||||
{{#returnType}}if(! $response) {
|
||||
try {
|
||||
$response = $this->apiClient->callAPI($resourcePath, $method,
|
||||
$queryParams, $httpBody,
|
||||
$headerParams);
|
||||
} catch (ApiException $e) {
|
||||
switch ($e->getCode()) { {{#responses}}{{#dataType}}
|
||||
case {{code}}:
|
||||
$data = $this->apiClient->getSerializer()->deserialize($e->getResponseBody(), '{{dataType}}');
|
||||
$e->setResponseObject($data);
|
||||
break;{{/dataType}}{{/responses}}
|
||||
}
|
||||
|
||||
throw $e;
|
||||
}
|
||||
{{#returnType}}
|
||||
if (!$response) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$responseObject = $this->apiClient->deserialize($response,'{{returnType}}');
|
||||
return $responseObject;{{/returnType}}
|
||||
$responseObject = $this->apiClient->getSerializer()->deserialize($response,'{{returnType}}');
|
||||
return $responseObject;
|
||||
{{/returnType}}
|
||||
}
|
||||
{{/operation}}
|
||||
{{newline}}
|
||||
{{/operations}}
|
||||
}
|
||||
{{/operations}}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
*
|
||||
* After registering this autoload function with SPL, the following line
|
||||
* would cause the function to attempt to load the \{{invokerPackage}}\Baz\Qux class
|
||||
* from /path/to/project/lib/Baz/Qux.php:
|
||||
* from /path/to/project/{{srcBasePath}}/Baz/Qux.php:
|
||||
*
|
||||
* new \{{invokerPackage}}\Baz\Qux;
|
||||
*
|
||||
@@ -17,7 +17,7 @@ spl_autoload_register(function ($class) {
|
||||
$prefix = '{{escapedInvokerPackage}}\\';
|
||||
|
||||
// base directory for the namespace prefix
|
||||
$base_dir = __DIR__ . '/lib/';
|
||||
$base_dir = __DIR__ . '/{{srcBasePath}}/';
|
||||
|
||||
// does the class use the namespace prefix?
|
||||
$len = strlen($prefix);
|
||||
|
||||
@@ -28,6 +28,6 @@
|
||||
"squizlabs/php_codesniffer": "~2.0"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": { "{{escapedInvokerPackage}}\\" : "lib/" }
|
||||
"psr-4": { "{{escapedInvokerPackage}}\\" : "{{srcBasePath}}/" }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,37 +17,250 @@
|
||||
|
||||
namespace {{invokerPackage}};
|
||||
|
||||
use \{{invokerPackage}}\ApiClient;
|
||||
|
||||
class Configuration {
|
||||
|
||||
private static $defaultConfiguration = null;
|
||||
|
||||
/** @var string[] Associate array to store API key(s) */
|
||||
public static $apiKey = array();
|
||||
protected $apiKeys = array();
|
||||
|
||||
/** string[] Associate array to store API prefix (e.g. Bearer) */
|
||||
public static $apiKeyPrefix = array();
|
||||
protected $apiKeyPrefixes = array();
|
||||
|
||||
/** @var string Username for HTTP basic authentication */
|
||||
public static $username = '';
|
||||
protected $username = '';
|
||||
|
||||
/** @var string Password for HTTP basic authentication */
|
||||
public static $password = '';
|
||||
protected $password = '';
|
||||
|
||||
/** @var \{{invokerPackage}}\ApiClient The default instance of ApiClient */
|
||||
public static $apiClient;
|
||||
protected $defaultHeaders = array();
|
||||
|
||||
/** @var string The host */
|
||||
protected $host = 'http://localhost';
|
||||
|
||||
/** @var string timeout (second) of the HTTP request, by default set to 0, no timeout */
|
||||
protected $curlTimeout = 0;
|
||||
|
||||
/** @var string user agent of the HTTP request, set to "PHP-Swagger" by default */
|
||||
protected $userAgent = "PHP-Swagger";
|
||||
|
||||
/** @var bool Debug switch (default set to false) */
|
||||
public static $debug = false;
|
||||
protected $debug = false;
|
||||
|
||||
/** @var string Debug file location (log to STDOUT by default) */
|
||||
public static $debug_file = 'php://output';
|
||||
protected $debugFile = 'php://output';
|
||||
|
||||
/*
|
||||
* manually initalize ApiClient
|
||||
/**
|
||||
* @param string $key
|
||||
* @param string $value
|
||||
* @return Configuration
|
||||
*/
|
||||
public static function init() {
|
||||
if (self::$apiClient === null)
|
||||
self::$apiClient = new ApiClient();
|
||||
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 Configuration
|
||||
*/
|
||||
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 Configuration
|
||||
*/
|
||||
public function setUsername($username) {
|
||||
$this->username = $username;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getUsername() {
|
||||
return $this->username;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $password
|
||||
* @return Configuration
|
||||
*/
|
||||
public function setPassword($password) {
|
||||
$this->password = $password;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getPassword() {
|
||||
return $this->password;
|
||||
}
|
||||
|
||||
/**
|
||||
* add default header
|
||||
*
|
||||
* @param string $headerName header name (e.g. Token)
|
||||
* @param string $headerValue header value (e.g. 1z8wp3)
|
||||
* @return ApiClient
|
||||
*/
|
||||
public function addDefaultHeader($headerName, $headerValue) {
|
||||
if (!is_string($headerName)) {
|
||||
throw new \InvalidArgumentException('Header name must be a string.');
|
||||
}
|
||||
|
||||
$this->defaultHeaders[$headerName] = $headerValue;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* get the default header
|
||||
*
|
||||
* @return array default header
|
||||
*/
|
||||
public function getDefaultHeaders() {
|
||||
return $this->defaultHeaders;
|
||||
}
|
||||
|
||||
/**
|
||||
* delete a default header
|
||||
* @param string $headerName the header to delete
|
||||
* @return Configuration
|
||||
*/
|
||||
public function deleteDefaultHeader($headerName) {
|
||||
unset($this->defaultHeaders[$headerName]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $host
|
||||
* @return Configuration
|
||||
*/
|
||||
public function setHost($host) {
|
||||
$this->host = $host;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getHost() {
|
||||
return $this->host;
|
||||
}
|
||||
|
||||
/**
|
||||
* set the user agent of the api client
|
||||
*
|
||||
* @param string $userAgent the user agent of the api client
|
||||
* @return ApiClient
|
||||
*/
|
||||
public function setUserAgent($userAgent) {
|
||||
if (!is_string($userAgent)) {
|
||||
throw new \InvalidArgumentException('User-agent must be a string.');
|
||||
}
|
||||
|
||||
$this->userAgent = $userAgent;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* get the user agent of the api client
|
||||
*
|
||||
* @return string user agent
|
||||
*/
|
||||
public function getUserAgent() {
|
||||
return $this->userAgent;
|
||||
}
|
||||
|
||||
/**
|
||||
* set the HTTP timeout value
|
||||
*
|
||||
* @param integer $seconds Number of seconds before timing out [set to 0 for no timeout]
|
||||
* @return ApiClient
|
||||
*/
|
||||
public function setCurlTimeout($seconds) {
|
||||
if (!is_numeric($seconds) || $seconds < 0) {
|
||||
throw new \InvalidArgumentException('Timeout value must be numeric and a non-negative number.');
|
||||
}
|
||||
|
||||
$this->curlTimeout = $seconds;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* get the HTTP timeout value
|
||||
*
|
||||
* @return string HTTP timeout value
|
||||
*/
|
||||
public function getCurlTimeout() {
|
||||
return $this->curlTimeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $debug
|
||||
* @return Configuration
|
||||
*/
|
||||
public function setDebug($debug) {
|
||||
$this->debug = $debug;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getDebug() {
|
||||
return $this->debug;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $debugFile
|
||||
* @return Configuration
|
||||
*/
|
||||
public function setDebugFile($debugFile) {
|
||||
$this->debugFile = $debugFile;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getDebugFile() {
|
||||
return $this->debugFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Configuration
|
||||
*/
|
||||
public static function getDefaultConfiguration() {
|
||||
if (self::$defaultConfiguration == null) {
|
||||
return new Configuration();
|
||||
}
|
||||
|
||||
return self::$defaultConfiguration;
|
||||
}
|
||||
|
||||
public static function setDefaultConfiguration(Configuration $config) {
|
||||
self::$defaultConfiguration = $config;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,18 +40,48 @@ class {{classname}} implements ArrayAccess {
|
||||
{{#vars}}'{{name}}' => '{{baseName}}'{{#hasMore}},
|
||||
{{/hasMore}}{{/vars}}
|
||||
);
|
||||
|
||||
/** @var string[] Array of attributes to setter functions (for deserialization of responses) */
|
||||
static $setters = array(
|
||||
{{#vars}}'{{name}}' => '{{setter}}'{{#hasMore}},
|
||||
{{/hasMore}}{{/vars}}
|
||||
);
|
||||
|
||||
/** @var string[] Array of attributes to getter functions (for serialization of requests) */
|
||||
static $getters = array(
|
||||
{{#vars}}'{{name}}' => '{{getter}}'{{#hasMore}},
|
||||
{{/hasMore}}{{/vars}}
|
||||
);
|
||||
|
||||
{{#vars}}
|
||||
/** @var {{datatype}} ${{name}} {{#description}}{{{description}}} {{/description}}*/
|
||||
public ${{name}};
|
||||
protected ${{name}};
|
||||
{{/vars}}
|
||||
/**
|
||||
* @param mixed[] Array of parameters to initialize the object with
|
||||
*/
|
||||
public function __construct(array $data = null) {
|
||||
{{#vars}}$this->{{name}} = @$data["{{name}}"];{{#hasMore}}
|
||||
{{/hasMore}}{{/vars}}
|
||||
if ($data != null) {
|
||||
{{#vars}}$this->{{name}} = $data["{{name}}"];{{#hasMore}}
|
||||
{{/hasMore}}{{/vars}}
|
||||
}
|
||||
}
|
||||
{{#vars}}
|
||||
/**
|
||||
* get {{name}}
|
||||
* @return {{datatype}}
|
||||
*/
|
||||
public function {{getter}}() {
|
||||
return $this->{{name}};
|
||||
}
|
||||
|
||||
/**
|
||||
* set {{name}}
|
||||
* @param {{datatype}} ${{name}}
|
||||
* @return $this
|
||||
*/
|
||||
public function {{setter}}(${{name}}) {
|
||||
$this->{{name}} = ${{name}};
|
||||
return $this;
|
||||
}
|
||||
{{/vars}}
|
||||
public function offsetExists($offset) {
|
||||
return isset($this->$offset);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user