From 3bc14cc6a727a7785f848736e26d7e068a8f7cf7 Mon Sep 17 00:00:00 2001 From: flashwave Date: Fri, 16 Aug 2024 15:58:15 +0000 Subject: [PATCH] Added some shorthand methods. --- src/Client/RpcClient.php | 23 +++++++++++++++++++---- src/Server/RpcRouteHandler.php | 5 +++-- src/Server/RpcServer.php | 11 +++++++++++ 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/Client/RpcClient.php b/src/Client/RpcClient.php index 4ae458d..554e2bb 100644 --- a/src/Client/RpcClient.php +++ b/src/Client/RpcClient.php @@ -6,7 +6,7 @@ namespace Aiwass\Client; use InvalidArgumentException; -use Aiwass\{AiwassMsgPack,IVerificationProvider}; +use Aiwass\{AiwassMsgPack,HmacVerificationProvider,IVerificationProvider}; /** * Implemens an RPC client. @@ -14,13 +14,13 @@ use Aiwass\{AiwassMsgPack,IVerificationProvider}; class RpcClient implements IRpcClient { /** * @param string $url Base RPC url, up to the /_aiwass part. - * @param callable(): IHttpRequest $createRequest Creates a HTTP request. * @param IVerificationProvider $verify A verification provider. + * @param callable(): IHttpRequest $createRequest Creates a HTTP request. */ public function __construct( private string $url, - private $createRequest, - private IVerificationProvider $verify + private IVerificationProvider $verify, + private $createRequest ) { if(!is_callable($createRequest)) throw new InvalidArgumentException('$createRequest must be a callable'); @@ -50,4 +50,19 @@ class RpcClient implements IRpcClient { public function procedure(string $action, array $params): mixed { return $this->callAction(true, $action, $params); } + + /** + * Creates an RPC client with HMAC verification and a suitable HTTP request implementation. + * + * @param string $url Base RPC url, up to the /_aiwass part. + * @param callable(): string $getSecretKey A method that returns the secret key to use. + * @return RpcClient An RPC client! + */ + public static function createHmac(string $url, $getSecretKey): RpcClient { + return new RpcClient( + $url, + new HmacVerificationProvider($getSecretKey), + fn() => new CurlHttpRequest, + ); + } } diff --git a/src/Server/RpcRouteHandler.php b/src/Server/RpcRouteHandler.php index 20ad7c6..ce43b41 100644 --- a/src/Server/RpcRouteHandler.php +++ b/src/Server/RpcRouteHandler.php @@ -18,10 +18,11 @@ use Index\Http\Routing\{HttpGet,HttpPost,RouteHandler}; class RpcRouteHandler extends RouteHandler { /** * @param IRpcServer $server An RPC server instance. + * @param IVerificationProvider $verification A verification provider instance. */ public function __construct( - private IVerificationProvider $verification, - private IRpcServer $server + private IRpcServer $server, + private IVerificationProvider $verification ) {} /** diff --git a/src/Server/RpcServer.php b/src/Server/RpcServer.php index 027eece..0e2ae71 100644 --- a/src/Server/RpcServer.php +++ b/src/Server/RpcServer.php @@ -7,6 +7,7 @@ namespace Aiwass\Server; use InvalidArgumentException; use RuntimeException; +use Aiwass\IVerificationProvider; /** * Implements an RPC server. @@ -17,6 +18,16 @@ class RpcServer implements IRpcServer { /** @var array */ private array $actions = []; + /** + * Creates an RPC route handler. + * + * @param IVerificationProvider $verification A verification provider. + * @return RpcRouteHandler RPC route handler. + */ + public function createRouteHandler(IVerificationProvider $verification): RpcRouteHandler { + return new RpcRouteHandler($this, $verification); + } + public function scopeTo(string $prefix): IRpcServer { return new RpcServerScoped($this, $prefix); }