diff --git a/src/Manager/CloudManager.php b/src/Manager/CloudManager.php new file mode 100644 index 0000000..0e4017f --- /dev/null +++ b/src/Manager/CloudManager.php @@ -0,0 +1,52 @@ +apiKey = $apiKey; + $this->http = new HttpClient(); + } + + public function index(string $indexId): IndexManager + { + return new IndexManager($indexId, $this); + } + + public function setIndexId(string $id): void + { + $this->indexId = $id; + } + + public function callIndexWebhook(string $endpoint, $payload = null) + { + $config = [ + 'headers' => [ + 'Content-Type' => 'application/json', + 'Authorization' => 'Bearer ' . $this->apiKey + ] + ]; + + if ($payload) { + $config['body'] = json_encode($payload); + } + + $response = $this->http->request('POST', $this->getEndpoint($endpoint), $config); + + return json_decode($response->getBody()->getContents()); + } + + private function getEndpoint(string $endpoint): string + { + return str_replace('{indexID}', $this->indexId, $endpoint); + } +} diff --git a/src/Manager/Endpoints.php b/src/Manager/Endpoints.php index d28e421..f3deacd 100644 --- a/src/Manager/Endpoints.php +++ b/src/Manager/Endpoints.php @@ -6,15 +6,15 @@ class Endpoints { const WEBHOOKS_BASE_URL = 'https://api.askorama.ai/api/v1/webhooks'; - const DEPLOY = self::WEBHOOKS_BASE_URL . '/[indexID]/deploy'; + const DEPLOY = self::WEBHOOKS_BASE_URL . '/{indexID}/deploy'; - const EMPTY = self::WEBHOOKS_BASE_URL . '/[indexID]/empty'; + const EMPTY = self::WEBHOOKS_BASE_URL . '/{indexID}/empty'; - const HAS_DATA = self::WEBHOOKS_BASE_URL . '/[indexID]/has-data'; + const HAS_DATA = self::WEBHOOKS_BASE_URL . '/{indexID}/has-data'; - const NOTIFY = self::WEBHOOKS_BASE_URL . '/[indexID]/notify'; + const NOTIFY = self::WEBHOOKS_BASE_URL . '/{indexID}/notify'; - const SNAPSHOT = self::WEBHOOKS_BASE_URL . '/[indexID]/snapshot'; + const SNAPSHOT = self::WEBHOOKS_BASE_URL . '/{indexID}/snapshot'; const ORAMA_ANSWER_ENDPOINT = 'https://answer.api.orama.com'; } diff --git a/src/Manager/IndexManager.php b/src/Manager/IndexManager.php new file mode 100644 index 0000000..2bc49c2 --- /dev/null +++ b/src/Manager/IndexManager.php @@ -0,0 +1,69 @@ +manager = $manager; + $this->indexId = $indexID; + + $this->manager->setIndexId($indexID); + } + + public function empty() + { + return $this->callIndexWebhook(Endpoints::SNAPSHOT, []); + } + + public function snapshot($data) + { + return $this->callIndexWebhook(Endpoints::SNAPSHOT, $data); + } + + public function insert($data) + { + return $this->callIndexWebhook(Endpoints::NOTIFY, ['upsert' => $data]); + } + + public function update($data) + { + return $this->callIndexWebhook(Endpoints::NOTIFY, ['upsert' => $data]); + } + + public function delete($data) + { + return $this->callIndexWebhook(Endpoints::NOTIFY, ['remove' => $data]); + } + + public function deploy() + { + return $this->callIndexWebhook(Endpoints::DEPLOY); + } + + public function hasPendingOperations() + { + $response = $this->callIndexWebhook(Endpoints::HAS_DATA); + return $response->hasData; + } + + private function checkIndexID() + { + if (!$this->indexId) { + throw new \Exception('Index ID is not set'); + } + } + + private function callIndexWebhook($endpoint, $payload = []) + { + $this->checkIndexID(); + + return $this->manager->callIndexWebhook($endpoint, $payload); + } +}