|
|
|
|
@@ -6,15 +6,12 @@ namespace Singularity\HDK\Core\Service;
|
|
|
|
|
|
|
|
|
|
use Ergebnis\Http\Method;
|
|
|
|
|
use GuzzleHttp\Client;
|
|
|
|
|
use GuzzleHttp\Exception\BadResponseException;
|
|
|
|
|
use GuzzleHttp\Exception\GuzzleException;
|
|
|
|
|
use GuzzleHttp\Exception\ServerException;
|
|
|
|
|
use GuzzleHttp\Psr7\Request;
|
|
|
|
|
use GuzzleHttp\Psr7\Utils;
|
|
|
|
|
use Hyperf\Codec\Json;
|
|
|
|
|
use Hyperf\Di\Annotation\Inject;
|
|
|
|
|
use Hyperf\Guzzle\ClientFactory;
|
|
|
|
|
use Psr\Http\Message\RequestInterface;
|
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@@ -22,7 +19,7 @@ use Psr\Http\Message\ResponseInterface;
|
|
|
|
|
*/
|
|
|
|
|
class HttpRequestService
|
|
|
|
|
{
|
|
|
|
|
public const TIMEOUT = 20;
|
|
|
|
|
// public const TIMEOUT = 20;
|
|
|
|
|
|
|
|
|
|
#[Inject]
|
|
|
|
|
private ClientFactory $client;
|
|
|
|
|
@@ -35,14 +32,16 @@ class HttpRequestService
|
|
|
|
|
/**
|
|
|
|
|
* @param string $url
|
|
|
|
|
* @param array<string, string|int> $params
|
|
|
|
|
* @param array $data
|
|
|
|
|
* @return ResponseInterface
|
|
|
|
|
* @throws GuzzleException
|
|
|
|
|
*/
|
|
|
|
|
public function requestGet(string $url, array $params = []): ResponseInterface
|
|
|
|
|
public function requestGet(string $url, array $params = [], array $data = []): ResponseInterface
|
|
|
|
|
{
|
|
|
|
|
$request = new Request(Method\Rfc\Rfc7231::GET, $url);
|
|
|
|
|
return $this->getClient()->send($request, [
|
|
|
|
|
'params' => $params,
|
|
|
|
|
'query' => $params,
|
|
|
|
|
'json' => $data,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -58,12 +57,17 @@ class HttpRequestService
|
|
|
|
|
* @return ResponseInterface
|
|
|
|
|
* @throws GuzzleException
|
|
|
|
|
*/
|
|
|
|
|
public function requestPost(string $url, array $data = [], array $params = []): ResponseInterface
|
|
|
|
|
public function requestPost(string $url, array $params = [], array $data = []): ResponseInterface
|
|
|
|
|
{
|
|
|
|
|
$data = Json::encode($data);
|
|
|
|
|
$request = new Request('post', $url, ['Content-Type' => 'application/json'], Utils::streamFor($data));
|
|
|
|
|
$request = new Request(
|
|
|
|
|
'post',
|
|
|
|
|
$url,
|
|
|
|
|
['Content-Type' => 'application/json'],
|
|
|
|
|
Utils::streamFor($data),
|
|
|
|
|
);
|
|
|
|
|
return $this->getClient()->send($request, [
|
|
|
|
|
'params' => $params,
|
|
|
|
|
'query' => $params,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -75,18 +79,13 @@ class HttpRequestService
|
|
|
|
|
* @return ResponseInterface
|
|
|
|
|
* @throws GuzzleException
|
|
|
|
|
*/
|
|
|
|
|
public function requestPut(string $url, array $data = [], array $params = []): ResponseInterface
|
|
|
|
|
public function requestPut(string $url, array $params = [], array $data = []): ResponseInterface
|
|
|
|
|
{
|
|
|
|
|
$data = Json::encode($data);
|
|
|
|
|
$request = new Request('put', $url, ['Content-Type' => 'application/json'], Utils::streamFor($data));
|
|
|
|
|
$result = $this->getClient()->send($request, [
|
|
|
|
|
'params' => $params,
|
|
|
|
|
return $this->getClient()->send($request, [
|
|
|
|
|
'query' => $params,
|
|
|
|
|
]);
|
|
|
|
|
$json = Json::decode($result->getBody()->getContents());
|
|
|
|
|
if ($json['code'] !== 200) {
|
|
|
|
|
throw new ServerException($json['message'], $request, $result);
|
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@@ -99,20 +98,4 @@ class HttpRequestService
|
|
|
|
|
$this->options = array_replace_recursive($this->options, $options);
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param ResponseInterface $response
|
|
|
|
|
* @param RequestInterface $request
|
|
|
|
|
* @return ResponseInterface
|
|
|
|
|
*/
|
|
|
|
|
private function parseResponse(
|
|
|
|
|
ResponseInterface $response,
|
|
|
|
|
RequestInterface $request
|
|
|
|
|
): ResponseInterface {
|
|
|
|
|
if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {
|
|
|
|
|
return $response;
|
|
|
|
|
} else {
|
|
|
|
|
throw new BadResponseException($response->getReasonPhrase(), $request, $response);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|