GitApi
Reads data from the hosts of Git repositories, with cached responses.
A host can be used when a "Git host token" key is set up for it, or when it's a provider's public host, such as github.com, which works without one.
- Full name:
\Drupal\git_host\GitApi
Constants
| Constant | Visibility | Type | Value |
|---|---|---|---|
CACHE_LIFETIME |
public | 3600 | |
MAX_PAGES |
public | 10 |
Properties
hosts
The hosts that can be used, keyed by host.
protected array|null $hosts
httpClient
protected \GuzzleHttp\ClientInterface $httpClient
cacheBackend
protected \Drupal\Core\Cache\CacheBackendInterface $cacheBackend
providerManager
protected \Drupal\git_host\GitProviderManager $providerManager
keyRepository
protected \Drupal\key\KeyRepositoryInterface $keyRepository
time
protected \Drupal\Component\Datetime\TimeInterface $time
Methods
__construct
Constructs a GitApi.
public __construct(\GuzzleHttp\ClientInterface $httpClient, \Drupal\Core\Cache\CacheBackendInterface $cacheBackend, \Drupal\git_host\GitProviderManager $providerManager, \Drupal\key\KeyRepositoryInterface $keyRepository, \Drupal\Component\Datetime\TimeInterface $time): mixed
Parameters:
| Parameter | Type | Description |
|---|---|---|
$httpClient |
\GuzzleHttp\ClientInterface | |
| The HTTP client. | ||
$cacheBackend |
\Drupal\Core\Cache\CacheBackendInterface | |
| The cache backend for API responses. | ||
$providerManager |
\Drupal\git_host\GitProviderManager | |
| The Git provider plugin manager. | ||
$keyRepository |
\Drupal\key\KeyRepositoryInterface | |
| The key repository. | ||
$time |
\Drupal\Component\Datetime\TimeInterface | |
| The time service. |
getHosts
Gets the hosts that can be used.
public getHosts(): array
Return Value:
Arrays with the 'provider' plugin ID and the 'key' ID (NULL for a public host without a key), keyed by host.
getProvider
Gets the provider for a host.
public getProvider(string $host): \Drupal\git_host\GitProviderInterface
Parameters:
| Parameter | Type | Description |
|---|---|---|
$host |
string | |
| The host, such as github.com. |
Return Value:
The provider plugin.
Throws:
If no key is set up for the host.
- RuntimeException
getProviderId
Gets the ID of a host's provider plugin.
public getProviderId(string $host): string|null
Parameters:
| Parameter | Type | Description |
|---|---|---|
$host |
string | |
| The host, such as github.com. |
Return Value:
The provider plugin ID, or NULL if the host can't be used.
getRaw
Gets a response body from a host's API.
public getRaw(string $host, string $endpoint, array $query = []): string
Parameters:
| Parameter | Type | Description |
|---|---|---|
$host |
string | |
| The host, such as github.com. | ||
$endpoint |
string | |
| The endpoint, relative to the host's API URL. | ||
$query |
array | |
| (optional) Query parameters. |
Return Value:
The response body. Successful responses are cached for an hour.
Throws:
GuzzleExceptionIf no key is set up for the host.RuntimeException
get
Gets JSON data from a host's API.
public get(string $host, string $endpoint, array $query = []): array
Parameters:
| Parameter | Type | Description |
|---|---|---|
$host |
string | |
| The host, such as github.com. | ||
$endpoint |
string | |
| The endpoint, relative to the host's API URL. | ||
$query |
array | |
| (optional) Query parameters. |
Return Value:
The decoded data.
Throws:
GuzzleExceptionJsonExceptionRuntimeException
getAllPages
Gets every page of a list from a host's API.
public getAllPages(string $host, string $endpoint, array $query = []): array
GitHub and GitLab both take per_page and page query parameters.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$host |
string | |
| The host, such as github.com. | ||
$endpoint |
string | |
| The endpoint, relative to the host's API URL. | ||
$query |
array | |
| (optional) Query parameters. |
Return Value:
The items of up to MAX_PAGES pages of 100.
Throws:
GuzzleExceptionJsonExceptionRuntimeException
getReleases
Gets a repository's published releases.
public getReleases(string $host, string $repository): array
Parameters:
| Parameter | Type | Description |
|---|---|---|
$host |
string | |
| The repository's host. | ||
$repository |
string | |
| The repository's path on the host, such as owner/name. |
Return Value:
The releases, as GitProviderInterface::getReleases() returns them.
Throws:
GuzzleExceptionJsonExceptionRuntimeException
getProtectedBranches
Gets a repository's protected branches.
public getProtectedBranches(string $host, string $repository): array
Parameters:
| Parameter | Type | Description |
|---|---|---|
$host |
string | |
| The repository's host. | ||
$repository |
string | |
| The repository's path on the host, such as owner/name. |
Return Value:
The branches, as GitProviderInterface::getProtectedBranches() returns them.
Throws:
GuzzleExceptionJsonExceptionRuntimeException
getFileContents
Gets the contents of a file in a repository.
public getFileContents(string $host, string $repository, string $path, string $ref = ''): string
Parameters:
| Parameter | Type | Description |
|---|---|---|
$host |
string | |
| The repository's host. | ||
$repository |
string | |
| The repository's path on the host, such as owner/name. | ||
$path |
string | |
| The file's path in the repository. | ||
$ref |
string | |
| (optional) A branch, tag or commit. Defaults to the default branch. |
Return Value:
The file's contents.
Throws:
GuzzleExceptionJsonExceptionRuntimeException