From 7555dda2bfffacceb2746c1a0d94620f202a0d50 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Wed, 11 Mar 2015 01:37:39 +0800 Subject: [PATCH 1/3] add logic to handle all 2xx response code, bug fix for string response, bug fix for showing error message using serialize --- .../src/main/resources/php/Swagger.mustache | 7 +++++-- samples/client/petstore/php/Swagger.php | 10 ++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/php/Swagger.mustache b/modules/swagger-codegen/src/main/resources/php/Swagger.mustache index d797bb53e9e..8aed52e2909 100644 --- a/modules/swagger-codegen/src/main/resources/php/Swagger.mustache +++ b/modules/swagger-codegen/src/main/resources/php/Swagger.mustache @@ -129,11 +129,14 @@ class APIClient { if ($response_info['http_code'] == 0) { throw new Exception("TIMEOUT: api call to " . $url . " took more than 5s to return" ); - } else if ($response_info['http_code'] == 200) { + } 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) { throw new Exception("Unauthorized API request to " . $url . - ": ".json_decode($response)->message ); + ": ".serialize($response)); } else if ($response_info['http_code'] == 404) { $data = null; } else { diff --git a/samples/client/petstore/php/Swagger.php b/samples/client/petstore/php/Swagger.php index a7a18866345..8aed52e2909 100644 --- a/samples/client/petstore/php/Swagger.php +++ b/samples/client/petstore/php/Swagger.php @@ -107,6 +107,9 @@ class APIClient { if ($method == self::$POST) { curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $postData); + } else if ($method == self::$PATCH) { + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH"); + curl_setopt($curl, CURLOPT_POSTFIELDS, $postData); } else if ($method == self::$PUT) { curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($curl, CURLOPT_POSTFIELDS, $postData); @@ -126,11 +129,14 @@ class APIClient { if ($response_info['http_code'] == 0) { throw new Exception("TIMEOUT: api call to " . $url . " took more than 5s to return" ); - } else if ($response_info['http_code'] == 200) { + } 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) { throw new Exception("Unauthorized API request to " . $url . - ": ".json_decode($response)->message ); + ": ".serialize($response)); } else if ($response_info['http_code'] == 404) { $data = null; } else { From 6bc8de1230342be471e70f62c5515e4881ee4ad6 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Sun, 15 Mar 2015 22:02:16 +0800 Subject: [PATCH 2/3] rebase on develop_2.0 --- .../src/main/resources/php/Swagger.mustache | 7 +++++-- samples/client/petstore/php/Swagger.php | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/php/Swagger.mustache b/modules/swagger-codegen/src/main/resources/php/Swagger.mustache index 1247be83f3f..5c6ac134307 100644 --- a/modules/swagger-codegen/src/main/resources/php/Swagger.mustache +++ b/modules/swagger-codegen/src/main/resources/php/Swagger.mustache @@ -129,11 +129,14 @@ class APIClient { 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) { + } 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) { throw new APIClientException("Unauthorized API request to " . $url . - ": " . json_decode($response)->message, 0, $response_info, $response); + ": " . serialize($response), 0, $response_info, $response); } else if ($response_info['http_code'] == 404) { $data = null; } else { diff --git a/samples/client/petstore/php/Swagger.php b/samples/client/petstore/php/Swagger.php index 1247be83f3f..1d5e7dbda50 100644 --- a/samples/client/petstore/php/Swagger.php +++ b/samples/client/petstore/php/Swagger.php @@ -127,13 +127,27 @@ class APIClient { // Handle the response if ($response_info['http_code'] == 0) { +<<<<<<< HEAD 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) { +======= + throw new Exception("TIMEOUT: api call to " . $url . + " took more than 5s to return" ); + } else if ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299 ) { +>>>>>>> add logic to handle all 2xx response code, bug fix for string response, $data = json_decode($response); + if (json_last_error() > 0) { // if response is a string + $data = $response; + } } else if ($response_info['http_code'] == 401) { +<<<<<<< HEAD throw new APIClientException("Unauthorized API request to " . $url . ": " . json_decode($response)->message, 0, $response_info, $response); +======= + throw new Exception("Unauthorized API request to " . $url . + ": ".serialize($response)); +>>>>>>> add logic to handle all 2xx response code, bug fix for string response, } else if ($response_info['http_code'] == 404) { $data = null; } else { From bcdee4e03822d6356ed175fc61aa9706b966e267 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Sun, 15 Mar 2015 22:06:42 +0800 Subject: [PATCH 3/3] update php petstore sample code --- samples/client/petstore/php/Swagger.php | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/samples/client/petstore/php/Swagger.php b/samples/client/petstore/php/Swagger.php index 1d5e7dbda50..5c6ac134307 100644 --- a/samples/client/petstore/php/Swagger.php +++ b/samples/client/petstore/php/Swagger.php @@ -127,27 +127,16 @@ class APIClient { // Handle the response if ($response_info['http_code'] == 0) { -<<<<<<< HEAD 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) { -======= - throw new Exception("TIMEOUT: api call to " . $url . - " took more than 5s to return" ); } else if ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299 ) { ->>>>>>> add logic to handle all 2xx response code, bug fix for string response, $data = json_decode($response); if (json_last_error() > 0) { // if response is a string $data = $response; } } else if ($response_info['http_code'] == 401) { -<<<<<<< HEAD throw new APIClientException("Unauthorized API request to " . $url . - ": " . json_decode($response)->message, 0, $response_info, $response); -======= - throw new Exception("Unauthorized API request to " . $url . - ": ".serialize($response)); ->>>>>>> add logic to handle all 2xx response code, bug fix for string response, + ": " . serialize($response), 0, $response_info, $response); } else if ($response_info['http_code'] == 404) { $data = null; } else {