mirror of
https://github.com/valitydev/openapi-generator.git
synced 2024-11-08 03:18:53 +00:00
api client as instance (not static class)
This commit is contained in:
parent
2b7bbd9513
commit
612abf134b
@ -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}}
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
*
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user