atp = $parent; $this->sessions = $sessions; $this->did = $did; $this->serviceUrl = $serviceUrl; if (! $this->isPublicMode()) { $this->dpopClient = app(DPoPClient::class); } } /** * Check if client is in public mode (no authentication). */ public function isPublicMode(): bool { return $this->sessions === null || $this->did === null; } /** * Get the current session. */ public function session(): Session { return $this->sessions->session($this->did); } /** * Get the service URL. */ public function serviceUrl(): string { return $this->serviceUrl; } /** * Make XRPC call - routes to public or authenticated based on mode. */ protected function call( string|BackedEnum $endpoint, string $method, ?array $params = null, ?array $body = null ): Response { if ($this->isPublicMode()) { return $this->publicCall($endpoint, $method, $params, $body); } return $this->authenticatedCall($endpoint, $method, $params, $body); } /** * Make public XRPC call (no authentication). */ protected function publicCall( string|BackedEnum $endpoint, string $method, ?array $params = null, ?array $body = null ): Response { $endpoint = $endpoint instanceof BackedEnum ? $endpoint->value : $endpoint; $url = rtrim($this->serviceUrl, '/') . '/xrpc/' . $endpoint; $params = array_filter($params ?? [], fn ($v) => ! is_null($v)); $response = match ($method) { 'GET' => Http::get($url, $params), 'POST' => Http::post($url, $body ?? $params), 'DELETE' => Http::delete($url, $params), default => throw new \InvalidArgumentException("Unsupported method: {$method}"), }; if ($response->failed() || isset($response->json()['error'])) { throw AtpResponseException::fromResponse($response, $endpoint); } return new Response($response); } /** * Make POST request with raw binary body (for blob uploads). * Only works in authenticated mode. */ public function postBlob(string|BackedEnum $endpoint, string $data, string $mimeType): Response { if ($this->isPublicMode()) { throw new \RuntimeException('Blob uploads require authentication.'); } return $this->authenticatedPostBlob($endpoint, $data, $mimeType); } }