Save to database #13
@@ -9,6 +9,7 @@ use IDCI\Bundle\GraphQLClientBundle\Client\GraphQLApiClientRegistryInterface;
|
|||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
|
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
|
||||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||||
|
use Symfony\Contracts\HttpClient\ResponseInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetches the last 365 days of contributions from the GitHub GraphQL API.
|
* Fetches the last 365 days of contributions from the GitHub GraphQL API.
|
||||||
@@ -46,10 +47,7 @@ final class GitHubProvider implements ProviderInterface
|
|||||||
])->getContent();
|
])->getContent();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function startFetch(): ResponseInterface
|
||||||
* @return array<string, int> date (Y-m-d) => contribution count
|
|
||||||
*/
|
|
||||||
public function fetch(): array
|
|
||||||
{
|
{
|
||||||
$this->logger->debug('GitHubProvider: fetching contributions', ['user' => $this->username]);
|
$this->logger->debug('GitHubProvider: fetching contributions', ['user' => $this->username]);
|
||||||
|
|
||||||
@@ -75,15 +73,24 @@ final class GitHubProvider implements ProviderInterface
|
|||||||
|
|
||||||
// GitHub's GraphQL API requires application/json — the bundle's built-in
|
// GitHub's GraphQL API requires application/json — the bundle's built-in
|
||||||
// transport sends form_params, so we use Symfony HttpClient here instead.
|
// transport sends form_params, so we use Symfony HttpClient here instead.
|
||||||
$response = $this->client->request('POST', self::GRAPHQL_URL, [
|
// request() doesn't block; the response is read in resolveFetch() so
|
||||||
|
// multiple providers' requests can be in flight at once.
|
||||||
|
return $this->client->request('POST', self::GRAPHQL_URL, [
|
||||||
'headers' => [
|
'headers' => [
|
||||||
'Authorization' => "Bearer {$this->token}",
|
'Authorization' => "Bearer {$this->token}",
|
||||||
'Content-Type' => 'application/json',
|
'Content-Type' => 'application/json',
|
||||||
],
|
],
|
||||||
'json' => ['query' => $query],
|
'json' => ['query' => $query],
|
||||||
]);
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
$data = $response->toArray();
|
/**
|
||||||
|
* @return array<string, int> date (Y-m-d) => contribution count
|
||||||
|
*/
|
||||||
|
public function resolveFetch(mixed $handle): array
|
||||||
|
{
|
||||||
|
/** @var ResponseInterface $handle */
|
||||||
|
$data = $handle->toArray();
|
||||||
|
|
||||||
if (isset($data['errors'])) {
|
if (isset($data['errors'])) {
|
||||||
throw new ServiceUnavailableHttpException(null, 'GitHub GraphQL error: ' . json_encode($data['errors']));
|
throw new ServiceUnavailableHttpException(null, 'GitHub GraphQL error: ' . json_encode($data['errors']));
|
||||||
|
|||||||
Reference in New Issue
Block a user