Files
slotdb-client-php/src/SlotDbClient.php

52 lines
1.5 KiB
PHP
Raw Normal View History

2025-03-10 16:43:56 +01:00
<?php
namespace SlotDb\Client;
2025-03-13 16:25:39 +01:00
use GuzzleHttp\Psr7\Request;
2025-03-10 16:43:56 +01:00
use Psr\Http\Client\ClientInterface;
2025-03-13 16:25:39 +01:00
use Psr\Http\Message\ResponseInterface;
2025-03-10 16:43:56 +01:00
class SlotDbClient
{
2025-03-13 16:25:39 +01:00
private string $server = "http://127.0.0.1:8080";
2025-03-10 16:43:56 +01:00
public function __construct(
private readonly ClientInterface $client
)
{
}
2025-03-13 16:25:39 +01:00
private function get(string $path, array $query = [], array $headers = []): ResponseInterface
{
$queryStr = \http_build_query($query);
$fullPath = $this->server . '/api/slotdb/v1/' . ltrim($path, '/') . '?' . $queryStr;
$request = new Request('GET', $fullPath, $headers);
return $this->client->sendRequest($request);
}
private function post(string $path, array $data, array $query = [], array $headers = []): ResponseInterface
{
$queryStr = \http_build_query($query);
$fullPath = $this->server . '/api/slotdb/v1/' . ltrim($path, '/') . '?' . $queryStr;
$headers['content-type'] ??= 'application/json';
$request = new Request('POST', $fullPath, $headers, json_encode($data));
return $this->client->sendRequest($request);
}
public function querySlot(string $slotId): array
{
$res = $this->get("/slot/{$slotId}");
$data = json_decode($res->getBody(), true);
return $data;
}
public function updateSlot(string $slotId, array $props): array
{
$res = $this->post("/slot/{$slotId}", data: $props);
$data = json_decode($res->getBody(), true);
return $data;
}
}