api client as instance (not static class)

This commit is contained in:
wing328 2015-05-22 22:09:51 +08:00
parent 2b7bbd9513
commit 612abf134b
7 changed files with 85 additions and 11 deletions

View File

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

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

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

View File

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

View File

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

View File

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