Compare commits

...

2 commits

Author SHA1 Message Date
bf092daec7 Added scoping to RPC client. 2024-08-16 15:30:59 +00:00
799688b3d2 Split server and client namespacing. 2024-08-16 15:20:15 +00:00
20 changed files with 104 additions and 39 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

@ -1,16 +1,17 @@
<?php
// RpcClient.php
// Created: 2024-08-13
// Updated: 2024-08-13
// Updated: 2024-08-16
namespace Aiwass;
namespace Aiwass\Client;
use InvalidArgumentException;
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.
@ -25,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);
@ -38,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

@ -3,7 +3,7 @@
// Created: 2024-08-15
// Updated: 2024-08-16
namespace Aiwass;
namespace Aiwass\Server;
/**
* Provides the interface for IRpcServer::register().

View file

@ -3,7 +3,7 @@
// Created: 2024-08-16
// Updated: 2024-08-16
namespace Aiwass;
namespace Aiwass\Server;
use RuntimeException;
use InvalidArgumentException;

View file

@ -3,7 +3,7 @@
// Created: 2024-08-15
// Updated: 2024-08-16
namespace Aiwass;
namespace Aiwass\Server;
use Attribute;
use ReflectionAttribute;

View file

@ -1,9 +1,9 @@
<?php
// RpcActionHandler.php
// Created: 2024-08-15
// Updated: 2024-08-15
// Updated: 2024-08-16
namespace Aiwass;
namespace Aiwass\Server;
/**
* Provides an abstract class version of IRpcActionHandler that already includes the trait as well,

View file

@ -3,7 +3,7 @@
// Created: 2024-08-15
// Updated: 2024-08-16
namespace Aiwass;
namespace Aiwass\Server;
/**
* Provides an implementation of IRpcActionHandler::registerRpcActions that uses the attributes.

View file

@ -3,7 +3,7 @@
// Created: 2024-08-13
// Updated: 2024-08-16
namespace Aiwass;
namespace Aiwass\Server;
use Closure;
use InvalidArgumentException;

View file

@ -3,7 +3,7 @@
// Created: 2024-08-15
// Updated: 2024-08-16
namespace Aiwass;
namespace Aiwass\Server;
use Attribute;
/**

View file

@ -3,7 +3,7 @@
// Created: 2024-08-15
// Updated: 2024-08-16
namespace Aiwass;
namespace Aiwass\Server;
use Attribute;
/**

View file

@ -3,10 +3,11 @@
// Created: 2024-08-13
// Updated: 2024-08-16
namespace Aiwass;
namespace Aiwass\Server;
use Exception;
use RuntimeException;
use Aiwass\{AiwassMsgPack,IVerificationProvider};
use Index\Http\{HttpResponseBuilder,HttpRequest};
use Index\Http\Content\FormContent;
use Index\Http\Routing\{HttpGet,HttpPost,RouteHandler};

View file

@ -3,7 +3,7 @@
// Created: 2024-08-13
// Updated: 2024-08-16
namespace Aiwass;
namespace Aiwass\Server;
use InvalidArgumentException;
use RuntimeException;

View file

@ -3,7 +3,7 @@
// Created: 2024-08-16
// Updated: 2024-08-16
namespace Aiwass;
namespace Aiwass\Server;
/**
* Provides a scoped RPC server implementation.
@ -11,13 +11,17 @@ namespace Aiwass;
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

@ -3,7 +3,7 @@
// Created: 2024-08-16
// Updated: 2024-08-16
namespace Aiwass;
namespace Aiwass\Server;
/**
* Provides implementations for IRpcServer methods that are likely going to be identical across implementations.

View file

@ -8,7 +8,7 @@ declare(strict_types=1);
use RuntimeException;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\{CoversClass,UsesClass};
use Aiwass\{RpcAction,RpcActionHandler,RpcProcedure,RpcQuery,RpcServer};
use Aiwass\Server\{RpcAction,RpcActionHandler,RpcProcedure,RpcQuery,RpcServer};
#[CoversClass(RpcAction::class)]
#[CoversClass(RpcActionHandler::class)]

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 {

View file

@ -7,7 +7,7 @@ declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\{CoversClass,UsesClass};
use Aiwass\{RpcServer,RpcServerScoped};
use Aiwass\Server\{RpcServer,RpcServerScoped};
#[CoversClass(RpcServerScoped::class)]
#[UsesClass(RpcServer::class)]