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
50 changes: 26 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ This client is a combination of code generated by [OpenAPI Generator](https://op
from the Hangar OpenAPI specification and some wrappers around it to improve the usability.

The generated code can be found in `src/Api` and `src/Model`. It is recommended to use the wrappers in
`src/Client` instead. The wrappers provide methods to fetch related data from the API, the data returned
by the API is available through `getData()`.
`src/Client` instead. The wrappers extend the generated models, so the data returned by the API is available
through the getters of the wrapper, e.g. `$project->getName()`. They also provide methods to fetch related data
from the API. These methods start with `fetch`, e.g. `$project->fetchVersions()`, and send a request each time
they are called. Only `fetchProject()` of `Version` and `ProjectPage` keeps the project once it is known.

## Requirements
- PHP 8.4 or newer
Expand Down Expand Up @@ -88,7 +90,7 @@ you can use it like an array. It also has a `getResults()` method which returns
$projects = $hangarClient->getProjects();

foreach ($projects as $project) {
echo $project->getData()->getName() . PHP_EOL;
echo $project->getName() . PHP_EOL;
}

if ($projects->hasNextPage()) {
Expand Down Expand Up @@ -126,7 +128,7 @@ $projects = $hangarClient->getProjects($options);
```php
// get a project by its slug or id
$project = $hangarClient->getProject("mclogs");
echo $project->getData()->getName() . ": " . $project->getData()->getStats()->getDownloads() . " downloads" . PHP_EOL;
echo $project->getName() . ": " . $project->getStats()->getDownloads() . " downloads" . PHP_EOL;

// get the project a file belongs to by the SHA-256 hash of the file
$project = $hangarClient->getProjectByVersionHash(hash_file("sha256", "/path/to/plugin.jar"));
Expand All @@ -138,30 +140,30 @@ The `Project` wrapper provides methods to fetch related data:
$project = $hangarClient->getProject("mclogs");

// versions of the project (paginated), optionally filtered by channel and platform
$versions = $project->getVersions();
$versions = $project->getVersions("Release", Platform::PAPER);
$versions = $project->fetchVersions();
$versions = $project->fetchVersions("Release", Platform::PAPER);

// a specific version by its name
$version = $project->getVersion("3.3.4");
$version = $project->fetchVersion("3.3.4");

// the owner of the project
$owner = $project->getOwner();
$owner = $project->fetchOwner();

// members, stargazers and watchers of the project (paginated)
$members = $project->getMembers();
$stargazers = $project->getStarGazers();
$watchers = $project->getWatchers();
$members = $project->fetchMembers();
$stargazers = $project->fetchStarGazers();
$watchers = $project->fetchWatchers();

// pages of the project
$mainPage = $project->getMainPage();
$page = $project->getPage("config");
$mainPage = $project->fetchMainPage();
$page = $project->fetchPage("config");
```

The daily stats of a project require an API key of a member of the project:
```php
// stats since the project was created, or for a specific period
$stats = $project->getDailyStats();
$stats = $project->getDailyStats(new DateTime("2026-01-01"), new DateTime("2026-02-01"));
$stats = $project->fetchDailyStats();
$stats = $project->fetchDailyStats(new DateTime("2026-01-01"), new DateTime("2026-02-01"));

foreach ($stats as $date => $dayStats) {
echo $date . ": " . $dayStats->getDownloads() . " downloads, " . $dayStats->getViews() . " views" . PHP_EOL;
Expand All @@ -172,8 +174,8 @@ foreach ($stats as $date => $dayStats) {
Some methods, e.g. the projects a user has starred, return `CompactProject` objects. They contain less data,
but provide the same methods as `Project`. You can fetch the full project:
```php
foreach ($hangarClient->getUser("Aternos")->getStarredProjects() as $compactProject) {
$project = $compactProject->getProject();
foreach ($hangarClient->getUser("Aternos")->fetchStarredProjects() as $compactProject) {
$project = $compactProject->fetchProject();
}
```

Expand All @@ -198,12 +200,12 @@ $latestRelease = $hangarClient->getLatestReleaseVersion("mclogs");
$latestVersion = $hangarClient->getLatestVersion("mclogs", "Release");

// get the project of a version
$project = $version->getProject();
$project = $version->fetchProject();
```

The daily stats of a version require an API key:
```php
$stats = $version->getDailyStats();
$stats = $version->fetchDailyStats();

foreach ($stats as $date => $dayStats) {
echo $date . ": " . $dayStats->getTotalDownloads() . " downloads" . PHP_EOL;
Expand All @@ -218,7 +220,7 @@ use Aternos\HangarApi\Client\Options\UserSearch\UserSortField;

// get a user by name
$user = $hangarClient->getUser("Aternos");
echo $user->getData()->getName() . ": " . $user->getData()->getProjectCount() . " projects" . PHP_EOL;
echo $user->getName() . ": " . $user->getProjectCount() . " projects" . PHP_EOL;

// search for users (paginated)
$users = $hangarClient->getUsers(new UserSearchOptions()->setQuery("aternos")->setSort(UserSortField::NAME));
Expand All @@ -228,14 +230,14 @@ $staff = $hangarClient->getStaff(sort: StaffSortField::ROLES);
$authors = $hangarClient->getAuthors("aternos");

// get the projects of a user (paginated)
$projects = $user->getProjects();
$projects = $user->fetchProjects();

// get the projects a user has starred or is watching (paginated)
$starredProjects = $user->getStarredProjects();
$watchedProjects = $user->getWatchedProjects();
$starredProjects = $user->fetchStarredProjects();
$watchedProjects = $user->fetchWatchedProjects();

// get the projects a user has pinned on their profile
$pinnedProjects = $user->getPinnedProjects();
$pinnedProjects = $user->fetchPinnedProjects();
```

## Project Pages
Expand Down
84 changes: 41 additions & 43 deletions src/Client/CompactProject.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,59 +18,49 @@
* @package Aternos\HangarApi\Client
* @description This class wraps a compact project (a project with fewer fields) and allows you to fetch additional data.
*/
class CompactProject
class CompactProject extends ProjectCompact
{
/**
* @param HangarAPIClient $client
* @param ProjectCompact $data
*/
public function __construct(
protected HangarAPIClient $client,
protected ProjectCompact $project,
ProjectCompact $data,
)
{
parent::__construct($data->container);
}

/**
* @return ProjectCompact
*/
public function getData(): ProjectCompact
{
return $this->project;
}

/**
* Get the slug of this project (shorthand for getData()->getNamespace()->getSlug())
* Get the slug of this project (shorthand for getNamespace()->getSlug())
* @return string
*/
public function getSlug(): string
{
return $this->project->getNamespace()->getSlug();
}

/**
* Get the id of this project (shorthand for getData()->getId())
* @return int
*/
public function getId(): int
{
return $this->project->getId();
return $this->getNamespace()->getSlug();
}

/**
* Get the full project data
* Fetch the full project data
* This sends a request to the Hangar API every time it is called.
* @return Project
* @throws ApiException
*/
public function getProject(): Project
public function fetchProject(): Project
{
return $this->client->getProject($this->getId());
}

/**
* Get all versions of this project (paginated)
* Fetch all versions of this project (paginated)
* This sends a request to the Hangar API every time it is called.
* @param string|null $channel
* @param Platform|null $platform
* @return ProjectVersionList
* @throws ApiException
*/
public function getVersions(?string $channel = null, ?Platform $platform = null): ProjectVersionList
public function fetchVersions(?string $channel = null, ?Platform $platform = null): ProjectVersionList
{
$options = new VersionSearchOptions($this->getId());
$options->setChannel($channel);
Expand All @@ -79,90 +69,98 @@ public function getVersions(?string $channel = null, ?Platform $platform = null)
}

/**
* get a single version
* Fetch a single version
* This sends a request to the Hangar API every time it is called.
* @param string $name
* @return Version
* @throws ApiException
*/
public function getVersion(string $name): Version
public function fetchVersion(string $name): Version
{
return $this->client->getVersion($this->getId(), $name);
}

/**
* Get all watchers of this project (paginated)
* Fetch all watchers of this project (paginated)
* This sends a request to the Hangar API every time it is called.
* @return UserList
* @throws ApiException
*/
public function getWatchers(): UserList
public function fetchWatchers(): UserList
{
return $this->client->getProjectWatchers($this->getId());
}

/**
* Get a list of daily project stats
* Fetch a list of daily project stats
* Days without downloads/views are not be included
* This sends a request to the Hangar API every time it is called.
* @param DateTime|null $from default: project creation date
* @param DateTime|null $to default: now
* @return array<string, DayProjectStats>
* @throws ApiException
*/
public function getDailyStats(?DateTime $from = null, ?DateTime $to = null): array
public function fetchDailyStats(?DateTime $from = null, ?DateTime $to = null): array
{
return $this->client->getDailyProjectStats(
$this->getId(),
$from ?? $this->getData()->getCreatedAt(),
$from ?? $this->getCreatedAt(),
$to
);
}

/**
* Get a list of users who starred this project (paginated)
* Fetch a list of users who starred this project (paginated)
* This sends a request to the Hangar API every time it is called.
* @return UserList
* @throws ApiException
*/
public function getStarGazers(): UserList
public function fetchStarGazers(): UserList
{
return $this->client->getProjectStarGazers($this->getId());
}

/**
* Get a list of users who are members of this project (paginated)
* Fetch a list of users who are members of this project (paginated)
* This sends a request to the Hangar API every time it is called.
* @return ProjectMemberList
* @throws ApiException
*/
public function getMembers(): ProjectMemberList
public function fetchMembers(): ProjectMemberList
{
return $this->client->getProjectMembers($this->getId());
}

/**
* Get the owner of this project
* Fetch the owner of this project
* This sends a request to the Hangar API every time it is called.
* @return User
* @throws ApiException
*/
public function getOwner(): User
public function fetchOwner(): User
{
return $this->client->getUser($this->project->getNamespace()->getOwner());
return $this->client->getUser($this->getNamespace()->getOwner());
}

/**
* Get the main page of this project
* Fetch the main page of this project
* This sends a request to the Hangar API every time it is called.
* @return ProjectPage
* @throws ApiException
*/
public function getMainPage(): ProjectPage
public function fetchMainPage(): ProjectPage
{
return $this->client->getProjectMainPage($this->getId());
}

/**
* Get a page of this project
* Fetch a page of this project
* This sends a request to the Hangar API every time it is called.
* @param string $path
* @return ProjectPage
* @throws ApiException
*/
public function getPage(string $path): ProjectPage
public function fetchPage(string $path): ProjectPage
{
return $this->client->getProjectPage($this->getId(), $path);
}
Expand Down
Loading
Loading