mirror of
https://codeberg.org/reflect/reflect-client-php.git
synced 2025-09-13 17:43:42 +02:00
wip: 1682607489
This commit is contained in:
parent
c961e44923
commit
39ad645cda
1 changed files with 32 additions and 16 deletions
|
@ -13,47 +13,61 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
// Supported connection methods
|
// Supported connection methods
|
||||||
enum ConType {
|
enum Connection {
|
||||||
case HTTP;
|
case HTTP;
|
||||||
case AF_UNIX;
|
case AF_UNIX;
|
||||||
}
|
}
|
||||||
|
|
||||||
class Client {
|
class Client {
|
||||||
public function __construct(string $endpoint, string $key = null, ConType $con = null) {
|
// Use this HTTP method if no method specified to call()
|
||||||
$this->_con = $con ?: $this::resolve_contype($endpoint);
|
const HTTP_DEFAULT_METHOD = Method::GET;
|
||||||
|
|
||||||
|
public function __construct(string $endpoint, string $key = null, Connection $con = null) {
|
||||||
|
$this->_con = $con ?: $this::resolve_connection($endpoint);
|
||||||
$this->_endpoint = $endpoint;
|
$this->_endpoint = $endpoint;
|
||||||
$this->_key = $key;
|
$this->_key = $key;
|
||||||
|
|
||||||
// Initialize socket
|
// Initialize socket
|
||||||
if ($this->_con === ConType::AF_UNIX) {
|
if ($this->_con === Connection::AF_UNIX) {
|
||||||
$this->_socket = socket_create(AF_UNIX, SOCK_STREAM, 0);
|
$this->_socket = socket_create(AF_UNIX, SOCK_STREAM, 0);
|
||||||
$conn = socket_connect($this->_socket, $this->_endpoint);
|
$conn = socket_connect($this->_socket, $this->_endpoint);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resolve connection type from endpoint string.
|
// Resolve connection type from endpoint string.
|
||||||
// If the string is a valid URL we will treat it as HTTP otherwise we will
|
// 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.
|
||||||
// assume it's a path on disk to a UNIX socket file.
|
private static function resolve_connection(string $endpoint): Connection {
|
||||||
private static function resolve_contype(string $endpoint): ConType {
|
|
||||||
return filter_var($endpoint, FILTER_VALIDATE_URL)
|
return filter_var($endpoint, FILTER_VALIDATE_URL)
|
||||||
? ConType::HTTP
|
? Connection::HTTP
|
||||||
: ConType::AF_UNIX;
|
: Connection::AF_UNIX;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Attempt to resolve Method from backed enum string, or return default
|
||||||
|
private static function resolve_method(string $method): Method {
|
||||||
|
return ($method instanceof Method)
|
||||||
|
? $method
|
||||||
|
: Method::tryFrom($method) ?? (__CLASS__)::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 {
|
||||||
$headers = ["Content-Type: application/json"];
|
$headers = ["Content-Type: application/json"];
|
||||||
|
|
||||||
|
// Append Authentication header if API key is provided
|
||||||
if (!empty($this->_key)) {
|
if (!empty($this->_key)) {
|
||||||
$headers[] = "Applications: Bearar {$this->_key}";
|
$headers[] = "Applications: Bearar {$this->_key}";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Append new line chars to each header
|
||||||
$headers = array_map(fn($header): string => $header . "\r\n", $headers);
|
$headers = array_map(fn($header): string => $header . "\r\n", $headers);
|
||||||
return implode("", $headers);
|
return implode("", $headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make request and return response over HTTP
|
// Make request and return response over HTTP
|
||||||
private function http_call(string $endpoint, Method $method, array $payload = null): array {
|
private function http_call(string $endpoint, Method|string $method = (__CLASS__)::HTTP_DEFAULT_METHOD, array $payload = null): array {
|
||||||
|
// Resolve string to enum
|
||||||
|
$method = $this::resolve_method($method);
|
||||||
|
|
||||||
$data = stream_context_create([
|
$data = stream_context_create([
|
||||||
"http" => [
|
"http" => [
|
||||||
"header" => $this->http_headers(),
|
"header" => $this->http_headers(),
|
||||||
|
@ -65,9 +79,8 @@
|
||||||
|
|
||||||
$resp = file_get_contents($this->_endpoint . $endpoint, false, $data);
|
$resp = file_get_contents($this->_endpoint . $endpoint, false, $data);
|
||||||
|
|
||||||
// Get HTTP response code from $http_response_header which materializes out of
|
// Get HTTP response code from $http_response_header which materializes out of thin air after file_get_contents().
|
||||||
// thin air after file_get_contents(). The first header line and second word will
|
// The first header line and second word will contain the status code.
|
||||||
// contain the status code.
|
|
||||||
$resp_code = (int) explode(" ", $http_response_header[0])[1];
|
$resp_code = (int) explode(" ", $http_response_header[0])[1];
|
||||||
|
|
||||||
return [$resp_code, $resp];
|
return [$resp_code, $resp];
|
||||||
|
@ -79,7 +92,7 @@
|
||||||
$rx = socket_read($this->_socket, 2024);
|
$rx = socket_read($this->_socket, 2024);
|
||||||
|
|
||||||
if (!$tx || !$rx) {
|
if (!$tx || !$rx) {
|
||||||
throw new Error("Failed to complete transaction");
|
throw new \Error("Failed to complete transaction");
|
||||||
}
|
}
|
||||||
|
|
||||||
return $rx;
|
return $rx;
|
||||||
|
@ -87,9 +100,12 @@
|
||||||
|
|
||||||
// Create HTTP-like JSON with ["<endpoint>","<method>","[payload]"] and return
|
// Create HTTP-like JSON with ["<endpoint>","<method>","[payload]"] and return
|
||||||
// respone from endpoint as ["<http_status_code", "<json_encoded_response_body>"]
|
// respone from endpoint as ["<http_status_code", "<json_encoded_response_body>"]
|
||||||
public function call(string $endpoint, Method $method, array $payload = null): array {
|
public function call(string $endpoint, Method|string $method = (__CLASS__)::HTTP_DEFAULT_METHOD, array $payload = null): array {
|
||||||
|
// Resolve string to enum
|
||||||
|
$method = $this::resolve_method($method);
|
||||||
|
|
||||||
// Call endpoint over UNIX socket
|
// Call endpoint over UNIX socket
|
||||||
if ($this->_con === ConType::AF_UNIX) {
|
if ($this->_con === Connection::AF_UNIX) {
|
||||||
return json_decode($this->_socket_txn(json_encode([
|
return json_decode($this->_socket_txn(json_encode([
|
||||||
$endpoint,
|
$endpoint,
|
||||||
$method->value,
|
$method->value,
|
||||||
|
|
Loading…
Add table
Reference in a new issue