Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions src/CCatClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
use Albocode\CcatphpSdk\Model\Why;
use GuzzleHttp\Promise\PromiseInterface;
use GuzzleHttp\Psr7\Utils;
use PhpParser\Node\Expr\Closure;
use Psr\Http\Message\ResponseInterface;
use WebSocket\ConnectionException;


class CCatClient
Expand Down Expand Up @@ -39,19 +39,15 @@ public function sendMessage(Message $message, ?\Closure $closure = null): Respon
while (true) {
try {
$message = $client->receive();
if (str_contains($message, "\"type\":\"notification\"")) {
continue;
}
if (str_contains($message, "\"type\":\"chat_token\"")) {

if (str_contains($message, "\"type\":\"notification\"") || str_contains($message, "\"type\":\"chat_token\"")) {
$closure?->call($this, $message);
continue;
}
if (empty($message)) {
throw new \Exception("Emptiy message from AI");
}
break;
} catch (\WebSocket\ConnectionException $e) {
} catch (ConnectionException $e) {
// Possibly log errors
}
}
Expand Down
27 changes: 23 additions & 4 deletions src/Clients/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,34 @@
namespace Albocode\CcatphpSdk\Clients;

use GuzzleHttp\Client;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use Phrity\Net\Uri;
use Psr\Http\Message\RequestInterface;

class HttpClient
{

private Client $httpClient;
private string $apikey;

public function __construct(string $host, ?int $port = null, string $apikey = '')
public function __construct(string $host, ?int $port = null, string $apikey = '', bool $isHTTPs = false)
{
//todo add support to https
$handlerStack = HandlerStack::create();
$handlerStack->push(Middleware::tap($this->beforeSecureRequest()));

$httpUri = (new Uri())
->withHost($host)
->withPort($port)
->withScheme('http');
->withScheme($isHTTPs ? 'https' : 'http');

$this->httpClient = new Client([
'base_uri' => $httpUri
'base_uri' => $httpUri,
'handler' => $handlerStack
]);

$this->apikey = $apikey;
}

/**
Expand All @@ -31,5 +41,14 @@ public function getHttpClient(): Client
return $this->httpClient;
}

protected function beforeSecureRequest(): \Closure
{
return function (RequestInterface &$request, array $requestOptions) {
if (!empty($this->apikey)) {
$request = $request->withHeader('access_token', $this->apikey);
}
$request = $request->withHeader('Content-Type', 'application/json');
};
}

}
6 changes: 4 additions & 2 deletions src/Clients/WSClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ class WSClient
private string $host;
private ?int $port;
private string $apikey;
private bool $isWSS;

public function __construct(string $host, ?int $port = null, string $apikey = '')
public function __construct(string $host, ?int $port = null, string $apikey = '', bool $isWSS = false)
{
//todo add support to wss

$this->host = $host;
$this->port = $port;
$this->apikey = $apikey;
$this->isWSS = $isWSS;
}

/**
Expand All @@ -27,7 +29,7 @@ public function __construct(string $host, ?int $port = null, string $apikey = ''
public function getWsClient(string $userid = 'user'): Client
{
$wsUri = (new Uri())
->withScheme('ws')
->withScheme($this->isWSS ? 'wss' : 'ws')
->withHost($this->host)
->withPath(sprintf('ws/%s', $userid))
->withPort($this->port)
Expand Down