Cache integration API reads and refresh them on a schedule
Deploy HickLab3D Dashboard / Deploy to production (push) Successful in 28s

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Hickmeister
2026-10-11 19:01:32 +01:00
co-authored by Claude Sonnet 5.5
parent 8bee8d42c7
commit 2fe4fbd91e
10 changed files with 253 additions and 24 deletions
+17 -6
View File
@@ -5,6 +5,7 @@ namespace App\Services;
use App\Models\IntegrationSetting;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
use RuntimeException;
@@ -19,7 +20,7 @@ class Parcel2Go
private ?string $profileToken = null;
public function __construct(private IntegrationSettings $settings) {}
public function __construct(private IntegrationSettings $settings, private IntegrationCache $cache) {}
public function hasScope(string $scope): bool
{
@@ -95,12 +96,13 @@ class Parcel2Go
public function fetch(string $path): array
{
$token = str_starts_with(ltrim($path, '/'), 'me/') && $this->profileConnected()
? $this->profileAccessToken()
: $this->accessToken();
$usesProfile = str_starts_with(ltrim($path, '/'), 'me/') && $this->profileConnected();
$settings = $this->settings->for('parcel2go');
$key = 'parcel2go.'.($usesProfile ? 'profile' : 'client').'.'.md5(implode('|', [$this->url($path), $settings['client_id'] ?? '', $settings['profile_client_id'] ?? '']));
return Http::withToken($token)->acceptJson()->connectTimeout(3)->timeout(15)
->get($this->url($path))->throw()->json() ?? [];
return $this->cache->remember($key, 300, fn (): array => Http::withToken($usesProfile ? $this->profileAccessToken() : $this->accessToken())
->acceptJson()->connectTimeout(3)->timeout(15)
->get($this->url($path))->throw()->json() ?? []);
}
public function quote(array $details): array
@@ -240,6 +242,14 @@ class Parcel2Go
throw new RuntimeException('Parcel2Go credentials are not configured.');
}
$tokenKey = 'parcel2go.token.'.md5(implode('|', [$settings['auth_url'], $settings['client_id'], $settings['client_secret'], $settings['scope'] ?? '']));
$cached = Cache::get($tokenKey);
if (is_array($cached) && filled($cached['token'] ?? null)) {
$this->activeScope = $cached['scope'];
return $this->token = $cached['token'];
}
$scope = (string) ($settings['scope'] ?? 'public-api');
$response = $this->tokenResponse($settings, $scope);
if ($scope !== 'public-api' && $response->status() === 400 && $response->json('error') === 'invalid_scope') {
@@ -254,6 +264,7 @@ class Parcel2Go
}
$this->activeScope = $scope;
Cache::put($tokenKey, ['token' => $token, 'scope' => $scope], max(60, (int) $response->json('expires_in', 3600) - 120));
return $this->token = $token;
}