better accept and content-type for php, added test cases

This commit is contained in:
wing328
2015-05-18 22:16:54 +08:00
parent 72a4d2df2f
commit 41e178e268
7 changed files with 149 additions and 91 deletions

View File

@@ -261,7 +261,6 @@ class APIClient {
* @param string $class class name is passed as a string * @param string $class class name is passed as a string
* @return object an instance of $class * @return object an instance of $class
*/ */
public static function deserialize($data, $class) public static function deserialize($data, $class)
{ {
if (null === $data) { if (null === $data) {
@@ -304,5 +303,37 @@ class APIClient {
return $deserialized; 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);
}
}
} }

View File

@@ -54,12 +54,11 @@ class {{classname}} {
$queryParams = array(); $queryParams = array();
$headerParams = array(); $headerParams = array();
$formParams = array(); $formParams = array();
$_header_accept = '{{#produces}}{{mediaType}}{{#hasMore}}, {{/hasMore}}{{/produces}}'; $_header_accept = $this->apiClient->selectHeaderAccept(array({{#produces}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/produces}}));
if ($_header_accept !== '') { if (!is_null($_header_accept)) {
$headerParams['Accept'] = $_header_accept; $headerParams['Accept'] = $_header_accept;
} }
$_header_content_type = array({{#consumes}}'{{mediaType}}'{{#hasMore}},{{/hasMore}}{{/consumes}}); $headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array({{#consumes}}'{{mediaType}}'{{#hasMore}},{{/hasMore}}{{/consumes}}));
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
{{#queryParams}}// query params {{#queryParams}}// query params
if(${{paramName}} !== null) { if(${{paramName}} !== null) {

View File

@@ -142,20 +142,21 @@ class APIClient {
$response_info = curl_getinfo($curl); $response_info = curl_getinfo($curl);
// Handle the response // Handle the response
if ($response === false) { // error, likely in the client side if ($response_info['http_code'] == 0) {
throw new APIClientException("API Error ($url): ".curl_error($curl), 0, $response_info, $response); 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 ) { } else if ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299 ) {
$data = json_decode($response); $data = json_decode($response);
if (json_last_error() > 0) { // if response is a string if (json_last_error() > 0) { // if response is a string
$data = $response; $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 . throw new APIClientException("Unauthorized API request to " . $url .
": " . serialize($response), 0, $response_info, $response); ": " . 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; $data = null;
} else { } else {
throw new APIClientException("Can't connect to the API: " . $url . throw new APIClientException("Can't connect to the api: " . $url .
" response code: " . " response code: " .
$response_info['http_code'], 0, $response_info, $response); $response_info['http_code'], 0, $response_info, $response);
} }
@@ -260,7 +261,6 @@ class APIClient {
* @param string $class class name is passed as a string * @param string $class class name is passed as a string
* @return object an instance of $class * @return object an instance of $class
*/ */
public static function deserialize($data, $class) public static function deserialize($data, $class)
{ {
if (null === $data) { if (null === $data) {
@@ -303,5 +303,37 @@ class APIClient {
return $deserialized; 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);
}
}
} }

View File

@@ -48,12 +48,11 @@ class PetApi {
$queryParams = array(); $queryParams = array();
$headerParams = array(); $headerParams = array();
$formParams = array(); $formParams = array();
$_header_accept = 'application/json, application/xml'; $_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
if ($_header_accept !== '') { if (!is_null($_header_accept)) {
$headerParams['Accept'] = $_header_accept; $headerParams['Accept'] = $_header_accept;
} }
$_header_content_type = array('application/json','application/xml',); $headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array('application/json','application/xml',));
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
@@ -100,12 +99,11 @@ class PetApi {
$queryParams = array(); $queryParams = array();
$headerParams = array(); $headerParams = array();
$formParams = array(); $formParams = array();
$_header_accept = 'application/json, application/xml'; $_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
if ($_header_accept !== '') { if (!is_null($_header_accept)) {
$headerParams['Accept'] = $_header_accept; $headerParams['Accept'] = $_header_accept;
} }
$_header_content_type = array('application/json','application/xml',); $headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array('application/json','application/xml',));
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
@@ -152,12 +150,11 @@ class PetApi {
$queryParams = array(); $queryParams = array();
$headerParams = array(); $headerParams = array();
$formParams = array(); $formParams = array();
$_header_accept = 'application/json, application/xml'; $_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
if ($_header_accept !== '') { if (!is_null($_header_accept)) {
$headerParams['Accept'] = $_header_accept; $headerParams['Accept'] = $_header_accept;
} }
$_header_content_type = array(); $headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array());
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
// query params // query params
if($status !== null) { if($status !== null) {
@@ -209,12 +206,11 @@ class PetApi {
$queryParams = array(); $queryParams = array();
$headerParams = array(); $headerParams = array();
$formParams = array(); $formParams = array();
$_header_accept = 'application/json, application/xml'; $_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
if ($_header_accept !== '') { if (!is_null($_header_accept)) {
$headerParams['Accept'] = $_header_accept; $headerParams['Accept'] = $_header_accept;
} }
$_header_content_type = array(); $headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array());
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
// query params // query params
if($tags !== null) { if($tags !== null) {
@@ -271,12 +267,11 @@ class PetApi {
$queryParams = array(); $queryParams = array();
$headerParams = array(); $headerParams = array();
$formParams = array(); $formParams = array();
$_header_accept = 'application/json, application/xml'; $_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
if ($_header_accept !== '') { if (!is_null($_header_accept)) {
$headerParams['Accept'] = $_header_accept; $headerParams['Accept'] = $_header_accept;
} }
$_header_content_type = array(); $headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array());
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
@@ -336,12 +331,11 @@ class PetApi {
$queryParams = array(); $queryParams = array();
$headerParams = array(); $headerParams = array();
$formParams = array(); $formParams = array();
$_header_accept = 'application/json, application/xml'; $_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
if ($_header_accept !== '') { if (!is_null($_header_accept)) {
$headerParams['Accept'] = $_header_accept; $headerParams['Accept'] = $_header_accept;
} }
$_header_content_type = array('application/x-www-form-urlencoded',); $headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array('application/x-www-form-urlencoded',));
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
@@ -400,12 +394,11 @@ class PetApi {
$queryParams = array(); $queryParams = array();
$headerParams = array(); $headerParams = array();
$formParams = array(); $formParams = array();
$_header_accept = 'application/json, application/xml'; $_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
if ($_header_accept !== '') { if (!is_null($_header_accept)) {
$headerParams['Accept'] = $_header_accept; $headerParams['Accept'] = $_header_accept;
} }
$_header_content_type = array(); $headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array());
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
// header params // header params
@@ -462,12 +455,11 @@ class PetApi {
$queryParams = array(); $queryParams = array();
$headerParams = array(); $headerParams = array();
$formParams = array(); $formParams = array();
$_header_accept = 'application/json, application/xml'; $_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
if ($_header_accept !== '') { if (!is_null($_header_accept)) {
$headerParams['Accept'] = $_header_accept; $headerParams['Accept'] = $_header_accept;
} }
$_header_content_type = array('multipart/form-data',); $headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array('multipart/form-data',));
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';

View File

@@ -47,12 +47,11 @@ class StoreApi {
$queryParams = array(); $queryParams = array();
$headerParams = array(); $headerParams = array();
$formParams = array(); $formParams = array();
$_header_accept = 'application/json, application/xml'; $_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
if ($_header_accept !== '') { if (!is_null($_header_accept)) {
$headerParams['Accept'] = $_header_accept; $headerParams['Accept'] = $_header_accept;
} }
$_header_content_type = array(); $headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array());
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
@@ -101,12 +100,11 @@ class StoreApi {
$queryParams = array(); $queryParams = array();
$headerParams = array(); $headerParams = array();
$formParams = array(); $formParams = array();
$_header_accept = 'application/json, application/xml'; $_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
if ($_header_accept !== '') { if (!is_null($_header_accept)) {
$headerParams['Accept'] = $_header_accept; $headerParams['Accept'] = $_header_accept;
} }
$_header_content_type = array(); $headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array());
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
@@ -164,12 +162,11 @@ class StoreApi {
$queryParams = array(); $queryParams = array();
$headerParams = array(); $headerParams = array();
$formParams = array(); $formParams = array();
$_header_accept = 'application/json, application/xml'; $_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
if ($_header_accept !== '') { if (!is_null($_header_accept)) {
$headerParams['Accept'] = $_header_accept; $headerParams['Accept'] = $_header_accept;
} }
$_header_content_type = array(); $headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array());
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
@@ -227,12 +224,11 @@ class StoreApi {
$queryParams = array(); $queryParams = array();
$headerParams = array(); $headerParams = array();
$formParams = array(); $formParams = array();
$_header_accept = 'application/json, application/xml'; $_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
if ($_header_accept !== '') { if (!is_null($_header_accept)) {
$headerParams['Accept'] = $_header_accept; $headerParams['Accept'] = $_header_accept;
} }
$_header_content_type = array(); $headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array());
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';

View File

@@ -48,12 +48,11 @@ class UserApi {
$queryParams = array(); $queryParams = array();
$headerParams = array(); $headerParams = array();
$formParams = array(); $formParams = array();
$_header_accept = 'application/json, application/xml'; $_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
if ($_header_accept !== '') { if (!is_null($_header_accept)) {
$headerParams['Accept'] = $_header_accept; $headerParams['Accept'] = $_header_accept;
} }
$_header_content_type = array(); $headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array());
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
@@ -100,12 +99,11 @@ class UserApi {
$queryParams = array(); $queryParams = array();
$headerParams = array(); $headerParams = array();
$formParams = array(); $formParams = array();
$_header_accept = 'application/json, application/xml'; $_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
if ($_header_accept !== '') { if (!is_null($_header_accept)) {
$headerParams['Accept'] = $_header_accept; $headerParams['Accept'] = $_header_accept;
} }
$_header_content_type = array(); $headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array());
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
@@ -152,12 +150,11 @@ class UserApi {
$queryParams = array(); $queryParams = array();
$headerParams = array(); $headerParams = array();
$formParams = array(); $formParams = array();
$_header_accept = 'application/json, application/xml'; $_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
if ($_header_accept !== '') { if (!is_null($_header_accept)) {
$headerParams['Accept'] = $_header_accept; $headerParams['Accept'] = $_header_accept;
} }
$_header_content_type = array(); $headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array());
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
@@ -205,12 +202,11 @@ class UserApi {
$queryParams = array(); $queryParams = array();
$headerParams = array(); $headerParams = array();
$formParams = array(); $formParams = array();
$_header_accept = 'application/json, application/xml'; $_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
if ($_header_accept !== '') { if (!is_null($_header_accept)) {
$headerParams['Accept'] = $_header_accept; $headerParams['Accept'] = $_header_accept;
} }
$_header_content_type = array(); $headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array());
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
// query params // query params
if($username !== null) { if($username !== null) {
@@ -264,12 +260,11 @@ class UserApi {
$queryParams = array(); $queryParams = array();
$headerParams = array(); $headerParams = array();
$formParams = array(); $formParams = array();
$_header_accept = 'application/json, application/xml'; $_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
if ($_header_accept !== '') { if (!is_null($_header_accept)) {
$headerParams['Accept'] = $_header_accept; $headerParams['Accept'] = $_header_accept;
} }
$_header_content_type = array(); $headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array());
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
@@ -317,12 +312,11 @@ class UserApi {
$queryParams = array(); $queryParams = array();
$headerParams = array(); $headerParams = array();
$formParams = array(); $formParams = array();
$_header_accept = 'application/json, application/xml'; $_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
if ($_header_accept !== '') { if (!is_null($_header_accept)) {
$headerParams['Accept'] = $_header_accept; $headerParams['Accept'] = $_header_accept;
} }
$_header_content_type = array(); $headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array());
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
@@ -381,12 +375,11 @@ class UserApi {
$queryParams = array(); $queryParams = array();
$headerParams = array(); $headerParams = array();
$formParams = array(); $formParams = array();
$_header_accept = 'application/json, application/xml'; $_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
if ($_header_accept !== '') { if (!is_null($_header_accept)) {
$headerParams['Accept'] = $_header_accept; $headerParams['Accept'] = $_header_accept;
} }
$_header_content_type = array(); $headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array());
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
@@ -442,12 +435,11 @@ class UserApi {
$queryParams = array(); $queryParams = array();
$headerParams = array(); $headerParams = array();
$formParams = array(); $formParams = array();
$_header_accept = 'application/json, application/xml'; $_header_accept = $this->apiClient->selectHeaderAccept(array('application/json', 'application/xml'));
if ($_header_accept !== '') { if (!is_null($_header_accept)) {
$headerParams['Accept'] = $_header_accept; $headerParams['Accept'] = $_header_accept;
} }
$_header_content_type = array(); $headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array());
$headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';

View File

@@ -31,6 +31,22 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
$add_response = $pet_api->addPet($new_pet); $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) // test getPetById with a Pet object (id 10005)
public function testGetPetById() public function testGetPetById()
{ {