From 50facbabcc12f31b2905ff1862b92ec1abe42f39 Mon Sep 17 00:00:00 2001 From: "Ronald A. Richardson" Date: Thu, 17 Oct 2024 19:31:20 +0800 Subject: [PATCH] fix logging of api request and webhook sending --- composer.json | 2 +- src/Jobs/LogApiRequest.php | 4 ++-- src/Listeners/SendResourceLifecycleWebhook.php | 18 ++++++++++++------ 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/composer.json b/composer.json index 65e7ca2..6077ba8 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "fleetbase/core-api", - "version": "1.5.15", + "version": "1.5.16", "description": "Core Framework and Resources for Fleetbase API", "keywords": [ "fleetbase", diff --git a/src/Jobs/LogApiRequest.php b/src/Jobs/LogApiRequest.php index 1e14fdb..3e629b4 100644 --- a/src/Jobs/LogApiRequest.php +++ b/src/Jobs/LogApiRequest.php @@ -91,8 +91,8 @@ public static function getPayload(Request $request, $response): array } // Check if it was a personal access token which made the request - if ($apiCredential && PersonalAccessToken::where('id', $apiCredential)->exists()) { - $payload['access_token_id'] = $apiCredential; + if ($apiCredential && is_numeric($apiCredential) && PersonalAccessToken::where('id', $apiCredential)->exists()) { + $payload['access_token_id'] = (int) $apiCredential; } // Get request duration diff --git a/src/Listeners/SendResourceLifecycleWebhook.php b/src/Listeners/SendResourceLifecycleWebhook.php index f9e4e8d..3e9ef1f 100644 --- a/src/Listeners/SendResourceLifecycleWebhook.php +++ b/src/Listeners/SendResourceLifecycleWebhook.php @@ -13,6 +13,7 @@ use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Database\QueryException; use Illuminate\Support\Carbon; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Str; use Laravel\Sanctum\PersonalAccessToken; @@ -47,20 +48,25 @@ public function handle($event) 'description' => $this->getHumanReadableEventDescription($event), ]; + // Get api credential from session + $apiCredential = session('api_credential'); + // Validate api credential, if not uuid then it could be internal - if (session('api_credential') && ApiCredential::where('uuid', session('api_credential'))->exists()) { - $eventData['api_credential_uuid'] = session('api_credential'); + if ($apiCredential && Str::isUuid($apiCredential) && ApiCredential::where('uuid', session('api_credential'))->exists()) { + $eventData['api_credential_uuid'] = $apiCredential; } // Check if it was a personal access token which made the request - if (session('api_credential') && PersonalAccessToken::where('id', session('api_credential'))->exists()) { - $eventData['access_token_id'] = session('api_credential'); + if ($apiCredential && is_numeric($apiCredential) && PersonalAccessToken::where('id', $apiCredential)->exists()) { + $eventData['access_token_id'] = (int) $apiCredential; } try { // log the api event $apiEvent = ApiEvent::create($eventData); - } catch (QueryException $e) { + } catch (\Exception|QueryException $e) { + Log::error($e->getMessage()); + return; } @@ -110,7 +116,7 @@ public function handle($event) 'api_event_uuid' => $apiEvent->uuid, 'method' => $request->getMethod(), 'status_code' => $exception->getStatusCode(), - 'reason_phrase' => $response->getReasonPhrase() ?? $exception->getMessage(), + 'reason_phrase' => $exception->getMessage() ?? $response->getReasonPhrase(), 'duration' => $durationStart->diffInSeconds(now()), 'url' => $request->getUri(), 'attempt' => 1,