Skip to content

Commit

Permalink
Error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Baspa committed Aug 30, 2023
1 parent 5d5a79c commit 17a796e
Showing 1 changed file with 32 additions and 21 deletions.
53 changes: 32 additions & 21 deletions src/Resources/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -41,11 +42,11 @@ public function list(array $params = []): array
}

/**
* @return array<string, mixed>|MethodNotAvailableException|ClientException|ServerException|BadResponseException|JsonException
*
* @throws ClientException|ServerException|BadResponseException|JsonException
*/
public function listAll(int $offset = 0, int $perPage = 1000)
* @return array<string, mixed>
*
* @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
Expand All @@ -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++;
Expand All @@ -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] ?? []);
Expand Down

0 comments on commit 17a796e

Please sign in to comment.