This commit is contained in:
Dr Masroor Ehsan 2025-01-23 10:30:02 +06:00
parent b3ceda84b8
commit 11d99ee746

View File

@ -4,11 +4,27 @@
use App\Models\DicomServer; use App\Models\DicomServer;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use Psr\Http\Message\ResponseInterface;
final class OrthancRestClient final class OrthancRestClient
{ {
public function __construct(private readonly DicomServer $orthancHost) {} public function __construct(private readonly DicomServer $orthancHost) {}
private static function studyPath(string $study_id, ?string $sub = null): string
{
$parts = ['studies', ltrim($study_id, '/')];
if (filled($sub)) {
$parts[] = ltrim($sub, '/');
}
return implode('/', $parts);
}
private static function jsonify(ResponseInterface $response, bool $associative = true): mixed
{
return json_decode($response->getBody()->getContents(), $associative, 512, JSON_THROW_ON_ERROR);
}
public function getClient(): Client public function getClient(): Client
{ {
$endpoint = $this->orthancHost->rest_api_endpoint; $endpoint = $this->orthancHost->rest_api_endpoint;
@ -23,38 +39,35 @@ public function getStudyStatistics(string $study_id): array
{ {
$response = $this->getClient()->get('/studies/' . $study_id . '/statistics'); $response = $this->getClient()->get('/studies/' . $study_id . '/statistics');
return json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR); return self::jsonify($response);
} }
public function getServerStatistics(): array public function getServerStatistics(): array
{ {
$response = $this->getClient()->get('/statistics'); $response = $this->getClient()->get('/statistics');
return json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR); return self::jsonify($response);
}
private static function studyPath(string $study_id, ?string $sub = null): string
{
$parts = ['studies', ltrim($study_id, '/')];
if (filled($sub)) {
$parts[] = ltrim($sub, '/');
}
return implode('/', $parts);
} }
public function getStudySeries(string $study_id): array public function getStudySeries(string $study_id): array
{ {
$response = $this->getClient()->get(self::studyPath($study_id, 'series?expand')); $response = $this->getClient()->get(self::studyPath($study_id, 'series?expand'));
return json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR); return self::jsonify($response);
} }
public function getStudyInstances(string $study_id): array public function getStudyInstances(string $study_id): array
{ {
$response = $this->getClient()->get(self::studyPath($study_id, 'instances?expand')); $response = $this->getClient()->get(self::studyPath($study_id, 'instances?expand'));
return json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR); return self::jsonify($response);
}
public function getStudyInstancesList(string $study_id): array
{
$response = $this->getClient()->get(self::studyPath($study_id, 'instances?expand=0'));
return self::jsonify($response);
} }
public function getStudyDetails(string $study_id): array public function getStudyDetails(string $study_id): array
@ -64,7 +77,7 @@ public function getStudyDetails(string $study_id): array
]; ];
$response = $this->getClient()->get('/studies/' . $study_id . '?' . http_build_query($query)); $response = $this->getClient()->get('/studies/' . $study_id . '?' . http_build_query($query));
return json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR); return self::jsonify($response);
} }
public function getStudies(): array public function getStudies(): array
@ -76,7 +89,7 @@ public function getStudies(): array
$url = '/studies?' . http_build_query($query); $url = '/studies?' . http_build_query($query);
$response = $this->getClient()->get($url); $response = $this->getClient()->get($url);
return json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR); return self::jsonify($response);
} }
public function getStudiesIds(): array public function getStudiesIds(): array