-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
141 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace SergiX44\Gradio\Client; | ||
|
||
use SergiX44\Gradio\Client; | ||
|
||
readonly class Endpoint | ||
{ | ||
|
||
public function __construct( | ||
public Client $client, | ||
public int $index, | ||
public ?string $apiName, | ||
public bool $useWebsockets | ||
) { | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace SergiX44\Gradio\Client; | ||
|
||
use GuzzleHttp\Client as Guzzle; | ||
use InvalidArgumentException; | ||
use Psr\Http\Message\ResponseInterface; | ||
use SergiX44\Hydrator\Hydrator; | ||
use SergiX44\Hydrator\HydratorInterface; | ||
use WebSocket\Client as WebSocket; | ||
|
||
abstract class RemoteClient | ||
{ | ||
private string $src; | ||
|
||
protected Guzzle $httpClient; | ||
|
||
protected HydratorInterface $hydrator; | ||
|
||
public function __construct(string $src) | ||
{ | ||
if ( | ||
!str_starts_with($src, 'http://') && | ||
!str_starts_with($src, 'https://') && | ||
!str_starts_with($src, 'ws://') && | ||
!str_starts_with($src, 'wss://') | ||
) { | ||
throw new InvalidArgumentException('The src must not contain the protocol'); | ||
} | ||
|
||
$this->src = str_ends_with($src, '/') ? $src : "{$src}/"; | ||
|
||
$this->hydrator = new Hydrator(); | ||
|
||
$this->httpClient = new Guzzle([ | ||
'base_uri' => str_replace('ws', 'http', $this->src), | ||
'headers' => [ | ||
'User-Agent' => 'gradio_client_php/1.0', | ||
], | ||
]); | ||
} | ||
|
||
|
||
protected function get(string $uri, array $params = [], ?string $dto = null) | ||
{ | ||
$response = $this->httpClient->get($uri, ['query' => $params]); | ||
|
||
return $this->parseResponse($response, $dto); | ||
} | ||
|
||
protected function post(string $uri, array $params = [], ?string $dto = null) | ||
{ | ||
$response = $this->httpClient->post($uri, ['json' => $params]); | ||
|
||
return $this->parseResponse($response, $dto); | ||
} | ||
|
||
protected function ws(string $uri, array $options = []): WebSocket | ||
{ | ||
return new WebSocket(str_replace('http', 'ws', $this->src).$uri, $options); | ||
} | ||
|
||
private function parseResponse(ResponseInterface $response, string $mapTo = null): mixed | ||
{ | ||
$body = $response->getBody()->getContents(); | ||
|
||
if ($mapTo !== null) { | ||
return $this->hydrator->hydrateWithJson($mapTo, $body); | ||
} | ||
|
||
return json_decode($body, flags: JSON_THROW_ON_ERROR); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters