From bf092daec724b32d79f1c355eb462a293e263b1c Mon Sep 17 00:00:00 2001 From: flashwave Date: Fri, 16 Aug 2024 15:30:59 +0000 Subject: [PATCH] Added scoping to RPC client. --- src/{ => Client}/CurlHttpRequest.php | 4 +-- src/{ => Client}/IHttpRequest.php | 4 +-- src/Client/IRpcClient.php | 37 ++++++++++++++++++++++++++++ src/Client/RpcClient.php | 22 +++++------------ src/Client/RpcClientScoped.php | 32 ++++++++++++++++++++++++ src/Server/RpcServerScoped.php | 6 ++++- tests/CurlHttpTest.php | 4 +-- 7 files changed, 86 insertions(+), 23 deletions(-) rename src/{ => Client}/CurlHttpRequest.php (98%) rename src/{ => Client}/IHttpRequest.php (95%) create mode 100644 src/Client/IRpcClient.php create mode 100644 src/Client/RpcClientScoped.php diff --git a/src/CurlHttpRequest.php b/src/Client/CurlHttpRequest.php similarity index 98% rename from src/CurlHttpRequest.php rename to src/Client/CurlHttpRequest.php index 9641f9e..79c1a71 100644 --- a/src/CurlHttpRequest.php +++ b/src/Client/CurlHttpRequest.php @@ -1,9 +1,9 @@ $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 $params Parameters to query with. + * @return mixed Result of the procedure call. + */ + function procedure(string $action, array $params): mixed; +} diff --git a/src/Client/RpcClient.php b/src/Client/RpcClient.php index d285dd9..4ae458d 100644 --- a/src/Client/RpcClient.php +++ b/src/Client/RpcClient.php @@ -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 $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 $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 $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); } diff --git a/src/Client/RpcClientScoped.php b/src/Client/RpcClientScoped.php new file mode 100644 index 0000000..ca3f4a2 --- /dev/null +++ b/src/Client/RpcClientScoped.php @@ -0,0 +1,32 @@ +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); + } +} diff --git a/src/Server/RpcServerScoped.php b/src/Server/RpcServerScoped.php index cd418f0..6ca6b7d 100644 --- a/src/Server/RpcServerScoped.php +++ b/src/Server/RpcServerScoped.php @@ -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 { diff --git a/tests/CurlHttpTest.php b/tests/CurlHttpTest.php index 8997a6e..ed58749 100644 --- a/tests/CurlHttpTest.php +++ b/tests/CurlHttpTest.php @@ -1,13 +1,13 @@