mirror of
https://codeberg.org/reflect/reflect-client-php.git
synced 2025-09-13 17:43:42 +02:00
fix: refactor methods
This commit is contained in:
parent
4513afcea2
commit
94cf0858ff
1 changed files with 37 additions and 77 deletions
|
@ -8,61 +8,27 @@
|
||||||
require_once "Method.php";
|
require_once "Method.php";
|
||||||
require_once "Response.php";
|
require_once "Response.php";
|
||||||
|
|
||||||
// Supported connection methods
|
|
||||||
enum Connection {
|
|
||||||
case HTTP;
|
|
||||||
case AF_UNIX;
|
|
||||||
}
|
|
||||||
|
|
||||||
class Client {
|
class Client {
|
||||||
// API key string
|
|
||||||
private ?string $key;
|
private ?string $key;
|
||||||
// Connection method
|
private string $base_url;
|
||||||
private Connection $con;
|
private bool $https_verify_peer;
|
||||||
// Request endpoitn string
|
|
||||||
private string $endpoint;
|
public function __construct(string $base_url, string $key = null, bool $verify_peer = true) {
|
||||||
|
// Optional API key
|
||||||
// Socket instance
|
|
||||||
private Socket|false $socket = false;
|
|
||||||
// Flag: Allow unverified SSL certificates for HTTPS
|
|
||||||
private bool $https_peer_verify = true;
|
|
||||||
|
|
||||||
// Use this HTTP method if no method specified to call()
|
|
||||||
const HTTP_DEFAULT_METHOD = Method::GET;
|
|
||||||
// The amount of bytes to read for each chunk from socket
|
|
||||||
const SOCKET_READ_BYTES = 2048;
|
|
||||||
|
|
||||||
public function __construct(string $endpoint, string $key = null, Connection $con = null, bool $https_peer_verify = true) {
|
|
||||||
$this->con = $con ?: self::resolve_connection($endpoint);
|
|
||||||
$this->endpoint = $endpoint;
|
|
||||||
$this->key = $key;
|
$this->key = $key;
|
||||||
|
|
||||||
if ($this->con === Connection::AF_UNIX) {
|
// Append tailing "/" if absent
|
||||||
// Connect to Reflect UNIX socket
|
$this->base_url = substr($this->base_url, -1) === "/" ? $this->base_url : $this->base_url . "/";
|
||||||
$this->_socket = socket_create(AF_UNIX, SOCK_STREAM, 0);
|
// Flag which enables or disables SSL peer validation (for self-signed certificates)
|
||||||
$conn = socket_connect($this->_socket, $this->endpoint);
|
$this->https_verify_peer = $verify_peer;
|
||||||
} else if ($this->con === Connection::HTTP) {
|
|
||||||
// Append tailing "/" for HTTP if absent
|
|
||||||
$this->endpoint = substr($this->endpoint, -1) === "/" ? $this->endpoint : $this->endpoint . "/";
|
|
||||||
// Flag which enables or disables SSL peer validation (for self-signed certificates)
|
|
||||||
$this->https_peer_verify = $https_peer_verify;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resolve connection type from endpoint string.
|
// Convert assoc array to URL-encoded string or empty string if array is empty
|
||||||
// If the string is a valid URL we will treat it as HTTP otherwise we will assume it's a path on disk to a UNIX socket file.
|
private static function get_params(array $params): string {
|
||||||
private static function resolve_connection(string $endpoint): Connection {
|
return !empty($params) ? "?" . http_build_query($params) : "";
|
||||||
return filter_var($endpoint, FILTER_VALIDATE_URL)
|
|
||||||
? Connection::HTTP
|
|
||||||
: Connection::AF_UNIX;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attempt to resolve Method from backed enum string, or return default
|
// ----
|
||||||
private static function resolve_method(Method|string $method): Method {
|
|
||||||
return ($method instanceof Method)
|
|
||||||
? $method
|
|
||||||
: Method::tryFrom($method) ?? self::HTTP_DEFAULT_METHOD;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Construct stream_context_create() compatible header string
|
// Construct stream_context_create() compatible header string
|
||||||
private function http_headers(): string {
|
private function http_headers(): string {
|
||||||
|
@ -93,9 +59,9 @@
|
||||||
"content" => !empty($payload) ? json_encode($payload) : ""
|
"content" => !empty($payload) ? json_encode($payload) : ""
|
||||||
],
|
],
|
||||||
"ssl" => [
|
"ssl" => [
|
||||||
"verify_peer" => $this->https_peer_verify,
|
"verify_peer" => $this->https_verify_peer,
|
||||||
"verify_peer_name" => $this->https_peer_verify,
|
"verify_peer_name" => $this->https_verify_peer,
|
||||||
"allow_self_signed" => !$this->https_peer_verify
|
"allow_self_signed" => !$this->https_verify_peer
|
||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
@ -109,37 +75,31 @@
|
||||||
return [$resp, $resp_code];
|
return [$resp, $resp_code];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make request and return response over socket
|
// ----
|
||||||
private function socket_txn(string $payload): string {
|
|
||||||
$tx = socket_write($this->_socket, $payload, strlen($payload));
|
|
||||||
$rx = socket_read($this->_socket, self::SOCKET_READ_BYTES);
|
|
||||||
|
|
||||||
if (!$tx || !$rx) {
|
// Make a GET request to endpoint with optional search parameters
|
||||||
throw new \Error("Failed to complete transaction");
|
public function get(string $endpoint, array $params = []): Response {
|
||||||
}
|
$resp = $this->http_call($endpoint . self::get_params($params), Method::GET);
|
||||||
|
return new Response(...$resp);
|
||||||
return $rx;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call a Reflect endpoint and return response as assoc array
|
public function patch(string $endpoint, array $params, array $payload): Response {
|
||||||
public function call(string $endpoint, Method|string $method = self::HTTP_DEFAULT_METHOD, array $payload = null): Response {
|
$resp = $this->http_call($endpoint . self::get_params($params), Method::PATCH, $payload);
|
||||||
// Resolve string to enum
|
return new Response(...$resp);
|
||||||
$method = self::resolve_method($method);
|
}
|
||||||
|
|
||||||
// Call endpoint over UNIX socket
|
public function put(string $endpoint, array $params, array $payload): Response {
|
||||||
if ($this->con === Connection::AF_UNIX) {
|
$resp = $this->http_call($endpoint . self::get_params($params), Method::PUT, $payload);
|
||||||
// Return response as assoc array
|
return new Response(...$resp);
|
||||||
return json_decode($this->socket_txn(
|
}
|
||||||
// Send request as stringified JSON
|
|
||||||
json_encode([
|
|
||||||
$endpoint,
|
|
||||||
$method->value,
|
|
||||||
$payload
|
|
||||||
])
|
|
||||||
), true);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Call endpoint over HTTP
|
public function post(string $endpoint, array $params, array $payload): Response {
|
||||||
return new Response($this->http_call(...func_get_args()));
|
$resp = $this->http_call($endpoint . self::get_params($params), Method::POST, $payload);
|
||||||
|
return new Response(...$resp);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function delete(string $endpoint, array $params, ?array $payload = []): Response {
|
||||||
|
$resp = $this->http_call($endpoint . self::get_params($params), Method::POST, $payload);
|
||||||
|
return new Response(...$resp);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue