koel/app/Services/ApiClients/ApiClient.php

152 lines
4.8 KiB
PHP
Raw Normal View History

<?php
2022-08-08 16:00:59 +00:00
namespace App\Services\ApiClients;
2022-08-08 16:00:59 +00:00
use GuzzleHttp\Client as GuzzleHttpClient;
use GuzzleHttp\Promise\Promise;
use Illuminate\Support\Str;
use InvalidArgumentException;
2018-08-24 15:27:19 +00:00
use SimpleXMLElement;
use Webmozart\Assert\Assert;
/**
2022-08-08 16:00:59 +00:00
* @method mixed get(string $uri, array $data = [], bool $appendKey = true)
* @method mixed post($uri, array $data = [], bool $appendKey = true)
* @method mixed put($uri, array $data = [], bool $appendKey = true)
* @method mixed patch($uri, array $data = [], bool $appendKey = true)
* @method mixed head($uri, array $data = [], bool $appendKey = true)
* @method mixed delete($uri, array $data = [], bool $appendKey = true)
* @method Promise getAsync(string $uri, array $data = [], bool $appendKey = true)
* @method Promise postAsync($uri, array $data = [], bool $appendKey = true)
* @method Promise putAsync($uri, array $data = [], bool $appendKey = true)
* @method Promise patchAsync($uri, array $data = [], bool $appendKey = true)
* @method Promise headAsync($uri, array $data = [], bool $appendKey = true)
* @method Promise deleteAsync($uri, array $data = [], bool $appendKey = true)
*/
2022-07-29 06:47:10 +00:00
abstract class ApiClient
{
private const MAGIC_METHODS = [
'get',
'post',
'put',
'patch',
'head',
'delete',
'getAsync',
'postAsync',
'putAsync',
'patchAsync',
'headAsync',
'deleteAsync',
];
2021-06-05 10:47:56 +00:00
protected string $responseFormat = 'json';
/**
* The query parameter name for the key.
* For example, Last.fm use api_key, like this:
* https://ws.audioscrobbler.com/2.0?method=artist.getInfo&artist=Kamelot&api_key=API_KEY.
*/
2021-06-05 10:47:56 +00:00
protected string $keyParam = 'key';
2022-08-08 16:00:59 +00:00
public function __construct(protected GuzzleHttpClient $wrapped)
{
}
/**
* Make a request to the API.
*
2021-06-05 10:47:56 +00:00
* @param string $method The HTTP method
* @param string $uri The API URI (segment)
* @param bool $appendKey Whether to automatically append the API key into the URI.
2018-08-29 07:07:44 +00:00
* While it's usually the case, some services (like Last.fm) requires
* an "API signature" of the request. Appending an API key will break the request.
2021-06-05 10:47:56 +00:00
* @param array $params An array of parameters
*
*/
2022-08-08 16:00:59 +00:00
public function request(string $method, string $uri, bool $appendKey = true, array $params = []): mixed
{
2022-08-08 16:00:59 +00:00
return attempt(function () use ($method, $uri, $appendKey, $params) {
$body = (string) $this->wrapped
2018-08-24 15:27:19 +00:00
->$method($this->buildUrl($uri, $appendKey), ['form_params' => $params])
->getBody();
if ($this->responseFormat === 'json') {
return json_decode($body);
}
if ($this->responseFormat === 'xml') {
return simplexml_load_string($body);
}
return $body;
2022-08-08 16:00:59 +00:00
});
}
public function requestAsync(string $method, string $uri, bool $appendKey = true, array $params = []): Promise
{
2022-08-08 16:00:59 +00:00
return $this->wrapped->$method($this->buildUrl($uri, $appendKey), ['form_params' => $params]);
}
/**
* Make an HTTP call to the external resource.
*
2021-06-05 10:47:56 +00:00
* @param string $method The HTTP method
2020-12-22 20:11:22 +00:00
* @param array<mixed> $args An array of parameters
*
2021-06-05 10:47:56 +00:00
* @return mixed|SimpleXMLElement|void
* @throws InvalidArgumentException
2016-08-03 10:42:39 +00:00
*
*/
2021-06-05 10:47:56 +00:00
public function __call(string $method, array $args) // @phpcs:ignore
{
Assert::inArray($method, self::MAGIC_METHODS);
if (count($args) < 1) {
throw new InvalidArgumentException('Magic request methods require a URI and optional options array');
}
$uri = $args[0];
$params = $args[1] ?? [];
2020-12-22 20:11:22 +00:00
$appendKey = $args[2] ?? true;
if (Str::endsWith($method, 'Async')) {
return $this->requestAsync($method, $uri, $appendKey, $params);
} else {
return $this->request($method, $uri, $appendKey, $params);
}
}
/**
* Turn a URI segment into a full API URL.
*
2020-09-06 21:20:42 +00:00
* @param bool $appendKey whether to automatically append the API key into the URL
*/
2018-08-24 15:27:19 +00:00
public function buildUrl(string $uri, bool $appendKey = true): string
{
if (!starts_with($uri, ['http://', 'https://'])) {
2016-08-16 15:12:11 +00:00
if ($uri[0] !== '/') {
$uri = "/$uri";
}
2020-12-22 20:11:22 +00:00
$uri = $this->getEndpoint() . $uri;
}
2015-12-20 12:17:35 +00:00
if ($appendKey) {
if (parse_url($uri, PHP_URL_QUERY)) {
$uri .= "&$this->keyParam=" . $this->getKey();
2015-12-20 12:17:35 +00:00
} else {
$uri .= "?$this->keyParam=" . $this->getKey();
2015-12-20 12:17:35 +00:00
}
}
return $uri;
}
2018-08-24 15:27:19 +00:00
abstract public function getKey(): ?string;
2018-08-24 15:27:19 +00:00
abstract public function getSecret(): ?string;
2018-08-29 10:36:05 +00:00
abstract public function getEndpoint(): ?string;
}