diff --git a/src/CCatClient.php b/src/CCatClient.php index a12e230..acb3d6a 100644 --- a/src/CCatClient.php +++ b/src/CCatClient.php @@ -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 @@ -39,11 +39,7 @@ 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; } @@ -51,7 +47,7 @@ public function sendMessage(Message $message, ?\Closure $closure = null): Respon throw new \Exception("Emptiy message from AI"); } break; - } catch (\WebSocket\ConnectionException $e) { + } catch (ConnectionException $e) { // Possibly log errors } } diff --git a/src/Clients/HttpClient.php b/src/Clients/HttpClient.php index e566cdb..2a2b16c 100644 --- a/src/Clients/HttpClient.php +++ b/src/Clients/HttpClient.php @@ -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; } /** @@ -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'); + }; + } } \ No newline at end of file diff --git a/src/Clients/WSClient.php b/src/Clients/WSClient.php index b425f45..11469d6 100644 --- a/src/Clients/WSClient.php +++ b/src/Clients/WSClient.php @@ -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; } /** @@ -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)