aternos / hangar-api
Hangar API 的 PHP 客户端。此客户端基于 openapi 规范。
v3.3.1
2024-09-03 15:24 UTC
Requires
- php: ^8.1
- ext-curl: *
- ext-json: *
- ext-mbstring: *
- guzzlehttp/guzzle: ^7.3
- guzzlehttp/psr7: ^1.7 || ^2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.5
- phpunit/phpunit: ^8.0 || ^9.0
README
PHP 编写的 Hangar API 的 API 客户端。此客户端是 OpenAPI 生成器生成的代码和一些用于提高可用性的包装器的组合。
生成的代码位于 lib/Api
和 lib/Model
中。建议使用 lib/Client
中的包装器而不是生成的代码。
安装
通过 composer 安装此包
composer require aternos/hangar-api
用法
API 的主要入口点是 HangarAPIClient
类。
<?php use Aternos\HangarApi\Client\HangarAPIClient; // create an API client. This is the main entry point for the API $hangarClient = new HangarAPIClient(); // set a user agent (recommended) $hangarClient->setUserAgent('aternos/php-hangar-api-example'); // set an api key (optional) $hangarClient->setApiKey("api-key");
API 密钥仅用于非公开请求,但如果提供,它将用于所有请求。
结果列表
大多数方法返回分页的结果列表,包含当前页面的结果列表以及导航到下一页和上一页的方法。结果列表实现了 Iterator
、ArrayAccess
和 Countable
接口,因此您可以像使用数组一样使用它。它还有一个 getResults()
方法,返回结果的基础数组。
搜索项目
$projects = $hangarClient->getProjects(); foreach ($project as $project) { // like most other methods, this method returns a wrapper // you can use the getData() method to get the project data echo $project->getData()->getName() . PHP_EOL; } $projects = $projects->getNextPage(); foreach ($projects as $project) { echo $project->getData()->getName() . PHP_EOL; }
使用选项搜索项目
在搜索项目时,您可以应用过滤器并更改排序顺序。所有选项都是可选的,可以组合使用。
use \Aternos\HangarApi\Client\Options\ProjectSearch\ProjectSearchOptions; use \Aternos\HangarApi\Client\Options\ProjectCategory; use \Aternos\HangarApi\Client\Options\ProjectSearch\ProjectSortField; $options = new ProjectSearchOptions(); $options->setCategory(ProjectCategory::ADMIN_TOOLS); $options->setQuery("mclogs"); $options->setSortField(ProjectSortField::UPDATED); $projects = $hangarClient->getProjects($options);
获取附加项目数据
项目包装器提供了获取有关项目附加数据的方法。
// get a specific project $project = $hangarClient->getProject("mclogs"); // get versions of the project (paginated) $versions = $project->getVersions(); // get a specific version $version = $project->getVersion("2.6.2"); // get the owner of the project $owner = $project->getOwner(); // get the members of the project (paginated) $members = $project->getMembers(); // get the people who starred the project (paginated) $stargazers = $project->getStargazers(); // get the people who are watching the project (paginated) $watchers = $project->getWatchers();
版本
// get versions of a project by name (paginated) $versions = $hangarClient->getProjectVersions("mclogs"); // get the versions from a project (paginated) $versions = $project->getVersions(); // get a specific version of a project by name $version = $hangarClient->getProjectVersion("mclogs", "2.6.2"); // get a specific version of a project $version = $project->getVersion("2.6.2"); // get the daily stats of the version $stats = $version->getDailyStats(); foreach ($stats as $date => $stat) { echo $stat->getData()->getDownloads() . " Downloads and on " $date . PHP_EOL; }
用户
// get a user $user = $hangarClient->getUser("Aternos"); // get all projects of a user (paginated) $projects = $user->getProjects(); // get the projects a user has starred (paginated) $starredProjects = $user->getStarredProjects(); // get the projects a user is watching (paginated) $watchedProjects = $user->getWatchedProjects();
项目页面
// get the main page of a project $page = $hangarClient->getProjectMainPage("mclogs"); // get other pages $page = $hangarClient->getProjectPage("mclogs", "Config"); // get a page from a project $page = $project->getPage("Config"); // edit a page $page->setContent("New content"); $page->save();
更新生成的代码
可以通过安装 openapi generator 并运行以下命令来更新生成的代码:
openapi-generator-cli generate -c config.yaml