Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 336237d338 | |||
| d075dd73a4 | |||
| 40c946b58e | |||
| 1d4554dbe7 |
+28
-9
@@ -48,10 +48,14 @@ abstract class ClientAbstracts implements IClient
|
||||
private int $port = 80;
|
||||
|
||||
|
||||
|
||||
private int $statusCode = 200;
|
||||
|
||||
|
||||
/**
|
||||
* @var ResponseInterface|null
|
||||
* @var string|null
|
||||
*/
|
||||
protected ?ResponseInterface $body;
|
||||
protected ?string $body;
|
||||
|
||||
|
||||
private ?StreamInterface $_data = null;
|
||||
@@ -63,21 +67,36 @@ abstract class ClientAbstracts implements IClient
|
||||
|
||||
|
||||
/**
|
||||
* @return string|ResponseInterface|null
|
||||
* @return int
|
||||
*/
|
||||
public function getBody(): string|ResponseInterface|null
|
||||
public function getStatusCode(): int
|
||||
{
|
||||
return $this->statusCode;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param int $statusCode
|
||||
*/
|
||||
public function setStatusCode(int $statusCode): void
|
||||
{
|
||||
$this->statusCode = $statusCode;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getBody(): string|null
|
||||
{
|
||||
if ($this->body instanceof ResponseInterface) {
|
||||
return $this->body->getBody()->getContents();
|
||||
}
|
||||
return $this->body;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ResponseInterface|null $body
|
||||
* @param ?string $body
|
||||
*/
|
||||
public function setBody(?ResponseInterface $body): void
|
||||
public function setBody(?string $body): void
|
||||
{
|
||||
$this->body = $body;
|
||||
}
|
||||
|
||||
@@ -10,8 +10,6 @@ declare(strict_types=1);
|
||||
namespace Http\Client;
|
||||
|
||||
use Exception;
|
||||
use Http\Message\Response;
|
||||
use Http\Message\Stream;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Kiri\Abstracts\Logger;
|
||||
use Kiri\Kiri;
|
||||
@@ -53,15 +51,12 @@ class CoroutineClient extends ClientAbstracts
|
||||
if ($this->client->statusCode < 0) {
|
||||
throw new Exception($this->client->errMsg);
|
||||
}
|
||||
$body = (new Response())->withStatus($this->client->getStatusCode())
|
||||
->withHeaders($this->client->getHeaders())
|
||||
->withBody(new Stream($this->client->getBody()));
|
||||
$this->setStatusCode($this->client->getStatusCode());
|
||||
$this->setBody($this->client->getBody());
|
||||
} catch (\Throwable $exception) {
|
||||
Kiri::getDi()->get(Logger::class)->error('rpc', [$exception]);
|
||||
$body = (new Response())->withStatus(-1)->withHeaders([])
|
||||
->withBody(new Stream(jTraceEx($exception)));
|
||||
} finally {
|
||||
$this->setBody($body);
|
||||
$this->setStatusCode(-1);
|
||||
$this->setBody(jTraceEx($exception));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+7
-7
@@ -138,11 +138,11 @@ class Curl extends ClientAbstracts
|
||||
{
|
||||
$output = curl_exec($this->client);
|
||||
if ($output === false) {
|
||||
$response = (new Response())->withStatus(400)->withBody(new Stream(curl_error($this->client)));
|
||||
$this->setStatusCode(404);
|
||||
$this->setBody(curl_error($this->client));
|
||||
} else {
|
||||
$response = $this->explode($output);
|
||||
$this->explode($output);
|
||||
}
|
||||
$this->setBody($response);
|
||||
}
|
||||
|
||||
|
||||
@@ -157,10 +157,10 @@ class Curl extends ClientAbstracts
|
||||
|
||||
/**
|
||||
* @param $output
|
||||
* @return ResponseInterface
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
private function explode($output): ResponseInterface
|
||||
private function explode($output): void
|
||||
{
|
||||
[$header, $body] = explode("\r\n\r\n", $output, 2);
|
||||
if ($header == 'HTTP/1.1 100 Continue') {
|
||||
@@ -170,8 +170,8 @@ class Curl extends ClientAbstracts
|
||||
$header = explode("\r\n", $header);
|
||||
$status = explode(' ', array_shift($header));
|
||||
|
||||
return (new Response())->withStatus(intval($status[1]))->withHeaders($this->headerFormat($header))
|
||||
->withBody(new Stream($body));
|
||||
$this->setStatusCode(intval($status[1]));
|
||||
$this->setBody($body);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user