diff --git a/composer.json b/composer.json index 6efbf21..429924c 100644 --- a/composer.json +++ b/composer.json @@ -13,16 +13,15 @@ } ], "require": { - "php": ">=5.5.9", - "docusign/esign-client": "^3.0" + "php": "^7.2", + "docusign/esign-client": "^4.0" }, "require-dev": { - "phpunit/phpunit": "5.3.*" + "phpunit/phpunit": "8.2.*" }, "autoload": { "psr-4": { "DocuSign\\Rest\\": "src/" } - }, - "minimum-stability": "dev" + } } diff --git a/src/Api/BaseApi.php b/src/Api/BaseApi.php index 5e2e901..20e51df 100644 --- a/src/Api/BaseApi.php +++ b/src/Api/BaseApi.php @@ -38,12 +38,17 @@ abstract class BaseApi public function __construct(ApiClient $apiClient) { $this->apiClient = $apiClient; + $this->initClient(); + } + + private function initClient() + { $docusignClass = str_replace(__NAMESPACE__, "DocuSign\\eSign\\Api", get_class($this)) . 'Api'; - $this->client = new $docusignClass($apiClient->getClient()); + $this->client = new $docusignClass($this->apiClient->getClient()); } /** - * Magic method to construct an options model or call an apimethod + * Magic method to construct an options model or call an api method * @param $method * @param $args * @return mixed @@ -56,7 +61,12 @@ public function __call($method, $args) } if ($method !== 'login' && !$this->apiClient->isAuthenticated()) { + $host = $this->apiClient->getHost(); $this->apiClient->authenticate(); + // If the host has changed, update host on client config + if ($host !== $this->apiClient->getHost()) { + $this->initClient(); + } } if ($this->usesAccountId) { @@ -68,7 +78,7 @@ public function __call($method, $args) /** * Get an options object or all of them for current Api class - * + * * @param null $method * @return array|mixed * @throws ClassNotFoundException @@ -119,4 +129,4 @@ public function setOptionsObject($method, $args) return $optionClass; } -} \ No newline at end of file +} diff --git a/src/Client.php b/src/Client.php index 690fff5..123cf2b 100644 --- a/src/Client.php +++ b/src/Client.php @@ -2,7 +2,7 @@ namespace DocuSign\Rest; -use DocuSign\eSign\ApiClient; +use DocuSign\eSign\Client\ApiClient; use DocuSign\eSign\Configuration; @@ -67,6 +67,11 @@ public function __construct($params = []) $this->{$key} = $val; } + $this->initApiClient(); + } + + private function initApiClient() + { $this->client = new ApiClient($this->setConfiguration()); } @@ -80,6 +85,11 @@ public function setConfiguration() ])); } + public function getHost() + { + return $this->host; + } + /** * This magic method is to instantiate all classes in the \DocuSign\eSign\Model namespace * @@ -123,7 +133,7 @@ public function __get($name) if (array_key_exists($name, $this->_api_container)) { return $this->_api_container[$name]; } - + if (!class_exists($apiClass = "DocuSign\\Rest\\Api\\" . ucfirst($name))) { throw new Exceptions\ClassNotFoundException("Cannot Find Api Class $apiClass"); } @@ -132,7 +142,7 @@ public function __get($name) } /** - * Authenticates api client and stores account_id + * Authenticates api client, stores account_id, and updates host if changed by docusign * * @return $this */ @@ -140,9 +150,17 @@ public function authenticate() { if (!isset($this->account_id)) { $accounts = $this->authentication->login(); - $allAccounts = $accounts->getLoginAccounts(); - $account = $allAccounts[0]; + $login_accounts = $accounts->getLoginAccounts(); + $account = $login_accounts[0]; $this->account_id = $account->getAccountId(); + $base_url = $account->getBaseUrl(); + $base_url = strtolower(substr($base_url, 0, strpos($base_url,'/restapi') + 8)); + // If the host has changed, update host on client config + if ($this->host !== $base_url) { + $this->host = $base_url; + $this->_api_container = []; + $this->initApiClient(); + } } $this->authenticated = true; @@ -177,4 +195,4 @@ public function getClient() { return $this->client; } -} \ No newline at end of file +} diff --git a/tests/LoginTest.php b/tests/LoginTest.php index 62b6964..c764467 100644 --- a/tests/LoginTest.php +++ b/tests/LoginTest.php @@ -1,6 +1,8 @@ assertStringMatchesFormat('%d', $docusign->getAccountId()); } -} \ No newline at end of file +} diff --git a/tests/SignatureRequestTest.php b/tests/SignatureRequestTest.php index 8cf3a42..7875009 100644 --- a/tests/SignatureRequestTest.php +++ b/tests/SignatureRequestTest.php @@ -1,6 +1,8 @@ assertInstanceOf(\DocuSign\eSign\Model\EnvelopeSummary::class, $envelopeSummary); } -} \ No newline at end of file +}