From ccdac0c4a0aae56a7026dc59946635aae168d96d Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Sun, 29 Dec 2024 00:46:28 +0100 Subject: [PATCH] force json response format, fix subscriber endpoint --- src/Resources/Account.php | 31 ++++-- src/Resources/Field.php | 18 +++- src/Resources/Model.php | 13 +++ src/Resources/Subscriber.php | 179 +++++++++++++++++++---------------- 4 files changed, 147 insertions(+), 94 deletions(-) diff --git a/src/Resources/Account.php b/src/Resources/Account.php index 4ba2a07..2cc1431 100644 --- a/src/Resources/Account.php +++ b/src/Resources/Account.php @@ -1,5 +1,18 @@ + * @link https://www.oxidmodule.com + */ + namespace D3\KlicktippPhpClient\Resources; use D3\KlicktippPhpClient\Exceptions\BaseException; @@ -9,34 +22,32 @@ use GuzzleHttp\RequestOptions; class Account extends Model { /** - * @throws BaseException|GuzzleException + * @throws BaseException */ public function login(): array { - $response = $this->connection->request( + return $this->connection->requestAndParse( 'POST', - 'account/login', + 'account/login.json', [ RequestOptions::FORM_PARAMS => [ 'username' => $this->connection->getClientKey(), - 'password' => $this->connection->getSecretKey() - ] + 'password' => $this->connection->getSecretKey(), + ], ] ); - - return $this->connection->parseResponse($response); } /** - * @throws BaseException|GuzzleException + * @throws BaseException */ public function logout(): bool { $response = $this->connection->requestAndParse( 'POST', - 'account/logout' + 'account/logout.json' ); - return (bool) current($response); + return (bool)current($response); } } diff --git a/src/Resources/Field.php b/src/Resources/Field.php index 29aaec1..8d7aa07 100644 --- a/src/Resources/Field.php +++ b/src/Resources/Field.php @@ -1,21 +1,33 @@ + * @link https://www.oxidmodule.com + */ + namespace D3\KlicktippPhpClient\Resources; use D3\KlicktippPhpClient\Entities\FieldList; use D3\KlicktippPhpClient\Exceptions\BaseException; -use GuzzleHttp\Exception\GuzzleException; class Field extends Model { /** - * @throws BaseException|GuzzleException + * @throws BaseException */ public function index(): FieldList { $data = $this->connection->requestAndParse( 'GET', - 'field' + 'field.json' ); return new FieldList($data); diff --git a/src/Resources/Model.php b/src/Resources/Model.php index fbc52d0..d8e3e29 100644 --- a/src/Resources/Model.php +++ b/src/Resources/Model.php @@ -1,5 +1,18 @@ + * @link https://www.oxidmodule.com + */ + namespace D3\KlicktippPhpClient\Resources; use D3\KlicktippPhpClient\Connection; diff --git a/src/Resources/Subscriber.php b/src/Resources/Subscriber.php index 7a82bec..a8637d9 100644 --- a/src/Resources/Subscriber.php +++ b/src/Resources/Subscriber.php @@ -1,5 +1,18 @@ + * @link https://www.oxidmodule.com + */ + namespace D3\KlicktippPhpClient\Resources; use D3\KlicktippPhpClient\Entities\Subscriber as SubscriberEntity; @@ -11,195 +24,199 @@ use GuzzleHttp\RequestOptions; class Subscriber extends Model { /** - * @throws BaseException|GuzzleException + * @throws BaseException */ public function index(): SubscriberList { $data = $this->connection->requestAndParse( 'GET', - 'subscriber' + 'subscriber.json' ); return new SubscriberList($data); } /** - * @throws BaseException|GuzzleException + * @throws BaseException */ public function get(string $subscriberId): SubscriberEntity { $data = $this->connection->requestAndParse( 'GET', - 'subscriber/'.urlencode(trim($subscriberId)) + 'subscriber/'.urlencode(trim($subscriberId)).'.json' ); return new SubscriberEntity($data); } /** - * @throws BaseException|GuzzleException + * @throws BaseException */ public function search(string $mailAddress): string { return current( $this->connection->requestAndParse( 'POST', - 'subscriber/search', - [ - RequestOptions::FORM_PARAMS => [ - 'email' => trim($mailAddress) - ] - ] - ) - ); - } - - /** - * @throws BaseException|GuzzleException - */ - public function subscribe( - string $mailAddress, - string $listId, - string $tagId, - array $fields, - string $smsNumber - ): string - { - return current( - $this->connection->requestAndParse( - 'POST', - 'subscriber', + 'subscriber/search.json', [ RequestOptions::FORM_PARAMS => [ 'email' => trim($mailAddress), - 'listid' => trim($listId), - 'tagid' => trim($tagId), - 'fields' => array_filter($fields), - 'smsnumber' => trim($smsNumber), - ] + ], ] ) ); } /** - * @throws BaseException|GuzzleException + * @throws BaseException */ - public function unsubscribe(string $mailAddress): string - { + public function subscribe( + string $mailAddress, + ?string $listId = null, + ?string $tagId = null, + ?array $fields = null, + ?string $smsNumber = null, + ): string { return current( $this->connection->requestAndParse( 'POST', - 'subscriber/unsubscribe', + 'subscriber.json', [ - RequestOptions::FORM_PARAMS => [ - 'email' => trim($mailAddress) - ] + RequestOptions::FORM_PARAMS => array_filter( + [ + 'email' => trim($mailAddress), + 'listid' => trim($listId ?? ''), + 'tagid' => trim($tagId ?? ''), + 'fields' => array_filter($fields ?? []), + 'smsnumber' => trim($smsNumber ?? ''), + ] + ), ] ) ); } /** - * @throws BaseException|GuzzleException + * @throws BaseException + */ + public function unsubscribe(string $mailAddress): bool + { + return (bool) current( + $this->connection->requestAndParse( + 'POST', + 'subscriber/unsubscribe.json', + [ + RequestOptions::FORM_PARAMS => [ + 'email' => trim($mailAddress), + ], + ] + ) + ); + } + + /** + * @throws BaseException */ public function tag(string $mailAddress, array $tagIds): string { return current( $this->connection->requestAndParse( 'POST', - 'subscriber/tag', + 'subscriber/tag.json', [ RequestOptions::FORM_PARAMS => [ 'email' => trim($mailAddress), - //ToDo: apply trim to array - 'tagids' => $tagIds - ] + 'tagids' => implode(',', array_filter( + array_map('trim', $tagIds) + )), + ], ] ) ); } /** - * @throws BaseException|GuzzleException + * @throws BaseException */ public function untag(string $mailAddress, string $tagId): string { return current( $this->connection->requestAndParse( 'POST', - 'subscriber/search', + 'subscriber/untag.json', [ RequestOptions::FORM_PARAMS => [ 'email' => trim($mailAddress), - 'tagid' => trim($tagId) - ] + 'tagid' => trim($tagId), + ], ] ) ); } /** - * @throws BaseException|GuzzleException + * @throws BaseException */ - public function tagged(string $tagId): string + public function tagged(string $tagId): array { - return current( - $this->connection->requestAndParse( - 'POST', - 'subscriber/tagged', - [ - RequestOptions::FORM_PARAMS => [ - 'tagid' => trim($tagId) - ] - ] - ) - ); + return $this->connection->requestAndParse( + 'POST', + 'subscriber/tagged.json', + [ + RequestOptions::FORM_PARAMS => [ + 'tagid' => trim($tagId), + ], + ] + ) ?? []; } /** - * @throws BaseException|GuzzleException + * @throws BaseException */ - public function update(string $subscriberId, array $fields, string $newEmail = '', string $newSmsNumber = ''): string - { + public function update( + string $subscriberId, + array $fields, + string $newEmail = '', + string $newSmsNumber = '' + ): string { return current( $this->connection->requestAndParse( 'PUT', - 'subscriber/'.urlencode(trim($subscriberId)), + 'subscriber/'.urlencode(trim($subscriberId)).'.json', [ RequestOptions::FORM_PARAMS => [ //ToDo: apply trim to array - 'fields' => $fields, + 'fields' => array_filter(array_map('trim', $fields)), 'newemail' => trim($newEmail), 'newsmsnumber' => trim($newSmsNumber), - ] + ], ] ) ); } /** - * @throws BaseException|GuzzleException + * @throws BaseException */ public function delete(string $subscriberId): string { return current( $this->connection->requestAndParse( 'DELETE', - 'subscriber/'.urlencode(trim($subscriberId)) + 'subscriber/'.urlencode(trim($subscriberId)).'.json' ) ); } /** - * @throws BaseException|GuzzleException + * @throws BaseException */ public function signin(string $apikey, string $emailAddress, array $fields, string $smsNumber): string { return current( $this->connection->requestAndParse( 'POST', - 'subscriber/signin', + 'subscriber/signin.json', [ RequestOptions::FORM_PARAMS => [ 'apikey' => trim($apikey), @@ -207,45 +224,45 @@ class Subscriber extends Model //ToDo: apply trim to array 'fields' => $fields, 'smsnumber' => trim($smsNumber), - ] + ], ] ) ); } /** - * @throws BaseException|GuzzleException + * @throws BaseException */ public function signout(string $apikey, string $emailAddress): string { return current( $this->connection->requestAndParse( 'POST', - 'subscriber/signout', + 'subscriber/signout.json', [ RequestOptions::FORM_PARAMS => [ 'apikey' => trim($apikey), 'email' => trim($emailAddress), - ] + ], ] ) ); } /** - * @throws BaseException|GuzzleException + * @throws BaseException */ public function signoff(string $apikey, string $emailAddress): string { return current( $this->connection->requestAndParse( 'POST', - 'subscriber/signoff', + 'subscriber/signoff.json', [ RequestOptions::FORM_PARAMS => [ 'apikey' => trim($apikey), 'email' => trim($emailAddress), - ] + ], ] ) );