2016-09-06 15:26:39 +05:30
|
|
|
<?php
|
2019-05-13 22:09:11 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-12-29 20:25:21 +05:30
|
|
|
namespace common\components;
|
2016-09-06 15:26:39 +05:30
|
|
|
|
2019-05-13 22:09:11 +05:30
|
|
|
use GuzzleHttp\ClientInterface;
|
2021-03-03 19:34:42 +05:30
|
|
|
use Webmozart\Assert\Assert;
|
2016-09-06 15:26:39 +05:30
|
|
|
use Yii;
|
|
|
|
|
2019-05-13 22:09:11 +05:30
|
|
|
// TODO: convert to complete Chrly client library
|
2019-12-29 20:25:21 +05:30
|
|
|
class SkinsSystemApi {
|
2016-09-06 15:26:39 +05:30
|
|
|
|
2021-03-04 10:28:07 +05:30
|
|
|
private string $baseDomain;
|
2016-09-06 15:26:39 +05:30
|
|
|
|
2021-03-03 19:34:42 +05:30
|
|
|
private ?ClientInterface $client = null;
|
2019-05-13 22:09:11 +05:30
|
|
|
|
2021-03-04 10:28:07 +05:30
|
|
|
public function __construct(string $baseDomain) {
|
|
|
|
$this->baseDomain = $baseDomain;
|
|
|
|
}
|
|
|
|
|
2016-12-23 04:20:34 +05:30
|
|
|
/**
|
|
|
|
* @param string $username
|
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2019-05-13 22:09:11 +05:30
|
|
|
public function textures(string $username): ?array {
|
|
|
|
$response = $this->getClient()->request('GET', $this->buildUrl('/textures/' . $username));
|
2020-10-12 02:59:54 +05:30
|
|
|
if ($response->getStatusCode() !== 200) {
|
2019-05-13 22:09:11 +05:30
|
|
|
return null;
|
|
|
|
}
|
2016-09-06 15:26:39 +05:30
|
|
|
|
2019-05-13 23:30:17 +05:30
|
|
|
return json_decode($response->getBody()->getContents(), true);
|
2016-09-06 15:26:39 +05:30
|
|
|
}
|
|
|
|
|
2021-03-03 19:34:42 +05:30
|
|
|
/**
|
|
|
|
* @param string $username
|
|
|
|
*
|
|
|
|
* @return array|null
|
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException
|
|
|
|
*/
|
|
|
|
public function profile(string $username, bool $signed = false): ?array {
|
|
|
|
$url = "/profile/{$username}";
|
|
|
|
if ($signed) {
|
|
|
|
$url .= '?unsigned=false';
|
|
|
|
}
|
|
|
|
|
|
|
|
$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 22:09:11 +05:30
|
|
|
public function setClient(ClientInterface $client): void {
|
|
|
|
$this->client = $client;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function buildUrl(string $url): string {
|
2021-03-04 10:28:07 +05:30
|
|
|
return $this->baseDomain . $url;
|
2016-09-06 15:26:39 +05:30
|
|
|
}
|
|
|
|
|
2019-05-13 22:09:11 +05:30
|
|
|
private function getClient(): ClientInterface {
|
|
|
|
if ($this->client === null) {
|
|
|
|
$this->client = Yii::$container->get(ClientInterface::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->client;
|
2016-09-06 15:26:39 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|