Skip to content

Commit

Permalink
Add VonageConfig object
Browse files Browse the repository at this point in the history
  • Loading branch information
SecondeJK committed Dec 16, 2024
1 parent 88594b4 commit 8fb6d07
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 132 deletions.
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
<testsuite name="default">
<directory>test</directory>
</testsuite>
<testsuite name="account">
<directory>test/Account</directory>
</testsuite>
<testsuite name="verify">
<directory>test/Verify</directory>
</testsuite>
Expand Down
98 changes: 18 additions & 80 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace Vonage;

use Composer\InstalledVersions;
use InvalidArgumentException;
use Psr\Container\ContainerInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Log\LoggerInterface;
use RuntimeException;
use Vonage\Account\ClientFactory;
use Vonage\Application\ClientFactory as ApplicationClientFactory;
Expand All @@ -20,6 +20,7 @@
use Vonage\Client\Credentials\SignatureSecret;
use Vonage\Client\Factory\FactoryInterface;
use Vonage\Client\Factory\MapFactory;
use Vonage\Client\VonageConfig;
use Vonage\Conversion\ClientFactory as ConversionClientFactory;
use Vonage\Insights\ClientFactory as InsightsClientFactory;
use Vonage\Meetings\ClientFactory as MeetingsClientFactory;
Expand Down Expand Up @@ -66,50 +67,31 @@
* @method Verify2\Client verify2()
* @method Voice\Client voice()
* @method Vonage\Video\Client video()
*
* @property string restUrl
* @property string apiUrl
*/
class Client implements LoggerAwareInterface
class Client
{
use LoggerTrait;

protected CredentialsInterface $credentials;

protected ClientInterface $client;

protected mixed $debug = false;

protected ContainerInterface $factory;
protected VonageConfig $vonageConfig;

/**
* @var LoggerInterface
*/
protected $logger;

protected array $options = ['show_deprecations' => false, 'debug' => false];
public const BASE_API = 'https://api.vonage.com/';

/**
* Create a new API client using the provided credentials.
*/
public function __construct(
CredentialsInterface $credentials,
?VonageConfig $options = null,
?ClientInterface $client = null
?VonageConfig $vonageConfig = null,
) {
if (is_null($client)) {
// Since the user did not pass a client, try and make a client
// using the Guzzle 6 adapter or Guzzle 7 (depending on availability)
[$guzzleVersion] = explode('@', (string) InstalledVersions::getVersion('guzzlehttp/guzzle'), 1);
$guzzleVersion = (float) $guzzleVersion;

if ($guzzleVersion >= 6.0 && $guzzleVersion < 7) {
$client = new \Http\Adapter\Guzzle6\Client();
}

if ($guzzleVersion >= 7.0 && $guzzleVersion < 8.0) {
$client = new \GuzzleHttp\Client();
}
if (is_null($vonageConfig)) {
$this->vonageConfig = new VonageConfig();
} else {
$this->vonageConfig = $vonageConfig;
}

if (
Expand All @@ -124,11 +106,9 @@ public function __construct(

$this->credentials = $credentials;

$this->options = array_merge($this->options, $options);

// If they've provided an app name, validate it
if (isset($options['app'])) {
$this->validateAppOptions($options['app']);
if (isset($vonageConfig['app'])) {
$this->validateAppOptions($vonageConfig['app']);
}

$services = [
Expand All @@ -154,7 +134,9 @@ public function __construct(

// Additional utility classes
APIResource::class => APIResource::class,
Client::class => fn () => $this
Client::class => fn () => $this,
VonageConfig::class => fn () => $this->vonageConfig,
'credentials' => fn () => $this->credentials,
];

if (class_exists('Vonage\Video\ClientFactory')) {
Expand All @@ -173,45 +155,15 @@ public function __construct(
);

// Disable throwing E_USER_DEPRECATED notices by default, the user can turn it on during development
if (array_key_exists('show_deprecations', $this->options) && ($this->options['show_deprecations'] == true)) {
if ($this->vonageConfig->getShowDeprecations()) {
set_error_handler(
static fn (int $errno, string $errstr, string $errfile = null, int $errline = null, array $errorcontext = null) => true,
static fn (int $errno, string $errstr, ?string $errfile = null, ?int $errline = null, ?array
$errorcontext = null) => true,
E_USER_DEPRECATED
);
}
}

public function getRestUrl(): string
{
return $this->restUrl;
}

public function getApiUrl(): string
{
return $this->apiUrl;
}

/**
* Set the Http Client to used to make API requests.
*
* This allows the default http client to be swapped out for a HTTPlug compatible
* replacement.
*/
public function setHttpClient(ClientInterface $client): self
{
$this->client = $client;

return $this;
}

/**
* Get the Http Client used to make API requests.
*/
public function getHttpClient(): ClientInterface
{
return $this->client;
}

/**
* Set the factory used to create API specific clients.
*/
Expand Down Expand Up @@ -271,20 +223,6 @@ public function __get($name)
return $this->factory->get($name);
}

protected function getVersion(): string
{
return InstalledVersions::getVersion('vonage/client-core');
}

public function getLogger(): ?LoggerInterface
{
if (!$this->logger && $this->getFactory()->has(LoggerInterface::class)) {
$this->setLogger($this->getFactory()->get(LoggerInterface::class));
}

return $this->logger;
}

public function getCredentials(): CredentialsInterface
{
return $this->credentials;
Expand Down
63 changes: 41 additions & 22 deletions src/Client/APIResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Laminas\Diactoros\Uri;
use Psr\Http\Client\ClientInterface;
use Psr\Log\LogLevel;
use Vonage\Client\Credentials\CredentialsInterface;
use Vonage\Client\Credentials\Handler\BasicHandler;
use Vonage\Entity\Filter\EmptyFilter;
use Psr\Http\Message\RequestInterface;
Expand Down Expand Up @@ -65,12 +66,13 @@ class APIResource

protected ?ResponseInterface $lastResponse = null;

protected ?ClientInterface $client = null;
protected bool $debug;
protected VonageConfig $vonageConfig;

public function __construct(?ClientInterface $client = null)
protected CredentialsInterface $credentials;

public function __construct(VonageConfig $vonageConfig)
{
if (is_null($client)) {
if (is_null($vonageConfig->getHttpClient())) {
// Since the user did not pass a client, try and make a client
// using the Guzzle 6 adapter or Guzzle 7 (depending on availability)
[$guzzleVersion] = explode('@', (string) InstalledVersions::getVersion('guzzlehttp/guzzle'), 1);
Expand All @@ -86,18 +88,29 @@ public function __construct(?ClientInterface $client = null)
$client = new GuzzleClient();
}

$this->setHttpClient($client);
$vonageConfig->setHttpClient($client);
}
}

public function getHttpClient(): ?ClientInterface
public function getVonageConfig(): VonageConfig
{
return $this->vonageConfig;
}

public function setVonageConfig(VonageConfig $vonageConfig): APIResource
{
$this->vonageConfig = $vonageConfig;
return $this;
}

public function getCredentials(): CredentialsInterface
{
return $this->client;
return $this->credentials;
}

public function setHttpClient(?ClientInterface $client): APIResource
public function setCredentials(CredentialsInterface $credentials): APIResource
{
$this->client = $client;
$this->credentials = $credentials;
return $this;
}

Expand All @@ -107,7 +120,7 @@ public function setHttpClient(?ClientInterface $client): APIResource
*/
public function addAuth(RequestInterface $request): RequestInterface
{
$credentials = $this->getClient()->getCredentials();
$credentials = $this->getCredentials();

if (is_array($this->getAuthHandlers())) {
foreach ($this->getAuthHandlers() as $handler) {
Expand Down Expand Up @@ -167,13 +180,14 @@ public function send(RequestInterface $request): ResponseInterface

// Set the header. Build by joining all the parts we have with a space
$request = $request->withHeader('User-Agent', implode(' ', $userAgent));
$response = $this->client->sendRequest($request);
$response = $this->getVonageConfig()->getHttpClient()->sendRequest($request);

if ($this->debug) {
if ($this->getVonageConfig()->isDebugMode()) {
$id = uniqid('', true);
$request->getBody()->rewind();
$response->getBody()->rewind();
$this->log(

$this->vonageConfig->getLogger()->log(
LogLevel::DEBUG,
'Request ' . $id,
[
Expand All @@ -182,7 +196,8 @@ public function send(RequestInterface $request): ResponseInterface
'body' => explode("\n", $request->getBody()->__toString())
]
);
$this->log(

$this->vonageConfig->getLogger()->log(
LogLevel::DEBUG,
'Response ' . $id,
[
Expand Down Expand Up @@ -223,7 +238,7 @@ public function create(array $body, string $uri = '', array $headers = []): ?arr

$this->lastRequest = $request;

$response = $this->getClient()->send($request);
$response = $this->getVonageConfig()->getHttpClient()->send($request);
$status = (int)$response->getStatusCode();

$this->setLastResponse($response);
Expand Down Expand Up @@ -269,7 +284,7 @@ public function delete(string $id, array $headers = []): ?array
$request = $this->addAuth($request);
}

$response = $this->getClient()->send($request);
$response = $this->getVonageConfig()->getHttpClient()->send($request);
$status = (int)$response->getStatusCode();

$this->lastRequest = $request;
Expand Down Expand Up @@ -327,7 +342,7 @@ public function get(
$request = $this->addAuth($request);
}

$response = $this->getClient()->send($request);
$response = $this->getVonageConfig()->getHttpClient()->send($request);
$status = (int)$response->getStatusCode();

$this->lastRequest = $request;
Expand All @@ -347,11 +362,11 @@ public function get(
return json_decode($response->getBody()->getContents(), true);
}

public function getAuthHandlers()
public function getAuthHandlers(): BasicHandler|array
{
// If we have not set a handler, default to Basic and issue warning.
if (!$this->authHandlers) {
$this->log(
$this->getVonageConfig()->getLogger()->log(
LogLevel::WARNING,
'Warning: no authorisation handler set for this Client. Defaulting to Basic which might not be
the correct authorisation for this API call'
Expand All @@ -365,8 +380,8 @@ public function getAuthHandlers()

public function getBaseUrl(): ?string
{
if (!$this->baseUrl && $this->client) {
$this->baseUrl = $this->client->getApiUrl();
if ($this->getVonageConfig()->getBaseUrl()) {
return $this->getVonageConfig()->getBaseUrl();
}

return $this->baseUrl;
Expand Down Expand Up @@ -455,7 +470,6 @@ public function search(?FilterInterface $filter = null, string $uri = ''): Itera
$collection
->setApiResource($api)
->setFilter($filter);
$collection->setClient($this->client);

return $collection;
}
Expand Down Expand Up @@ -621,4 +635,9 @@ public function setErrorsOn200(bool $value): self

return $this;
}

protected function getVersion(): string
{
return InstalledVersions::getVersion('vonage/client-core');
}
}
Loading

0 comments on commit 8fb6d07

Please sign in to comment.