Spaces:
No application file
No application file
File size: 1,973 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
<?php
declare(strict_types=1);
namespace Mautic\MarketplaceBundle\Api;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Request;
use Mautic\MarketplaceBundle\Exception\ApiException;
use Psr\Log\LoggerInterface;
class Connection
{
public function __construct(
private ClientInterface $httpClient,
private LoggerInterface $logger
) {
}
/**
* @throws ApiException
*/
public function getPlugins(int $page, int $limit, string $query = ''): array
{
return $this->makeRequest("https://packagist.org/search.json?page={$page}&per_page={$limit}&type=mautic-plugin&q={$query}");
}
/**
* @throws ApiException
*/
public function getPackage(string $pluginName): array
{
return $this->makeRequest("https://packagist.org/packages/{$pluginName}.json");
}
public function makeRequest(string $url): array
{
$this->logger->debug('About to query the Packagist API: '.$url);
$request = new Request('GET', $url, $this->getHeaders());
try {
$response = $this->httpClient->send($request);
} catch (GuzzleException $e) {
throw new ApiException($e->getMessage(), $e->getCode());
}
$body = (string) $response->getBody();
if ($response->getStatusCode() >= 300) {
throw new ApiException($body, $response->getStatusCode());
}
$payload = json_decode($body, true);
$this->logger->debug('Successful Packagist API response', ['payload' => $payload]);
return $payload;
}
private function getHeaders(): array
{
return [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Accept-Encoding' => 'gzip, deflate, br',
'Connection' => 'keep-alive',
'User-Agent' => 'Mautic Marketplace',
];
}
}
|