48 lines
1.4 KiB
PHP
48 lines
1.4 KiB
PHP
<?php
|
|
namespace ProVM\Common\Service;
|
|
|
|
use Psr\Http\Client\ClientInterface;
|
|
use Psr\Http\Message\RequestFactoryInterface;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
class Api
|
|
{
|
|
public function __construct(ClientInterface $client, RequestFactoryInterface $factory)
|
|
{
|
|
$this->setClient($client)
|
|
->setFactory($factory);
|
|
}
|
|
|
|
protected ClientInterface $client;
|
|
protected RequestFactoryInterface $factory;
|
|
|
|
public function getClient(): ClientInterface
|
|
{
|
|
return $this->client;
|
|
}
|
|
public function getFactory(): RequestFactoryInterface
|
|
{
|
|
return $this->factory;
|
|
}
|
|
public function setClient(ClientInterface $client): Api
|
|
{
|
|
$this->client = $client;
|
|
return $this;
|
|
}
|
|
public function setFactory(RequestFactoryInterface $factory): Api
|
|
{
|
|
$this->factory = $factory;
|
|
return $this;
|
|
}
|
|
|
|
public function sendRequest(array $request_data): ResponseInterface
|
|
{
|
|
$request = $this->getFactory()->createRequest(strtoupper($request_data['method']) ?? 'GET', $request_data['uri']);
|
|
if (strtolower($request_data['method']) !== 'get') {
|
|
$request->getBody()->write(\Safe\json_encode($request_data['data']));
|
|
$request->withHeader('Content-Type', 'application/json');
|
|
}
|
|
return $this->getClient()->sendRequest($request);
|
|
}
|
|
}
|