diff --git a/composer.json b/composer.json index c6bb164..0bd69be 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,8 @@ }, "require": { "php": ">=8.0", - "utopia-php/cli": "^0.15.0" + "utopia-php/cli": "^0.15.0", + "sahils/utopia-fetch": "dev-main" }, "require-dev": { "phpunit/phpunit": "^9.3", diff --git a/src/Analytics/Adapter/ActiveCampaign.php b/src/Analytics/Adapter/ActiveCampaign.php index 71f84a4..1ae55f6 100644 --- a/src/Analytics/Adapter/ActiveCampaign.php +++ b/src/Analytics/Adapter/ActiveCampaign.php @@ -7,6 +7,7 @@ use Utopia\Analytics\Adapter; use Utopia\Analytics\Event; +use Sahils\UtopiaFetch\Client; class ActiveCampaign extends Adapter { @@ -44,9 +45,13 @@ public function getName(): string public function contactExists(string $email): bool|int { try { - $result = $this->call('GET', '/api/3/contacts', [], [ - 'email' => $email, - ]); + $result = Client::fetch( + url: $this->endpoint.'/api/3/contacts', + method: 'GET', + query: [ + 'email' => $email, + ], + )->getBody(); $result = json_decode($result, true); @@ -75,9 +80,11 @@ public function createContact(string $email, string $firstName = '', string $las ]]; try { - $this->call('POST', '/api/3/contacts', [ - 'Content-Type' => 'application/json', - ], $body); + Client::fetch( + url: $this->endpoint.'/api/3/contacts', + method: 'POST', + body: $body, + ); return true; } catch (\Exception $e) { @@ -100,9 +107,11 @@ public function updateContact(string $contactId, string $email, string $firstNam ]]; try { - $this->call('PUT', '/api/3/contacts/'.$contactId, [ - 'Content-Type' => 'application/json', - ], $body); + Client::fetch( + url: $this->endpoint.'/api/3/contacts'.$contactId, + method: 'PUT', + body: $body, + ); return true; } catch (\Exception $e) { @@ -124,7 +133,10 @@ public function deleteContact(string $email): bool } try { - $this->call('DELETE', '/api/3/contacts/'.$contact); + Client::fetch( + url: $this->endpoint.'/api/3/contacts'.$contact, + method: 'DELETE' + ); return true; } catch (\Exception $e) { @@ -140,9 +152,13 @@ public function deleteContact(string $email): bool public function accountExists(string $name): bool|int { try { - $result = $this->call('GET', '/api/3/accounts', [], [ - 'search' => $name, - ]); + $result = Client::fetch( + url: $this->endpoint.'/api/3/accounts', + method: 'GET', + query: [ + 'search' => $name + ] + )->getBody(); if (intval(json_decode($result, true)['meta']['total']) > 0) { return intval((json_decode($result, true))['accounts'][0]['id']); @@ -171,9 +187,11 @@ public function createAccount(string $name, string $url = '', int $ownerId = 1, ]]; try { - $this->call('POST', '/api/3/accounts', [ - 'Content-Type' => 'application/json', - ], $body); + Client::fetch( + url: $this->endpoint.'/api/3/accounts', + method: 'POST', + body: $body, + ); return true; } catch (\Exception $e) { @@ -198,9 +216,11 @@ public function updateAccount(string $accountId, string $name, string $url = '', ]]; try { - $this->call('PUT', '/api/3/accounts/'.$accountId, [ - 'Content-Type' => 'application/json', - ], array_filter($body)); + Client::fetch( + url: $this->endpoint.'/api/3/accounts/'.$accountId, + method: 'PUT', + body: array_filter($body), + ); return true; } catch (\Exception $e) { @@ -216,7 +236,10 @@ public function updateAccount(string $accountId, string $name, string $url = '', public function deleteAccount(string $accountId): bool { try { - $this->call('DELETE', '/api/3/accounts/'.$accountId); + Client::fetch( + url: $this->endpoint.'/api/3/accounts/'.$accountId, + method: 'DELETE' + ); return true; } catch (\Exception $e) { @@ -236,10 +259,14 @@ public function syncAssociation(string $accountId, string $contactId, string $ro // See if the association already exists try { - $result = $this->call('GET', '/api/3/accountContacts', [], [ - 'filters[account]' => $accountId, - 'filters[contact]' => $contactId, - ]); + $result = Client::fetch( + url: $this->endpoint.'/api/3/accountContacts', + method: 'GET', + query: [ + 'filters[account]' => $accountId, + 'filters[contact]' => $contactId, + ] + )->getBody(); } catch (\Exception $e) { $this->logError($e); @@ -251,13 +278,15 @@ public function syncAssociation(string $accountId, string $contactId, string $ro $associationId = intval((json_decode($result, true))['accountContacts'][0]['id']); try { - $result = $this->call('PUT', '/api/3/accountContacts/'.$associationId, [ - 'Content-Type' => 'application/json', - ], [ - 'accountContact' => [ - 'jobTitle' => $role, - ], - ]); + $result = Client::fetch( + url: $this->endpoint.'/api/3/accountContacts/'.$associationId, + method: 'PUT', + body: [ + 'accountContact' => [ + 'jobTitle' => $role, + ], + ] + )->getBody(); return true; } catch (\Exception $e) { @@ -267,13 +296,17 @@ public function syncAssociation(string $accountId, string $contactId, string $ro } } else { // Create the association - $result = $this->call('POST', '/api/3/accountContacts', [ - 'Content-Type' => 'application/json', - ], ['accountContact' => [ - 'account' => $accountId, - 'contact' => $contactId, - 'jobTitle' => $role, - ]]); + $result = Client::fetch( + url: $this->endpoint.'/api/3/accountContacts', + method: 'POST', + body: [ + 'accountContact' => [ + 'account' => $accountId, + 'contact' => $contactId, + 'jobTitle' => $role, + ], + ] + )->getBody(); return true; } @@ -312,8 +345,11 @@ public function send(Event $event): bool ]; $query = array_filter($query, fn ($value) => ! is_null($value) && $value !== ''); - - $res = $this->call('POST', 'https://trackcmp.net/event', [], $query); // Active Campaign event URL, Refer to https://developers.activecampaign.com/reference/track-event/ for more details + $res = Client::fetch( + url: 'https://trackcmp.net/event', + method: 'POST', + body: $query + )->getBody(); if (json_decode($res, true)['success'] === 1) { return true; } else { @@ -348,14 +384,16 @@ public function setTags(string $contactId, array $tags): bool { foreach ($tags as $tag) { try { - $this->call('POST', '/api/3/contactTags', [ - 'Content-Type' => 'application/json', - ], [ - 'contactTag' => [ - 'contact' => $contactId, - 'tag' => $tag, - ], - ]); + Client::fetch( + url: $this->endpoint.'/api/3/contactTags', + method: 'POST', + body: [ + 'contactTag' => [ + 'contact' => $contactId, + 'tag' => $tag, + ], + ] + ); } catch (\Exception $e) { $this->logError($e); @@ -386,10 +424,14 @@ public function validate(Event $event): bool $foundLog = false; // Get contact again, since AC doesn't refresh logs immediately - $response = $this->call('GET', '/api/3/activities', [], [ - 'contact' => $contactID, - 'orders[tstamp]' => 'DESC', - ]); + $response = Client::fetch( + url: $this->endpoint.'/api/3/activities', + method: 'GET', + query: [ + 'contact' => $contactID, + 'orders[tstamp]' => 'DESC', + ] + )->getBody(); $response = json_decode($response, true); diff --git a/src/Analytics/Adapter/GoogleAnalytics.php b/src/Analytics/Adapter/GoogleAnalytics.php index dacd90b..593dca9 100644 --- a/src/Analytics/Adapter/GoogleAnalytics.php +++ b/src/Analytics/Adapter/GoogleAnalytics.php @@ -4,6 +4,7 @@ use Utopia\Analytics\Adapter; use Utopia\Analytics\Event; +use Sahils\UtopiaFetch\Client; class GoogleAnalytics extends Adapter { @@ -81,15 +82,18 @@ public function validate(Event $event): bool ]; $query = array_filter($query, fn ($value) => ! is_null($value) && $value !== ''); - - $validateResponse = $this->call('POST', $this->debugEndpoint, [], array_merge( - $query, - [ - 'tid' => $this->tid, - 'cid' => $this->cid, - 'v' => 1, - ] - )); + $validateResponse = Client::fetch( + url: $this->debugEndpoint, + method: 'POST', + body: array_merge( + $query, + [ + 'tid' => $this->tid, + 'cid' => $this->cid, + 'v' => 1, + ] + ) + )->getBody(); $validateResponse = json_decode($validateResponse, true); @@ -140,11 +144,18 @@ public function send(Event $event): bool $query = array_filter($query, fn ($value) => ! is_null($value) && $value !== ''); - $result = $this->call('POST', $this->endpoint, [], array_merge([ - 'tid' => $this->tid, - 'cid' => $this->cid, - 'v' => 1, - ], $query)); + $result = Client::fetch( + url: $this->endpoint, + method: 'POST', + body: array_merge( + $query, + [ + 'tid' => $this->tid, + 'cid' => $this->cid, + 'v' => 1, + ] + ) + )->getBody(); // Parse Debug data if ($this->endpoint == 'https://www.google-analytics.com/debug/collect') { diff --git a/src/Analytics/Adapter/Mixpanel.php b/src/Analytics/Adapter/Mixpanel.php index 5555c10..ae536d2 100644 --- a/src/Analytics/Adapter/Mixpanel.php +++ b/src/Analytics/Adapter/Mixpanel.php @@ -5,6 +5,7 @@ use Exception; use Utopia\Analytics\Adapter; use Utopia\Analytics\Event; +use Sahils\UtopiaFetch\Client; class Mixpanel extends Adapter { @@ -74,8 +75,12 @@ public function send(Event $event): bool 'Content-Type' => 'application/json', 'accept' => 'text/plain', ]; - - $res = $this->call('POST', '/track', $headers, $payload); + $res = Client::fetch( + url: $this->endpoint.'/track', + method: 'POST', + headers: $headers, + body: $payload + )->getBody(); if ($res !== '1') { throw new Exception('Failed to send event for '.$event->getProp('email')); @@ -105,7 +110,12 @@ public function createProfile(string $distinctId, string $ip, array $properties 'accept' => 'text/plain', ]; - $res = $this->call('POST', '/engage#profile-set', $headers, $payload); + $res = Client::fetch( + method: 'POST', + url: $this->endpoint.'/engage#profile-set', + headers: $headers, + body: $payload + ); if ($res !== '1') { return false; @@ -131,7 +141,12 @@ public function appendProperties(string $distinctId, array $properties): bool 'accept' => 'text/plain', ]; - $res = $this->call('POST', '/engage#profile-union', $headers, $payload); + $res = Client::fetch( + method: 'POST', + url: $this->endpoint.'/engage#profile-union', + headers: $headers, + body: $payload + ); if ($res !== '1') { return false; diff --git a/src/Analytics/Adapter/Orbit.php b/src/Analytics/Adapter/Orbit.php index 33477ce..2559847 100644 --- a/src/Analytics/Adapter/Orbit.php +++ b/src/Analytics/Adapter/Orbit.php @@ -4,6 +4,7 @@ use Utopia\Analytics\Adapter; use Utopia\Analytics\Event; +use Sahils\UtopiaFetch\Client; class Orbit extends Adapter { @@ -81,13 +82,17 @@ public function send(Event $event): bool unset($activity['properties']['name']); $activity = array_filter($activity, fn ($value) => ! is_null($value) && $value !== ''); - - $this->call('POST', $this->endpoint.'/activities', [ - 'Content-Type' => 'application/json', - 'Authorization' => 'Bearer '.$this->apiKey, - ], [ - 'activity' => $activity, - ]); + Client::fetch( + url: $this->endpoint.'/activities', + method: 'POST', + headers: [ + 'Content-Type' => 'application/json', + 'Authorization' => 'Bearer '.$this->apiKey, + ], + body: [ + 'activity' => $activity, + ], + ); return true; } @@ -139,12 +144,17 @@ public function validate(Event $event): bool } // Check if event made it. - $listMembers = $this->call('GET', '/members/find', [ + $listMembers = Client::fetch( + url: $this->endpoint.'/members/find', + method: 'GET', + headers: [ 'Authorization' => 'Bearer '.$this->apiKey, - ], [ - 'source' => 'email', - 'email' => $event->getProp('email'), - ]); + ], + query: [ + 'source' => 'email', + 'email' => $event->getProp('email'), + ], + )->getBody(); $listMembers = json_decode($listMembers, true); @@ -154,11 +164,16 @@ public function validate(Event $event): bool $member = $listMembers['data']; - $activities = $this->call('GET', '/members/'.$member['id'].'/activities', [ + $activities = Client::fetch( + url: $this->endpoint.'/members/'.$member['id'].'/activities', + method: 'GET', + headers: [ 'Authorization' => 'Bearer '.$this->apiKey, - ], [ - 'activity_type' => $event->getType(), - ]); + ], + query: [ + 'activity_type' => $event->getType(), + ], + )->getBody(); $activities = json_decode($activities, true); @@ -177,10 +192,13 @@ public function validate(Event $event): bool if (! $foundActivity) { throw new \Exception('Failed to find event in Orbit'); } - - $this->call('DELETE', '/members/'.$member['id'].'/activities/'.$foundActivity, [ - 'Authorization' => 'Bearer '.$this->apiKey, - ], []); + Client::fetch( + url: $this->endpoint.'/members/'.$member['id'].'/activities/'.$foundActivity, + method: 'DELETE', + headers: [ + 'Authorization' => 'Bearer '.$this->apiKey, + ] + ); return true; } diff --git a/src/Analytics/Adapter/Plausible.php b/src/Analytics/Adapter/Plausible.php index 04abd64..958b7d2 100644 --- a/src/Analytics/Adapter/Plausible.php +++ b/src/Analytics/Adapter/Plausible.php @@ -16,6 +16,7 @@ use Exception; use Utopia\Analytics\Adapter; use Utopia\Analytics\Event; +use Sahils\UtopiaFetch\Client; class Plausible extends Adapter { @@ -96,7 +97,12 @@ public function send(Event $event): bool 'Content-Type' => 'application/json', ]; - $this->call('POST', '/event', $headers, $params); + Client::fetch( + url: $this->endpoint.'/event', + method: 'POST', + headers: $headers, + body: $params + ); return true; } @@ -119,7 +125,12 @@ private function provisionGoal(string $eventName): bool 'Authorization' => 'Bearer '.$this->apiKey, ]; - $this->call('PUT', '/v1/sites/goals', $headers, $params); + Client::fetch( + url: $this->endpoint.'/v1/sites/goals', + method: 'PUT', + headers: $headers, + body: $params + ); return true; } @@ -142,11 +153,13 @@ public function validate(Event $event): bool 'site_id' => $this->domain, 'filters' => json_encode(['goal' => $event->getName()]), ]); - - $checkCreated = $this->call('GET', $validateURL, [ - 'Content-Type' => '', - 'Authorization' => 'Bearer '.$this->apiKey, - ]); + $checkCreated = Client::fetch( + url: $validateURL, + headers: [ + 'Content-Type' => '', + 'Authorization' => 'Bearer '.$this->apiKey, + ] + )->getBody(); $checkCreated = json_decode($checkCreated, true); if (! isset($checkCreated['results']['visitors']['value'])) { diff --git a/tests/Analytics/AnalyticsTest.php b/tests/Analytics/AnalyticsTest.php index e2a1d7a..3edb08f 100644 --- a/tests/Analytics/AnalyticsTest.php +++ b/tests/Analytics/AnalyticsTest.php @@ -147,7 +147,8 @@ public function testActiveCampaignUpdateAccount($data) $data['accountID'], 'Utopia', 'utopia.com', - 1)); + 1 + )); } /**