From af260cba41df5c30d8a1c696902a62508a2df63b Mon Sep 17 00:00:00 2001 From: wing328 Date: Tue, 19 May 2015 15:33:03 +0800 Subject: [PATCH] update api client to support default header --- .../src/main/resources/php/APIClient.mustache | 53 ++++++++++++++++--- .../php/SwaggerClient-php/lib/APIClient.php | 53 ++++++++++++++++--- .../SwaggerClient-php/tests/PetApiTest.php | 10 +++- samples/client/petstore/php/test.php | 2 + 4 files changed, 101 insertions(+), 17 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/php/APIClient.mustache b/modules/swagger-codegen/src/main/resources/php/APIClient.mustache index c239dfb26f3..e6f08d4b5aa 100644 --- a/modules/swagger-codegen/src/main/resources/php/APIClient.mustache +++ b/modules/swagger-codegen/src/main/resources/php/APIClient.mustache @@ -24,6 +24,8 @@ class APIClient { public static $GET = "GET"; public static $PUT = "PUT"; public static $DELETE = "DELETE"; + + private static $default_header = array(); /* * @var string timeout (second) of the HTTP request, by default set to 0, no timeout @@ -46,24 +48,55 @@ class APIClient { } /** - * Set the user agent of the API client + * add default header * - * @param string $user_agent The user agent of the API client + * @param string $header_name header name (e.g. Token) + * @param string $header_value header value (e.g. 1z8wp3) + */ + public function addDefaultHeader($header_name, $header_value) { + if (!is_string($header_name)) + throw new exception('heaer name must be a string.'); + + self::$default_header[$header_name] = $header_value; + } + + /** + * get the default header + * + * @return array default header + */ + public function getDefaultHeader() { + return self::$default_header; + } + + /** + * delete the default header based on header name + * + * @param string $header_name header name (e.g. Token) + */ + public function deleteDefaultHeader($header_name) { + unset(self::$default_header[$header_name]); + } + + /** + * set the user agent of the api client + * + * @param string $user_agent the user agent of the api client */ public function setUserAgent($user_agent) { - if (!is_string($user_agent)) { - throw new Exception('User-agent must be a string.'); - } + if (!is_string($user_agent)) + throw new exception('user-agent must be a string.'); + $this->user_agent= $user_agent; } /** * @param integer $seconds Number of seconds before timing out [set to 0 for no timeout] - */ + */ public function setTimeout($seconds) { - if (!is_numeric($seconds)) { + if (!is_numeric($seconds)) throw new Exception('Timeout variable must be numeric.'); - } + $this->curl_timeout = $seconds; } @@ -83,6 +116,9 @@ class APIClient { # Allow API key from $headerParams to override default $added_api_key = False; if ($headerParams != null) { + # add default header + $headerParams = array_merge((array)self::$default_header, $headerParams); + foreach ($headerParams as $key => $val) { $headers[] = "$key: $val"; if ($key == $this->headerName) { @@ -111,6 +147,7 @@ class APIClient { } // return the result on success, rather than just TRUE curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); if (! empty($queryParams)) { diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/APIClient.php b/samples/client/petstore/php/SwaggerClient-php/lib/APIClient.php index e57b726e66f..d4b7d8f7f63 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/APIClient.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/APIClient.php @@ -24,6 +24,8 @@ class APIClient { public static $GET = "GET"; public static $PUT = "PUT"; public static $DELETE = "DELETE"; + + private static $default_header = array(); /* * @var string timeout (second) of the HTTP request, by default set to 0, no timeout @@ -46,24 +48,55 @@ class APIClient { } /** - * Set the user agent of the API client + * add default header * - * @param string $user_agent The user agent of the API client + * @param string $header_name header name (e.g. Token) + * @param string $header_value header value (e.g. 1z8wp3) + */ + public function addDefaultHeader($header_name, $header_value) { + if (!is_string($header_name)) + throw new exception('heaer name must be a string.'); + + self::$default_header[$header_name] = $header_value; + } + + /** + * get the default header + * + * @return array default header + */ + public function getDefaultHeader() { + return self::$default_header; + } + + /** + * delete the default header based on header name + * + * @param string $header_name header name (e.g. Token) + */ + public function deleteDefaultHeader($header_name) { + unset(self::$default_header[$header_name]); + } + + /** + * set the user agent of the api client + * + * @param string $user_agent the user agent of the api client */ public function setUserAgent($user_agent) { - if (!is_string($user_agent)) { - throw new Exception('User-agent must be a string.'); - } + if (!is_string($user_agent)) + throw new exception('user-agent must be a string.'); + $this->user_agent= $user_agent; } /** * @param integer $seconds Number of seconds before timing out [set to 0 for no timeout] - */ + */ public function setTimeout($seconds) { - if (!is_numeric($seconds)) { + if (!is_numeric($seconds)) throw new Exception('Timeout variable must be numeric.'); - } + $this->curl_timeout = $seconds; } @@ -83,6 +116,9 @@ class APIClient { # Allow API key from $headerParams to override default $added_api_key = False; if ($headerParams != null) { + # add default header + $headerParams = array_merge((array)self::$default_header, $headerParams); + foreach ($headerParams as $key => $val) { $headers[] = "$key: $val"; if ($key == $this->headerName) { @@ -111,6 +147,7 @@ class APIClient { } // return the result on success, rather than just TRUE curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); if (! empty($queryParams)) { diff --git a/samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php b/samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php index 0d015ec47b2..4751c29dd78 100644 --- a/samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php +++ b/samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php @@ -43,8 +43,16 @@ class PetApiTest extends \PHPUnit_Framework_TestCase $this->assertSame('application/json', SwaggerClient\APIClient::selectHeaderContentType(array('application/xml','application/json'))); $this->assertSame('application/json', SwaggerClient\APIClient::selectHeaderContentType(array())); $this->assertSame('application/yaml,application/xml', SwaggerClient\APIClient::selectHeaderContentType(array('application/yaml','application/xml'))); - + # test addDefaultHeader and getDefaultHeader + SwaggerClient\APIClient::addDefaultHeader('test1', 'value1'); + SwaggerClient\APIClient::addDefaultHeader('test2', 200); + $this->assertSame('value1', SwaggerClient\APIClient::getDefaultHeader()['test1']); + $this->assertSame(200, SwaggerClient\APIClient::getDefaultHeader()['test2']); + + # test deleteDefaultHeader + SwaggerClient\APIClient::deleteDefaultHeader('test2'); + $this->assertFalse(isset(SwaggerClient\APIClient::getDefaultHeader()['test2'])); } // test getPetById with a Pet object (id 10005) diff --git a/samples/client/petstore/php/test.php b/samples/client/petstore/php/test.php index 7cbbfce3cbb..7d46d8b5821 100644 --- a/samples/client/petstore/php/test.php +++ b/samples/client/petstore/php/test.php @@ -4,6 +4,8 @@ require_once('SwaggerClient-php/SwaggerClient.php'); // initialize the API client $api_client = new SwaggerClient\APIClient('http://petstore.swagger.io/v2'); +$api_client->addDefaultHeader("test1", "value1"); + $petId = 10005; // ID of pet that needs to be fetched try { $pet_api = new SwaggerClient\PetAPI($api_client);