Skip to content

Commit

Permalink
Merge pull request #7 from aternosorg/allow-custom-http-client
Browse files Browse the repository at this point in the history
Allow custom http client in constructor of ModrinthAPIClient
  • Loading branch information
JulianVennen committed Aug 21, 2023
2 parents 9d443f4 + 009f6df commit f97c145
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions lib/Client/ModrinthAPIClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
use Aternos\ModrinthApi\Model\UserPayoutHistory;
use Aternos\ModrinthApi\Model\Version as VersionModel;
use Aternos\ModrinthApi\Model\Thread as ThreadModel;
use Psr\Http\Client\ClientInterface;


/**
Expand All @@ -57,6 +58,8 @@
*/
class ModrinthAPIClient
{
protected ?ClientInterface $httpClient;

protected Configuration $configuration;

protected ?string $apiToken = null;
Expand All @@ -83,9 +86,11 @@ class ModrinthAPIClient
* ModrinthAPIClient constructor.
* @param string|null $apiToken API token used for authentication
* @param Configuration|null $configuration
* @param ClientInterface|null $httpClient
*/
public function __construct(?string $apiToken = null, ?Configuration $configuration = null)
public function __construct(?string $apiToken = null, ?Configuration $configuration = null, ClientInterface $httpClient = null)
{
$this->httpClient = $httpClient;
$this->configuration = $configuration ?? (Configuration::getDefaultConfiguration())
->setUserAgent("php-modrinth-api/1.0.0");
$this->setApiToken($apiToken);
Expand All @@ -100,15 +105,15 @@ public function setConfiguration(Configuration $configuration): static
$this->configuration = $configuration;
$this->configuration->setBooleanFormatForQueryString(Configuration::BOOLEAN_FORMAT_STRING);

$this->projects = new ProjectsApi(null, $this->configuration);
$this->versions = new VersionsApi(null, $this->configuration);
$this->versionFiles = new VersionFilesApi(null, $this->configuration);
$this->users = new UsersApi(null, $this->configuration);
$this->teams = new TeamsApi(null, $this->configuration);
$this->tags = new TagsApi(null, $this->configuration);
$this->misc = new MiscApi(null, $this->configuration);
$this->notifications = new NotificationsApi(null, $this->configuration);
$this->threads = new ThreadsApi(null, $this->configuration);
$this->projects = new ProjectsApi($this->httpClient, $this->configuration);
$this->versions = new VersionsApi($this->httpClient, $this->configuration);
$this->versionFiles = new VersionFilesApi($this->httpClient, $this->configuration);
$this->users = new UsersApi($this->httpClient, $this->configuration);
$this->teams = new TeamsApi($this->httpClient, $this->configuration);
$this->tags = new TagsApi($this->httpClient, $this->configuration);
$this->misc = new MiscApi($this->httpClient, $this->configuration);
$this->notifications = new NotificationsApi($this->httpClient, $this->configuration);
$this->threads = new ThreadsApi($this->httpClient, $this->configuration);

return $this;
}
Expand Down Expand Up @@ -136,6 +141,18 @@ public function setApiToken(?string $token): static
return $this->setConfiguration($this->configuration);
}

/**
* Set the HTTP client used for all requests.
* When null, the default HTTP client from Guzzle will be used.
* @param ClientInterface|null $httpClient
* @return $this
*/
public function setHttpClient(?ClientInterface $httpClient): static
{
$this->httpClient = $httpClient;
return $this->setConfiguration($this->configuration);
}

/**
* Search projects
* @param ProjectSearchOptions|null $options
Expand Down

0 comments on commit f97c145

Please sign in to comment.