56 lines
1.3 KiB
PHP
56 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Modules\Lemmy;
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Http\Client\Response;
|
|
|
|
class LemmyRequest
|
|
{
|
|
private string $instance;
|
|
private ?string $token;
|
|
|
|
public function __construct(string $instance, ?string $token = null)
|
|
{
|
|
$this->instance = $instance;
|
|
$this->token = $token;
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $params
|
|
*/
|
|
public function get(string $endpoint, array $params = []): Response
|
|
{
|
|
$url = "https://{$this->instance}/api/v3/{$endpoint}";
|
|
|
|
$request = Http::timeout(30);
|
|
|
|
if ($this->token) {
|
|
$request = $request->withToken($this->token);
|
|
}
|
|
|
|
return $request->get($url, $params);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $data
|
|
*/
|
|
public function post(string $endpoint, array $data = []): Response
|
|
{
|
|
$url = "https://{$this->instance}/api/v3/{$endpoint}";
|
|
|
|
$request = Http::timeout(30);
|
|
|
|
if ($this->token) {
|
|
$request = $request->withToken($this->token);
|
|
}
|
|
|
|
return $request->post($url, $data);
|
|
}
|
|
|
|
public function withToken(string $token): self
|
|
{
|
|
$this->token = $token;
|
|
return $this;
|
|
}
|
|
}
|