Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"homepage": "https://github.com/aternosorg/php-modrinth-api",
"require": {
"php": "^8.4.1",
"composer-runtime-api": "^2.0",
"ext-curl": "*",
"ext-json": "*",
"ext-mbstring": "*",
Expand Down
3 changes: 2 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 32 additions & 1 deletion src/Client/ModrinthAPIClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
use Aternos\ModrinthApi\Model\User as UserModel;
use Aternos\ModrinthApi\Model\UserPayoutHistory;
use Aternos\ModrinthApi\Model\Version as VersionModel;
use Composer\InstalledVersions;
use OutOfBoundsException;
use Psr\Http\Client\ClientInterface;


Expand All @@ -58,6 +60,11 @@
*/
class ModrinthAPIClient
{
/**
* Name of this package on packagist, used to look up the installed version
*/
public const string PACKAGE_NAME = "aternos/modrinth-api";

protected ?ClientInterface $httpClient;

protected Configuration $configuration;
Expand Down Expand Up @@ -92,10 +99,34 @@ public function __construct(?string $apiToken = null, ?Configuration $configurat
{
$this->httpClient = $httpClient;
$this->configuration = $configuration ?? (Configuration::getDefaultConfiguration())
->setUserAgent("php-modrinth-api/2.0.0");
->setUserAgent(static::getDefaultUserAgent());
$this->setApiToken($apiToken);
}

/**
* Get the installed version of this library, e.g. "4.0.0" or "dev-master"
* Returns "unknown" if the version cannot be determined, e.g. if the library was not installed using composer.
* @return string
*/
public static function getLibraryVersion(): string
{
try {
return InstalledVersions::getPrettyVersion(static::PACKAGE_NAME) ?? "unknown";
} catch (OutOfBoundsException) {
return "unknown";
}
}

/**
* Get the user agent that identifies this library, e.g. "aternos/php-modrinth-api/4.0.0"
* It is used unless a different user agent is set with {@see setUserAgent()}.
* @return string
*/
public static function getDefaultUserAgent(): string
{
return "aternos/php-modrinth-api/" . static::getLibraryVersion();
}

/**
* @param Configuration $configuration
* @return $this
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Client/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ClientTest extends TestCase
public function setUp(): void
{
$this->apiClient = new ModrinthAPIClient();
$this->apiClient->setUserAgent("aternos/php-modrinth-api@2.0.0 (contact@aternos.org)");
$this->apiClient->setUserAgent(ModrinthAPIClient::getDefaultUserAgent() . " (contact@aternos.org)");
}

protected function assertValidProjectList($list): void
Expand Down
34 changes: 32 additions & 2 deletions tests/Unit/Client/ModrinthAPIClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
use Aternos\ModrinthApi\Client\User;
use Aternos\ModrinthApi\Configuration;
use Aternos\ModrinthApi\Model\UserPayoutHistory;
use Composer\InstalledVersions;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\Attributes\DataProvider;

Expand All @@ -31,7 +33,35 @@ public function testConstructorUsesDefaultConfiguration(): void
$client = new ModrinthAPIClient();

$this->assertInstanceOf(ModrinthAPIClient::class, $client);
$this->assertStringStartsWith("php-modrinth-api/", Configuration::getDefaultConfiguration()->getUserAgent());
$this->assertEquals(ModrinthAPIClient::getDefaultUserAgent(), Configuration::getDefaultConfiguration()->getUserAgent());
}

public function testGetLibraryVersionReturnsInstalledVersion(): void
{
$this->assertEquals(
InstalledVersions::getPrettyVersion(ModrinthAPIClient::PACKAGE_NAME),
ModrinthAPIClient::getLibraryVersion()
);
$this->assertNotEquals("unknown", ModrinthAPIClient::getLibraryVersion());
}

public function testDefaultUserAgentContainsPackageAndVersion(): void
{
$this->assertEquals(
"aternos/php-modrinth-api/" . ModrinthAPIClient::getLibraryVersion(),
ModrinthAPIClient::getDefaultUserAgent()
);
}

public function testDefaultUserAgentIsSentWithRequests(): void
{
$responses = [$this->fixtureResponse("get_project_response")];
$stack = HandlerStack::create(new MockHandler($responses));
$stack->push(Middleware::history($this->requests));
$client = new ModrinthAPIClient(null, null, new Client(['handler' => $stack]));

$client->getProject("modmenu");
$this->assertEquals(ModrinthAPIClient::getDefaultUserAgent(), $this->getRequest()->getHeaderLine("User-Agent"));
}

/**
Expand All @@ -52,7 +82,7 @@ public function testClientsWithoutExplicitConfigurationShareOne(): void

// reset the shared configuration so that it cannot affect other tests
Configuration::getDefaultConfiguration()
->setUserAgent("php-modrinth-api/2.0.0")
->setUserAgent(ModrinthAPIClient::getDefaultUserAgent())
->setApiKey("Authorization", null);
}

Expand Down
Loading