Skip to content

Commit

Permalink
Add missed possibility to proxy responses as is for elastic
Browse files Browse the repository at this point in the history
  • Loading branch information
donhardman committed Dec 10, 2024
1 parent 07d75a9 commit ba2762e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
36 changes: 26 additions & 10 deletions src/ManticoreSearch/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class Response {
*/
protected Struct $result;

/** @var bool */
protected bool $isRaw = false;

/**
* @var array<string,mixed> $columns
*/
Expand Down Expand Up @@ -242,27 +245,40 @@ protected function parse(): void {
$struct = Struct::fromData($data, $struct->getBigIntFields());
}

$this->assign($struct, 'error');
$this->assign($struct, 'warning');
$this->assign($struct, 'total');
$this->assign($struct, 'data');
$this->assign($struct, 'columns');
$this->assign($struct, 'error')
->assign($struct, 'warning')
->assign($struct, 'total')
->assign($struct, 'data')
->assign($struct, 'columns');

// A bit tricky but we need to know if we have data or not
// For table formatter in current architecture
$this->hasData = $struct->hasKey('data');

// Check if this is type of response that is not our scheme
// in this case we just may proxy it as is without any extra
$this->isRaw = !$struct->hasKey('warning') &&
!$struct->hasKey('error') &&
!$struct->hasKey('total');
}

/**
* @return bool
*/
public function isRaw(): bool {
return $this->isRaw;
}

/**
* @param Struct<int|string, mixed> $struct
* @param string $key
* @return void
* @return static
*/
public function assign(Struct $struct, string $key): void {
if (!$struct->hasKey($key)) {
return;
public function assign(Struct $struct, string $key): static {
if ($struct->hasKey($key)) {
$this->$key = $struct[$key];
}
$this->$key = $struct[$key];
return $this;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Network/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
use Manticoresearch\Buddy\Core\ManticoreSearch\Endpoint;
use Manticoresearch\Buddy\Core\ManticoreSearch\MySQLTool;
use Manticoresearch\Buddy\Core\ManticoreSearch\RequestFormat;
use Manticoresearch\Buddy\Core\Tool\Buddy;
use Manticoresearch\Buddy\Core\ManticoreSearch\Settings;
use Manticoresearch\Buddy\Core\Tool\Buddy;

final class Request {
const PAYLOAD_FIELDS = [
Expand Down
4 changes: 4 additions & 0 deletions src/Task/TaskResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public static function raw(mixed $raw): static {
* @return static
*/
public static function fromResponse(Response $response): static {
if ($response->isRaw()) {
return static::raw($response->getBody());
}

if ($response->hasError()) {
return new static(null, $response->getError() ?? '', $response->getWarning() ?? '');
}
Expand Down
1 change: 0 additions & 1 deletion test/BuddyCore/Network/ManticoreSearch/ClientTest.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
program; if you did not, you can find it at http://www.gnu.org/
*/

//use Manticoresearch\Buddy\Core\Error\ManticoreSearchClientError;
use Manticoresearch\Buddy\Core\ManticoreSearch\Client as HTTPClient;
use Manticoresearch\Buddy\CoreTest\Trait\TestInEnvironmentTrait;
use Manticoresearch\Buddy\CoreTest\Trait\TestProtectedTrait;
Expand Down

0 comments on commit ba2762e

Please sign in to comment.