Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
LemmyRequest
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 4
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 get
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 post
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 withToken
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace App\Modules\Lemmy;
4
5use Illuminate\Support\Facades\Http;
6use Illuminate\Http\Client\Response;
7
8class LemmyRequest
9{
10    private string $instance;
11    private ?string $token;
12
13    public function __construct(string $instance, ?string $token = null)
14    {
15        $this->instance = $instance;
16        $this->token = $token;
17    }
18
19    /**
20     * @param array<string, mixed> $params
21     */
22    public function get(string $endpoint, array $params = []): Response
23    {
24        $url = "https://{$this->instance}/api/v3/{$endpoint}";
25        
26        $request = Http::timeout(30);
27        
28        if ($this->token) {
29            $request = $request->withToken($this->token);
30        }
31        
32        return $request->get($url, $params);
33    }
34
35    /**
36     * @param array<string, mixed> $data
37     */
38    public function post(string $endpoint, array $data = []): Response
39    {
40        $url = "https://{$this->instance}/api/v3/{$endpoint}";
41        
42        $request = Http::timeout(30);
43        
44        if ($this->token) {
45            $request = $request->withToken($this->token);
46        }
47        
48        return $request->post($url, $data);
49    }
50
51    public function withToken(string $token): self
52    {
53        $this->token = $token;
54        return $this;
55    }
56}