-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
126 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |