-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathClient.php
More file actions
75 lines (64 loc) · 2.2 KB
/
Client.php
File metadata and controls
75 lines (64 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
namespace Openapi;
use Openapi\Interfaces\HttpTransportInterface;
use Openapi\Transports\CurlTransport;
use Psr\Http\Client\ClientInterface as PsrClientInterface;
class Client
{
private string $token;
private HttpTransportInterface|PsrClientInterface $transport;
private ?string $baseUrl = null;
public function __construct(?string $token = null, HttpTransportInterface|PsrClientInterface|null $transport = null)
{
$this->token = $token ?? getenv('OPEN_API_TOKEN');
if (getenv('OPENAPI_BASE_URL')) {
$this->baseUrl = getenv('OPENAPI_BASE_URL');
}
$this->transport = $transport ?? new CurlTransport($this->token);
}
/**
* Execute HTTP request
*
* @param string $method HTTP method (GET, POST, PUT, DELETE, PATCH)
* @param string $url Target URL
* @param mixed $payload Request body (for POST/PUT/PATCH)
* @param array<string, scalar|null>|null $params Query parameters (for GET) or form data (for other methods)
* @return string Response body
*/
public function request(
string $method,
string $url,
mixed $payload = null,
?array $params = null
): string {
if (!str_starts_with(strtolower($url), 'http') && !empty($this->baseUrl)) {
$url = rtrim($this->baseUrl, '/') . '/' . ltrim($url, '/');
}
return $this->transport->request($method, $url, $payload, $params);
}
/**
* Execute GET request
*
* @param array<string, scalar|null>|null $params Query parameters
*/
public function get(string $url, ?array $params = null): string
{
return $this->request('GET', $url, null, $params);
}
public function post(string $url, mixed $payload = null): string
{
return $this->request('POST', $url, $payload);
}
public function put(string $url, mixed $payload = null): string
{
return $this->request('PUT', $url, $payload);
}
public function delete(string $url): string
{
return $this->request('DELETE', $url);
}
public function patch(string $url, mixed $payload = null): string
{
return $this->request('PATCH', $url, $payload);
}
}