<?php
namespace App\Remote\Api;
use ApiClientBundle\Client\AbstractService;
class MyService extends AbstractService
{
protected string $scheme = 'http';
protected string $host = 'some-host.ru'
}
<?php
namespace App\Remote\Api;
use ApiClientBundle\Client\AbstractQuery;use ApiClientBundle\Enum\HttpMethodEnum;
class MyQuery extends AbstractQuery
{
protected ?string $path = '/some-path'
protected HttpMethodEnum $method = HttpMethodEnum::POST;
protected string $format = 'json'; // Any format supported by symfony/serializer
protected string $service = MyService::class;
protected string $response = MyResponse::class;
protected ?array $files = ['file' => '/some/file/path/to/image.png']; // sending file with multipart/form-data
/**
* @param array $query
*/
public function setQuery(array $query):void {
$this->query = $query;
}
}
<?php
namespace App\Remote\Api;
use ApiClientBundle\Client\ResponseInterface;
class MyResponse implements ResponseInterface
{
public function __construct(
public readonly string $status
) {}
}
<?php
namespace App\Controller;
use ApiClientBundle\HTTP\HttpClientInterface;
use App\Remote\Api\MyQuery;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
class Controller extends AbstractController
{
public function __invoke(HttpClientInterface $httpClient)
{
$query = new MyQuery();
$query->setQuery(['example' => 'simple'])
$response = $httpClient->request($query);
return new Response($response->status);
}
}
Library may throw 3 types of exceptions:
\ApiClientBundle\HTTP\HttpClientException
- throw when some client problems occurs (unknown host, etc.)\ApiClientBundle\Exception\HttpRequestException
- throws when response status code >= 400 and <= 499\ApiClientBundle\Exception\ServerErrorException
- throws when response status code >= 500