Skip to content

Commit 156afa7

Browse files
committed
Merge pull request #778 from wing328/php_apiclient_instance
[PHP] Make API client more pluggable
2 parents 74dc05b + 691838c commit 156afa7

File tree

10 files changed

+206
-20
lines changed

10 files changed

+206
-20
lines changed

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,36 @@ class APIClient {
9292
}
9393

9494
/**
95-
* @param integer $seconds Number of seconds before timing out [set to 0 for no timeout]
95+
* get the user agent of the api client
96+
*
97+
* @return string user agent
98+
*/
99+
public function getUserAgent($user_agent) {
100+
return $this->user_agent;
101+
}
102+
103+
/**
104+
* set the HTTP timeout value
105+
*
106+
* @param integer $seconds Number of seconds before timing out [set to 0 for no timeout]
96107
*/
97108
public function setTimeout($seconds) {
98-
if (!is_numeric($seconds))
99-
throw new \InvalidArgumentException('Timeout variable must be numeric.');
109+
if (!is_numeric($seconds) || $seconds < 0)
110+
throw new \InvalidArgumentException('Timeout value must be numeric and a non-negative number.');
100111
101112
$this->curl_timeout = $seconds;
102113
}
103114

115+
/**
116+
* get the HTTP timeout value
117+
*
118+
* @return string HTTP timeout value
119+
*/
120+
public function getTimeout() {
121+
return $this->curl_timeout;
122+
}
123+
124+
104125
/**
105126
* Get API key (with prefix if set)
106127
* @param string key name

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,32 @@ namespace {{invokerPackage}};
2525
{{#operations}}
2626
class {{classname}} {
2727
28-
function __construct($apiClient) {
28+
function __construct($apiClient = null) {
29+
if (null === $apiClient) {
30+
if (Configuration::$apiClient === null) {
31+
Configuration::$apiClient = new APIClient(); // create a new API client if not present
32+
$this->apiClient = Configuration::$apiClient;
33+
}
34+
else
35+
$this->apiClient = Configuration::$apiClient; // use the default one
36+
} else {
37+
$this->apiClient = $apiClient; // use the one provided by the user
38+
}
39+
}
40+
41+
private $apiClient; // instance of the APIClient
42+
43+
/**
44+
* get the API client
45+
*/
46+
public function getApiClient() {
47+
return $this->apiClient;
48+
}
49+
50+
/**
51+
* set the API client
52+
*/
53+
public function setApiClient($apiClient) {
2954
$this->apiClient = $apiClient;
3055
}
3156

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ class Configuration {
3131
public static $username = '';
3232
public static $password = '';
3333
34+
// an instance of APIClient
35+
public static $apiClient;
36+
37+
/*
38+
* manually initalize API client
39+
*/
40+
public static function init() {
41+
if (self::$apiClient === null)
42+
self::$apiClient = new APIClient();
43+
}
3444

3545
}
3646

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,36 @@ public function setUserAgent($user_agent) {
9292
}
9393

9494
/**
95-
* @param integer $seconds Number of seconds before timing out [set to 0 for no timeout]
95+
* get the user agent of the api client
96+
*
97+
* @return string user agent
98+
*/
99+
public function getUserAgent($user_agent) {
100+
return $this->user_agent;
101+
}
102+
103+
/**
104+
* set the HTTP timeout value
105+
*
106+
* @param integer $seconds Number of seconds before timing out [set to 0 for no timeout]
96107
*/
97108
public function setTimeout($seconds) {
98-
if (!is_numeric($seconds))
99-
throw new \InvalidArgumentException('Timeout variable must be numeric.');
109+
if (!is_numeric($seconds) || $seconds < 0)
110+
throw new \InvalidArgumentException('Timeout value must be numeric and a non-negative number.');
100111

101112
$this->curl_timeout = $seconds;
102113
}
103114

115+
/**
116+
* get the HTTP timeout value
117+
*
118+
* @return string HTTP timeout value
119+
*/
120+
public function getTimeout() {
121+
return $this->curl_timeout;
122+
}
123+
124+
104125
/**
105126
* Get API key (with prefix if set)
106127
* @param string key name

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ class Configuration {
3131
public static $username = '';
3232
public static $password = '';
3333

34+
// an instance of APIClient
35+
public static $apiClient;
36+
37+
/*
38+
* manually initalize API client
39+
*/
40+
public static function init() {
41+
if (self::$apiClient === null)
42+
self::$apiClient = new APIClient();
43+
}
3444

3545
}
3646

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,32 @@
2424

2525
class PetApi {
2626

27-
function __construct($apiClient) {
27+
function __construct($apiClient = null) {
28+
if (null === $apiClient) {
29+
if (Configuration::$apiClient === null) {
30+
Configuration::$apiClient = new APIClient(); // create a new API client if not present
31+
$this->apiClient = Configuration::$apiClient;
32+
}
33+
else
34+
$this->apiClient = Configuration::$apiClient; // use the default one
35+
} else {
36+
$this->apiClient = $apiClient; // use the one provided by the user
37+
}
38+
}
39+
40+
private $apiClient; // instance of the APIClient
41+
42+
/**
43+
* get the API client
44+
*/
45+
public function getApiClient() {
46+
return $this->apiClient;
47+
}
48+
49+
/**
50+
* set the API client
51+
*/
52+
public function setApiClient($apiClient) {
2853
$this->apiClient = $apiClient;
2954
}
3055

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,32 @@
2424

2525
class StoreApi {
2626

27-
function __construct($apiClient) {
27+
function __construct($apiClient = null) {
28+
if (null === $apiClient) {
29+
if (Configuration::$apiClient === null) {
30+
Configuration::$apiClient = new APIClient(); // create a new API client if not present
31+
$this->apiClient = Configuration::$apiClient;
32+
}
33+
else
34+
$this->apiClient = Configuration::$apiClient; // use the default one
35+
} else {
36+
$this->apiClient = $apiClient; // use the one provided by the user
37+
}
38+
}
39+
40+
private $apiClient; // instance of the APIClient
41+
42+
/**
43+
* get the API client
44+
*/
45+
public function getApiClient() {
46+
return $this->apiClient;
47+
}
48+
49+
/**
50+
* set the API client
51+
*/
52+
public function setApiClient($apiClient) {
2853
$this->apiClient = $apiClient;
2954
}
3055

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,32 @@
2424

2525
class UserApi {
2626

27-
function __construct($apiClient) {
27+
function __construct($apiClient = null) {
28+
if (null === $apiClient) {
29+
if (Configuration::$apiClient === null) {
30+
Configuration::$apiClient = new APIClient(); // create a new API client if not present
31+
$this->apiClient = Configuration::$apiClient;
32+
}
33+
else
34+
$this->apiClient = Configuration::$apiClient; // use the default one
35+
} else {
36+
$this->apiClient = $apiClient; // use the one provided by the user
37+
}
38+
}
39+
40+
private $apiClient; // instance of the APIClient
41+
42+
/**
43+
* get the API client
44+
*/
45+
public function getApiClient() {
46+
return $this->apiClient;
47+
}
48+
49+
/**
50+
* set the API client
51+
*/
52+
public function setApiClient($apiClient) {
2853
$this->apiClient = $apiClient;
2954
}
3055

samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
77

88
// add a new pet (id 10005) to ensure the pet object is available for all the tests
99
public static function setUpBeforeClass() {
10-
// initialize the API client
11-
$api_client = new SwaggerClient\APIClient('http://petstore.swagger.io/v2');
10+
// skip initializing the API client as it should be automatic
11+
//$api_client = new SwaggerClient\APIClient('http://petstore.swagger.io/v2');
1212
// new pet
1313
$new_pet_id = 10005;
1414
$new_pet = new SwaggerClient\models\Pet;
@@ -26,36 +26,59 @@ public static function setUpBeforeClass() {
2626
$new_pet->tags = array($tag);
2727
$new_pet->category = $category;
2828

29-
$pet_api = new SwaggerClient\PetAPI($api_client);
29+
$pet_api = new SwaggerClient\PetAPI();
3030
// add a new pet (model)
3131
$add_response = $pet_api->addPet($new_pet);
3232
}
3333

3434
// test static functions defined in APIClient
3535
public function testAPIClient()
3636
{
37-
# test selectHeaderAccept
37+
// test selectHeaderAccept
3838
$this->assertSame('application/json', SwaggerClient\APIClient::selectHeaderAccept(array('application/xml','application/json')));
3939
$this->assertSame(NULL, SwaggerClient\APIClient::selectHeaderAccept(array()));
4040
$this->assertSame('application/yaml,application/xml', SwaggerClient\APIClient::selectHeaderAccept(array('application/yaml','application/xml')));
4141

42-
# test selectHeaderContentType
42+
// test selectHeaderContentType
4343
$this->assertSame('application/json', SwaggerClient\APIClient::selectHeaderContentType(array('application/xml','application/json')));
4444
$this->assertSame('application/json', SwaggerClient\APIClient::selectHeaderContentType(array()));
4545
$this->assertSame('application/yaml,application/xml', SwaggerClient\APIClient::selectHeaderContentType(array('application/yaml','application/xml')));
4646

47-
# test addDefaultHeader and getDefaultHeader
47+
// test addDefaultHeader and getDefaultHeader
4848
SwaggerClient\APIClient::addDefaultHeader('test1', 'value1');
4949
SwaggerClient\APIClient::addDefaultHeader('test2', 200);
5050
$defaultHeader = SwaggerClient\APIClient::getDefaultHeader();
5151
$this->assertSame('value1', $defaultHeader['test1']);
5252
$this->assertSame(200, $defaultHeader['test2']);
5353

54-
# test deleteDefaultHeader
54+
// test deleteDefaultHeader
5555
SwaggerClient\APIClient::deleteDefaultHeader('test2');
5656
$defaultHeader = SwaggerClient\APIClient::getDefaultHeader();
5757
$this->assertFalse(isset($defaultHeader['test2']));
5858

59+
$pet_api = new SwaggerClient\PetAPI();
60+
$pet_api2 = new SwaggerClient\PetAPI();
61+
$apiClient3 = new SwaggerClient\APIClient();
62+
$apiClient3->setUserAgent = 'api client 3';
63+
$apiClient4 = new SwaggerClient\APIClient();
64+
$apiClient4->setUserAgent = 'api client 4';
65+
$pet_api3 = new SwaggerClient\PetAPI($apiClient3);
66+
67+
// same default api client
68+
$this->assertSame($pet_api->getApiClient(), $pet_api2->getApiClient());
69+
// confirm using the default api client in the Configuration
70+
$this->assertSame($pet_api->getApiClient(), SwaggerClient\Configuration::$apiClient);
71+
// 2 different api clients are not the same
72+
$this->assertNotEquals($apiClient3, $apiClient4);
73+
// customized pet api not using the default (configuration) api client
74+
$this->assertNotEquals($pet_api3->getApiClient(), SwaggerClient\Configuration::$apiClient);
75+
// customied pet api not using the old pet api's api client
76+
$this->assertNotEquals($pet_api2->getApiClient(), $pet_api3->getApiClient());
77+
78+
// both pet api and pet api2 share the same api client and confirm using timeout value
79+
$pet_api->getApiClient()->setTimeout(999);
80+
$this->assertSame(999, $pet_api2->getApiClient()->getTimeout());
81+
5982
}
6083

6184
// test getPetById with a Pet object (id 10005)

samples/client/petstore/php/test.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
require_once('SwaggerClient-php/SwaggerClient.php');
44

55
// initialize the API client
6-
$api_client = new SwaggerClient\APIClient('http://petstore.swagger.io/v2');
7-
$api_client->addDefaultHeader("test1", "value1");
6+
//$api_client = new SwaggerClient\APIClient('http://petstore.swagger.io/v2');
7+
//$api_client->addDefaultHeader("test1", "value1");
88

99
$petId = 10005; // ID of pet that needs to be fetched
1010
try {
11-
$pet_api = new SwaggerClient\PetAPI($api_client);
11+
//$pet_api = new SwaggerClient\PetAPI($api_client);
12+
$pet_api = new SwaggerClient\PetAPI();
1213
// return Pet (model)
1314
$response = $pet_api->getPetById($petId);
1415
var_dump($response);

0 commit comments

Comments
 (0)