From 29c7d1f40c20d272f475dd6b5bb768b5b51481a3 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Thu, 13 Mar 2025 16:25:39 +0100 Subject: [PATCH] Implement basic API --- src/SlotDbClient.php | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/SlotDbClient.php b/src/SlotDbClient.php index 100eb87..3b87414 100644 --- a/src/SlotDbClient.php +++ b/src/SlotDbClient.php @@ -2,14 +2,50 @@ namespace SlotDb\Client; +use GuzzleHttp\Psr7\Request; use Psr\Http\Client\ClientInterface; +use Psr\Http\Message\ResponseInterface; class SlotDbClient { + private string $server = "http://127.0.0.1:8080"; + public function __construct( private readonly ClientInterface $client ) { } -} \ No newline at end of file + + 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; + } + +}