Implement client session management and refactoring
This commit is contained in:
parent
cad2ea3419
commit
3a10e01bd8
11 changed files with 234 additions and 23 deletions
57
src/Socialbox/SocialClient.php
Normal file
57
src/Socialbox/SocialClient.php
Normal file
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
namespace Socialbox;
|
||||
|
||||
use Socialbox\Classes\RpcClient;
|
||||
use Socialbox\Classes\Utilities;
|
||||
use Socialbox\Exceptions\CryptographyException;
|
||||
use Socialbox\Exceptions\ResolutionException;
|
||||
use Socialbox\Exceptions\RpcException;
|
||||
use Socialbox\Objects\KeyPair;
|
||||
use Socialbox\Objects\RpcError;
|
||||
use Socialbox\Objects\RpcRequest;
|
||||
|
||||
class SocialClient extends RpcClient
|
||||
{
|
||||
/**
|
||||
* Constructs a new instance with the specified domain.
|
||||
*
|
||||
* @param string $domain The domain to be set for the instance.
|
||||
* @throws ResolutionException
|
||||
*/
|
||||
public function __construct(string $domain)
|
||||
{
|
||||
parent::__construct($domain);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new session using the provided key pair.
|
||||
*
|
||||
* @param KeyPair $keyPair The key pair to be used for creating the session.
|
||||
* @return string The UUID of the created session.
|
||||
* @throws CryptographyException if there is an error in the cryptographic operations.
|
||||
* @throws RpcException if there is an error in the RPC request or if no response is received.
|
||||
*/
|
||||
public function createSession(KeyPair $keyPair): string
|
||||
{
|
||||
$response = $this->sendRequest(new RpcRequest('createSession', Utilities::randomCrc32(), [
|
||||
'public_key' => $keyPair->getPublicKey()
|
||||
]));
|
||||
|
||||
if($response === null)
|
||||
{
|
||||
throw new RpcException('Failed to create the session, no response received');
|
||||
}
|
||||
|
||||
if($response instanceof RpcError)
|
||||
{
|
||||
throw RpcException::fromRpcError($response);
|
||||
}
|
||||
|
||||
$this->setSessionUuid($response->getResult());
|
||||
$this->setPrivateKey($keyPair->getPrivateKey());
|
||||
|
||||
return $response->getResult();
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue