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
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ 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.

## Requirements
- PHP 8.4 or newer
Expand Down Expand Up @@ -88,7 +89,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 +127,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 Down Expand Up @@ -218,7 +219,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 Down
34 changes: 11 additions & 23 deletions src/Client/CompactProject.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,27 @@
* @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();
}

/**
Expand Down Expand Up @@ -111,7 +99,7 @@ public function getDailyStats(?DateTime $from = null, ?DateTime $to = null): arr
{
return $this->client->getDailyProjectStats(
$this->getId(),
$from ?? $this->getData()->getCreatedAt(),
$from ?? $this->getCreatedAt(),
$to
);
}
Expand Down Expand Up @@ -143,7 +131,7 @@ public function getMembers(): ProjectMemberList
*/
public function getOwner(): User
{
return $this->client->getUser($this->project->getNamespace()->getOwner());
return $this->client->getUser($this->getNamespace()->getOwner());
}

/**
Expand Down
37 changes: 13 additions & 24 deletions src/Client/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Aternos\HangarApi\Client\Options\VersionSearch\VersionSearchOptions;
use Aternos\HangarApi\Model\DayProjectStats;
use Aternos\HangarApi\Model\Platform;
use Aternos\HangarApi\Model\Project as ProjectModel;
use DateTime;

/**
Expand All @@ -17,39 +18,27 @@
* @package Aternos\HangarApi\Client
* @description This class wraps a full project on hangar and allows you to fetch additional data.
*/
class Project
class Project extends ProjectModel
{
public function __construct(
protected HangarAPIClient $client,
protected \Aternos\HangarApi\Model\Project $project,
)
{
}

/**
* @return \Aternos\HangarApi\Model\Project
* @param HangarAPIClient $client
* @param ProjectModel $data
*/
public function getData(): \Aternos\HangarApi\Model\Project
public function __construct(
protected HangarAPIClient $client,
ProjectModel $data,
)
{
return $this->project;
parent::__construct($data->container);
}

/**
* 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();
}

/**
Expand Down Expand Up @@ -101,7 +90,7 @@ public function getDailyStats(?DateTime $from = null, ?DateTime $to = null): arr
{
return $this->client->getDailyProjectStats(
$this->getId(),
$from ?? $this->getData()->getCreatedAt(),
$from ?? $this->getCreatedAt(),
$to
);
}
Expand Down Expand Up @@ -133,7 +122,7 @@ public function getMembers(): ProjectMemberList
*/
public function getOwner(): User
{
return $this->client->getUser($this->project->getNamespace()->getOwner());
return $this->client->getUser($this->getNamespace()->getOwner());
}

/**
Expand Down
27 changes: 12 additions & 15 deletions src/Client/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,26 @@
use Aternos\HangarApi\Client\List\CompactProject\WatchedProjectList;
use Aternos\HangarApi\Client\List\ProjectList;
use Aternos\HangarApi\Client\Options\ProjectSearch\ProjectSearchOptions;
use Aternos\HangarApi\Model\User as UserModel;

/**
* Class User
*
* @package Aternos\HangarApi\Client
* @description This class wraps a hangar user and allows you to fetch additional data.
*/
class User
class User extends UserModel
{
/**
* @param HangarAPIClient $client
* @param UserModel $data
*/
public function __construct(
protected HangarAPIClient $client,
protected \Aternos\HangarApi\Model\User $user,
UserModel $data,
)
{
}

/**
* Get the user data
* @return \Aternos\HangarApi\Model\User
*/
public function getData(): \Aternos\HangarApi\Model\User
{
return $this->user;
parent::__construct($data->container);
}

/**
Expand All @@ -39,7 +36,7 @@ public function getData(): \Aternos\HangarApi\Model\User
*/
public function getWatchedProjects(): WatchedProjectList
{
return $this->client->getProjectsWatchedByUser($this->user->getId());
return $this->client->getProjectsWatchedByUser($this->getId());
}

/**
Expand All @@ -49,7 +46,7 @@ public function getWatchedProjects(): WatchedProjectList
*/
public function getStarredProjects(): StarredProjectList
{
return $this->client->getProjectsStarredByUser($this->user->getId());
return $this->client->getProjectsStarredByUser($this->getId());
}

/**
Expand All @@ -59,7 +56,7 @@ public function getStarredProjects(): StarredProjectList
*/
public function getPinnedProjects(): array
{
return $this->client->getProjectsPinnedByUser($this->user->getId());
return $this->client->getProjectsPinnedByUser($this->getId());
}

/**
Expand All @@ -71,7 +68,7 @@ public function getPinnedProjects(): array
public function getProjects(): ProjectList
{
$options = new ProjectSearchOptions();
$options->setOwner($this->user->getName());
$options->setOwner($this->getName());
return $this->client->getProjects($options);
}
}
30 changes: 15 additions & 15 deletions src/Client/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Aternos\HangarApi\Client;

use Aternos\HangarApi\ApiException;
use Aternos\HangarApi\Model\Version as VersionModel;
use Aternos\HangarApi\Model\VersionStats;
use DateTime;

Expand All @@ -12,15 +13,22 @@
* @package Aternos\HangarApi\Client
* @description This class wraps a version of a project on hangar and allows you to fetch additional data.
*/
class Version
class Version extends VersionModel
{
/**
* @param HangarAPIClient $client
* @param VersionModel $data
* @param string $projectSlugOrId slug or id of the project this version belongs to
* @param Project|null $project the project this version belongs to, if it is already known
*/
public function __construct(
protected HangarAPIClient $client,
protected \Aternos\HangarApi\Model\Version $version,
protected string $projectSlugOrId,
protected ?Project $project = null,
protected HangarAPIClient $client,
VersionModel $data,
protected string $projectSlugOrId,
protected ?Project $project = null,
)
{
parent::__construct($data->container);
}

/**
Expand All @@ -47,14 +55,6 @@ public function getProject(): Project
return $this->project;
}

/**
* @return \Aternos\HangarApi\Model\Version
*/
public function getData(): \Aternos\HangarApi\Model\Version
{
return $this->version;
}

/**
* Fetch a daily list of statistics for this version
* Days without downloads/views are not be included
Expand All @@ -66,8 +66,8 @@ public function getData(): \Aternos\HangarApi\Model\Version
public function getDailyStats(?DateTime $from = null, ?DateTime $to = null): array
{
return $this->client->getDailyVersionStatsById(
$this->getData()->getId(),
$from ?? $this->getData()->getCreatedAt(),
$this->getId(),
$from ?? $this->getCreatedAt(),
$to
);
}
Expand Down
Loading
Loading