feat: add method chaining

This commit is contained in:
Victor Westerlund 2024-03-18 12:57:57 +01:00
parent 94cf0858ff
commit 228b3c665d

View file

@ -9,9 +9,12 @@
require_once "Response.php"; require_once "Response.php";
class Client { class Client {
private ?string $key; protected ?string $key;
private string $base_url; protected string $base_url;
private bool $https_verify_peer; protected bool $https_verify_peer;
protected string $endpoint;
protected string $params;
public function __construct(string $base_url, string $key = null, bool $verify_peer = true) { public function __construct(string $base_url, string $key = null, bool $verify_peer = true) {
// Optional API key // Optional API key
@ -45,13 +48,8 @@
} }
// Make request and return response over HTTP // Make request and return response over HTTP
private function http_call(string $endpoint, Method|string $method = self::HTTP_DEFAULT_METHOD, array $payload = null): array { private function http_call(Method $method, array $payload = null): array {
// Resolve string to enum $context = stream_context_create([
$method = self::resolve_method($method);
// Remove leading "/" if present, as it's already present in $this->endpoint
$endpoint = substr($endpoint, 0, 1) !== "/" ? $endpoint : substr($endpoint, 1, strlen($endpoint) - 1);
$data = stream_context_create([
"http" => [ "http" => [
"header" => $this->http_headers(), "header" => $this->http_headers(),
"method" => $method->value, "method" => $method->value,
@ -65,41 +63,57 @@
] ]
]); ]);
$resp = file_get_contents($this->endpoint . $endpoint, false, $data); $resp = file_get_contents($this->base_url . $this->endpoint, false, $context);
// Get HTTP response code from $http_response_header which materializes out of thin air after file_get_contents(). // Get HTTP response code from $http_response_header which materializes out of thin air after file_get_contents().
// 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 response as [<http_status_code>, <resp_body_assoc_array>] // Return response as [<resp_body_assoc_array>, <http_status_code>]
return [$resp, $resp_code]; return [$resp, $resp_code];
} }
// ---- // ----
// Construct URL search parameters from array if set
public function params(?array $params = null): self {
$this->params = !empty($params) ? self::get_params($params) : "";
return $this;
}
// Create a new call to an endpoint
public function call(string $endpoint): self {
// Remove leading "/" if present, as it's already present in $this->base_url
$this->endpoint = substr($this->endpoint, 0, 1) !== "/"
? $this->endpoint
: substr($this->endpoint, 1, strlen($this->endpoint) - 1);
// Reset search parameters
$this->params();
return $this;
}
// ----
// Make a GET request to endpoint with optional search parameters // Make a GET request to endpoint with optional search parameters
public function get(string $endpoint, array $params = []): Response { public function get(): Response {
$resp = $this->http_call($endpoint . self::get_params($params), Method::GET); return new Response(...$this->http_call(Method::GET));
return new Response(...$resp);
} }
public function patch(string $endpoint, array $params, array $payload): Response { public function patch(array $payload): Response {
$resp = $this->http_call($endpoint . self::get_params($params), Method::PATCH, $payload); return new Response(...$this->http_call(Method::PATCH, $payload));
return new Response(...$resp);
} }
public function put(string $endpoint, array $params, array $payload): Response { public function put(array $payload): Response {
$resp = $this->http_call($endpoint . self::get_params($params), Method::PUT, $payload); return new Response(...$this->http_call(Method::PUT, $payload));
return new Response(...$resp);
} }
public function post(string $endpoint, array $params, array $payload): Response { public function post(array $payload): Response {
$resp = $this->http_call($endpoint . self::get_params($params), Method::POST, $payload); return new Response(...$this->http_call(Method::POST, $payload));
return new Response(...$resp);
} }
public function delete(string $endpoint, array $params, ?array $payload = []): Response { public function delete(?array $payload = []): Response {
$resp = $this->http_call($endpoint . self::get_params($params), Method::POST, $payload); return new Response(...$this->http_call(Method::DELETE, $payload));
return new Response(...$resp);
} }
} }