Merge pull request #778 from wing328/php_apiclient_instance

[PHP] Make API client more pluggable
This commit is contained in:
Tony Tam
2015-05-23 08:54:33 -07:00
10 changed files with 206 additions and 20 deletions

View File

@@ -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

View File

@@ -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();
}
}

View File

@@ -24,7 +24,32 @@ namespace SwaggerClient;
class PetApi {
function __construct($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; // 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;
}

View File

@@ -24,7 +24,32 @@ namespace SwaggerClient;
class StoreApi {
function __construct($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; // 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;
}

View File

@@ -24,7 +24,32 @@ namespace SwaggerClient;
class UserApi {
function __construct($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; // 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;
}