From a5c78924ad4faf2f35da8175a48af104351ff98f Mon Sep 17 00:00:00 2001 From: Bas van Dinther Date: Fri, 25 Aug 2023 16:23:06 +0200 Subject: [PATCH 1/7] listAll method to list all resources --- src/Resources/Resource.php | 51 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/Resources/Resource.php b/src/Resources/Resource.php index 13a8f00..9212d61 100644 --- a/src/Resources/Resource.php +++ b/src/Resources/Resource.php @@ -40,6 +40,57 @@ public function list(array $params = []): array ); } + /** + * @param int $offset + * @param int $perPage + * @return array|MethodNotAvailableException|ClientException|ServerException|BadResponseException|JsonException + * + * @throws ClientException|ServerException|BadResponseException|JsonException + */ + public function listAll(int $offset = 0, int $perPage = 1000) + { + // Rate limit the requests to prevent IP blocking. + $limitPerSecond = 300 / 60; // Per minute / seconds + $calls = 1; + + $data = []; + + // TODO: This is a hacky way to get the plural resource name. + $pluralResourceName = $this->getResourceName() . 's'; + + $result = $this->sendRequest( + controller: $this->getResourceName(), + action: Action::LIST->value, + params: [ + 'limit' => $perPage, + 'offset' => $offset, + ] + ); + + foreach($result[$pluralResourceName] as $index => $item) { + $calls++; + + if ($calls % $limitPerSecond == 0) { + sleep(1); + } + + $resultItem = $this->show([ + 'Identifier' => $item['Identifier'], + ]); + + $result[$pluralResourceName][$index] = $resultItem[$pluralResourceName]; + } + + $data = array_merge($data, $result[$pluralResourceName] ?? []); + + if ($result['currentresults'] >= $perPage) { + $offset += $perPage; + $data = array_merge($data, $this->listAll($offset, $perPage)); + } + + return $data; + } + /** * @param array $params * @return array|MethodNotAvailableException|ClientException|ServerException|BadResponseException|JsonException From 9feb7eece6b2cde1ac4f59a3bf669a6fa462085d Mon Sep 17 00:00:00 2001 From: Baspa Date: Fri, 25 Aug 2023 14:23:40 +0000 Subject: [PATCH 2/7] Fix styling --- src/Resources/Resource.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Resources/Resource.php b/src/Resources/Resource.php index 9212d61..644a69a 100644 --- a/src/Resources/Resource.php +++ b/src/Resources/Resource.php @@ -41,8 +41,6 @@ public function list(array $params = []): array } /** - * @param int $offset - * @param int $perPage * @return array|MethodNotAvailableException|ClientException|ServerException|BadResponseException|JsonException * * @throws ClientException|ServerException|BadResponseException|JsonException @@ -56,7 +54,7 @@ public function listAll(int $offset = 0, int $perPage = 1000) $data = []; // TODO: This is a hacky way to get the plural resource name. - $pluralResourceName = $this->getResourceName() . 's'; + $pluralResourceName = $this->getResourceName().'s'; $result = $this->sendRequest( controller: $this->getResourceName(), @@ -67,7 +65,7 @@ public function listAll(int $offset = 0, int $perPage = 1000) ] ); - foreach($result[$pluralResourceName] as $index => $item) { + foreach ($result[$pluralResourceName] as $index => $item) { $calls++; if ($calls % $limitPerSecond == 0) { From 065d74735f14493020c4e3f81abeed327049ff13 Mon Sep 17 00:00:00 2001 From: Bas van Dinther Date: Wed, 30 Aug 2023 14:23:35 +0200 Subject: [PATCH 3/7] Get plural resource name method --- src/Resources/CreditInvoice.php | 5 +++++ src/Resources/Creditor.php | 5 +++++ src/Resources/Debtor.php | 5 +++++ src/Resources/Group.php | 5 +++++ src/Resources/Invoice.php | 5 +++++ src/Resources/Product.php | 5 +++++ src/Resources/Resource.php | 5 +++-- src/Resources/Settings/CostCategory.php | 7 ++++++- src/Resources/Settings/Settings.php | 5 +++++ src/Resources/Subscription.php | 5 +++++ 10 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/Resources/CreditInvoice.php b/src/Resources/CreditInvoice.php index c0b0adf..82dc384 100644 --- a/src/Resources/CreditInvoice.php +++ b/src/Resources/CreditInvoice.php @@ -20,4 +20,9 @@ public function getResourceName(): string { return self::CONTROLLER_NAME; } + + public function getPluralResourceName(): string + { + return self::CONTROLLER_NAME . 's'; + } } diff --git a/src/Resources/Creditor.php b/src/Resources/Creditor.php index 3f3064f..62154b4 100644 --- a/src/Resources/Creditor.php +++ b/src/Resources/Creditor.php @@ -20,4 +20,9 @@ public function getResourceName(): string { return self::CONTROLLER_NAME; } + + public function getPluralResourceName(): string + { + return self::CONTROLLER_NAME . 's'; + } } diff --git a/src/Resources/Debtor.php b/src/Resources/Debtor.php index 272e5a2..4512ba3 100644 --- a/src/Resources/Debtor.php +++ b/src/Resources/Debtor.php @@ -13,6 +13,11 @@ public function getResourceName(): string return self::CONTROLLER_NAME; } + public function getPluralResourceName(): string + { + return self::CONTROLLER_NAME . 's'; + } + /** * @return array */ diff --git a/src/Resources/Group.php b/src/Resources/Group.php index ea57b4d..21b933d 100644 --- a/src/Resources/Group.php +++ b/src/Resources/Group.php @@ -10,4 +10,9 @@ public function getResourceName(): string { return self::CONTROLLER_NAME; } + + public function getPluralResourceName(): string + { + return self::CONTROLLER_NAME . 's'; + } } diff --git a/src/Resources/Invoice.php b/src/Resources/Invoice.php index 20a7a12..4762868 100644 --- a/src/Resources/Invoice.php +++ b/src/Resources/Invoice.php @@ -26,6 +26,11 @@ public function getResourceName(): string return self::CONTROLLER_NAME; } + public function getPluralResourceName(): string + { + return self::CONTROLLER_NAME . 's'; + } + /** * @param array $params * @return array diff --git a/src/Resources/Product.php b/src/Resources/Product.php index 46ea99c..1ca8647 100644 --- a/src/Resources/Product.php +++ b/src/Resources/Product.php @@ -10,4 +10,9 @@ public function getResourceName(): string { return self::CONTROLLER_NAME; } + + public function getPluralResourceName(): string + { + return self::CONTROLLER_NAME . 's'; + } } diff --git a/src/Resources/Resource.php b/src/Resources/Resource.php index 9212d61..4ec6156 100644 --- a/src/Resources/Resource.php +++ b/src/Resources/Resource.php @@ -55,8 +55,7 @@ public function listAll(int $offset = 0, int $perPage = 1000) $data = []; - // TODO: This is a hacky way to get the plural resource name. - $pluralResourceName = $this->getResourceName() . 's'; + $pluralResourceName = $this->getPluralResourceName(); $result = $this->sendRequest( controller: $this->getResourceName(), @@ -160,4 +159,6 @@ public function delete(array $params = []): array|MethodNotAvailableException|Cl } abstract public function getResourceName(): string; + + abstract public function getPluralResourceName(): string; } diff --git a/src/Resources/Settings/CostCategory.php b/src/Resources/Settings/CostCategory.php index 3fafb51..56fa4a5 100644 --- a/src/Resources/Settings/CostCategory.php +++ b/src/Resources/Settings/CostCategory.php @@ -15,13 +15,18 @@ class CostCategory extends Resource { use Request; - final public const CONTROLLER_NAME = 'settings'; + final public const CONTROLLER_NAME = 'costcategory'; public function getResourceName(): string { return self::CONTROLLER_NAME; } + public function getPluralResourceName(): string + { + return 'costcategories'; + } + /** * @param array $params * @return array diff --git a/src/Resources/Settings/Settings.php b/src/Resources/Settings/Settings.php index caa8dca..54245a8 100644 --- a/src/Resources/Settings/Settings.php +++ b/src/Resources/Settings/Settings.php @@ -18,6 +18,11 @@ public function getResourceName(): string return self::CONTROLLER_NAME; } + public function getPluralResourceName(): string + { + return self::CONTROLLER_NAME; + } + /** * @return array */ diff --git a/src/Resources/Subscription.php b/src/Resources/Subscription.php index 1d0da8c..99527ce 100644 --- a/src/Resources/Subscription.php +++ b/src/Resources/Subscription.php @@ -18,6 +18,11 @@ public function getResourceName(): string return self::CONTROLLER_NAME; } + public function getPluralResourceName(): string + { + return self::CONTROLLER_NAME . 's'; + } + /** * @return array * From 0e19c04a51e80085b443a78a91743e186e86e092 Mon Sep 17 00:00:00 2001 From: Baspa Date: Wed, 30 Aug 2023 12:24:24 +0000 Subject: [PATCH 4/7] Fix styling --- src/Resources/CreditInvoice.php | 2 +- src/Resources/Creditor.php | 2 +- src/Resources/Debtor.php | 2 +- src/Resources/Group.php | 2 +- src/Resources/Invoice.php | 2 +- src/Resources/Product.php | 2 +- src/Resources/Settings/CostCategory.php | 2 +- src/Resources/Subscription.php | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Resources/CreditInvoice.php b/src/Resources/CreditInvoice.php index 82dc384..0bc104c 100644 --- a/src/Resources/CreditInvoice.php +++ b/src/Resources/CreditInvoice.php @@ -23,6 +23,6 @@ public function getResourceName(): string public function getPluralResourceName(): string { - return self::CONTROLLER_NAME . 's'; + return self::CONTROLLER_NAME.'s'; } } diff --git a/src/Resources/Creditor.php b/src/Resources/Creditor.php index 62154b4..077476e 100644 --- a/src/Resources/Creditor.php +++ b/src/Resources/Creditor.php @@ -23,6 +23,6 @@ public function getResourceName(): string public function getPluralResourceName(): string { - return self::CONTROLLER_NAME . 's'; + return self::CONTROLLER_NAME.'s'; } } diff --git a/src/Resources/Debtor.php b/src/Resources/Debtor.php index 4512ba3..d7482eb 100644 --- a/src/Resources/Debtor.php +++ b/src/Resources/Debtor.php @@ -15,7 +15,7 @@ public function getResourceName(): string public function getPluralResourceName(): string { - return self::CONTROLLER_NAME . 's'; + return self::CONTROLLER_NAME.'s'; } /** diff --git a/src/Resources/Group.php b/src/Resources/Group.php index 21b933d..80461bb 100644 --- a/src/Resources/Group.php +++ b/src/Resources/Group.php @@ -13,6 +13,6 @@ public function getResourceName(): string public function getPluralResourceName(): string { - return self::CONTROLLER_NAME . 's'; + return self::CONTROLLER_NAME.'s'; } } diff --git a/src/Resources/Invoice.php b/src/Resources/Invoice.php index 4762868..89f743d 100644 --- a/src/Resources/Invoice.php +++ b/src/Resources/Invoice.php @@ -28,7 +28,7 @@ public function getResourceName(): string public function getPluralResourceName(): string { - return self::CONTROLLER_NAME . 's'; + return self::CONTROLLER_NAME.'s'; } /** diff --git a/src/Resources/Product.php b/src/Resources/Product.php index 1ca8647..36c00f8 100644 --- a/src/Resources/Product.php +++ b/src/Resources/Product.php @@ -13,6 +13,6 @@ public function getResourceName(): string public function getPluralResourceName(): string { - return self::CONTROLLER_NAME . 's'; + return self::CONTROLLER_NAME.'s'; } } diff --git a/src/Resources/Settings/CostCategory.php b/src/Resources/Settings/CostCategory.php index 56fa4a5..c76db16 100644 --- a/src/Resources/Settings/CostCategory.php +++ b/src/Resources/Settings/CostCategory.php @@ -26,7 +26,7 @@ public function getPluralResourceName(): string { return 'costcategories'; } - + /** * @param array $params * @return array diff --git a/src/Resources/Subscription.php b/src/Resources/Subscription.php index 99527ce..29d62a5 100644 --- a/src/Resources/Subscription.php +++ b/src/Resources/Subscription.php @@ -20,7 +20,7 @@ public function getResourceName(): string public function getPluralResourceName(): string { - return self::CONTROLLER_NAME . 's'; + return self::CONTROLLER_NAME.'s'; } /** From 569c881c5e16050028dd305df29823f04eb3ae19 Mon Sep 17 00:00:00 2001 From: Bas van Dinther Date: Wed, 30 Aug 2023 14:32:56 +0200 Subject: [PATCH 5/7] List all methods --- src/Resources/Resource.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Resources/Resource.php b/src/Resources/Resource.php index dea5f56..ff942e9 100644 --- a/src/Resources/Resource.php +++ b/src/Resources/Resource.php @@ -75,7 +75,7 @@ public function listAll(int $offset = 0, int $perPage = 1000) 'Identifier' => $item['Identifier'], ]); - $result[$pluralResourceName][$index] = $resultItem[$pluralResourceName]; + $result[$pluralResourceName][$index] = $resultItem[$this->getResourceName()]; } $data = array_merge($data, $result[$pluralResourceName] ?? []); From 17a796e4bdb36fb8b7a5de5e47d089cbcfb6f199 Mon Sep 17 00:00:00 2001 From: Bas van Dinther Date: Wed, 30 Aug 2023 14:44:07 +0200 Subject: [PATCH 6/7] Error handling --- src/Resources/Resource.php | 53 +++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/src/Resources/Resource.php b/src/Resources/Resource.php index ff942e9..f52caea 100644 --- a/src/Resources/Resource.php +++ b/src/Resources/Resource.php @@ -2,14 +2,15 @@ namespace Vormkracht10\WeFact\Resources; +use Exception; +use JsonException; use GuzzleHttp\Client; -use GuzzleHttp\Exception\BadResponseException; +use Vormkracht10\WeFact\Enums\Action; +use Vormkracht10\WeFact\Traits\Request; use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Exception\ServerException; -use JsonException; -use Vormkracht10\WeFact\Enums\Action; +use GuzzleHttp\Exception\BadResponseException; use Vormkracht10\WeFact\Exceptions\MethodNotAvailableException; -use Vormkracht10\WeFact\Traits\Request; abstract class Resource { @@ -41,11 +42,11 @@ public function list(array $params = []): array } /** - * @return array|MethodNotAvailableException|ClientException|ServerException|BadResponseException|JsonException - * - * @throws ClientException|ServerException|BadResponseException|JsonException - */ - public function listAll(int $offset = 0, int $perPage = 1000) + * @return array + * + * @throws ClientException|ServerException|BadResponseException|JsonException + */ + public function listAll(int $offset = 0, int $perPage = 1000): array { // Rate limit the requests to prevent IP blocking. $limitPerSecond = 300 / 60; // Per minute / seconds @@ -55,14 +56,15 @@ public function listAll(int $offset = 0, int $perPage = 1000) $pluralResourceName = $this->getPluralResourceName(); - $result = $this->sendRequest( - controller: $this->getResourceName(), - action: Action::LIST->value, - params: [ - 'limit' => $perPage, - 'offset' => $offset, - ] - ); + try { + $result = $this->list(params: [ + 'limit' => $perPage, + 'offset' => $offset, + ] + ); + } catch (Exception $e) { + throw $e; + } foreach ($result[$pluralResourceName] as $index => $item) { $calls++; @@ -71,11 +73,20 @@ public function listAll(int $offset = 0, int $perPage = 1000) sleep(1); } - $resultItem = $this->show([ - 'Identifier' => $item['Identifier'], - ]); + try { + $resultItem = $this->show([ + 'Identifier' => $item['Identifier'], + ]); + } catch (Exception $e) { + throw $e; + } - $result[$pluralResourceName][$index] = $resultItem[$this->getResourceName()]; + if (is_array($resultItem) && isset($resultItem[$this->getResourceName()])) { + $result[$pluralResourceName][$index] = $resultItem[$this->getResourceName()]; + } else { + // Handle the case where $resultItem is not an array or does not contain the expected resource name. + continue; + } } $data = array_merge($data, $result[$pluralResourceName] ?? []); From 7ac897fea3834a861eb2f0fca624b7741425db7f Mon Sep 17 00:00:00 2001 From: Baspa Date: Wed, 30 Aug 2023 12:44:44 +0000 Subject: [PATCH 7/7] Fix styling --- src/Resources/Resource.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Resources/Resource.php b/src/Resources/Resource.php index f52caea..e19a93c 100644 --- a/src/Resources/Resource.php +++ b/src/Resources/Resource.php @@ -3,14 +3,14 @@ namespace Vormkracht10\WeFact\Resources; use Exception; -use JsonException; use GuzzleHttp\Client; -use Vormkracht10\WeFact\Enums\Action; -use Vormkracht10\WeFact\Traits\Request; +use GuzzleHttp\Exception\BadResponseException; use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Exception\ServerException; -use GuzzleHttp\Exception\BadResponseException; +use JsonException; +use Vormkracht10\WeFact\Enums\Action; use Vormkracht10\WeFact\Exceptions\MethodNotAvailableException; +use Vormkracht10\WeFact\Traits\Request; abstract class Resource { @@ -42,10 +42,10 @@ public function list(array $params = []): array } /** - * @return array - * - * @throws ClientException|ServerException|BadResponseException|JsonException - */ + * @return array + * + * @throws ClientException|ServerException|BadResponseException|JsonException + */ public function listAll(int $offset = 0, int $perPage = 1000): array { // Rate limit the requests to prevent IP blocking. @@ -58,9 +58,9 @@ public function listAll(int $offset = 0, int $perPage = 1000): array try { $result = $this->list(params: [ - 'limit' => $perPage, - 'offset' => $offset, - ] + 'limit' => $perPage, + 'offset' => $offset, + ] ); } catch (Exception $e) { throw $e;