From 139cc0272727e494c169bf7c2f5915d1aef036d6 Mon Sep 17 00:00:00 2001 From: chadicus Date: Wed, 2 Jul 2025 09:28:46 -0400 Subject: [PATCH 1/6] Allow netacuity url to be injected --- src/Databases/AbstractNetAcuityDatabase.php | 23 ++++++++++++++++----- src/Databases/EdgeDatabase.php | 13 ++++++++---- src/Databases/PulseDatabase.php | 13 ++++++++++-- 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/src/Databases/AbstractNetAcuityDatabase.php b/src/Databases/AbstractNetAcuityDatabase.php index 6f05cff..0c0a064 100644 --- a/src/Databases/AbstractNetAcuityDatabase.php +++ b/src/Databases/AbstractNetAcuityDatabase.php @@ -13,6 +13,12 @@ */ abstract class AbstractNetAcuityDatabase implements NetAcuityDatabaseInterface { + /** + * @var string + * @internal + */ + const DEFAULT_NETACUITY_BASE_URL = 'https://usa.cloud.netacuity.com/webservice/query'; + /** * @var ClientInterface The GuzzleHttp Client. */ @@ -23,6 +29,11 @@ abstract class AbstractNetAcuityDatabase implements NetAcuityDatabaseInterface */ protected $translations; + /** + * @var string + */ + protected $netacuityBaseUrl; + /** * @var string The API User Token. */ @@ -36,15 +47,18 @@ abstract class AbstractNetAcuityDatabase implements NetAcuityDatabaseInterface /** * AbstractNetAcuityDatabase constructor. * - * @param ClientInterface $client The injected GuzzleHttp Client. - * @param string $apiUserToken The Net Acuity API User Token. + * @param ClientInterface $client The injected GuzzleHttp Client. + * @param string $apiUserToken The Net Acuity API User Token. + * @param string $netacuityBaseUrl The base url for the netacuity webservice. */ public function __construct( ClientInterface $client, - string $apiUserToken + string $apiUserToken, + string $netacuityBaseUrl ) { $this->client = $client; $this->apiUserToken = $apiUserToken; + $this->netacuityBaseUrl = $netacuityBaseUrl; } /** @@ -117,7 +131,6 @@ protected function parseBody(array $response) */ protected function buildQuery(string $userToken, string $ip): string { - $baseUrl = 'https://usa.cloud.netacuity.com/webservice/query'; - return "{$baseUrl}?u={$userToken}&dbs={$this->databaseIdentifier}&ip={$ip}&json=true"; + return "{$this->netacuityBaseUrl}?u={$userToken}&dbs={$this->databaseIdentifier}&ip={$ip}&json=true"; } } diff --git a/src/Databases/EdgeDatabase.php b/src/Databases/EdgeDatabase.php index 2360d15..f9689bb 100644 --- a/src/Databases/EdgeDatabase.php +++ b/src/Databases/EdgeDatabase.php @@ -12,12 +12,17 @@ final class EdgeDatabase extends AbstractNetAcuityDatabase /** * EdgeDatabase constructor. * - * @param ClientInterface $client The injected GuzzleHttp Client. - * @param string $apiUserToken The Net Acuity API User Token. + * @param ClientInterface $client The injected GuzzleHttp Client. + * @param string $apiUserToken The Net Acuity API User Token. + * @param string $netacuityBaseUrl The base url for the netacuity webservice. */ - public function __construct(ClientInterface $client, string $apiUserToken) + public function __construct( + ClientInterface $client, + string $apiUserToken, + string $netacuityBaseUrl = self::DEFAULT_NETACUITY_BASE_URL + ) { - parent::__construct($client, $apiUserToken); + parent::__construct($client, $apiUserToken, $netacuityBaseUrl); $this->databaseIdentifier = 4; diff --git a/src/Databases/PulseDatabase.php b/src/Databases/PulseDatabase.php index 8402b64..06e0c22 100644 --- a/src/Databases/PulseDatabase.php +++ b/src/Databases/PulseDatabase.php @@ -6,9 +6,18 @@ class PulseDatabase extends AbstractNetAcuityDatabase { - public function __construct(ClientInterface $client, string $apiUserToken) + /** + * @param ClientInterface $client The injected GuzzleHttp Client. + * @param string $apiUserToken The Net Acuity API User Token. + * @param string $netacuityBaseUrl The base url for the netacuity webservice. + */ + public function __construct( + ClientInterface $client, + string $apiUserToken, + string $netacuityBaseUrl = self::DEFAULT_NETACUITY_BASE_URL + ) { - parent::__construct($client, $apiUserToken); + parent::__construct($client, $apiUserToken, $netacuityBaseUrl); $this->databaseIdentifier = 26; From fa721b40635cfc905fd9bf982f5527692ec3ffd7 Mon Sep 17 00:00:00 2001 From: chadicus Date: Wed, 2 Jul 2025 09:38:45 -0400 Subject: [PATCH 2/6] Ignore constant visibility warning since the library supports PHP < 7.1 --- phpcs.xml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/phpcs.xml b/phpcs.xml index a72f6d1..48c9d6d 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -4,5 +4,7 @@ */vendor/* - + + + From 260cf3687476fe5cadecf230d52cf5d64584ae2a Mon Sep 17 00:00:00 2001 From: chadicus Date: Wed, 2 Jul 2025 13:41:15 -0400 Subject: [PATCH 3/6] Add NetacuityException --- src/Exceptions/NetacuityException.php | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/Exceptions/NetacuityException.php diff --git a/src/Exceptions/NetacuityException.php b/src/Exceptions/NetacuityException.php new file mode 100644 index 0000000..973de81 --- /dev/null +++ b/src/Exceptions/NetacuityException.php @@ -0,0 +1,9 @@ + Date: Wed, 2 Jul 2025 13:43:08 -0400 Subject: [PATCH 4/6] Fixup url --- src/Databases/EdgeDatabase.php | 3 +-- src/Databases/PulseDatabase.php | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Databases/EdgeDatabase.php b/src/Databases/EdgeDatabase.php index f9689bb..14d6935 100644 --- a/src/Databases/EdgeDatabase.php +++ b/src/Databases/EdgeDatabase.php @@ -20,8 +20,7 @@ public function __construct( ClientInterface $client, string $apiUserToken, string $netacuityBaseUrl = self::DEFAULT_NETACUITY_BASE_URL - ) - { + ) { parent::__construct($client, $apiUserToken, $netacuityBaseUrl); $this->databaseIdentifier = 4; diff --git a/src/Databases/PulseDatabase.php b/src/Databases/PulseDatabase.php index 06e0c22..ce00c17 100644 --- a/src/Databases/PulseDatabase.php +++ b/src/Databases/PulseDatabase.php @@ -15,8 +15,7 @@ public function __construct( ClientInterface $client, string $apiUserToken, string $netacuityBaseUrl = self::DEFAULT_NETACUITY_BASE_URL - ) - { + ) { parent::__construct($client, $apiUserToken, $netacuityBaseUrl); $this->databaseIdentifier = 26; From 6d287c82ea2929428fa4557ab908ae28429c8ce4 Mon Sep 17 00:00:00 2001 From: chadicus Date: Wed, 2 Jul 2025 13:45:49 -0400 Subject: [PATCH 5/6] Use NetacuityException --- src/Databases/AbstractNetAcuityDatabase.php | 10 +++++----- tests/Databases/AbstractNetAcuityDatabaseTest.php | 3 +++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Databases/AbstractNetAcuityDatabase.php b/src/Databases/AbstractNetAcuityDatabase.php index 0c0a064..eb3ad7a 100644 --- a/src/Databases/AbstractNetAcuityDatabase.php +++ b/src/Databases/AbstractNetAcuityDatabase.php @@ -2,8 +2,8 @@ namespace TraderInteractive\NetAcuity\Databases; +use TraderInteractive\NetAcuity\Exceptions\NetacuityException; use TraderInteractive\Util\Arrays; -use Exception; use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Psr7\Request; @@ -66,7 +66,7 @@ public function __construct( * * @return array The formatted data set. * - * @throws Exception On failure to send a Guzzle request. + * @throws NetacuityException On failure to send a Guzzle request. */ public function fetch(string $ip) { @@ -87,7 +87,7 @@ public function fetch(string $ip) /** * @param ClientException $e The thrown exception for handling. * - * @throws Exception A formatted exception masking the API User Token in the event that it becomes invalid. + * @throws NetacuityException A formatted exception masking the API User Token in the event that it becomes invalid. * * @return void */ @@ -97,13 +97,13 @@ protected function handleGuzzleException(ClientException $e) $code = $response->getStatusCode(); if ($code === 403) { - throw new Exception('NetAcuity API rejected the provided api user token.', $code); + throw new NetacuityException('NetAcuity API rejected the provided api user token.', $code); } $error = json_decode($response->getBody()->getContents(), true); $reason = Arrays::getNested($error, 'error.message'); - throw new Exception("NetAcuity API rejected the request, Reason: {$reason}", $code); + throw new NetacuityException("NetAcuity API rejected the request, Reason: {$reason}", $code); } /** diff --git a/tests/Databases/AbstractNetAcuityDatabaseTest.php b/tests/Databases/AbstractNetAcuityDatabaseTest.php index b2e6d78..dfee335 100644 --- a/tests/Databases/AbstractNetAcuityDatabaseTest.php +++ b/tests/Databases/AbstractNetAcuityDatabaseTest.php @@ -3,6 +3,7 @@ namespace TraderInteractive\NetAcuity\Databases\Tests; use TraderInteractive\NetAcuity\Databases\EdgeDatabase; +use TraderInteractive\NetAcuity\Exceptions\NetacuityException; use TraderInteractive\NetAcuity\Tests\NetAcuityTestSuite; use Exception; @@ -154,6 +155,7 @@ public function getGeoWithExtraFieldEdge() */ public function getGeoNonStringIp() { + $this->expectException(NetacuityException::class); $this->expectExceptionMessage('NetAcuity API rejected the request, Reason: Invalid IP (1)'); $this->expectExceptionCode(400); @@ -171,6 +173,7 @@ public function getGeoNonStringIp() */ public function netAcuityUserTokenInvalid() { + $this->expectException(NetacuityException::class); $this->expectExceptionMessage('NetAcuity API rejected the provided api user token.'); $this->expectExceptionCode(403); From a97cf64a55f4f0d8e143375de30c4aea511885e5 Mon Sep 17 00:00:00 2001 From: Chad Gray <1182337+chadicus@users.noreply.github.com> Date: Thu, 3 Jul 2025 08:38:52 -0400 Subject: [PATCH 6/6] Update src/Databases/AbstractNetAcuityDatabase.php Co-authored-by: James Carver --- src/Databases/AbstractNetAcuityDatabase.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Databases/AbstractNetAcuityDatabase.php b/src/Databases/AbstractNetAcuityDatabase.php index eb3ad7a..8ab5e5d 100644 --- a/src/Databases/AbstractNetAcuityDatabase.php +++ b/src/Databases/AbstractNetAcuityDatabase.php @@ -54,7 +54,7 @@ abstract class AbstractNetAcuityDatabase implements NetAcuityDatabaseInterface public function __construct( ClientInterface $client, string $apiUserToken, - string $netacuityBaseUrl + string $netacuityBaseUrl = self::DEFAULT_NETACUITY_BASE_URL ) { $this->client = $client; $this->apiUserToken = $apiUserToken;