radfusion/app/Services/Pacs/OrthancRestClient.php
2025-01-23 14:25:02 +06:00

117 lines
3.4 KiB
PHP

<?php
namespace App\Services\Pacs;
use App\Models\DicomServer;
use App\Services\StudyRouter\RawDicomTags;
use GuzzleHttp\Client;
use Psr\Http\Message\ResponseInterface;
final class OrthancRestClient
{
public function __construct(private readonly DicomServer $orthancHost) {}
private static function relativePath(string $study_id, ?string $child = null, array $query = [], string $parent = 'studies'): string
{
$parts = [$parent, ltrim($study_id, '/')];
if (filled($child)) {
$parts[] = ltrim($child, '/');
}
$path = implode('/', $parts);
if (! empty($query)) {
$path .= '?' . http_build_query($query);
}
return $path;
}
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
{
$endpoint = $this->orthancHost->rest_api_endpoint;
return new Client([
'base_uri' => $endpoint,
// 'base_uri' => config('pacs.api.endpoint'),
]);
}
public function getStudyStatistics(string $study_id): array
{
$response = $this->getClient()->get('/studies/' . $study_id . '/statistics');
return self::jsonify($response);
}
public function getServerStatistics(): array
{
$response = $this->getClient()->get('/statistics');
return self::jsonify($response);
}
public function getStudySeries(string $study_id): array
{
$response = $this->getClient()->get(self::relativePath($study_id, 'series', ['expand' => 1]));
return self::jsonify($response);
}
public function getStudyInstances(string $study_id): array
{
$response = $this->getClient()->get(self::relativePath($study_id, 'instances', ['expand' => 1]));
return self::jsonify($response);
}
public function getStudyInstancesList(string $study_id): array
{
$response = $this->getClient()->get(self::relativePath($study_id, 'instances', ['expand' => 0]));
return self::jsonify($response);
}
public function getStudyDetails(string $study_id): array
{
$query = [
'requested-tags' => implode(';', array_column(DicomTags::cases(), 'value')),
];
$response = $this->getClient()->get(self::relativePath($study_id, null, $query));
return self::jsonify($response);
}
public function getInstanceDetails(string $instance_id, bool $rawTags = false): array
{
$query = $rawTags ? [
'requested-tags' => implode(';', array_column(RawDicomTags::cases(), 'value')),
'short' => 1,
] : [];
$response = $this->getClient()->get(self::relativePath($instance_id, null, $query, 'instances'));
return self::jsonify($response);
}
public function getStudies(): array
{
$query = [
'expand' => '1',
'requested-tags' => implode(';', array_column(DicomTags::cases(), 'value')),
];
$url = '/studies?' . http_build_query($query);
$response = $this->getClient()->get($url);
return self::jsonify($response);
}
public function getStudiesIds(): array
{
return json_decode($this->getClient()->get('/studies')->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);
}
}