Spaces:
Running
Running
File size: 3,120 Bytes
907b200 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | <?php
namespace App\Http\Controllers\Api;
use App\Http\Requests\Ai\ExportLigandsRequest;
use App\Models\LigandExport;
use Illuminate\Support\Facades\Http;
use Throwable;
class LigandsController extends BaseController
{
private function aiServiceUrl(): string
{
return rtrim(config('services.generation.url', 'http://generation:8000'), '/');
}
public function exportLigands(ExportLigandsRequest $request)
{
$validated = $request->validated();
$ligandExport = LigandExport::where('smiles', $validated['smiles'])
->where('file_format', $validated['format'])
->first();
if ($ligandExport) {
return $this->getLigand($ligandExport);
}
return $this->callAiService('/ligands/export', $validated);
}
private function callAiService(string $endpoint, array $validated)
{
try {
$response = Http::timeout(120)
->retry(5, 1000)
->post(
$this->aiServiceUrl() . $endpoint,
$validated
);
if ($response->failed()) {
return $this->errorResponse(
'AI service error: ' . $response->body(),
$response->status()
);
}
$aiData = $response->json();
if (!isset($aiData['job_id'], $aiData['status'], $aiData['canonical_smiles'], $aiData['file']['format'], $aiData['file']['filename'], $aiData['file']['download_url'])) {
return $this->errorResponse('Invalid AI service response', 500);
}
$ligandExport = LigandExport::firstOrCreate(
[
'smiles' => $aiData['canonical_smiles'],
'file_format' => $aiData['file']['format'],
],
[
'job_id' => $aiData['job_id'],
'status' => $aiData['status'],
'filename' => $aiData['file']['filename'],
'download_url' => $aiData['file']['download_url'],
]
);
return $this->successResponse(
'Ligands exported successfully',
$this->formatResponse($ligandExport)
);
} catch (Throwable $e) {
return $this->errorResponse(
'Failed to connect to AI service',
500
);
}
}
private function getLigand(LigandExport $ligandExport)
{
return $this->successResponse(
'Ligands exported successfully',
$this->formatResponse($ligandExport)
);
}
private function formatResponse(LigandExport $ligandExport): array
{
return [
'job_id' => $ligandExport->job_id,
'status' => $ligandExport->status,
'format' => $ligandExport->file_format,
'filename' => $ligandExport->filename,
'smiles' => $ligandExport->smiles,
'download_url' => $ligandExport->download_url,
];
}
}
|