2016-09-06 12:56:39 +03:00
|
|
|
<?php
|
2019-05-13 19:39:11 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-12-29 17:55:21 +03:00
|
|
|
namespace common\components;
|
2016-09-06 12:56:39 +03:00
|
|
|
|
2019-05-13 19:39:11 +03:00
|
|
|
use GuzzleHttp\ClientInterface;
|
2021-03-03 15:04:42 +01:00
|
|
|
use Webmozart\Assert\Assert;
|
2016-09-06 12:56:39 +03:00
|
|
|
use Yii;
|
|
|
|
|
2019-05-13 19:39:11 +03:00
|
|
|
// TODO: convert to complete Chrly client library
|
2019-12-29 17:55:21 +03:00
|
|
|
class SkinsSystemApi {
|
2016-09-06 12:56:39 +03:00
|
|
|
|
2021-03-03 15:04:42 +01:00
|
|
|
private ?ClientInterface $client = null;
|
2019-05-13 19:39:11 +03:00
|
|
|
|
2024-12-02 15:10:55 +05:00
|
|
|
public function __construct(private readonly string $baseDomain) {
|
2021-03-04 05:58:07 +01:00
|
|
|
}
|
|
|
|
|
2016-12-23 01:50:34 +03:00
|
|
|
/**
|
|
|
|
* @param string $username
|
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2019-05-13 19:39:11 +03:00
|
|
|
public function textures(string $username): ?array {
|
|
|
|
$response = $this->getClient()->request('GET', $this->buildUrl('/textures/' . $username));
|
2020-10-12 00:29:54 +03:00
|
|
|
if ($response->getStatusCode() !== 200) {
|
2019-05-13 19:39:11 +03:00
|
|
|
return null;
|
|
|
|
}
|
2016-09-06 12:56:39 +03:00
|
|
|
|
2019-05-13 21:00:17 +03:00
|
|
|
return json_decode($response->getBody()->getContents(), true);
|
2016-09-06 12:56:39 +03:00
|
|
|
}
|
|
|
|
|
2021-03-03 15:04:42 +01:00
|
|
|
/**
|
|
|
|
* @param string $username
|
|
|
|
*
|
|
|
|
* @return array|null
|
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException
|
|
|
|
*/
|
2024-06-11 03:50:10 +02:00
|
|
|
public function profile(string $username, bool $signed = false, ?string $fallbackUuid = null): ?array {
|
|
|
|
$query = [];
|
2021-03-03 15:04:42 +01:00
|
|
|
if ($signed) {
|
2024-06-11 03:50:10 +02:00
|
|
|
$query['unsigned'] = 'false';
|
2021-03-03 15:04:42 +01:00
|
|
|
}
|
|
|
|
|
2024-06-11 03:50:10 +02:00
|
|
|
if ($fallbackUuid !== null) {
|
|
|
|
$query['onUnknownProfileRespondWithUuid'] = $fallbackUuid;
|
|
|
|
}
|
|
|
|
|
2024-06-11 04:16:36 +02:00
|
|
|
$url = "/profile/{$username}" . (empty($query) ? '' : ('?' . http_build_query($query)));
|
2021-03-03 15:04:42 +01:00
|
|
|
$response = $this->getClient()->request('GET', $this->buildUrl($url));
|
|
|
|
if ($response->getStatusCode() !== 200) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return json_decode($response->getBody()->getContents(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param 'pem'|'der' $format
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException
|
|
|
|
*/
|
|
|
|
public function getSignatureVerificationKey(string $format = 'pem'): string {
|
|
|
|
Assert::inArray($format, ['pem', 'der']);
|
|
|
|
|
|
|
|
return $this->getClient()
|
|
|
|
->request('GET', $this->buildUrl("/signature-verification-key.{$format}"))
|
|
|
|
->getBody()
|
|
|
|
->getContents();
|
|
|
|
}
|
|
|
|
|
2019-05-13 19:39:11 +03:00
|
|
|
public function setClient(ClientInterface $client): void {
|
|
|
|
$this->client = $client;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function buildUrl(string $url): string {
|
2021-03-04 05:58:07 +01:00
|
|
|
return $this->baseDomain . $url;
|
2016-09-06 12:56:39 +03:00
|
|
|
}
|
|
|
|
|
2019-05-13 19:39:11 +03:00
|
|
|
private function getClient(): ClientInterface {
|
|
|
|
if ($this->client === null) {
|
|
|
|
$this->client = Yii::$container->get(ClientInterface::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->client;
|
2016-09-06 12:56:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|