scheme = strtolower($m[1]); } // Handle both full URLs and just domain names $this->instance = $this->normalizeInstance($instance); $this->token = $token; } /** * Normalize instance URL to just the domain name */ private function normalizeInstance(string $instance): string { // Remove protocol if present $instance = preg_replace('/^https?:\/\//i', '', $instance); // Remove trailing slash if present $instance = rtrim($instance, '/'); return $instance; } /** * Explicitly set the scheme (http or https) for subsequent requests. */ public function withScheme(string $scheme): self { $scheme = strtolower($scheme); if (in_array($scheme, ['http', 'https'], true)) { $this->scheme = $scheme; } return $this; } /** * @param array $params */ public function get(string $endpoint, array $params = []): Response { $url = sprintf('%s://%s/api/v3/%s', $this->scheme, $this->instance, $endpoint); $request = Http::timeout(30); if ($this->token) { $request = $request->withToken($this->token); } return $request->get($url, $params); } /** * @param array $data */ public function post(string $endpoint, array $data = []): Response { $url = sprintf('%s://%s/api/v3/%s', $this->scheme, $this->instance, $endpoint); $request = Http::timeout(30); if ($this->token) { $request = $request->withToken($this->token); } return $request->post($url, $data); } public function withToken(string $token): self { $this->token = $token; return $this; } }