mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-11 16:12:49 +00:00
Merge pull request #794 from wing328/php_debug_switch
[PHP] add debug switch
This commit is contained in:
@@ -83,11 +83,11 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
typeMapping.put("array", "array");
|
||||
typeMapping.put("list", "array");
|
||||
|
||||
supportingFiles.add(new SupportingFile("composer.mustache", packagePath, "composer.json"));
|
||||
supportingFiles.add(new SupportingFile("configuration.mustache", packagePath + "/lib", "Configuration.php"));
|
||||
supportingFiles.add(new SupportingFile("APIClient.mustache", packagePath + "/lib", "APIClient.php"));
|
||||
supportingFiles.add(new SupportingFile("APIClientException.mustache", packagePath + "/lib", "APIClientException.php"));
|
||||
supportingFiles.add(new SupportingFile("require.mustache", packagePath, invokerPackage + ".php"));
|
||||
supportingFiles.add(new SupportingFile("composer.mustache", packagePath.replace('/', File.separatorChar), "composer.json"));
|
||||
supportingFiles.add(new SupportingFile("configuration.mustache", (packagePath + "/lib").replace('/', File.separatorChar), "Configuration.php"));
|
||||
supportingFiles.add(new SupportingFile("ApiClient.mustache", (packagePath + "/lib").replace('/', File.separatorChar), "ApiClient.php"));
|
||||
supportingFiles.add(new SupportingFile("ApiException.mustache", (packagePath + "/lib").replace('/', File.separatorChar), "ApiException.php"));
|
||||
supportingFiles.add(new SupportingFile("require.mustache", packagePath.replace('/', File.separatorChar), invokerPackage + ".php"));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -97,11 +97,11 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
|
||||
@Override
|
||||
public String apiFileFolder() {
|
||||
return outputFolder + "/" + apiPackage().replace('.', File.separatorChar);
|
||||
return (outputFolder + "/" + apiPackage()).replace('/', File.separatorChar);
|
||||
}
|
||||
|
||||
public String modelFileFolder() {
|
||||
return outputFolder + "/" + modelPackage().replace('.', File.separatorChar);
|
||||
return (outputFolder + "/" + modelPackage()).replace('/', File.separatorChar);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
|
||||
namespace {{invokerPackage}};
|
||||
|
||||
class APIClient {
|
||||
class ApiClient {
|
||||
|
||||
public static $PATCH = "PATCH";
|
||||
public static $POST = "POST";
|
||||
@@ -173,7 +173,7 @@ class APIClient {
|
||||
* @param array $headerParams parameters to be place in request header
|
||||
* @return mixed
|
||||
*/
|
||||
public function callAPI($resourcePath, $method, $queryParams, $postData,
|
||||
public function callApi($resourcePath, $method, $queryParams, $postData,
|
||||
$headerParams, $authSettings) {
|
||||
|
||||
$headers = array();
|
||||
@@ -228,35 +228,49 @@ class APIClient {
|
||||
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
|
||||
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
|
||||
} else if ($method != self::$GET) {
|
||||
throw new APIClientException('Method ' . $method . ' is not recognized.');
|
||||
throw new ApiException('Method ' . $method . ' is not recognized.');
|
||||
}
|
||||
curl_setopt($curl, CURLOPT_URL, $url);
|
||||
|
||||
// Set user agent
|
||||
curl_setopt($curl, CURLOPT_USERAGENT, $this->user_agent);
|
||||
|
||||
// debugging for curl
|
||||
if (Configuration::$debug) {
|
||||
error_log("[DEBUG] HTTP Request body ~BEGIN~\n".print_r($postData, true)."\n~END~\n", 3, Configuration::$debug_file);
|
||||
|
||||
curl_setopt($curl, CURLOPT_VERBOSE, 1);
|
||||
curl_setopt($curl, CURLOPT_STDERR, fopen(Configuration::$debug_file, 'a'));
|
||||
} else {
|
||||
curl_setopt($curl, CURLOPT_VERBOSE, 0);
|
||||
}
|
||||
|
||||
// obtain the HTTP response headers
|
||||
curl_setopt($curl, CURLOPT_HEADER, 1);
|
||||
|
||||
// Make the request
|
||||
$response = curl_exec($curl);
|
||||
$http_header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
|
||||
$http_header = substr($response, 0, $http_header_size);
|
||||
$http_body = substr($response, $http_header_size);
|
||||
$response_info = curl_getinfo($curl);
|
||||
|
||||
// debug HTTP response body
|
||||
if (Configuration::$debug) {
|
||||
error_log("[DEBUG] HTTP Response body ~BEGIN~\n".print_r($http_body, true)."\n~END~\n", 3, Configuration::$debug_file);
|
||||
}
|
||||
|
||||
// Handle the response
|
||||
if ($response_info['http_code'] == 0) {
|
||||
throw new APIClientException("TIMEOUT: api call to " . $url .
|
||||
" took more than 5s to return", 0, $response_info, $response);
|
||||
throw new ApiException("API call to $url timed out: ".serialize($response_info), 0, null, null);
|
||||
} else if ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299 ) {
|
||||
$data = json_decode($response);
|
||||
$data = json_decode($http_body);
|
||||
if (json_last_error() > 0) { // if response is a string
|
||||
$data = $response;
|
||||
$data = $http_body;
|
||||
}
|
||||
} else if ($response_info['http_code'] == 401) {
|
||||
throw new APIClientException("Unauthorized API request to " . $url .
|
||||
": " . serialize($response), 0, $response_info, $response);
|
||||
} else if ($response_info['http_code'] == 404) {
|
||||
$data = null;
|
||||
} else {
|
||||
throw new APIClientException("Can't connect to the api: " . $url .
|
||||
" response code: " .
|
||||
$response_info['http_code'], 0, $response_info, $response);
|
||||
throw new ApiException("[".$response_info['http_code']."] Error connecting to the API ($url)",
|
||||
$response_info['http_code'], $http_header, $http_body);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
@@ -19,20 +19,40 @@ namespace {{invokerPackage}};
|
||||
|
||||
use \Exception;
|
||||
|
||||
class APIClientException extends Exception {
|
||||
protected $response, $response_info;
|
||||
class ApiException extends Exception {
|
||||
|
||||
public function __construct($message="", $code=0, $response_info=null, $response=null) {
|
||||
/**
|
||||
* The HTTP body of the server response.
|
||||
*/
|
||||
protected $response_body;
|
||||
|
||||
/**
|
||||
* The HTTP header of the server response.
|
||||
*/
|
||||
protected $response_headers;
|
||||
|
||||
public function __construct($message="", $code=0, $responseHeaders=null, $responseBody=null) {
|
||||
parent::__construct($message, $code);
|
||||
$this->response_info = $response_info;
|
||||
$this->response = $response;
|
||||
$this->response_headers = $responseHeaders;
|
||||
$this->response_body = $responseBody;
|
||||
}
|
||||
|
||||
public function getResponse() {
|
||||
return $this->response;
|
||||
/**
|
||||
* Get the HTTP response header
|
||||
*
|
||||
* @return string HTTP response header
|
||||
*/
|
||||
public function getResponseHeaders() {
|
||||
return $this->response_headers;
|
||||
}
|
||||
|
||||
public function getResponseInfo() {
|
||||
return $this->response_info;
|
||||
/**
|
||||
* Get the HTTP response body
|
||||
*
|
||||
* @return string HTTP response body
|
||||
*/
|
||||
public function getResponseBody() {
|
||||
return $this->response_body;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -28,7 +28,7 @@ class {{classname}} {
|
||||
function __construct($apiClient = null) {
|
||||
if (null === $apiClient) {
|
||||
if (Configuration::$apiClient === null) {
|
||||
Configuration::$apiClient = new APIClient(); // create a new API client if not present
|
||||
Configuration::$apiClient = new ApiClient(); // create a new API client if not present
|
||||
$this->apiClient = Configuration::$apiClient;
|
||||
}
|
||||
else
|
||||
@@ -38,7 +38,7 @@ class {{classname}} {
|
||||
}
|
||||
}
|
||||
|
||||
private $apiClient; // instance of the APIClient
|
||||
private $apiClient; // instance of the ApiClient
|
||||
|
||||
/**
|
||||
* get the API client
|
||||
|
||||
@@ -19,27 +19,47 @@ namespace {{invokerPackage}};
|
||||
|
||||
class Configuration {
|
||||
|
||||
public static $PATCH = "PATCH";
|
||||
public static $POST = "POST";
|
||||
public static $GET = "GET";
|
||||
public static $PUT = "PUT";
|
||||
public static $DELETE = "DELETE";
|
||||
|
||||
// authentication setting
|
||||
/**
|
||||
* Associate array to store API key(s)
|
||||
*/
|
||||
public static $apiKey = array();
|
||||
|
||||
/**
|
||||
* Associate array to store API prefix (e.g. Bearer)
|
||||
*/
|
||||
public static $apiKeyPrefix = array();
|
||||
|
||||
/**
|
||||
* Username for HTTP basic authentication
|
||||
*/
|
||||
public static $username = '';
|
||||
|
||||
/**
|
||||
* Password for HTTP basic authentication
|
||||
*/
|
||||
public static $password = '';
|
||||
|
||||
// an instance of APIClient
|
||||
/**
|
||||
* The default instance of ApiClient
|
||||
*/
|
||||
public static $apiClient;
|
||||
|
||||
/*
|
||||
* manually initalize API client
|
||||
*/
|
||||
/**
|
||||
* Debug switch (default set to false)
|
||||
*/
|
||||
public static $debug = false;
|
||||
|
||||
/**
|
||||
* Debug file location (log to STDOUT by default)
|
||||
*/
|
||||
public static $debug_file = 'php://output';
|
||||
|
||||
/*
|
||||
* manually initalize ApiClient
|
||||
*/
|
||||
public static function init() {
|
||||
if (self::$apiClient === null)
|
||||
self::$apiClient = new APIClient();
|
||||
self::$apiClient = new ApiClient();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user