Skip to content

Commit f97c145

Browse files
authored
Merge pull request #7 from aternosorg/allow-custom-http-client
Allow custom http client in constructor of ModrinthAPIClient
2 parents 9d443f4 + 009f6df commit f97c145

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

lib/Client/ModrinthAPIClient.php

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
use Aternos\ModrinthApi\Model\UserPayoutHistory;
4848
use Aternos\ModrinthApi\Model\Version as VersionModel;
4949
use Aternos\ModrinthApi\Model\Thread as ThreadModel;
50+
use Psr\Http\Client\ClientInterface;
5051

5152

5253
/**
@@ -57,6 +58,8 @@
5758
*/
5859
class ModrinthAPIClient
5960
{
61+
protected ?ClientInterface $httpClient;
62+
6063
protected Configuration $configuration;
6164

6265
protected ?string $apiToken = null;
@@ -83,9 +86,11 @@ class ModrinthAPIClient
8386
* ModrinthAPIClient constructor.
8487
* @param string|null $apiToken API token used for authentication
8588
* @param Configuration|null $configuration
89+
* @param ClientInterface|null $httpClient
8690
*/
87-
public function __construct(?string $apiToken = null, ?Configuration $configuration = null)
91+
public function __construct(?string $apiToken = null, ?Configuration $configuration = null, ClientInterface $httpClient = null)
8892
{
93+
$this->httpClient = $httpClient;
8994
$this->configuration = $configuration ?? (Configuration::getDefaultConfiguration())
9095
->setUserAgent("php-modrinth-api/1.0.0");
9196
$this->setApiToken($apiToken);
@@ -100,15 +105,15 @@ public function setConfiguration(Configuration $configuration): static
100105
$this->configuration = $configuration;
101106
$this->configuration->setBooleanFormatForQueryString(Configuration::BOOLEAN_FORMAT_STRING);
102107

103-
$this->projects = new ProjectsApi(null, $this->configuration);
104-
$this->versions = new VersionsApi(null, $this->configuration);
105-
$this->versionFiles = new VersionFilesApi(null, $this->configuration);
106-
$this->users = new UsersApi(null, $this->configuration);
107-
$this->teams = new TeamsApi(null, $this->configuration);
108-
$this->tags = new TagsApi(null, $this->configuration);
109-
$this->misc = new MiscApi(null, $this->configuration);
110-
$this->notifications = new NotificationsApi(null, $this->configuration);
111-
$this->threads = new ThreadsApi(null, $this->configuration);
108+
$this->projects = new ProjectsApi($this->httpClient, $this->configuration);
109+
$this->versions = new VersionsApi($this->httpClient, $this->configuration);
110+
$this->versionFiles = new VersionFilesApi($this->httpClient, $this->configuration);
111+
$this->users = new UsersApi($this->httpClient, $this->configuration);
112+
$this->teams = new TeamsApi($this->httpClient, $this->configuration);
113+
$this->tags = new TagsApi($this->httpClient, $this->configuration);
114+
$this->misc = new MiscApi($this->httpClient, $this->configuration);
115+
$this->notifications = new NotificationsApi($this->httpClient, $this->configuration);
116+
$this->threads = new ThreadsApi($this->httpClient, $this->configuration);
112117

113118
return $this;
114119
}
@@ -136,6 +141,18 @@ public function setApiToken(?string $token): static
136141
return $this->setConfiguration($this->configuration);
137142
}
138143

144+
/**
145+
* Set the HTTP client used for all requests.
146+
* When null, the default HTTP client from Guzzle will be used.
147+
* @param ClientInterface|null $httpClient
148+
* @return $this
149+
*/
150+
public function setHttpClient(?ClientInterface $httpClient): static
151+
{
152+
$this->httpClient = $httpClient;
153+
return $this->setConfiguration($this->configuration);
154+
}
155+
139156
/**
140157
* Search projects
141158
* @param ProjectSearchOptions|null $options

0 commit comments

Comments
 (0)