66 lines
2.0 KiB
PHP
66 lines
2.0 KiB
PHP
<?php
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class IceServerController extends Controller
|
|
{
|
|
public function __invoke(): JsonResponse
|
|
{
|
|
if (!Auth::check() && !session()->has('candidate_interview_id')) {
|
|
return response()->json(['error' => 'Unauthenticated access.'], 401);
|
|
}
|
|
|
|
try {
|
|
$baseUrl = config(
|
|
"services.metered.url",
|
|
);
|
|
$apiKey = config(
|
|
"services.metered.key",
|
|
);
|
|
|
|
$url = $baseUrl;
|
|
if ($apiKey && !str_contains($url, "apiKey=")) {
|
|
$separator = str_contains($url, "?") ? "&" : "?";
|
|
$url .= $separator . "apiKey=" . urlencode($apiKey);
|
|
}
|
|
|
|
$data = Cache::remember('metered_credentials_cache', now()->addMinute(10), function() use($url) {
|
|
$response = Http::timeout(5)->get($url);
|
|
if(!$response->successful())
|
|
{
|
|
Log::error(
|
|
"Metered API error response",
|
|
[
|
|
"status" => $response->status(),
|
|
"body" => $response->body(),
|
|
],
|
|
);
|
|
|
|
throw new \RuntimeException('Metered API Error');
|
|
}
|
|
|
|
return $response->json();
|
|
});
|
|
return response()->json($data);
|
|
|
|
} catch (\Throwable $e) {
|
|
Log::error(
|
|
"ICE servers fetch exception: " . $e->getMessage(),
|
|
);
|
|
|
|
return response()->json(
|
|
[
|
|
"error" => "An error occurred while fetching ICE servers.",
|
|
"message" => $e->getMessage(),
|
|
],
|
|
500,
|
|
);
|
|
}
|
|
}
|
|
}
|