Added scoping to RPC client.

This commit is contained in:
flash 2024-08-16 15:30:59 +00:00
parent 799688b3d2
commit bf092daec7
7 changed files with 86 additions and 23 deletions

View file

@ -1,9 +1,9 @@
<?php
// CurlHttpRequest.php
// Created: 2024-08-13
// Updated: 2024-08-13
// Updated: 2024-08-16
namespace Aiwass;
namespace Aiwass\Client;
use CurlHandle;
use RuntimeException;

View file

@ -1,9 +1,9 @@
<?php
// IHttpRequest.php
// Created: 2024-08-13
// Updated: 2024-08-13
// Updated: 2024-08-16
namespace Aiwass;
namespace Aiwass\Client;
use RuntimeException;
use Index\ICloseable;

37
src/Client/IRpcClient.php Normal file
View file

@ -0,0 +1,37 @@
<?php
// IRpcClient.php
// Created: 2024-08-16
// Updated: 2024-08-16
namespace Aiwass\Client;
/**
* Provides a common interface for RPC clients.
*/
interface IRpcClient {
/**
* Creates a proxy for this RPC client with a specified namespace.
*
* @param string $prefix Prefix to apply to the scoped RPC client.
* @return IRpcClient A scoped RPC client instance.
*/
function scopeTo(string $prefix): IRpcClient;
/**
* Makes an RPC query.
*
* @param string $action Name of the action.
* @param array<string, mixed> $params Parameters to query with.
* @return mixed Result of the query.
*/
function query(string $action, array $params): mixed;
/**
* Makes an RPC procedure call.
*
* @param string $action Name of the action.
* @param array<string, mixed> $params Parameters to query with.
* @return mixed Result of the procedure call.
*/
function procedure(string $action, array $params): mixed;
}

View file

@ -6,12 +6,12 @@
namespace Aiwass\Client;
use InvalidArgumentException;
use Aiwass\{AiwassMsgPack,IHttpRequest,IVerificationProvider};
use Aiwass\{AiwassMsgPack,IVerificationProvider};
/**
* Implemens an RPC client.
*/
class RpcClient {
class RpcClient implements IRpcClient {
/**
* @param string $url Base RPC url, up to the /_aiwass part.
* @param callable(): IHttpRequest $createRequest Creates a HTTP request.
@ -26,6 +26,10 @@ class RpcClient {
throw new InvalidArgumentException('$createRequest must be a callable');
}
public function scopeTo(string $prefix): IRpcClient {
return new RpcClientScoped($this, $prefix);
}
/** @param array<string, mixed> $params */
private function callAction(bool $isProcedure, string $action, array $params): mixed {
$params = http_build_query($params, '', '&', PHP_QUERY_RFC3986);
@ -39,24 +43,10 @@ class RpcClient {
return AiwassMsgPack::decode($request->execute());
}
/**
* Makes an RPC query.
*
* @param string $action Name of the action.
* @param array<string, mixed> $params Parameters to query with.
* @return mixed Result of the query.
*/
public function query(string $action, array $params): mixed {
return $this->callAction(false, $action, $params);
}
/**
* Makes an RPC procedure call.
*
* @param string $action Name of the action.
* @param array<string, mixed> $params Parameters to query with.
* @return mixed Result of the procedure call.
*/
public function procedure(string $action, array $params): mixed {
return $this->callAction(true, $action, $params);
}

View file

@ -0,0 +1,32 @@
<?php
// RpcClientScoped.php
// Created: 2024-08-16
// Updated: 2024-08-16
namespace Aiwass\Client;
/**
* Provides a scoped interface to an underlying RPC client.
*/
class RpcClientScoped implements IRpcClient {
/**
* @param IRpcClient $base RPC client instance to use as a base.
* @param string $prefix Prefix to apply to action names passed.
*/
public function __construct(
private IRpcClient $base,
private string $prefix
) {}
public function scopeTo(string $prefix): IRpcClient {
return $this->base->scopeTo($this->prefix . $prefix);
}
public function query(string $action, array $params): mixed {
return $this->base->query($this->prefix . $action, $params);
}
public function procedure(string $action, array $params): mixed {
return $this->base->procedure($this->prefix . $action, $params);
}
}

View file

@ -11,13 +11,17 @@ namespace Aiwass\Server;
class RpcServerScoped implements IRpcServer {
use RpcServerTrait;
/**
* @param IRpcServer $base RPC server instance to use as a base.
* @param string $prefix Prefix to apply to action names passed.
*/
public function __construct(
private IRpcServer $base,
private string $prefix
) {}
public function scopeTo(string $prefix): IRpcServer {
return new RpcServerScoped($this->base, $this->prefix . $prefix);
return $this->base->scopeTo($this->prefix . $prefix);
}
public function registerAction(bool $isProcedure, string $name, $handler): void {

View file

@ -1,13 +1,13 @@
<?php
// CurlHttpTest.php
// Created: 2024-08-13
// Updated: 2024-08-13
// Updated: 2024-08-16
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use Aiwass\CurlHttpRequest;
use Aiwass\Client\CurlHttpRequest;
#[CoversClass(CurlHttpRequest::class)]
final class CurlHttpTest extends TestCase {