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 * @return string the serialized object
*/ */
public static function toPathValue($value) { public static function toPathValue($value) {
return rawurlencode($value); return rawurlencode(toString($value));
} }
/** /**
@ -193,20 +193,48 @@ class APIClient {
if (is_array($object)) { if (is_array($object)) {
return implode(',', $object); return implode(',', $object);
} else { } else {
return $object; return toString($object);
} }
} }
/** /**
* Just pass through the header value for now. Placeholder in case we * Take value and turn it into a string suitable for inclusion in
* find out we need to do something with header values. * 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 * @param string $value a string which will be part of the header
* @return string the header string * @return string the header string
*/ */
public static function toHeaderValue($value) { public static function toHeaderValue($value) {
return $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 * Deserialize a JSON string into an object
* *

View File

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

View File

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

View File

@ -127,19 +127,19 @@ class APIClient {
// Handle the response // Handle the response
if ($response_info['http_code'] == 0) { if ($response_info['http_code'] == 0) {
throw new Exception("TIMEOUT: api call to " . $url . throw new APIClientException("TIMEOUT: api call to " . $url .
" took more than 5s to return" ); " took more than 5s to return", 0, $response_info, $response);
} else if ($response_info['http_code'] == 200) { } else if ($response_info['http_code'] == 200) {
$data = json_decode($response); $data = json_decode($response);
} else if ($response_info['http_code'] == 401) { } else if ($response_info['http_code'] == 401) {
throw new Exception("Unauthorized API request to " . $url . throw new APIClientException("Unauthorized API request to " . $url .
": ".json_decode($response)->message ); ": " . json_decode($response)->message, 0, $response_info, $response);
} else if ($response_info['http_code'] == 404) { } else if ($response_info['http_code'] == 404) {
$data = null; $data = null;
} else { } else {
throw new Exception("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']); $response_info['http_code'], 0, $response_info, $response);
} }
return $data; return $data;
} }
@ -178,7 +178,7 @@ class APIClient {
* @return string the serialized object * @return string the serialized object
*/ */
public static function toPathValue($value) { public static function toPathValue($value) {
return rawurlencode($value); return rawurlencode(toString($value));
} }
/** /**
@ -193,20 +193,48 @@ class APIClient {
if (is_array($object)) { if (is_array($object)) {
return implode(',', $object); return implode(',', $object);
} else { } else {
return $object; return toString($object);
} }
} }
/** /**
* Just pass through the header value for now. Placeholder in case we * Take value and turn it into a string suitable for inclusion in
* find out we need to do something with header values. * 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 * @param string $value a string which will be part of the header
* @return string the header string * @return string the header string
*/ */
public static function toHeaderValue($value) { public static function toHeaderValue($value) {
return $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 * 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;
}
}