Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions antd-php/src/AntdClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,7 @@ function (\Throwable $e) {
*/
public function health(): HealthStatus
{
$json = $this->doJson('GET', '/health');
return new HealthStatus(
ok: ($json['status'] ?? '') === 'ok',
network: $json['network'] ?? '',
);
return self::healthStatusFromJson($this->doJson('GET', '/health'));
}

/**
Expand All @@ -197,10 +193,28 @@ public function health(): HealthStatus
public function healthAsync(): PromiseInterface
{
return $this->doJsonAsync('GET', '/health')->then(
fn(?array $json) => new HealthStatus(
ok: ($json['status'] ?? '') === 'ok',
network: $json['network'] ?? '',
),
fn(?array $json) => self::healthStatusFromJson($json),
);
}

/**
* Convert a /health JSON response to a typed HealthStatus. Diagnostic
* fields default to '' / 0 when talking to a pre-0.4.0 daemon.
*
* @param array<string, mixed>|null $json
*/
private static function healthStatusFromJson(?array $json): HealthStatus
{
$json ??= [];
return new HealthStatus(
ok: ($json['status'] ?? '') === 'ok',
network: $json['network'] ?? '',
version: $json['version'] ?? '',
evmNetwork: $json['evm_network'] ?? '',
uptimeSeconds: (int)($json['uptime_seconds'] ?? 0),
buildCommit: $json['build_commit'] ?? '',
paymentTokenAddress: $json['payment_token_address'] ?? '',
paymentVaultAddress: $json['payment_vault_address'] ?? '',
);
}

Expand Down
15 changes: 15 additions & 0 deletions antd-php/src/Models/HealthStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,25 @@

namespace Autonomi\Antd\Models;

/**
* Health check result from the antd daemon.
*
* The diagnostic fields ({@see $version}, {@see $evmNetwork},
* {@see $uptimeSeconds}, {@see $buildCommit}, {@see $paymentTokenAddress},
* {@see $paymentVaultAddress}) were added in antd 0.4.0. They default to
* empty / 0 so the class stays constructable when talking to a pre-0.4.0
* daemon that doesn't report them.
*/
readonly class HealthStatus
{
public function __construct(
public bool $ok,
public string $network,
public string $version = '',
public string $evmNetwork = '',
public int $uptimeSeconds = 0,
public string $buildCommit = '',
public string $paymentTokenAddress = '',
public string $paymentVaultAddress = '',
) {}
}
33 changes: 32 additions & 1 deletion antd-php/tests/AntdClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,43 @@ private function jsonResponse(int $status, array $body): Response
public function testHealth(): void
{
$mock = new MockHandler([
$this->jsonResponse(200, ['status' => 'ok', 'network' => 'local']),
$this->jsonResponse(200, [
'status' => 'ok',
'network' => 'local',
'version' => '0.4.0',
'evm_network' => 'local',
'uptime_seconds' => 42,
'build_commit' => 'abcdef123456',
'payment_token_address' => '0xtoken',
'payment_vault_address' => '0xvault',
]),
]);
$client = $this->createClient($mock);
$health = $client->health();
$this->assertTrue($health->ok);
$this->assertSame('local', $health->network);
$this->assertSame('0.4.0', $health->version);
$this->assertSame('local', $health->evmNetwork);
$this->assertSame(42, $health->uptimeSeconds);
$this->assertSame('abcdef123456', $health->buildCommit);
$this->assertSame('0xtoken', $health->paymentTokenAddress);
$this->assertSame('0xvault', $health->paymentVaultAddress);
}

public function testHealthPreV0_4_0Daemon(): void
{
// Older daemons reply with just status + network; the empty defaults
// populate the diagnostic fields rather than throwing.
$mock = new MockHandler([
$this->jsonResponse(200, ['status' => 'ok', 'network' => 'default']),
]);
$client = $this->createClient($mock);
$health = $client->health();
$this->assertTrue($health->ok);
$this->assertSame('default', $health->network);
$this->assertSame('', $health->version);
$this->assertSame('', $health->evmNetwork);
$this->assertSame(0, $health->uptimeSeconds);
}

// --- Data Public ---
Expand Down