Added some shorthand methods.

This commit is contained in:
flash 2024-08-16 15:58:15 +00:00
parent 11427d34fc
commit 3bc14cc6a7
3 changed files with 33 additions and 6 deletions

View file

@ -6,7 +6,7 @@
namespace Aiwass\Client; namespace Aiwass\Client;
use InvalidArgumentException; use InvalidArgumentException;
use Aiwass\{AiwassMsgPack,IVerificationProvider}; use Aiwass\{AiwassMsgPack,HmacVerificationProvider,IVerificationProvider};
/** /**
* Implemens an RPC client. * Implemens an RPC client.
@ -14,13 +14,13 @@ use Aiwass\{AiwassMsgPack,IVerificationProvider};
class RpcClient implements IRpcClient { class RpcClient implements IRpcClient {
/** /**
* @param string $url Base RPC url, up to the /_aiwass part. * @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 IVerificationProvider $verify A verification provider.
* @param callable(): IHttpRequest $createRequest Creates a HTTP request.
*/ */
public function __construct( public function __construct(
private string $url, private string $url,
private $createRequest, private IVerificationProvider $verify,
private IVerificationProvider $verify private $createRequest
) { ) {
if(!is_callable($createRequest)) if(!is_callable($createRequest))
throw new InvalidArgumentException('$createRequest must be a callable'); throw new InvalidArgumentException('$createRequest must be a callable');
@ -50,4 +50,19 @@ class RpcClient implements IRpcClient {
public function procedure(string $action, array $params): mixed { public function procedure(string $action, array $params): mixed {
return $this->callAction(true, $action, $params); 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,
);
}
} }

View file

@ -18,10 +18,11 @@ use Index\Http\Routing\{HttpGet,HttpPost,RouteHandler};
class RpcRouteHandler extends RouteHandler { class RpcRouteHandler extends RouteHandler {
/** /**
* @param IRpcServer $server An RPC server instance. * @param IRpcServer $server An RPC server instance.
* @param IVerificationProvider $verification A verification provider instance.
*/ */
public function __construct( public function __construct(
private IVerificationProvider $verification, private IRpcServer $server,
private IRpcServer $server private IVerificationProvider $verification
) {} ) {}
/** /**

View file

@ -7,6 +7,7 @@ namespace Aiwass\Server;
use InvalidArgumentException; use InvalidArgumentException;
use RuntimeException; use RuntimeException;
use Aiwass\IVerificationProvider;
/** /**
* Implements an RPC server. * Implements an RPC server.
@ -17,6 +18,16 @@ class RpcServer implements IRpcServer {
/** @var array<string, RpcActionInfo> */ /** @var array<string, RpcActionInfo> */
private array $actions = []; 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 { public function scopeTo(string $prefix): IRpcServer {
return new RpcServerScoped($this, $prefix); return new RpcServerScoped($this, $prefix);
} }