mirror of
https://codeberg.org/reflect/reflect-client-php.git
synced 2025-09-13 17:43:42 +02:00
feat: return response from HTTP and Socket as assoc array (#4)
* feat: return response as assoc array * feat: return http resp as assoc array
This commit is contained in:
parent
a2587624e5
commit
1aae608011
1 changed files with 12 additions and 8 deletions
|
@ -97,7 +97,8 @@
|
||||||
// The first header line and second word will contain the status code.
|
// The first header line and second word will 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 response as [<http_status_code>, <resp_body_assoc_array>]
|
||||||
|
return [$resp_code, json_decode($resp)];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make request and return response over socket
|
// Make request and return response over socket
|
||||||
|
@ -112,19 +113,22 @@
|
||||||
return $rx;
|
return $rx;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create HTTP-like JSON with ["<endpoint>","<method>","[payload]"] and return
|
// Call a Reflect endpoint and return response as assoc array
|
||||||
// respone from endpoint as ["<http_status_code", "<json_encoded_response_body>"]
|
|
||||||
public function call(string $endpoint, Method|string $method = (__CLASS__)::HTTP_DEFAULT_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
|
// Resolve string to enum
|
||||||
$method = $this::resolve_method($method);
|
$method = $this::resolve_method($method);
|
||||||
|
|
||||||
// Call endpoint over UNIX socket
|
// Call endpoint over UNIX socket
|
||||||
if ($this->_con === Connection::AF_UNIX) {
|
if ($this->_con === Connection::AF_UNIX) {
|
||||||
return json_decode($this->socket_txn(json_encode([
|
// Return response as assoc array
|
||||||
$endpoint,
|
return json_decode($this->socket_txn(
|
||||||
$method->value,
|
// Send request as stringified JSON
|
||||||
$payload
|
json_encode([
|
||||||
])));
|
$endpoint,
|
||||||
|
$method->value,
|
||||||
|
$payload
|
||||||
|
])
|
||||||
|
), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call endpoint over HTTP
|
// Call endpoint over HTTP
|
||||||
|
|
Loading…
Add table
Reference in a new issue