Spaces:
Running
Running
File size: 4,473 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | <?php
namespace App\Services;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\UploadedFile;
class ChemistryApiService
{
protected string $baseUrl;
protected int $timeout;
public function __construct()
{
$this->baseUrl = rtrim((string) config('services.chemistry.base_url') ?: '', '/');
$this->timeout = (int) config('services.chemistry.timeout', 30);
}
// ==================== Health ====================
public function healthCheck(): array
{
return $this->request('get', '/health');
}
// ==================== Threads ====================
public function createThread(): array
{
return $this->request('post', '/thread/new');
}
// ==================== Chat ====================
public function chat(string $message, ?string $threadId = null): array
{
return $this->request('post', '/chat', [
'message' => $message,
'thread_id' => $threadId,
]);
}
// ==================== Analyze SMILES ====================
public function analyzeSmiles(string $smiles, ?string $threadId = null): array
{
$query = ['smiles' => $smiles];
if ($threadId) {
$query['thread_id'] = $threadId;
}
$queryString = http_build_query($query);
$response = Http::timeout($this->timeout)
->withHeaders(['Content-Type' => 'application/json'])
->post("{$this->baseUrl}/analyze/smiles?{$queryString}");
return $this->handleResponse($response);
}
// ==================== Compare ====================
public function compareMolecules(array $smilesList, ?string $threadId = null): array
{
return $this->request('post', '/analyze/compare', [
'message' => implode(', ', $smilesList),
'thread_id' => $threadId,
]);
}
// ==================== Docking ====================
public function analyzeDocking(string $dockingData, ?string $threadId = null): array
{
return $this->request('post', '/analyze/docking', [
'docking_data' => $dockingData,
'thread_id' => $threadId,
]);
}
// ==================== CSV ====================
public function uploadCsv(UploadedFile $file, string $analysisType = 'full'): array
{
return $this->request('post', "/csv/upload?analysis_type={$analysisType}", [], [
'file' => $file,
]);
}
public function getCsvStatus(string $jobId): array
{
return $this->request('get', "/csv/status/{$jobId}");
}
public function getCsvResults(string $jobId): string|array
{
$response = Http::timeout($this->timeout)
->get("{$this->baseUrl}/csv/results/{$jobId}");
if ($response->successful()) {
return $response->body();
}
return $this->handleResponse($response);
}
public function listCsvJobs(): array
{
return $this->request('get', '/csv/jobs');
}
public function deleteCsvJob(string $jobId): array
{
return $this->request('delete', "/csv/jobs/{$jobId}");
}
// ==================== Helper Methods ====================
protected function request(string $method, string $endpoint, array $json = [], array $multipart = []): array
{
$url = "{$this->baseUrl}{$endpoint}";
$http = Http::timeout($this->timeout);
if (!empty($multipart)) {
foreach ($multipart as $key => $value) {
$http = $http->attach($key, file_get_contents($value->getRealPath()), $value->getClientOriginalName());
}
$response = $http->{$method}($url);
} else {
$response = $http->withHeaders([
'Content-Type' => 'application/json',
])->{$method}($url, $json);
}
return $this->handleResponse($response);
}
protected function handleResponse($response): array
{
if ($response->successful()) {
return [
'success' => true,
'data' => $response->json() ?? $response->body(),
'status' => $response->status(),
];
}
return [
'success' => false,
'error' => $response->json('detail') ?? $response->body(),
'status' => $response->status(),
];
}
}
|