From 0d2285a16616382afb8bd0cecc37b766b176e6f3 Mon Sep 17 00:00:00 2001 From: Andrea Marco Sartori Date: Tue, 2 Jan 2024 23:00:37 +1000 Subject: [PATCH] Implement the endpoint source --- src/Sources/Endpoint.php | 64 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/Sources/Endpoint.php diff --git a/src/Sources/Endpoint.php b/src/Sources/Endpoint.php new file mode 100644 index 0000000..76b66f7 --- /dev/null +++ b/src/Sources/Endpoint.php @@ -0,0 +1,64 @@ +source instanceof UriInterface + || (is_string($this->source) && $this->isEndpoint($this->source)); + } + + /** + * Retrieve the HTTP request. + */ + public function request(): RequestInterface + { + return $this->request ??= new Request('GET', $this->source, [ + 'Accept' => 'application/json', + 'Content-Type' => 'application/json', + ]); + } + + /** + * Retrieve the HTTP response or part of it. + * + * @return ($key is string ? mixed : \Cerbero\LazyJsonPages\ValueObjects\Response) + */ + public function response(?string $key = null): mixed + { + $this->response ??= (new Client())->send($this->request()); + + return $key === null ? $this->response : $this->response->get($key); + } +}