From 612abf134b76e5941461dc093806b4d3cc6bddb4 Mon Sep 17 00:00:00 2001 From: wing328 Date: Fri, 22 May 2015 22:09:51 +0800 Subject: [PATCH 1/2] api client as instance (not static class) --- .../src/main/resources/php/api.mustache | 13 ++++++-- .../main/resources/php/configuration.mustache | 10 +++++++ .../SwaggerClient-php/lib/Configuration.php | 10 +++++++ .../php/SwaggerClient-php/lib/PetApi.php | 30 +++++++++++++++++-- .../php/SwaggerClient-php/lib/StoreApi.php | 13 ++++++-- .../php/SwaggerClient-php/lib/UserApi.php | 13 ++++++-- samples/client/petstore/php/test.php | 7 +++-- 7 files changed, 85 insertions(+), 11 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/php/api.mustache b/modules/swagger-codegen/src/main/resources/php/api.mustache index 98be8ff3a5cf..25185e077347 100644 --- a/modules/swagger-codegen/src/main/resources/php/api.mustache +++ b/modules/swagger-codegen/src/main/resources/php/api.mustache @@ -25,8 +25,17 @@ namespace {{invokerPackage}}; {{#operations}} class {{classname}} { - function __construct($apiClient) { - $this->apiClient = $apiClient; + function __construct($apiClient = null) { + if (null === $apiClient) { + if (Configuration::$apiClient === null) { + Configuration::$apiClient = new APIClient(); // create a new API client if not present + $this->apiClient = Configuration::$apiClient; + } + else + $this->apiClient = Configuration::$apiClient; // use the default one + } else { + $this->apiClient = $apiClient; // use the one provided by the user + } } {{#operation}} diff --git a/modules/swagger-codegen/src/main/resources/php/configuration.mustache b/modules/swagger-codegen/src/main/resources/php/configuration.mustache index 9af553702aeb..3eaa8dba1f10 100644 --- a/modules/swagger-codegen/src/main/resources/php/configuration.mustache +++ b/modules/swagger-codegen/src/main/resources/php/configuration.mustache @@ -31,6 +31,16 @@ class Configuration { public static $username = ''; public static $password = ''; + // an instance of APIClient + public static $apiClient; + + /* + * manually initalize API client + */ + public static function init() { + if (self::$apiClient === null) + self::$apiClient = new APIClient(); + } } diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Configuration.php b/samples/client/petstore/php/SwaggerClient-php/lib/Configuration.php index 9426efa91705..1f91f8d9063a 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Configuration.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Configuration.php @@ -31,6 +31,16 @@ class Configuration { public static $username = ''; public static $password = ''; + // an instance of APIClient + public static $apiClient; + + /* + * manually initalize API client + */ + public static function init() { + if (self::$apiClient === null) + self::$apiClient = new APIClient(); + } } diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/PetApi.php b/samples/client/petstore/php/SwaggerClient-php/lib/PetApi.php index eece13acff7a..77a9eaf0d422 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/PetApi.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/PetApi.php @@ -24,11 +24,37 @@ namespace SwaggerClient; class PetApi { - function __construct($apiClient) { - $this->apiClient = $apiClient; + + function __construct($apiClient = null) { + if (null === $apiClient) { + if (Configuration::$apiClient === null) { + Configuration::$apiClient = new APIClient(); // create a new API client if not present + $this->apiClient = Configuration::$apiClient; + } + else + $this->apiClient = Configuration::$apiClient; // use the default one + } else { + $this->apiClient = $apiClient; // use the one provided by the user + } } + private $apiClient; + + /** + * get the API client + */ + public function getApiClient() { + return $this->apiClient; + } + + /** + * set the API client + */ + public function getApiClient($apiClient) { + $this->apiClient = $apiClient; + } + /** * updatePet * diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/StoreApi.php b/samples/client/petstore/php/SwaggerClient-php/lib/StoreApi.php index 609e6568ed4a..1c20a8994cf2 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/StoreApi.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/StoreApi.php @@ -24,8 +24,17 @@ namespace SwaggerClient; class StoreApi { - function __construct($apiClient) { - $this->apiClient = $apiClient; + function __construct($apiClient = null) { + if (null === $apiClient) { + if (Configuration::$apiClient === null) { + Configuration::$apiClient = new APIClient(); // create a new API client if not present + $this->apiClient = Configuration::$apiClient; + } + else + $this->apiClient = Configuration::$apiClient; // use the default one + } else { + $this->apiClient = $apiClient; // use the one provided by the user + } } diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/UserApi.php b/samples/client/petstore/php/SwaggerClient-php/lib/UserApi.php index 985ed22a40c5..69822f39f1d5 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/UserApi.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/UserApi.php @@ -24,8 +24,17 @@ namespace SwaggerClient; class UserApi { - function __construct($apiClient) { - $this->apiClient = $apiClient; + function __construct($apiClient = null) { + if (null === $apiClient) { + if (Configuration::$apiClient === null) { + Configuration::$apiClient = new APIClient(); // create a new API client if not present + $this->apiClient = Configuration::$apiClient; + } + else + $this->apiClient = Configuration::$apiClient; // use the default one + } else { + $this->apiClient = $apiClient; // use the one provided by the user + } } diff --git a/samples/client/petstore/php/test.php b/samples/client/petstore/php/test.php index 7d46d8b5821a..41c206ee6b0b 100644 --- a/samples/client/petstore/php/test.php +++ b/samples/client/petstore/php/test.php @@ -3,12 +3,13 @@ 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"); +//$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); + //$pet_api = new SwaggerClient\PetAPI($api_client); + $pet_api = new SwaggerClient\PetAPI(); // return Pet (model) $response = $pet_api->getPetById($petId); var_dump($response); From 691838c47d414bb3662bdd1080a41281844ec885 Mon Sep 17 00:00:00 2001 From: wing328 Date: Sat, 23 May 2015 10:35:19 +0800 Subject: [PATCH 2/2] add test case for api client instance --- .../src/main/resources/php/APIClient.mustache | 27 ++++++++++++-- .../src/main/resources/php/api.mustache | 16 ++++++++ .../php/SwaggerClient-php/lib/APIClient.php | 27 ++++++++++++-- .../php/SwaggerClient-php/lib/PetApi.php | 7 ++-- .../php/SwaggerClient-php/lib/StoreApi.php | 16 ++++++++ .../php/SwaggerClient-php/lib/UserApi.php | 16 ++++++++ .../SwaggerClient-php/tests/PetApiTest.php | 37 +++++++++++++++---- 7 files changed, 129 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 b8b8eff4295c..7d9f35446ff2 100644 --- a/modules/swagger-codegen/src/main/resources/php/APIClient.mustache +++ b/modules/swagger-codegen/src/main/resources/php/APIClient.mustache @@ -92,15 +92,36 @@ class APIClient { } /** - * @param integer $seconds Number of seconds before timing out [set to 0 for no timeout] + * get the user agent of the api client + * + * @return string user agent + */ + public function getUserAgent($user_agent) { + return $this->user_agent; + } + + /** + * set the HTTP timeout value + * + * @param integer $seconds Number of seconds before timing out [set to 0 for no timeout] */ public function setTimeout($seconds) { - if (!is_numeric($seconds)) - throw new \InvalidArgumentException('Timeout variable must be numeric.'); + if (!is_numeric($seconds) || $seconds < 0) + throw new \InvalidArgumentException('Timeout value must be numeric and a non-negative number.'); $this->curl_timeout = $seconds; } + /** + * get the HTTP timeout value + * + * @return string HTTP timeout value + */ + public function getTimeout() { + return $this->curl_timeout; + } + + /** * Get API key (with prefix if set) * @param string key name diff --git a/modules/swagger-codegen/src/main/resources/php/api.mustache b/modules/swagger-codegen/src/main/resources/php/api.mustache index 25185e077347..1c32eeb01c56 100644 --- a/modules/swagger-codegen/src/main/resources/php/api.mustache +++ b/modules/swagger-codegen/src/main/resources/php/api.mustache @@ -38,6 +38,22 @@ class {{classname}} { } } + private $apiClient; // instance of the APIClient + + /** + * get the API client + */ + public function getApiClient() { + return $this->apiClient; + } + + /** + * set the API client + */ + public function setApiClient($apiClient) { + $this->apiClient = $apiClient; + } + {{#operation}} /** * {{{nickname}}} diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/APIClient.php b/samples/client/petstore/php/SwaggerClient-php/lib/APIClient.php index 85087520c8fb..efc420b11a62 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/APIClient.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/APIClient.php @@ -92,15 +92,36 @@ class APIClient { } /** - * @param integer $seconds Number of seconds before timing out [set to 0 for no timeout] + * get the user agent of the api client + * + * @return string user agent + */ + public function getUserAgent($user_agent) { + return $this->user_agent; + } + + /** + * set the HTTP timeout value + * + * @param integer $seconds Number of seconds before timing out [set to 0 for no timeout] */ public function setTimeout($seconds) { - if (!is_numeric($seconds)) - throw new \InvalidArgumentException('Timeout variable must be numeric.'); + if (!is_numeric($seconds) || $seconds < 0) + throw new \InvalidArgumentException('Timeout value must be numeric and a non-negative number.'); $this->curl_timeout = $seconds; } + /** + * get the HTTP timeout value + * + * @return string HTTP timeout value + */ + public function getTimeout() { + return $this->curl_timeout; + } + + /** * Get API key (with prefix if set) * @param string key name diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/PetApi.php b/samples/client/petstore/php/SwaggerClient-php/lib/PetApi.php index 77a9eaf0d422..9456f42bc158 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/PetApi.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/PetApi.php @@ -24,7 +24,6 @@ namespace SwaggerClient; class PetApi { - function __construct($apiClient = null) { if (null === $apiClient) { if (Configuration::$apiClient === null) { @@ -38,8 +37,7 @@ class PetApi { } } - - private $apiClient; + private $apiClient; // instance of the APIClient /** * get the API client @@ -51,10 +49,11 @@ class PetApi { /** * set the API client */ - public function getApiClient($apiClient) { + public function setApiClient($apiClient) { $this->apiClient = $apiClient; } + /** * updatePet * diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/StoreApi.php b/samples/client/petstore/php/SwaggerClient-php/lib/StoreApi.php index 1c20a8994cf2..629201965dcc 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/StoreApi.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/StoreApi.php @@ -37,6 +37,22 @@ class StoreApi { } } + private $apiClient; // instance of the APIClient + + /** + * get the API client + */ + public function getApiClient() { + return $this->apiClient; + } + + /** + * set the API client + */ + public function setApiClient($apiClient) { + $this->apiClient = $apiClient; + } + /** * getInventory diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/UserApi.php b/samples/client/petstore/php/SwaggerClient-php/lib/UserApi.php index 69822f39f1d5..0f4a1bf2dac1 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/UserApi.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/UserApi.php @@ -37,6 +37,22 @@ class UserApi { } } + private $apiClient; // instance of the APIClient + + /** + * get the API client + */ + public function getApiClient() { + return $this->apiClient; + } + + /** + * set the API client + */ + public function setApiClient($apiClient) { + $this->apiClient = $apiClient; + } + /** * createUser diff --git a/samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php b/samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php index bf941756fa69..1f8dad4cc4ca 100644 --- a/samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php +++ b/samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php @@ -7,8 +7,8 @@ class PetApiTest extends \PHPUnit_Framework_TestCase // add a new pet (id 10005) to ensure the pet object is available for all the tests public static function setUpBeforeClass() { - // initialize the API client - $api_client = new SwaggerClient\APIClient('http://petstore.swagger.io/v2'); + // skip initializing the API client as it should be automatic + //$api_client = new SwaggerClient\APIClient('http://petstore.swagger.io/v2'); // new pet $new_pet_id = 10005; $new_pet = new SwaggerClient\models\Pet; @@ -26,7 +26,7 @@ class PetApiTest extends \PHPUnit_Framework_TestCase $new_pet->tags = [$tag]; $new_pet->category = $category; - $pet_api = new SwaggerClient\PetAPI($api_client); + $pet_api = new SwaggerClient\PetAPI(); // add a new pet (model) $add_response = $pet_api->addPet($new_pet); } @@ -34,26 +34,49 @@ class PetApiTest extends \PHPUnit_Framework_TestCase // test static functions defined in APIClient public function testAPIClient() { - # test selectHeaderAccept + // test selectHeaderAccept $this->assertSame('application/json', SwaggerClient\APIClient::selectHeaderAccept(array('application/xml','application/json'))); $this->assertSame(NULL, SwaggerClient\APIClient::selectHeaderAccept(array())); $this->assertSame('application/yaml,application/xml', SwaggerClient\APIClient::selectHeaderAccept(array('application/yaml','application/xml'))); - # test selectHeaderContentType + // test selectHeaderContentType $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 + // 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 + // test deleteDefaultHeader SwaggerClient\APIClient::deleteDefaultHeader('test2'); $this->assertFalse(isset(SwaggerClient\APIClient::getDefaultHeader()['test2'])); + $pet_api = new SwaggerClient\PetAPI(); + $pet_api2 = new SwaggerClient\PetAPI(); + $apiClient3 = new SwaggerClient\APIClient(); + $apiClient3->setUserAgent = 'api client 3'; + $apiClient4 = new SwaggerClient\APIClient(); + $apiClient4->setUserAgent = 'api client 4'; + $pet_api3 = new SwaggerClient\PetAPI($apiClient3); + + // same default api client + $this->assertSame($pet_api->getApiClient(), $pet_api2->getApiClient()); + // confirm using the default api client in the Configuration + $this->assertSame($pet_api->getApiClient(), SwaggerClient\Configuration::$apiClient); + // 2 different api clients are not the same + $this->assertNotEquals($apiClient3, $apiClient4); + // customized pet api not using the default (configuration) api client + $this->assertNotEquals($pet_api3->getApiClient(), SwaggerClient\Configuration::$apiClient); + // customied pet api not using the old pet api's api client + $this->assertNotEquals($pet_api2->getApiClient(), $pet_api3->getApiClient()); + + // both pet api and pet api2 share the same api client and confirm using timeout value + $pet_api->getApiClient()->setTimeout(999); + $this->assertSame(999, $pet_api2->getApiClient()->getTimeout()); + } // test getPetById with a Pet object (id 10005)