Skip to content

Commit 98126b2

Browse files
committed
Add wrapper client
1 parent 20c1db9 commit 98126b2

File tree

6 files changed

+412
-0
lines changed

6 files changed

+412
-0
lines changed

src/Client/BaseProject.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace Aternos\BlobBuild\Client;
4+
5+
use Aternos\BlobBuild\ApiException;
6+
use Aternos\BlobBuild\Model\ProjectDefaultReleaseChannel;
7+
8+
abstract class BaseProject
9+
{
10+
public function __construct(
11+
protected BlobBuildAPIClient $client,
12+
)
13+
{
14+
}
15+
16+
protected abstract function getName(): string;
17+
18+
protected abstract function getDefaultReleaseChannel(): ProjectDefaultReleaseChannel;
19+
20+
/**
21+
* Get the URL of the project page on blob.build
22+
* @return string
23+
*/
24+
public function getPageUrl(): string
25+
{
26+
return "https://blob.build/project/" . $this->getName();
27+
}
28+
29+
/**
30+
* Get all builds of this project
31+
* @return array<string, Build[]> Map of release channels to builds in that channel
32+
* @throws ApiException
33+
*/
34+
public function getBuilds(): array
35+
{
36+
return $this->client->getProjectBuilds($this->getName());
37+
}
38+
39+
/**
40+
* Get the latest build of this project in a specific channel
41+
* @param string $channelName
42+
* @return Build
43+
* @throws ApiException
44+
*/
45+
public function getLatestBuildInChannel(string $channelName): Build
46+
{
47+
return $this->client->getLatestProjectBuildInChannel($this->getName(), $channelName);
48+
}
49+
50+
/**
51+
* Get the latest build of this project in its default channel
52+
* @return Build
53+
* @throws ApiException
54+
*/
55+
public function getLatestBuildInDefaultChannel(): Build
56+
{
57+
return $this->getLatestBuildInChannel($this->getDefaultReleaseChannel()->getName());
58+
}
59+
60+
/**
61+
* Get a specific build of this project in a specific channel by its build number
62+
* @param string $channel
63+
* @param int $buildNumber
64+
* @return Build
65+
* @throws ApiException
66+
*/
67+
public function getBuild(string $channel, int $buildNumber): Build
68+
{
69+
return $this->client->getProjectBuild($this->getName(), $channel, $buildNumber);
70+
}
71+
}

src/Client/BlobBuildAPIClient.php

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
<?php
2+
3+
namespace Aternos\BlobBuild\Client;
4+
5+
use Aternos\BlobBuild\Api\BuildsApi;
6+
use Aternos\BlobBuild\Api\ProjectsApi;
7+
use Aternos\BlobBuild\Api\UsersApi;
8+
use Aternos\BlobBuild\ApiException;
9+
use Aternos\BlobBuild\Configuration;
10+
use Aternos\BlobBuild\Model\Build as BuildModel;
11+
use Aternos\BlobBuild\Model\ProjectListItem as ProjectListItemModel;
12+
use Aternos\BlobBuild\Model\SearchRequest;
13+
use GuzzleHttp\ClientInterface;
14+
15+
class BlobBuildAPIClient
16+
{
17+
protected Configuration $configuration;
18+
protected ?ClientInterface $httpClient;
19+
protected UsersApi $users;
20+
protected ProjectsApi $projects;
21+
protected BuildsApi $builds;
22+
23+
public function __construct(
24+
?Configuration $configuration = null,
25+
?ClientInterface $httpClient = null,
26+
?string $userAgent = null,
27+
?string $apiToken = null,
28+
)
29+
{
30+
$this->httpClient = $httpClient;
31+
if ($configuration === null) {
32+
$configuration = new Configuration();
33+
$configuration->setUserAgent($userAgent ?? "php-blob-build-api/1.0.0");
34+
if ($apiToken !== null) {
35+
$configuration->setAccessToken($apiToken);
36+
}
37+
}
38+
$this->setConfiguration($configuration);
39+
}
40+
41+
/**
42+
* Set the configuration used for all HTTP requests
43+
*
44+
* @param Configuration $configuration
45+
* @return $this
46+
*/
47+
public function setConfiguration(Configuration $configuration): static
48+
{
49+
$this->configuration = $configuration;
50+
$this->users = new UsersApi($this->httpClient, $this->configuration);
51+
$this->projects = new ProjectsApi($this->httpClient, $this->configuration);
52+
$this->builds = new BuildsApi($this->httpClient, $this->configuration);
53+
return $this;
54+
}
55+
56+
/**
57+
* Set the user agent used for all HTTP requests
58+
*
59+
* @param string $userAgent
60+
* @return $this
61+
*/
62+
public function setUserAgent(string $userAgent): static
63+
{
64+
$this->configuration->setUserAgent($userAgent);
65+
return $this->setConfiguration($this->configuration);
66+
}
67+
68+
/**
69+
* Set the api token used for all HTTP requests
70+
*
71+
* @param string $token
72+
* @return $this
73+
*/
74+
public function setApiToken(string $token): static
75+
{
76+
$this->configuration->setAccessToken($token);
77+
return $this->setConfiguration($this->configuration);
78+
}
79+
80+
/**
81+
* Set the HTTP client used for all requests.
82+
* When null, the default {@link \GuzzleHttp\Client} will be used.
83+
*
84+
* @param ClientInterface|null $httpClient
85+
* @return $this
86+
*/
87+
public function setHttpClient(?ClientInterface $httpClient): static
88+
{
89+
$this->httpClient = $httpClient;
90+
return $this->setConfiguration($this->configuration);
91+
}
92+
93+
/**
94+
* Get the authenticated user
95+
* @return User
96+
* @throws ApiException
97+
*/
98+
public function getAuthenticatedUser(): User
99+
{
100+
return new User(
101+
$this->users->getAuthenticatedUser()->getData(),
102+
$this,
103+
);
104+
}
105+
106+
/**
107+
* Search for projects by name
108+
* @param string $query
109+
* @return ProjectListItem[]
110+
* @throws ApiException
111+
*/
112+
public function searchProjects(string $query): array
113+
{
114+
$request = (new SearchRequest())->setQuery($query);
115+
return array_map($this->mapProjectListItem(...), $this->projects->searchProjects($request)->getData());
116+
}
117+
118+
/**
119+
* List all projects
120+
* @return ProjectListItem[]
121+
* @throws ApiException
122+
*/
123+
public function listProjects(): array
124+
{
125+
return array_map($this->mapProjectListItem(...), $this->projects->listProjects()->getData());
126+
}
127+
128+
/**
129+
* Get a project by its name
130+
* @param string $name
131+
* @return Project
132+
* @throws ApiException
133+
*/
134+
public function getProject(string $name): Project
135+
{
136+
return new Project($this->projects->getProject($name)->getData(), $this);
137+
}
138+
139+
/**
140+
* Get all builds of a project by its name
141+
* @param string $projectName
142+
* @return array<string, Build[]> Map of release channels to builds in that channel
143+
* @throws ApiException
144+
*/
145+
public function getProjectBuilds(string $projectName): array
146+
{
147+
return array_map(
148+
fn(array $builds) => array_map($this->mapBuild(...), $builds),
149+
$this->builds->getProjectBuilds($projectName)->getData()
150+
);
151+
}
152+
153+
/**
154+
* Get the latest build of a project in a specific channel
155+
* @param string $projectName
156+
* @param string $channel
157+
* @return Build
158+
* @throws ApiException
159+
*/
160+
public function getLatestProjectBuildInChannel(string $projectName, string $channel): Build
161+
{
162+
return $this->mapBuild($this->builds->getLatestProjectBuildInChannel($projectName, $channel)->getData());
163+
}
164+
165+
/**
166+
* Get a specific build of a project in a specific channel by its build number
167+
* @param string $projectName
168+
* @param string $channel
169+
* @param int $buildNumber
170+
* @return Build
171+
* @throws ApiException
172+
*/
173+
public function getProjectBuild(string $projectName, string $channel, int $buildNumber): Build
174+
{
175+
return $this->mapBuild($this->builds->getProjectBuild($projectName, $channel, $buildNumber)->getData());
176+
}
177+
178+
/**
179+
* Map a ProjectModel to a Project
180+
* @param ProjectListItemModel $model
181+
* @return ProjectListItem
182+
*/
183+
protected function mapProjectListItem(ProjectListItemModel $model): ProjectListItem
184+
{
185+
return new ProjectListItem($model, $this);
186+
}
187+
188+
/**
189+
* Map a BuildModel to a Build
190+
* @param BuildModel $model
191+
* @return Build
192+
*/
193+
protected function mapBuild(BuildModel $model): Build
194+
{
195+
return new Build($model, $this);
196+
}
197+
}

src/Client/Build.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Aternos\BlobBuild\Client;
4+
5+
use Aternos\BlobBuild\ApiException;
6+
use Aternos\BlobBuild\Model\Build as BuildModel;
7+
8+
class Build
9+
{
10+
public function __construct(
11+
protected BuildModel $data,
12+
protected BlobBuildAPIClient $client,
13+
)
14+
{
15+
}
16+
17+
/**
18+
* Get the build object as returned by the API
19+
* @return BuildModel
20+
*/
21+
public function getData(): BuildModel
22+
{
23+
return $this->data;
24+
}
25+
26+
/**
27+
* Get the project this build belongs to
28+
* @return Project
29+
* @throws ApiException
30+
*/
31+
public function getProject(): Project
32+
{
33+
return $this->client->getProject($this->data->getProjectName());
34+
}
35+
}

src/Client/Project.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Aternos\BlobBuild\Client;
4+
5+
use Aternos\BlobBuild\Model\ProjectDefaultReleaseChannel;
6+
use Aternos\BlobBuild\Model\Project as ProjectModel;
7+
8+
class Project extends BaseProject
9+
{
10+
public function __construct(
11+
protected ProjectModel $data,
12+
BlobBuildAPIClient $client
13+
)
14+
{
15+
parent::__construct($client);
16+
}
17+
18+
/**
19+
* Get the project object as returned by the API
20+
* @return ProjectModel
21+
*/
22+
public function getData(): ProjectModel
23+
{
24+
return $this->data;
25+
}
26+
27+
protected function getName(): string
28+
{
29+
return $this->data->getName();
30+
}
31+
32+
protected function getDefaultReleaseChannel(): ProjectDefaultReleaseChannel
33+
{
34+
return $this->data->getDefaultReleaseChannel();
35+
}
36+
}

src/Client/ProjectListItem.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Aternos\BlobBuild\Client;
4+
5+
use Aternos\BlobBuild\Model\ProjectDefaultReleaseChannel;
6+
use Aternos\BlobBuild\Model\ProjectListItem as ProjectListItemModel;
7+
8+
class ProjectListItem extends BaseProject
9+
{
10+
public function __construct(
11+
protected ProjectListItemModel $data,
12+
BlobBuildAPIClient $client
13+
)
14+
{
15+
parent::__construct($client);
16+
}
17+
18+
/**
19+
* Get the project object as returned by the API
20+
* @return ProjectListItemModel
21+
*/
22+
public function getData(): ProjectListItemModel
23+
{
24+
return $this->data;
25+
}
26+
27+
protected function getName(): string
28+
{
29+
return $this->data->getName();
30+
}
31+
32+
protected function getDefaultReleaseChannel(): ProjectDefaultReleaseChannel
33+
{
34+
return $this->data->getDefaultReleaseChannel();
35+
}
36+
}

0 commit comments

Comments
 (0)