Skip to content

Commit

Permalink
Merge pull request #119 from fleetbase/dev-v1.5.16
Browse files Browse the repository at this point in the history
v1.5.16 - fix logging of api request and webhook sending
  • Loading branch information
roncodes authored Oct 17, 2024
2 parents 8db60ca + 50facba commit 320522a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/Jobs/LogApiRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 12 additions & 6 deletions src/Listeners/SendResourceLifecycleWebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 320522a

Please sign in to comment.