Skip to content

Commit 05700a6

Browse files
committed
Merge pull request #794 from wing328/php_debug_switch
[PHP] add debug switch
2 parents 29bd743 + 2d6a709 commit 05700a6

File tree

13 files changed

+233
-111
lines changed

13 files changed

+233
-111
lines changed

modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/PhpClientCodegen.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ public PhpClientCodegen() {
8383
typeMapping.put("array", "array");
8484
typeMapping.put("list", "array");
8585

86-
supportingFiles.add(new SupportingFile("composer.mustache", packagePath, "composer.json"));
87-
supportingFiles.add(new SupportingFile("configuration.mustache", packagePath + "/lib", "Configuration.php"));
88-
supportingFiles.add(new SupportingFile("APIClient.mustache", packagePath + "/lib", "APIClient.php"));
89-
supportingFiles.add(new SupportingFile("APIClientException.mustache", packagePath + "/lib", "APIClientException.php"));
90-
supportingFiles.add(new SupportingFile("require.mustache", packagePath, invokerPackage + ".php"));
86+
supportingFiles.add(new SupportingFile("composer.mustache", packagePath.replace('/', File.separatorChar), "composer.json"));
87+
supportingFiles.add(new SupportingFile("configuration.mustache", (packagePath + "/lib").replace('/', File.separatorChar), "Configuration.php"));
88+
supportingFiles.add(new SupportingFile("ApiClient.mustache", (packagePath + "/lib").replace('/', File.separatorChar), "ApiClient.php"));
89+
supportingFiles.add(new SupportingFile("ApiException.mustache", (packagePath + "/lib").replace('/', File.separatorChar), "ApiException.php"));
90+
supportingFiles.add(new SupportingFile("require.mustache", packagePath.replace('/', File.separatorChar), invokerPackage + ".php"));
9191
}
9292

9393
@Override
@@ -97,11 +97,11 @@ public String escapeReservedWord(String name) {
9797

9898
@Override
9999
public String apiFileFolder() {
100-
return outputFolder + "/" + apiPackage().replace('.', File.separatorChar);
100+
return (outputFolder + "/" + apiPackage()).replace('/', File.separatorChar);
101101
}
102102

103103
public String modelFileFolder() {
104-
return outputFolder + "/" + modelPackage().replace('.', File.separatorChar);
104+
return (outputFolder + "/" + modelPackage()).replace('/', File.separatorChar);
105105
}
106106

107107
@Override

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

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
namespace {{invokerPackage}};
1919

20-
class APIClient {
20+
class ApiClient {
2121
2222
public static $PATCH = "PATCH";
2323
public static $POST = "POST";
@@ -173,7 +173,7 @@ class APIClient {
173173
* @param array $headerParams parameters to be place in request header
174174
* @return mixed
175175
*/
176-
public function callAPI($resourcePath, $method, $queryParams, $postData,
176+
public function callApi($resourcePath, $method, $queryParams, $postData,
177177
$headerParams, $authSettings) {
178178
179179
$headers = array();
@@ -228,35 +228,49 @@ class APIClient {
228228
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
229229
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
230230
} else if ($method != self::$GET) {
231-
throw new APIClientException('Method ' . $method . ' is not recognized.');
231+
throw new ApiException('Method ' . $method . ' is not recognized.');
232232
}
233233
curl_setopt($curl, CURLOPT_URL, $url);
234234

235235
// Set user agent
236236
curl_setopt($curl, CURLOPT_USERAGENT, $this->user_agent);
237237

238+
// debugging for curl
239+
if (Configuration::$debug) {
240+
error_log("[DEBUG] HTTP Request body ~BEGIN~\n".print_r($postData, true)."\n~END~\n", 3, Configuration::$debug_file);
241+
242+
curl_setopt($curl, CURLOPT_VERBOSE, 1);
243+
curl_setopt($curl, CURLOPT_STDERR, fopen(Configuration::$debug_file, 'a'));
244+
} else {
245+
curl_setopt($curl, CURLOPT_VERBOSE, 0);
246+
}
247+
248+
// obtain the HTTP response headers
249+
curl_setopt($curl, CURLOPT_HEADER, 1);
250+
238251
// Make the request
239252
$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);
240256
$response_info = curl_getinfo($curl);
241257

258+
// debug HTTP response body
259+
if (Configuration::$debug) {
260+
error_log("[DEBUG] HTTP Response body ~BEGIN~\n".print_r($http_body, true)."\n~END~\n", 3, Configuration::$debug_file);
261+
}
262+
242263
// Handle the response
243264
if ($response_info['http_code'] == 0) {
244-
throw new APIClientException("TIMEOUT: api call to " . $url .
245-
" 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);
246266
} else if ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299 ) {
247-
$data = json_decode($response);
267+
$data = json_decode($http_body);
248268
if (json_last_error() > 0) { // if response is a string
249-
$data = $response;
269+
$data = $http_body;
250270
}
251-
} else if ($response_info['http_code'] == 401) {
252-
throw new APIClientException("Unauthorized API request to " . $url .
253-
": " . serialize($response), 0, $response_info, $response);
254-
} else if ($response_info['http_code'] == 404) {
255-
$data = null;
256271
} else {
257-
throw new APIClientException("Can't connect to the api: " . $url .
258-
" response code: " .
259-
$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);
260274
}
261275
return $data;
262276
}

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

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,40 @@ namespace {{invokerPackage}};
1919

2020
use \Exception;
2121

22-
class APIClientException extends Exception {
23-
protected $response, $response_info;
22+
class ApiException extends Exception {
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/api.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class {{classname}} {
2828
function __construct($apiClient = null) {
2929
if (null === $apiClient) {
3030
if (Configuration::$apiClient === null) {
31-
Configuration::$apiClient = new APIClient(); // create a new API client if not present
31+
Configuration::$apiClient = new ApiClient(); // create a new API client if not present
3232
$this->apiClient = Configuration::$apiClient;
3333
}
3434
else
@@ -38,7 +38,7 @@ class {{classname}} {
3838
}
3939
}
4040

41-
private $apiClient; // instance of the APIClient
41+
private $apiClient; // instance of the ApiClient
4242

4343
/**
4444
* get the API client

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

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,47 @@ 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-
/*
38-
* manually initalize API client
39-
*/
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';
56+
57+
/*
58+
* manually initalize ApiClient
59+
*/
4060
public static function init() {
4161
if (self::$apiClient === null)
42-
self::$apiClient = new APIClient();
62+
self::$apiClient = new ApiClient();
4363
}
4464

4565
}

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

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
namespace SwaggerClient;
1919

20-
class APIClient {
20+
class ApiClient {
2121

2222
public static $PATCH = "PATCH";
2323
public static $POST = "POST";
@@ -178,7 +178,7 @@ public function updateParamsForAuth(&$headerParams, &$queryParams, $authSettings
178178
* @param array $headerParams parameters to be place in request header
179179
* @return mixed
180180
*/
181-
public function callAPI($resourcePath, $method, $queryParams, $postData,
181+
public function callApi($resourcePath, $method, $queryParams, $postData,
182182
$headerParams, $authSettings) {
183183

184184
$headers = array();
@@ -233,35 +233,49 @@ public function callAPI($resourcePath, $method, $queryParams, $postData,
233233
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
234234
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
235235
} else if ($method != self::$GET) {
236-
throw new APIClientException('Method ' . $method . ' is not recognized.');
236+
throw new ApiException('Method ' . $method . ' is not recognized.');
237237
}
238238
curl_setopt($curl, CURLOPT_URL, $url);
239239

240240
// Set user agent
241241
curl_setopt($curl, CURLOPT_USERAGENT, $this->user_agent);
242242

243+
// debugging for curl
244+
if (Configuration::$debug) {
245+
error_log("[DEBUG] HTTP Request body ~BEGIN~\n".print_r($postData, true)."\n~END~\n", 3, Configuration::$debug_file);
246+
247+
curl_setopt($curl, CURLOPT_VERBOSE, 1);
248+
curl_setopt($curl, CURLOPT_STDERR, fopen(Configuration::$debug_file, 'a'));
249+
} else {
250+
curl_setopt($curl, CURLOPT_VERBOSE, 0);
251+
}
252+
253+
// obtain the HTTP response headers
254+
curl_setopt($curl, CURLOPT_HEADER, 1);
255+
243256
// Make the request
244257
$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);
245261
$response_info = curl_getinfo($curl);
246262

263+
// debug HTTP response body
264+
if (Configuration::$debug) {
265+
error_log("[DEBUG] HTTP Response body ~BEGIN~\n".print_r($http_body, true)."\n~END~\n", 3, Configuration::$debug_file);
266+
}
267+
247268
// Handle the response
248269
if ($response_info['http_code'] == 0) {
249-
throw new APIClientException("TIMEOUT: api call to " . $url .
250-
" 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);
251271
} else if ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299 ) {
252-
$data = json_decode($response);
272+
$data = json_decode($http_body);
253273
if (json_last_error() > 0) { // if response is a string
254-
$data = $response;
274+
$data = $http_body;
255275
}
256-
} else if ($response_info['http_code'] == 401) {
257-
throw new APIClientException("Unauthorized API request to " . $url .
258-
": " . serialize($response), 0, $response_info, $response);
259-
} else if ($response_info['http_code'] == 404) {
260-
$data = null;
261276
} else {
262-
throw new APIClientException("Can't connect to the api: " . $url .
263-
" response code: " .
264-
$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);
265279
}
266280
return $data;
267281
}

samples/client/petstore/php/SwaggerClient-php/lib/APIClientException.php renamed to samples/client/petstore/php/SwaggerClient-php/lib/ApiException.php

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,40 @@
1919

2020
use \Exception;
2121

22-
class APIClientException extends Exception {
23-
protected $response, $response_info;
22+
class ApiException extends Exception {
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
}

0 commit comments

Comments
 (0)