Skip to content

Commit 9f2ec03

Browse files
authored
Introduction of Auth Handlers per API client (#344)
* Introduce structure to allow credentials to be injected into clients * Import trait into all of the clients. Add a test in anticipation of the factories generating clients * Added preferred types for individual clients where possible * take out oauth as it isnt being used yet * more oauth removals * remove the preferred trait. Instead we're going to use a handler in the API resource * More unused imports, refactored account API to take out the marked secret deprecations * fix tests to pull out the secrets from the account API * make me more modern php please * This should technically cover the structure of auth before rolling out the factories * WIP * More WIP * Add handlers for all remaining clients * Add handlers in the test mocks * Mountain of deprecations and rewrites of failing tests
1 parent 0ef4eee commit 9f2ec03

File tree

93 files changed

+215
-6285
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+215
-6285
lines changed

src/Account/Client.php

Lines changed: 11 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -38,64 +38,18 @@ class Client implements ClientAwareInterface, APIClient
3838
use ClientAwareTrait;
3939

4040
/**
41-
* @var APIResource
41+
* @var APIResource|null
4242
*/
43-
protected $accountAPI;
43+
protected ?APIResource $accountAPI;
4444

45-
/**
46-
* @var APIResource
47-
*/
48-
protected $secretsAPI;
49-
50-
public function __construct(?APIResource $accountAPI = null, ?APIResource $secretsAPI = null)
45+
public function __construct(?APIResource $accountAPI = null)
5146
{
5247
$this->accountAPI = $accountAPI;
53-
$this->secretsAPI = $secretsAPI;
54-
}
55-
56-
/**
57-
* Shim to handle older instantiations of this class
58-
*
59-
* @deprecated Will remove in v3
60-
*/
61-
public function getAccountAPI(): APIResource
62-
{
63-
if (is_null($this->accountAPI)) {
64-
$api = new APIResource();
65-
$api->setClient($this->getClient())
66-
->setBaseUrl($this->getClient()->getRestUrl())
67-
->setIsHAL(false)
68-
->setBaseUri('/account')
69-
->setCollectionName('');
70-
$this->accountAPI = $api;
71-
}
72-
73-
return clone $this->accountAPI;
7448
}
7549

7650
public function getAPIResource(): APIResource
7751
{
78-
return $this->getAccountAPI();
79-
}
80-
81-
/**
82-
* Shim to handle older instantiations of this class
83-
*
84-
* @deprecated Will remove in v3
85-
*/
86-
public function getSecretsAPI(): APIResource
87-
{
88-
if (is_null($this->secretsAPI)) {
89-
$api = new APIResource();
90-
$api->setClient($this->getClient())
91-
->setBaseUrl($this->getClient()->getApiUrl())
92-
->setIsHAL(false)
93-
->setBaseUri('/accounts')
94-
->setCollectionName('');
95-
$this->secretsAPI = $api;
96-
}
97-
98-
return clone $this->secretsAPI;
52+
return $this->accountAPI;
9953
}
10054

10155
/**
@@ -105,7 +59,7 @@ public function getSecretsAPI(): APIResource
10559
*/
10660
public function getPrefixPricing($prefix): array
10761
{
108-
$api = $this->getAccountAPI();
62+
$api = $this->getAPIResource();
10963
$api->setBaseUri('/account/get-prefix-pricing/outbound');
11064
$api->setCollectionName('prices');
11165

@@ -171,7 +125,7 @@ public function getVoicePrice(string $country): VoicePrice
171125
*/
172126
protected function makePricingRequest($country, $pricingType): array
173127
{
174-
$api = $this->getAccountAPI();
128+
$api = $this->getAPIResource();
175129
$api->setBaseUri('/account/get-pricing/outbound/' . $pricingType);
176130
$results = $api->search(new KeyValueFilter(['country' => $country]));
177131
$data = $results->getPageData();
@@ -194,7 +148,7 @@ protected function makePricingRequest($country, $pricingType): array
194148
*/
195149
public function getBalance(): Balance
196150
{
197-
$data = $this->getAccountAPI()->get('get-balance', [], ['accept' => 'application/json']);
151+
$data = $this->getAPIResource()->get('get-balance', [], ['accept' => 'application/json']);
198152

199153
if (is_null($data)) {
200154
throw new ClientException\Server('No results found');
@@ -209,7 +163,8 @@ public function getBalance(): Balance
209163
*/
210164
public function topUp($trx): void
211165
{
212-
$api = $this->getAccountAPI();
166+
$api = $this->getAPIResource();
167+
// @TODO why is this re-setting the base URL
213168
$api->setBaseUri('/account/top-up');
214169
$api->submit(['trx' => $trx]);
215170
}
@@ -223,7 +178,7 @@ public function topUp($trx): void
223178
*/
224179
public function getConfig(): Config
225180
{
226-
$api = $this->getAccountAPI();
181+
$api = $this->getAPIResource();
227182
$api->setBaseUri('/account/settings');
228183
$body = $api->submit();
229184

@@ -262,7 +217,7 @@ public function updateConfig(array $options): Config
262217
$params['drCallBackUrl'] = $options['dr_callback_url'];
263218
}
264219

265-
$api = $this->getAccountAPI();
220+
$api = $this->getAPIResource();
266221
$api->setBaseUri('/account/settings');
267222

268223
$rawBody = $api->submit($params);
@@ -281,89 +236,4 @@ public function updateConfig(array $options): Config
281236
$body['max-calls-per-second']
282237
);
283238
}
284-
285-
/**
286-
* @deprecated use the Vonage\Secrets\Client::list method
287-
*
288-
* @throws ClientExceptionInterface
289-
* @throws ClientException\Exception
290-
* @throws InvalidResponseException
291-
*/
292-
public function listSecrets(string $apiKey): SecretCollection
293-
{
294-
trigger_error('Vonage\Account\Client::listSecrets is deprecated, please use the Vonage\Secrets\Client::list method', E_USER_DEPRECATED);
295-
$api = $this->getSecretsAPI();
296-
297-
$data = $api->get($apiKey . '/secrets');
298-
return new SecretCollection($data['_embedded']['secrets'], $data['_links']);
299-
}
300-
301-
/**
302-
* @deprecated use the Vonage\Secrets\Client::get method
303-
*
304-
* @throws ClientExceptionInterface
305-
* @throws ClientException\Exception
306-
* @throws InvalidResponseException
307-
*/
308-
public function getSecret(string $apiKey, string $secretId): Secret
309-
{
310-
trigger_error('Vonage\Account\Client::getSecret is deprecated, please use the Vonage\Secrets\Client::get method', E_USER_DEPRECATED);
311-
$api = $this->getSecretsAPI();
312-
313-
$data = $api->get($apiKey . '/secrets/' . $secretId);
314-
return new Secret($data);
315-
}
316-
317-
/**
318-
* Create a new account secret
319-
*
320-
* @deprecated use the Vonage\Secrets\Client::create method
321-
*
322-
* @throws ClientExceptionInterface
323-
* @throws ClientRequestException
324-
* @throws ClientException\Exception
325-
* @throws InvalidResponseException
326-
* @throws ClientValidationException
327-
*/
328-
public function createSecret(string $apiKey, string $newSecret): Secret
329-
{
330-
trigger_error('Vonage\Account\Client::createSecret is deprecated, please use the Vonage\Secrets\Client::create method', E_USER_DEPRECATED);
331-
$api = $this->getSecretsAPI();
332-
$api->setBaseUri('/accounts/' . $apiKey . '/secrets');
333-
334-
try {
335-
$response = $api->create(['secret' => $newSecret]);
336-
} catch (ClientRequestException $e) {
337-
// @deprecated Throw a Validation exception to preserve old behavior
338-
// This will change to a general Request exception in the future
339-
$rawResponse = json_decode(@$e->getResponse()->getBody()->getContents(), true);
340-
341-
if (array_key_exists('invalid_parameters', $rawResponse)) {
342-
throw new ClientValidationException(
343-
$e->getMessage(),
344-
$e->getCode(),
345-
null,
346-
$rawResponse['invalid_parameters']
347-
);
348-
}
349-
350-
throw $e;
351-
}
352-
353-
return new Secret($response);
354-
}
355-
356-
/**
357-
* @deprecated use the Vonage\Secrets\Client::revoke method
358-
*
359-
* @throws ClientExceptionInterface
360-
* @throws ClientException\Exception
361-
*/
362-
public function deleteSecret(string $apiKey, string $secretId): void
363-
{
364-
trigger_error('Vonage\Account\Client::deleteSecret is deprecated, please use the Vonage\Secrets\Client::revoke method', E_USER_DEPRECATED);
365-
$api = $this->getSecretsAPI();
366-
$api->setBaseUri('/accounts/' . $apiKey . '/secrets');
367-
$api->delete($secretId);
368-
}
369239
}

src/Account/ClientFactory.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Psr\Container\ContainerInterface;
1515
use Vonage\Client\APIResource;
16+
use Vonage\Client\Credentials\Handler\BasicHandler;
1617

1718
class ClientFactory
1819
{
@@ -24,11 +25,9 @@ public function __invoke(ContainerInterface $container): Client
2425
->setBaseUrl($accountApi->getClient()->getRestUrl())
2526
->setIsHAL(false)
2627
->setBaseUri('/account')
28+
->setAuthHandler(new BasicHandler())
2729
;
2830

29-
$secretsApi = $container->make(APIResource::class);
30-
$secretsApi->setBaseUri('/accounts');
31-
32-
return new Client($accountApi, $secretsApi);
31+
return new Client($accountApi);
3332
}
3433
}

src/Account/Secret.php

Lines changed: 0 additions & 70 deletions
This file was deleted.

src/Account/SecretCollection.php

Lines changed: 0 additions & 72 deletions
This file was deleted.

src/Application/ClientFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Psr\Container\ContainerInterface;
1515
use Vonage\Client\APIResource;
16+
use Vonage\Client\Credentials\Handler\BasicHandler;
1617

1718
class ClientFactory
1819
{
@@ -22,7 +23,8 @@ public function __invoke(ContainerInterface $container): Client
2223
$api = $container->make(APIResource::class);
2324
$api
2425
->setBaseUri('/v2/applications')
25-
->setCollectionName('applications');
26+
->setCollectionName('applications')
27+
->setAuthHandler(new BasicHandler());
2628

2729
return new Client($api, new Hydrator());
2830
}

0 commit comments

Comments
 (0)