Skip to content

Commit

Permalink
feat: integrate with WebHooks API
Browse files Browse the repository at this point in the history
  • Loading branch information
faustoq committed Jul 7, 2024
1 parent 0cf5dd0 commit fc36745
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 5 deletions.
52 changes: 52 additions & 0 deletions src/Manager/CloudManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace OramaCloud\Manager;

use GuzzleHttp\Client as HttpClient;
use OramaCloud\Manager\IndexManager;

class CloudManager
{
private $indexId;
private $apiKey;
private $http;

public function __construct($apiKey)
{
$this->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);
}
}
10 changes: 5 additions & 5 deletions src/Manager/Endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
69 changes: 69 additions & 0 deletions src/Manager/IndexManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace OramaCloud\Manager;

use OramaCloud\Manager\Endpoints;

class IndexManager
{
private $manager;
private $indexId = null;

public function __construct($indexID, CloudManager $manager)
{
$this->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);
}
}

0 comments on commit fc36745

Please sign in to comment.