Merge pull request #504 from wing328/php_param_supppor_datetime

PHP parameters to suppport datetime (ISO8601)
This commit is contained in:
Tony Tam 2015-03-14 22:41:54 -07:00
commit ad7a5967ff
4 changed files with 94 additions and 21 deletions

View File

@ -178,7 +178,7 @@ class APIClient {
* @return string the serialized object
*/
public static function toPathValue($value) {
return rawurlencode($value);
return rawurlencode(toString($value));
}
/**
@ -193,19 +193,47 @@ class APIClient {
if (is_array($object)) {
return implode(',', $object);
} else {
return $object;
return toString($object);
}
}
/**
* Just pass through the header value for now. Placeholder in case we
* find out we need to do something with header values.
* 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 static function toHeaderValue($value) {
return 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 static function toFormValue($value) {
return 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 static 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

View File

@ -63,7 +63,7 @@ class {{classname}} {
}{{/pathParams}}
{{#formParams}}// form params
if (${{paramName}} !== null) {
$formParams['{{baseName}}'] = {{#isFile}}'@' . {{/isFile}}${{paramName}};
$formParams['{{baseName}}'] = {{#isFile}}'@' . {{/isFile}}$this->apiClient->toFormValue(${{paramName}});
}{{/formParams}}
{{#bodyParams}}// body params
$body = null;

View File

@ -304,10 +304,10 @@ class PetApi {
}
// form params
if ($name !== null) {
$formParams['name'] = $name;
$formParams['name'] = $this->apiClient->toFormValue($name);
}// form params
if ($status !== null) {
$formParams['status'] = $status;
$formParams['status'] = $this->apiClient->toFormValue($status);
}
@ -408,10 +408,10 @@ class PetApi {
}
// form params
if ($additionalMetadata !== null) {
$formParams['additionalMetadata'] = $additionalMetadata;
$formParams['additionalMetadata'] = $this->apiClient->toFormValue($additionalMetadata);
}// form params
if ($file !== null) {
$formParams['file'] = '@' . $file;
$formParams['file'] = '@' . $this->apiClient->toFormValue($file);
}

View File

@ -127,19 +127,19 @@ class APIClient {
// Handle the response
if ($response_info['http_code'] == 0) {
throw new Exception("TIMEOUT: api call to " . $url .
" took more than 5s to return" );
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) {
$data = json_decode($response);
} else if ($response_info['http_code'] == 401) {
throw new Exception("Unauthorized API request to " . $url .
": ".json_decode($response)->message );
throw new APIClientException("Unauthorized API request to " . $url .
": " . json_decode($response)->message, 0, $response_info, $response);
} else if ($response_info['http_code'] == 404) {
$data = null;
} else {
throw new Exception("Can't connect to the api: " . $url .
throw new APIClientException("Can't connect to the api: " . $url .
" response code: " .
$response_info['http_code']);
$response_info['http_code'], 0, $response_info, $response);
}
return $data;
}
@ -178,7 +178,7 @@ class APIClient {
* @return string the serialized object
*/
public static function toPathValue($value) {
return rawurlencode($value);
return rawurlencode(toString($value));
}
/**
@ -193,19 +193,47 @@ class APIClient {
if (is_array($object)) {
return implode(',', $object);
} else {
return $object;
return toString($object);
}
}
/**
* Just pass through the header value for now. Placeholder in case we
* find out we need to do something with header values.
* 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 static function toHeaderValue($value) {
return 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 static function toFormValue($value) {
return 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 static 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
@ -257,3 +285,20 @@ class APIClient {
}
class APIClientException extends Exception {
protected $response, $response_info;
public function __construct($message="", $code=0, $response_info=null, $response=null) {
parent::__construct($message, $code);
$this->response_info = $response_info;
$this->response = $response;
}
public function getResponse() {
return $this->response;
}
public function getResponseInfo() {
return $this->response_info;
}
}