Skip to content

Commit 7080983

Browse files
committed
return exception instead of null for 404, add properties to api exception class
1 parent 412fcf1 commit 7080983

File tree

8 files changed

+606
-71
lines changed

8 files changed

+606
-71
lines changed

'

Lines changed: 458 additions & 0 deletions
Large diffs are not rendered by default.

modules/swagger-codegen/src/main/resources/php/APIClient.mustache

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -245,33 +245,32 @@ class ApiClient {
245245
curl_setopt($curl, CURLOPT_VERBOSE, 0);
246246
}
247247

248+
// obtain the HTTP response headers
249+
curl_setopt($curl, CURLOPT_HEADER, 1);
250+
248251
// Make the request
249252
$response = curl_exec($curl);
253+
$http_header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
254+
$http_header = substr($response, 0, $http_header_size);
255+
$http_body = substr($response, $http_header_size);
250256
$response_info = curl_getinfo($curl);
251257

252258
// debug HTTP response body
253259
if (Configuration::$debug) {
254-
error_log("[DEBUG] HTTP Response body ~BEGIN~\n".print_r($response, true)."\n~END~\n", 3, Configuration::$debug_file);
260+
error_log("[DEBUG] HTTP Response body ~BEGIN~\n".print_r($http_body, true)."\n~END~\n", 3, Configuration::$debug_file);
255261
}
256262

257263
// Handle the response
258264
if ($response_info['http_code'] == 0) {
259-
throw new ApiException("TIMEOUT: api call to " . $url .
260-
" took more than 5s to return", 0, $response_info, $response);
265+
throw new ApiException("API call to $url timed out: ".serialize($response_info), 0, null, null);
261266
} else if ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299 ) {
262-
$data = json_decode($response);
267+
$data = json_decode($http_body);
263268
if (json_last_error() > 0) { // if response is a string
264-
$data = $response;
269+
$data = $http_body;
265270
}
266-
} else if ($response_info['http_code'] == 401) {
267-
throw new ApiException("Unauthorized API request to " . $url .
268-
": " . serialize($response), 0, $response_info, $response);
269-
} else if ($response_info['http_code'] == 404) {
270-
$data = null;
271271
} else {
272-
throw new ApiException("Can't connect to the api: " . $url .
273-
" response code: " .
274-
$response_info['http_code'], 0, $response_info, $response);
272+
throw new ApiException("[".$response_info['http_code']."] Error connecting to the API ($url)",
273+
$response_info['http_code'], $http_header, $http_body);
275274
}
276275
return $data;
277276
}

modules/swagger-codegen/src/main/resources/php/ApiException.mustache

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,39 @@ namespace {{invokerPackage}};
2020
use \Exception;
2121

2222
class ApiException extends Exception {
23-
protected $response, $response_info;
2423
25-
public function __construct($message="", $code=0, $response_info=null, $response=null) {
24+
/**
25+
* The HTTP body of the server response.
26+
*/
27+
protected $response_body;
28+
29+
/**
30+
* The HTTP header of the server response.
31+
*/
32+
protected $response_headers;
33+
34+
public function __construct($message="", $code=0, $responseHeaders=null, $responseBody=null) {
2635
parent::__construct($message, $code);
27-
$this->response_info = $response_info;
28-
$this->response = $response;
36+
$this->response_headers = $responseHeaders;
37+
$this->response_body = $responseBody;
2938
}
3039

31-
public function getResponse() {
32-
return $this->response;
40+
/**
41+
* Get the HTTP response header
42+
*
43+
* @return string HTTP response header
44+
*/
45+
public function getResponseHeaders() {
46+
return $this->response_headers;
3347
}
3448

35-
public function getResponseInfo() {
36-
return $this->response_info;
49+
/**
50+
* Get the HTTP response body
51+
*
52+
* @return string HTTP response body
53+
*/
54+
public function getResponseBody() {
55+
return $this->response_body;
3756
}
57+
3858
}

modules/swagger-codegen/src/main/resources/php/configuration.mustache

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,44 @@ namespace {{invokerPackage}};
1919

2020
class Configuration {
2121
22-
public static $PATCH = "PATCH";
23-
public static $POST = "POST";
24-
public static $GET = "GET";
25-
public static $PUT = "PUT";
26-
public static $DELETE = "DELETE";
27-
28-
// authentication setting
22+
/**
23+
* Associate array to store API key(s)
24+
*/
2925
public static $apiKey = array();
26+
27+
/**
28+
* Associate array to store API prefix (e.g. Bearer)
29+
*/
3030
public static $apiKeyPrefix = array();
31+
32+
/**
33+
* Username for HTTP basic authentication
34+
*/
3135
public static $username = '';
36+
37+
/**
38+
* Password for HTTP basic authentication
39+
*/
3240
public static $password = '';
3341
34-
// an instance of ApiClient
42+
/**
43+
* The default instance of ApiClient
44+
*/
3545
public static $apiClient;
3646
37-
// debugging
38-
public static $debug = false; // by default debugging is disabled
39-
public static $debug_file = 'php://output'; //output debug log to STDOUT by default
47+
/**
48+
* Debug switch (default set to false)
49+
*/
50+
public static $debug = false;
51+
52+
/**
53+
* Debug file location (log to STDOUT by default)
54+
*/
55+
public static $debug_file = 'php://output';
4056
41-
/*
42-
* manually initalize Api client
43-
*/
57+
/*
58+
* manually initalize ApiClient
59+
*/
4460
public static function init() {
4561
if (self::$apiClient === null)
4662
self::$apiClient = new ApiClient();

samples/client/petstore/php/SwaggerClient-php/lib/APIClient.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -250,33 +250,32 @@ public function callApi($resourcePath, $method, $queryParams, $postData,
250250
curl_setopt($curl, CURLOPT_VERBOSE, 0);
251251
}
252252

253+
// obtain the HTTP response headers
254+
curl_setopt($curl, CURLOPT_HEADER, 1);
255+
253256
// Make the request
254257
$response = curl_exec($curl);
258+
$http_header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
259+
$http_header = substr($response, 0, $http_header_size);
260+
$http_body = substr($response, $http_header_size);
255261
$response_info = curl_getinfo($curl);
256262

257263
// debug HTTP response body
258264
if (Configuration::$debug) {
259-
error_log("[DEBUG] HTTP Response body ~BEGIN~\n".print_r($response, true)."\n~END~\n", 3, Configuration::$debug_file);
265+
error_log("[DEBUG] HTTP Response body ~BEGIN~\n".print_r($http_body, true)."\n~END~\n", 3, Configuration::$debug_file);
260266
}
261267

262268
// Handle the response
263269
if ($response_info['http_code'] == 0) {
264-
throw new ApiException("TIMEOUT: api call to " . $url .
265-
" took more than 5s to return", 0, $response_info, $response);
270+
throw new ApiException("API call to $url timed out: ".serialize($response_info), 0, null, null);
266271
} else if ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299 ) {
267-
$data = json_decode($response);
272+
$data = json_decode($http_body);
268273
if (json_last_error() > 0) { // if response is a string
269-
$data = $response;
274+
$data = $http_body;
270275
}
271-
} else if ($response_info['http_code'] == 401) {
272-
throw new ApiException("Unauthorized API request to " . $url .
273-
": " . serialize($response), 0, $response_info, $response);
274-
} else if ($response_info['http_code'] == 404) {
275-
$data = null;
276276
} else {
277-
throw new ApiException("Can't connect to the api: " . $url .
278-
" response code: " .
279-
$response_info['http_code'], 0, $response_info, $response);
277+
throw new ApiException("[".$response_info['http_code']."] Error connecting to the API ($url)",
278+
$response_info['http_code'], $http_header, $http_body);
280279
}
281280
return $data;
282281
}

samples/client/petstore/php/SwaggerClient-php/lib/ApiException.php

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,39 @@
2020
use \Exception;
2121

2222
class ApiException extends Exception {
23-
protected $response, $response_info;
2423

25-
public function __construct($message="", $code=0, $response_info=null, $response=null) {
24+
/**
25+
* The HTTP body of the server response.
26+
*/
27+
protected $response_body;
28+
29+
/**
30+
* The HTTP header of the server response.
31+
*/
32+
protected $response_headers;
33+
34+
public function __construct($message="", $code=0, $responseHeaders=null, $responseBody=null) {
2635
parent::__construct($message, $code);
27-
$this->response_info = $response_info;
28-
$this->response = $response;
36+
$this->response_headers = $responseHeaders;
37+
$this->response_body = $responseBody;
2938
}
3039

31-
public function getResponse() {
32-
return $this->response;
40+
/**
41+
* Get the HTTP response header
42+
*
43+
* @return string HTTP response header
44+
*/
45+
public function getResponseHeaders() {
46+
return $this->response_headers;
3347
}
3448

35-
public function getResponseInfo() {
36-
return $this->response_info;
49+
/**
50+
* Get the HTTP response body
51+
*
52+
* @return string HTTP response body
53+
*/
54+
public function getResponseBody() {
55+
return $this->response_body;
3756
}
57+
3858
}

samples/client/petstore/php/SwaggerClient-php/lib/Configuration.php

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,44 @@
1919

2020
class Configuration {
2121

22-
public static $PATCH = "PATCH";
23-
public static $POST = "POST";
24-
public static $GET = "GET";
25-
public static $PUT = "PUT";
26-
public static $DELETE = "DELETE";
27-
28-
// authentication setting
22+
/**
23+
* Associate array to store API key(s)
24+
*/
2925
public static $apiKey = array();
26+
27+
/**
28+
* Associate array to store API prefix (e.g. Bearer)
29+
*/
3030
public static $apiKeyPrefix = array();
31+
32+
/**
33+
* Username for HTTP basic authentication
34+
*/
3135
public static $username = '';
36+
37+
/**
38+
* Password for HTTP basic authentication
39+
*/
3240
public static $password = '';
3341

34-
// an instance of ApiClient
42+
/**
43+
* The default instance of ApiClient
44+
*/
3545
public static $apiClient;
3646

37-
// debugging
38-
public static $debug = false; // by default debugging is disabled
39-
public static $debug_file = 'php://output'; //output debug log to STDOUT by default
47+
/**
48+
* Debug switch (default set to false)
49+
*/
50+
public static $debug = false;
51+
52+
/**
53+
* Debug file location (log to STDOUT by default)
54+
*/
55+
public static $debug_file = 'php://output';
4056

41-
/*
42-
* manually initalize Api client
43-
*/
57+
/*
58+
* manually initalize ApiClient
59+
*/
4460
public static function init() {
4561
if (self::$apiClient === null)
4662
self::$apiClient = new ApiClient();

samples/client/petstore/php/test.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,15 @@
1717
// return Pet (model)
1818
$response = $pet_api->getPetById($petId);
1919
var_dump($response);
20-
} catch (Exception $e) {
20+
21+
// test upload file (exception)
22+
$upload_response = $pet_api->uploadFile($petId, "test meta", NULL);
23+
24+
} catch (SwaggerClient\ApiException $e) {
2125
echo 'Caught exception: ', $e->getMessage(), "\n";
26+
echo 'HTTP response headers: ', $e->getResponseHeaders(), "\n";
27+
echo 'HTTP response body: ', $e->getResponseBody(), "\n";
28+
echo 'HTTP status code: ', $e->getCode(), "\n";
2229
}
2330

2431
?>

0 commit comments

Comments
 (0)