From a0dc2097eb2f6e947fc0e58e4c05163f814ff011 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Sat, 14 Mar 2015 23:48:05 +0800 Subject: [PATCH] update php api client to support datetime (iso8601) for parameters (header, path, url, form) --- .../src/main/resources/php/Swagger.mustache | 38 +++++++++-- .../src/main/resources/php/api.mustache | 2 +- samples/client/petstore/php/PetApi.php | 8 +-- samples/client/petstore/php/Swagger.php | 67 ++++++++++++++++--- 4 files changed, 94 insertions(+), 21 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/php/Swagger.mustache b/modules/swagger-codegen/src/main/resources/php/Swagger.mustache index d332cbc2e71..1247be83f3f 100644 --- a/modules/swagger-codegen/src/main/resources/php/Swagger.mustache +++ b/modules/swagger-codegen/src/main/resources/php/Swagger.mustache @@ -178,7 +178,7 @@ class APIClient { * @return string the serialized object */ public static function toPathValue($value) { - return rawurlencode($value); + return rawurlencode(toString($value)); } /** @@ -193,20 +193,48 @@ 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 $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 * diff --git a/modules/swagger-codegen/src/main/resources/php/api.mustache b/modules/swagger-codegen/src/main/resources/php/api.mustache index 7a36c9aa0a3..bdf66defcaf 100644 --- a/modules/swagger-codegen/src/main/resources/php/api.mustache +++ b/modules/swagger-codegen/src/main/resources/php/api.mustache @@ -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; diff --git a/samples/client/petstore/php/PetApi.php b/samples/client/petstore/php/PetApi.php index 8c7d74cb474..8993a609595 100644 --- a/samples/client/petstore/php/PetApi.php +++ b/samples/client/petstore/php/PetApi.php @@ -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); } diff --git a/samples/client/petstore/php/Swagger.php b/samples/client/petstore/php/Swagger.php index b65add845c8..1247be83f3f 100644 --- a/samples/client/petstore/php/Swagger.php +++ b/samples/client/petstore/php/Swagger.php @@ -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,20 +193,48 @@ 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 $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; + } +}