2025-06-29 21:20:45 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Modules\Lemmy;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
|
use Illuminate\Http\Client\Response;
|
|
|
|
|
|
|
|
|
|
class LemmyRequest
|
|
|
|
|
{
|
2025-08-09 13:48:25 +02:00
|
|
|
private string $host;
|
|
|
|
|
private string $scheme;
|
2025-06-29 21:20:45 +02:00
|
|
|
private ?string $token;
|
|
|
|
|
|
|
|
|
|
public function __construct(string $instance, ?string $token = null)
|
|
|
|
|
{
|
2025-08-09 02:51:18 +02:00
|
|
|
// Handle both full URLs and just domain names
|
2025-08-09 13:48:25 +02:00
|
|
|
[$this->scheme, $this->host] = $this->parseInstance($instance);
|
2025-06-29 21:20:45 +02:00
|
|
|
$this->token = $token;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-09 02:51:18 +02:00
|
|
|
/**
|
2025-08-09 13:48:25 +02:00
|
|
|
* Parse instance into scheme and host. Defaults to https when scheme missing.
|
|
|
|
|
*
|
|
|
|
|
* @return array{0:string,1:string} [scheme, host]
|
2025-08-09 02:51:18 +02:00
|
|
|
*/
|
2025-08-09 13:48:25 +02:00
|
|
|
private function parseInstance(string $instance): array
|
2025-08-09 02:51:18 +02:00
|
|
|
{
|
2025-08-09 13:48:25 +02:00
|
|
|
$scheme = 'https';
|
|
|
|
|
|
|
|
|
|
// If instance includes a scheme, honor it
|
|
|
|
|
if (preg_match('/^(https?):\/\//i', $instance, $m)) {
|
|
|
|
|
$scheme = strtolower($m[1]);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-09 02:51:18 +02:00
|
|
|
// Remove protocol if present
|
2025-08-09 13:48:25 +02:00
|
|
|
$host = preg_replace('/^https?:\/\//i', '', $instance);
|
2025-08-09 02:51:18 +02:00
|
|
|
// Remove trailing slash if present
|
2025-08-09 13:48:25 +02:00
|
|
|
$host = rtrim($host ?? '', '/');
|
|
|
|
|
|
|
|
|
|
return [$scheme, $host];
|
2025-08-09 02:51:18 +02:00
|
|
|
}
|
|
|
|
|
|
2025-07-07 00:51:32 +02:00
|
|
|
/**
|
|
|
|
|
* @param array<string, mixed> $params
|
|
|
|
|
*/
|
2025-06-29 21:20:45 +02:00
|
|
|
public function get(string $endpoint, array $params = []): Response
|
|
|
|
|
{
|
2025-08-09 13:48:25 +02:00
|
|
|
$url = sprintf('%s://%s/api/v3/%s', $this->scheme, $this->host, ltrim($endpoint, '/'));
|
|
|
|
|
|
2025-06-29 21:20:45 +02:00
|
|
|
$request = Http::timeout(30);
|
2025-08-09 13:48:25 +02:00
|
|
|
|
2025-06-29 21:20:45 +02:00
|
|
|
if ($this->token) {
|
|
|
|
|
$request = $request->withToken($this->token);
|
|
|
|
|
}
|
2025-08-09 13:48:25 +02:00
|
|
|
|
2025-06-29 21:20:45 +02:00
|
|
|
return $request->get($url, $params);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-07 00:51:32 +02:00
|
|
|
/**
|
|
|
|
|
* @param array<string, mixed> $data
|
|
|
|
|
*/
|
2025-06-29 21:20:45 +02:00
|
|
|
public function post(string $endpoint, array $data = []): Response
|
|
|
|
|
{
|
2025-08-09 13:48:25 +02:00
|
|
|
$url = sprintf('%s://%s/api/v3/%s', $this->scheme, $this->host, ltrim($endpoint, '/'));
|
|
|
|
|
|
2025-06-29 21:20:45 +02:00
|
|
|
$request = Http::timeout(30);
|
2025-08-09 13:48:25 +02:00
|
|
|
|
2025-06-29 21:20:45 +02:00
|
|
|
if ($this->token) {
|
|
|
|
|
$request = $request->withToken($this->token);
|
|
|
|
|
}
|
2025-08-09 13:48:25 +02:00
|
|
|
|
2025-06-29 21:20:45 +02:00
|
|
|
return $request->post($url, $data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function withToken(string $token): self
|
|
|
|
|
{
|
|
|
|
|
$this->token = $token;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
2025-08-09 13:48:25 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return a cloned request with a different scheme (http or https)
|
|
|
|
|
*/
|
|
|
|
|
public function withScheme(string $scheme): self
|
|
|
|
|
{
|
|
|
|
|
$clone = clone $this;
|
|
|
|
|
$clone->scheme = strtolower($scheme) === 'http' ? 'http' : 'https';
|
|
|
|
|
return $clone;
|
|
|
|
|
}
|
2025-06-29 21:20:45 +02:00
|
|
|
}
|