Skip to content

Commit

Permalink
fix: basic search query
Browse files Browse the repository at this point in the history
  • Loading branch information
faustoq committed Jul 2, 2024
1 parent aad5274 commit 3cc2a1a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
8 changes: 4 additions & 4 deletions src/Client.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace OramaCloud;

use GuzzleHttp\Client as HttpClient;
Expand All @@ -21,13 +22,12 @@ public function __construct($endpoint, $apiKey, $answersApiBaseURL = null) {

public function search(Query $query) {
$endpoint = "{$this->endpoint}/search?api-key={$this->apiKey}";
$options = [

$response = $this->http->request('POST', $endpoint, [
'form_params' => [
'q' => $query->toJson()
]
];

$response = $this->http->request('POST', $endpoint, $options);
]);

return json_decode($response->getBody(), true);
}
Expand Down
15 changes: 10 additions & 5 deletions src/Query.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace OramaCloud;

class Query {
Expand Down Expand Up @@ -29,11 +30,6 @@ public function setMode($mode) {
return $this;
}

public static function fromArray($array) {
extract($array);
return new Query($term, $mode);
}

public function toArray() {
return [
'term' => $this->term,
Expand All @@ -44,4 +40,13 @@ public function toArray() {
public function toJson() {
return json_encode($this->toArray());
}

public static function fromArray($array) {
$query = new Query();
$query
->setTerm(isset($array['term']) ? $array['term'] : '')
->setMode(isset($array['mode']) ? $array['mode'] : 'fulltext');

return $query;
}
}
6 changes: 3 additions & 3 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
use OramaCloud\Client;
use OramaCloud\Query;

const ENDPOINT = 'https://cloud.orama.run/v1/indexes/askorama-ai-development-uc6oxa';
const API_KEY = 'P9buEfpy8rWvT265McikCG1tP4pT6cBg';
const API_ENDPOINT = 'https://cloud.orama.run/v1/indexes/askorama-ai-development-uc6oxa';
const PUBLIC_API_KEY = 'P9buEfpy8rWvT265McikCG1tP4pT6cBg';

class ClientTest extends TestCase {

Expand All @@ -14,7 +14,7 @@ class ClientTest extends TestCase {
protected function setUp(): void {
parent::setUp();

$this->client = new Client(ENDPOINT, API_KEY);
$this->client = new Client(API_ENDPOINT, PUBLIC_API_KEY);
}

public function testBasicFullTextSearch() {
Expand Down
9 changes: 9 additions & 0 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ public function testQueryParams() {
$this->assertEquals($mode, $query->getMode());
}

public function testDefaultQueryParamsFromArray() {
$query = Query::fromArray([]);

$this->assertEquals($query->toArray(), [
'term' => '',
'mode' => 'fulltext'
]);
}

public function testQueryParamsFromArray() {
$params = [
'term' => 'mock-term',
Expand Down

0 comments on commit 3cc2a1a

Please sign in to comment.