Added some shorthand methods.
This commit is contained in:
parent
11427d34fc
commit
3bc14cc6a7
3 changed files with 33 additions and 6 deletions
|
@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
) {}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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<string, RpcActionInfo> */
|
||||
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);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue