Skip to content

Commit 47f8795

Browse files
update
1 parent a800e92 commit 47f8795

9 files changed

Lines changed: 449 additions & 4 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
},
2828
"autoload": {
2929
"psr-4": {
30-
"Supabase\\Client\\": "src/"
30+
"Supabase\\Client\\": "src/Supabase/"
3131
}
3232
},
3333
"autoload-dev": {

src/Supabase/Http/CurlClient.php

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Supabase\Client\Http;
6+
7+
use JsonException;
8+
use RuntimeException;
9+
10+
class CurlClient
11+
{
12+
public function send(
13+
string $method,
14+
string $url,
15+
array $headers = [],
16+
array|string|null $body = null
17+
): Response {
18+
$curl = curl_init();
19+
20+
if ($curl === false) {
21+
throw new RuntimeException('Unable to initialize cURL.');
22+
}
23+
24+
$formattedHeaders = [];
25+
26+
foreach ($headers as $key => $value) {
27+
$formattedHeaders[] = "{$key}: {$value}";
28+
}
29+
30+
$payload = null;
31+
32+
if ($body !== null) {
33+
$payload = is_array($body)
34+
? json_encode($body, JSON_THROW_ON_ERROR)
35+
: $body;
36+
}
37+
38+
curl_setopt_array($curl, [
39+
CURLOPT_URL => $url,
40+
41+
CURLOPT_CUSTOMREQUEST => strtoupper($method),
42+
43+
CURLOPT_RETURNTRANSFER => true,
44+
45+
CURLOPT_HTTPHEADER => $formattedHeaders,
46+
47+
CURLOPT_POSTFIELDS => $payload,
48+
49+
/*
50+
|--------------------------------------------------------------------------
51+
| Security
52+
|--------------------------------------------------------------------------
53+
*/
54+
55+
CURLOPT_SSL_VERIFYPEER => true,
56+
CURLOPT_SSL_VERIFYHOST => 2,
57+
58+
CURLOPT_PROTOCOLS => CURLPROTO_HTTPS,
59+
60+
CURLOPT_REDIR_PROTOCOLS => CURLPROTO_HTTPS,
61+
62+
CURLOPT_FOLLOWLOCATION => false,
63+
64+
/*
65+
|--------------------------------------------------------------------------
66+
| Performance
67+
|--------------------------------------------------------------------------
68+
*/
69+
70+
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2TLS,
71+
72+
CURLOPT_TCP_KEEPALIVE => 1,
73+
74+
CURLOPT_TCP_KEEPIDLE => 120,
75+
76+
CURLOPT_TCP_KEEPINTVL => 60,
77+
78+
CURLOPT_ENCODING => '',
79+
80+
CURLOPT_FORBID_REUSE => false,
81+
82+
CURLOPT_FRESH_CONNECT => false,
83+
84+
/*
85+
|--------------------------------------------------------------------------
86+
| Timeouts
87+
|--------------------------------------------------------------------------
88+
*/
89+
90+
CURLOPT_CONNECTTIMEOUT => 10,
91+
92+
CURLOPT_TIMEOUT => 30,
93+
94+
/*
95+
|--------------------------------------------------------------------------
96+
| Safety
97+
|--------------------------------------------------------------------------
98+
*/
99+
100+
CURLOPT_MAXREDIRS => 0,
101+
102+
CURLOPT_FAILONERROR => false,
103+
104+
CURLOPT_HEADER => false,
105+
]);
106+
107+
$responseBody = curl_exec($curl);
108+
109+
if ($responseBody === false) {
110+
$error = curl_error($curl);
111+
112+
curl_close($curl);
113+
114+
throw new RuntimeException(
115+
message: $error
116+
);
117+
}
118+
119+
$statusCode = curl_getinfo(
120+
$curl,
121+
CURLINFO_HTTP_CODE
122+
);
123+
124+
curl_close($curl);
125+
126+
return new Response(
127+
body: $responseBody,
128+
status: $statusCode
129+
);
130+
}
131+
}

src/Supabase/Http/Request.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Supabase\Client\Http;
6+
7+
use Psr\Http\Message\RequestInterface;
8+
9+
class Request
10+
{
11+
private RequestInterface $request;
12+
13+
public function __construct(RequestInterface $request)
14+
{
15+
$this->request = $request;
16+
}
17+
18+
public function send(): void
19+
{
20+
$this->client->sendRequest($this->request);
21+
}
22+
}

src/Supabase/Http/Response.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Supabase\Client\Http;
6+
7+
use Psr\Http\Message\ResponseInterface;
8+
use JsonException;
9+
10+
class Response
11+
{
12+
private ResponseInterface $response;
13+
14+
public function __construct(ResponseInterface $response)
15+
{
16+
$this->response = $response;
17+
}
18+
19+
public function json(): mixed
20+
{
21+
$content = (string) $this->response->getBody();
22+
23+
return json_decode($content, true, 512, JSON_THROW_ON_ERROR);
24+
}
25+
}

0 commit comments

Comments
 (0)