forked from loafle/openapi-generator-original
better accept and content-type for php, added test cases
This commit is contained in:
@@ -261,7 +261,6 @@ class APIClient {
|
||||
* @param string $class class name is passed as a string
|
||||
* @return object an instance of $class
|
||||
*/
|
||||
|
||||
public static function deserialize($data, $class)
|
||||
{
|
||||
if (null === $data) {
|
||||
@@ -304,5 +303,37 @@ class APIClient {
|
||||
return $deserialized;
|
||||
}
|
||||
|
||||
/*
|
||||
* return the header 'Accept' based on an array of Accept provided
|
||||
*
|
||||
* @param array[string] $accept Array of header
|
||||
* @return string Accept (e.g. application/json)
|
||||
*/
|
||||
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)) {
|
||||
return 'application/json';
|
||||
} else {
|
||||
return implode(',', $accept);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* return the content type based on an array of content-type provided
|
||||
*
|
||||
* @param array[string] content_type_array Array fo content-type
|
||||
* @return string Content-Type (e.g. application/json)
|
||||
*/
|
||||
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)) {
|
||||
return 'application/json';
|
||||
} else {
|
||||
return implode(',', $content_type);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -54,12 +54,11 @@ class {{classname}} {
|
||||
$queryParams = array();
|
||||
$headerParams = array();
|
||||
$formParams = array();
|
||||
$_header_accept = '{{#produces}}{{mediaType}}{{#hasMore}}, {{/hasMore}}{{/produces}}';
|
||||
if ($_header_accept !== '') {
|
||||
$_header_accept = $this->apiClient->selectHeaderAccept(array({{#produces}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/produces}}));
|
||||
if (!is_null($_header_accept)) {
|
||||
$headerParams['Accept'] = $_header_accept;
|
||||
}
|
||||
$_header_content_type = array({{#consumes}}'{{mediaType}}'{{#hasMore}},{{/hasMore}}{{/consumes}});
|
||||
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
|
||||
$headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array({{#consumes}}'{{mediaType}}'{{#hasMore}},{{/hasMore}}{{/consumes}}));
|
||||
|
||||
{{#queryParams}}// query params
|
||||
if(${{paramName}} !== null) {
|
||||
|
||||
@@ -142,20 +142,21 @@ class APIClient {
|
||||
$response_info = curl_getinfo($curl);
|
||||
|
||||
// Handle the response
|
||||
if ($response === false) { // error, likely in the client side
|
||||
throw new APIClientException("API Error ($url): ".curl_error($curl), 0, $response_info, $response);
|
||||
if ($response_info['http_code'] == 0) {
|
||||
throw new APIClientException("TIMEOUT: api call to " . $url .
|
||||
" took more than 5s to return", 0, $response_info, $response);
|
||||
} else if ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299 ) {
|
||||
$data = json_decode($response);
|
||||
if (json_last_error() > 0) { // if response is a string
|
||||
$data = $response;
|
||||
}
|
||||
} else if ($response_info['http_code'] == 401) { // server returns 401
|
||||
} else if ($response_info['http_code'] == 401) {
|
||||
throw new APIClientException("Unauthorized API request to " . $url .
|
||||
": " . serialize($response), 0, $response_info, $response);
|
||||
} else if ($response_info['http_code'] == 404) { // server returns 404
|
||||
} else if ($response_info['http_code'] == 404) {
|
||||
$data = null;
|
||||
} else {
|
||||
throw new APIClientException("Can't connect to the API: " . $url .
|
||||
throw new APIClientException("Can't connect to the api: " . $url .
|
||||
" response code: " .
|
||||
$response_info['http_code'], 0, $response_info, $response);
|
||||
}
|
||||
@@ -260,7 +261,6 @@ class APIClient {
|
||||
* @param string $class class name is passed as a string
|
||||
* @return object an instance of $class
|
||||
*/
|
||||
|
||||
public static function deserialize($data, $class)
|
||||
{
|
||||
if (null === $data) {
|
||||
@@ -303,5 +303,37 @@ class APIClient {
|
||||
return $deserialized;
|
||||
}
|
||||
|
||||
/*
|
||||
* return the header 'Accept' based on an array of Accept provided
|
||||
*
|
||||
* @param array[string] $accept Array of header
|
||||
* @return string Accept (e.g. application/json)
|
||||
*/
|
||||
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)) {
|
||||
return 'application/json';
|
||||
} else {
|
||||
return implode(',', $accept);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* return the content type based on an array of content-type provided
|
||||
*
|
||||
* @param array[string] content_type_array Array fo content-type
|
||||
* @return string Content-Type (e.g. application/json)
|
||||
*/
|
||||
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)) {
|
||||
return 'application/json';
|
||||
} else {
|
||||
return implode(',', $content_type);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -48,12 +48,11 @@ class PetApi {
|
||||
$queryParams = array();
|
||||
$headerParams = array();
|
||||
$formParams = array();
|
||||
$_header_accept = 'application/json, application/xml';
|
||||
if ($_header_accept !== '') {
|
||||
$_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
|
||||
if (!is_null($_header_accept)) {
|
||||
$headerParams['Accept'] = $_header_accept;
|
||||
}
|
||||
$_header_content_type = array('application/json','application/xml',);
|
||||
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
|
||||
$headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array('application/json','application/xml',));
|
||||
|
||||
|
||||
|
||||
@@ -100,12 +99,11 @@ class PetApi {
|
||||
$queryParams = array();
|
||||
$headerParams = array();
|
||||
$formParams = array();
|
||||
$_header_accept = 'application/json, application/xml';
|
||||
if ($_header_accept !== '') {
|
||||
$_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
|
||||
if (!is_null($_header_accept)) {
|
||||
$headerParams['Accept'] = $_header_accept;
|
||||
}
|
||||
$_header_content_type = array('application/json','application/xml',);
|
||||
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
|
||||
$headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array('application/json','application/xml',));
|
||||
|
||||
|
||||
|
||||
@@ -152,12 +150,11 @@ class PetApi {
|
||||
$queryParams = array();
|
||||
$headerParams = array();
|
||||
$formParams = array();
|
||||
$_header_accept = 'application/json, application/xml';
|
||||
if ($_header_accept !== '') {
|
||||
$_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
|
||||
if (!is_null($_header_accept)) {
|
||||
$headerParams['Accept'] = $_header_accept;
|
||||
}
|
||||
$_header_content_type = array();
|
||||
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
|
||||
$headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array());
|
||||
|
||||
// query params
|
||||
if($status !== null) {
|
||||
@@ -209,12 +206,11 @@ class PetApi {
|
||||
$queryParams = array();
|
||||
$headerParams = array();
|
||||
$formParams = array();
|
||||
$_header_accept = 'application/json, application/xml';
|
||||
if ($_header_accept !== '') {
|
||||
$_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
|
||||
if (!is_null($_header_accept)) {
|
||||
$headerParams['Accept'] = $_header_accept;
|
||||
}
|
||||
$_header_content_type = array();
|
||||
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
|
||||
$headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array());
|
||||
|
||||
// query params
|
||||
if($tags !== null) {
|
||||
@@ -271,12 +267,11 @@ class PetApi {
|
||||
$queryParams = array();
|
||||
$headerParams = array();
|
||||
$formParams = array();
|
||||
$_header_accept = 'application/json, application/xml';
|
||||
if ($_header_accept !== '') {
|
||||
$_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
|
||||
if (!is_null($_header_accept)) {
|
||||
$headerParams['Accept'] = $_header_accept;
|
||||
}
|
||||
$_header_content_type = array();
|
||||
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
|
||||
$headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array());
|
||||
|
||||
|
||||
|
||||
@@ -336,12 +331,11 @@ class PetApi {
|
||||
$queryParams = array();
|
||||
$headerParams = array();
|
||||
$formParams = array();
|
||||
$_header_accept = 'application/json, application/xml';
|
||||
if ($_header_accept !== '') {
|
||||
$_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
|
||||
if (!is_null($_header_accept)) {
|
||||
$headerParams['Accept'] = $_header_accept;
|
||||
}
|
||||
$_header_content_type = array('application/x-www-form-urlencoded',);
|
||||
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
|
||||
$headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array('application/x-www-form-urlencoded',));
|
||||
|
||||
|
||||
|
||||
@@ -400,12 +394,11 @@ class PetApi {
|
||||
$queryParams = array();
|
||||
$headerParams = array();
|
||||
$formParams = array();
|
||||
$_header_accept = 'application/json, application/xml';
|
||||
if ($_header_accept !== '') {
|
||||
$_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
|
||||
if (!is_null($_header_accept)) {
|
||||
$headerParams['Accept'] = $_header_accept;
|
||||
}
|
||||
$_header_content_type = array();
|
||||
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
|
||||
$headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array());
|
||||
|
||||
|
||||
// header params
|
||||
@@ -462,12 +455,11 @@ class PetApi {
|
||||
$queryParams = array();
|
||||
$headerParams = array();
|
||||
$formParams = array();
|
||||
$_header_accept = 'application/json, application/xml';
|
||||
if ($_header_accept !== '') {
|
||||
$_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
|
||||
if (!is_null($_header_accept)) {
|
||||
$headerParams['Accept'] = $_header_accept;
|
||||
}
|
||||
$_header_content_type = array('multipart/form-data',);
|
||||
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
|
||||
$headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array('multipart/form-data',));
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -47,12 +47,11 @@ class StoreApi {
|
||||
$queryParams = array();
|
||||
$headerParams = array();
|
||||
$formParams = array();
|
||||
$_header_accept = 'application/json, application/xml';
|
||||
if ($_header_accept !== '') {
|
||||
$_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
|
||||
if (!is_null($_header_accept)) {
|
||||
$headerParams['Accept'] = $_header_accept;
|
||||
}
|
||||
$_header_content_type = array();
|
||||
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
|
||||
$headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array());
|
||||
|
||||
|
||||
|
||||
@@ -101,12 +100,11 @@ class StoreApi {
|
||||
$queryParams = array();
|
||||
$headerParams = array();
|
||||
$formParams = array();
|
||||
$_header_accept = 'application/json, application/xml';
|
||||
if ($_header_accept !== '') {
|
||||
$_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
|
||||
if (!is_null($_header_accept)) {
|
||||
$headerParams['Accept'] = $_header_accept;
|
||||
}
|
||||
$_header_content_type = array();
|
||||
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
|
||||
$headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array());
|
||||
|
||||
|
||||
|
||||
@@ -164,12 +162,11 @@ class StoreApi {
|
||||
$queryParams = array();
|
||||
$headerParams = array();
|
||||
$formParams = array();
|
||||
$_header_accept = 'application/json, application/xml';
|
||||
if ($_header_accept !== '') {
|
||||
$_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
|
||||
if (!is_null($_header_accept)) {
|
||||
$headerParams['Accept'] = $_header_accept;
|
||||
}
|
||||
$_header_content_type = array();
|
||||
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
|
||||
$headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array());
|
||||
|
||||
|
||||
|
||||
@@ -227,12 +224,11 @@ class StoreApi {
|
||||
$queryParams = array();
|
||||
$headerParams = array();
|
||||
$formParams = array();
|
||||
$_header_accept = 'application/json, application/xml';
|
||||
if ($_header_accept !== '') {
|
||||
$_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
|
||||
if (!is_null($_header_accept)) {
|
||||
$headerParams['Accept'] = $_header_accept;
|
||||
}
|
||||
$_header_content_type = array();
|
||||
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
|
||||
$headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array());
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -48,12 +48,11 @@ class UserApi {
|
||||
$queryParams = array();
|
||||
$headerParams = array();
|
||||
$formParams = array();
|
||||
$_header_accept = 'application/json, application/xml';
|
||||
if ($_header_accept !== '') {
|
||||
$_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
|
||||
if (!is_null($_header_accept)) {
|
||||
$headerParams['Accept'] = $_header_accept;
|
||||
}
|
||||
$_header_content_type = array();
|
||||
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
|
||||
$headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array());
|
||||
|
||||
|
||||
|
||||
@@ -100,12 +99,11 @@ class UserApi {
|
||||
$queryParams = array();
|
||||
$headerParams = array();
|
||||
$formParams = array();
|
||||
$_header_accept = 'application/json, application/xml';
|
||||
if ($_header_accept !== '') {
|
||||
$_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
|
||||
if (!is_null($_header_accept)) {
|
||||
$headerParams['Accept'] = $_header_accept;
|
||||
}
|
||||
$_header_content_type = array();
|
||||
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
|
||||
$headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array());
|
||||
|
||||
|
||||
|
||||
@@ -152,12 +150,11 @@ class UserApi {
|
||||
$queryParams = array();
|
||||
$headerParams = array();
|
||||
$formParams = array();
|
||||
$_header_accept = 'application/json, application/xml';
|
||||
if ($_header_accept !== '') {
|
||||
$_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
|
||||
if (!is_null($_header_accept)) {
|
||||
$headerParams['Accept'] = $_header_accept;
|
||||
}
|
||||
$_header_content_type = array();
|
||||
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
|
||||
$headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array());
|
||||
|
||||
|
||||
|
||||
@@ -205,12 +202,11 @@ class UserApi {
|
||||
$queryParams = array();
|
||||
$headerParams = array();
|
||||
$formParams = array();
|
||||
$_header_accept = 'application/json, application/xml';
|
||||
if ($_header_accept !== '') {
|
||||
$_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
|
||||
if (!is_null($_header_accept)) {
|
||||
$headerParams['Accept'] = $_header_accept;
|
||||
}
|
||||
$_header_content_type = array();
|
||||
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
|
||||
$headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array());
|
||||
|
||||
// query params
|
||||
if($username !== null) {
|
||||
@@ -264,12 +260,11 @@ class UserApi {
|
||||
$queryParams = array();
|
||||
$headerParams = array();
|
||||
$formParams = array();
|
||||
$_header_accept = 'application/json, application/xml';
|
||||
if ($_header_accept !== '') {
|
||||
$_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
|
||||
if (!is_null($_header_accept)) {
|
||||
$headerParams['Accept'] = $_header_accept;
|
||||
}
|
||||
$_header_content_type = array();
|
||||
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
|
||||
$headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array());
|
||||
|
||||
|
||||
|
||||
@@ -317,12 +312,11 @@ class UserApi {
|
||||
$queryParams = array();
|
||||
$headerParams = array();
|
||||
$formParams = array();
|
||||
$_header_accept = 'application/json, application/xml';
|
||||
if ($_header_accept !== '') {
|
||||
$_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
|
||||
if (!is_null($_header_accept)) {
|
||||
$headerParams['Accept'] = $_header_accept;
|
||||
}
|
||||
$_header_content_type = array();
|
||||
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
|
||||
$headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array());
|
||||
|
||||
|
||||
|
||||
@@ -381,12 +375,11 @@ class UserApi {
|
||||
$queryParams = array();
|
||||
$headerParams = array();
|
||||
$formParams = array();
|
||||
$_header_accept = 'application/json, application/xml';
|
||||
if ($_header_accept !== '') {
|
||||
$_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
|
||||
if (!is_null($_header_accept)) {
|
||||
$headerParams['Accept'] = $_header_accept;
|
||||
}
|
||||
$_header_content_type = array();
|
||||
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
|
||||
$headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array());
|
||||
|
||||
|
||||
|
||||
@@ -442,12 +435,11 @@ class UserApi {
|
||||
$queryParams = array();
|
||||
$headerParams = array();
|
||||
$formParams = array();
|
||||
$_header_accept = 'application/json, application/xml';
|
||||
if ($_header_accept !== '') {
|
||||
$_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
|
||||
if (!is_null($_header_accept)) {
|
||||
$headerParams['Accept'] = $_header_accept;
|
||||
}
|
||||
$_header_content_type = array();
|
||||
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
|
||||
$headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array());
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -31,6 +31,22 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
|
||||
$add_response = $pet_api->addPet($new_pet);
|
||||
}
|
||||
|
||||
// test static functions defined in APIClient
|
||||
public function testAPIClient()
|
||||
{
|
||||
# test selectHeaderAccept
|
||||
$this->assertSame('application/json', SwaggerClient\APIClient::selectHeaderAccept(array('application/xml','application/json')));
|
||||
$this->assertSame(NULL, SwaggerClient\APIClient::selectHeaderAccept(array()));
|
||||
$this->assertSame('application/yaml,application/xml', SwaggerClient\APIClient::selectHeaderAccept(array('application/yaml','application/xml')));
|
||||
|
||||
# test selectHeaderContentType
|
||||
$this->assertSame('application/json', SwaggerClient\APIClient::selectHeaderContentType(array('application/xml','application/json')));
|
||||
$this->assertSame('application/json', SwaggerClient\APIClient::selectHeaderContentType(array()));
|
||||
$this->assertSame('application/yaml,application/xml', SwaggerClient\APIClient::selectHeaderContentType(array('application/yaml','application/xml')));
|
||||
|
||||
|
||||
}
|
||||
|
||||
// test getPetById with a Pet object (id 10005)
|
||||
public function testGetPetById()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user