This commit is contained in:
2021-11-18 10:46:24 +08:00
parent 6f4f4c2beb
commit 70b4400f95
4 changed files with 128 additions and 94 deletions
+32 -27
View File
@@ -15,7 +15,6 @@ use Http\Message\Stream;
use JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\Logger;
use Kiri\Kiri;
use Psr\Http\Message\ResponseInterface;
use Swoole\Coroutine\Http\Client as SwowClient;
/**
@@ -29,12 +28,12 @@ class CoroutineClient extends ClientAbstracts
* @param string $method
* @param $path
* @param array $params
* @return ResponseInterface
* @return void
* @throws Exception
*/
public function request(string $method, $path, array $params = []): ResponseInterface
public function request(string $method, $path, array $params = []): void
{
return $this->withMethod($method)
$this->withMethod($method)
->coroutine(
$this->matchHost($path),
$this->paramEncode($params)
@@ -45,23 +44,24 @@ class CoroutineClient extends ClientAbstracts
/**
* @param $url
* @param array|string $data
* @return ResponseInterface
* @throws Exception 使用swoole协程方式请求
*/
private function coroutine($url, array|string $data = []): ResponseInterface
private function coroutine($url, array|string $data = []): void
{
try {
$client = $this->generate_client($data, ...$url);
if ($client->statusCode < 0) {
throw new Exception($client->errMsg);
$this->generate_client($data, ...$url);
if ($this->client->statusCode < 0) {
throw new Exception($this->client->errMsg);
}
return (new Response())->withStatus($client->getStatusCode())
->withHeaders($client->getHeaders())
->withBody(new Stream($client->getBody()));
$body = (new Response())->withStatus($this->client->getStatusCode())
->withHeaders($this->client->getHeaders())
->withBody(new Stream($this->client->getBody()));
} catch (\Throwable $exception) {
Kiri::getDi()->get(Logger::class)->error('rpc', [$exception]);
return (new Response())->withStatus(-1)->withHeaders([])
$body = (new Response())->withStatus(-1)->withHeaders([])
->withBody(new Stream(jTraceEx($exception)));
} finally {
$this->setBody($body);
}
}
@@ -71,50 +71,55 @@ class CoroutineClient extends ClientAbstracts
* @param $host
* @param $isHttps
* @param $path
* @return SwowClient
*/
private function generate_client($data, $host, $isHttps, $path): SwowClient
private function generate_client($data, $host, $isHttps, $path): void
{
if ($isHttps || $this->isSSL()) {
$client = new SwowClient($host, 443, true);
$this->client = new SwowClient($host, 443, true);
} else {
$client = new SwowClient($host, $this->getPort(), false);
$this->client = new SwowClient($host, $this->getPort(), false);
}
$client->set($this->settings());
$this->client->set($this->settings());
if (!empty($this->getAgent())) {
$this->withAddedHeader('User-Agent', $this->getAgent());
}
$client->setHeaders($this->getHeader());
$client->setMethod(strtoupper($this->getMethod()));
$client->execute($this->setParams($client, $path, $data));
$client->close();
return $client;
$this->client->setHeaders($this->getHeader());
$this->client->setMethod(strtoupper($this->getMethod()));
$this->client->execute($this->setParams($path, $data));
}
/**
* @param SwowClient $client
* @param $path
* @param $data
* @return string
*/
private function setParams(SwowClient $client, $path, $data): string
private function setParams($path, $data): string
{
$content = $this->getData()->getContents();
if (!empty($content)) {
$client->setData($content);
$this->client->setData($content);
}
if ($this->isGet()) {
if (!empty($data)) $path .= '?' . $data;
} else {
$data = $this->mergeParams($data);
if (!empty($data)) {
$client->setData($data);
$this->client->setData($data);
}
}
return $path;
}
/**
*
*/
public function close(): void
{
$this->client->close();
}
/**
* @return array
*/