|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\Notifications\WebPush; |
| 11 | + |
| 12 | +use OCA\Notifications\Vendor\GuzzleHttp\Psr7\Response; |
| 13 | +use OCA\Notifications\Vendor\Http\Client\HttpAsyncClient; |
| 14 | +use OCA\Notifications\Vendor\Http\Promise\Promise; |
| 15 | +use OCA\Notifications\Vendor\Http\Promise\RejectedPromise; |
| 16 | +use OCA\Notifications\Vendor\Psr\Http\Client\ClientInterface; |
| 17 | +use OCA\Notifications\Vendor\Psr\Http\Message\RequestInterface; |
| 18 | +use OCA\Notifications\Vendor\Psr\Http\Message\ResponseInterface; |
| 19 | +use OCP\Http\Client\IClient; |
| 20 | +use OCP\Http\Client\IResponse; |
| 21 | + |
| 22 | +/** |
| 23 | + * Adapts the Nextcloud HTTP client to the PSR-18 and HTTPlug client interfaces |
| 24 | + * of the vendored web-push library, so requests to the push services honour the |
| 25 | + * proxy, timeout and certificate configuration of the instance. |
| 26 | + */ |
| 27 | +class ClientAdapter implements ClientInterface, HttpAsyncClient { |
| 28 | + public function __construct( |
| 29 | + protected IClient $client, |
| 30 | + ) { |
| 31 | + } |
| 32 | + |
| 33 | + #[\Override] |
| 34 | + public function sendRequest(RequestInterface $request): ResponseInterface { |
| 35 | + try { |
| 36 | + $response = $this->client->request( |
| 37 | + $request->getMethod(), |
| 38 | + (string)$request->getUri(), |
| 39 | + self::buildOptions($request), |
| 40 | + ); |
| 41 | + } catch (\Throwable $e) { |
| 42 | + throw new ClientException($e->getMessage(), (int)$e->getCode(), $e); |
| 43 | + } |
| 44 | + |
| 45 | + return self::convertResponse($response); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Web push notifications are always sent as POST requests, so only those can |
| 50 | + * be handed to the asynchronous API of the Nextcloud client. |
| 51 | + */ |
| 52 | + #[\Override] |
| 53 | + public function sendAsyncRequest(RequestInterface $request): Promise { |
| 54 | + if (strtoupper($request->getMethod()) !== 'POST') { |
| 55 | + return new RejectedPromise(new ClientException('Only POST requests can be sent asynchronously')); |
| 56 | + } |
| 57 | + |
| 58 | + try { |
| 59 | + $promise = $this->client->postAsync((string)$request->getUri(), self::buildOptions($request)); |
| 60 | + } catch (\Throwable $e) { |
| 61 | + return new RejectedPromise($e); |
| 62 | + } |
| 63 | + |
| 64 | + return new PromiseAdapter($promise); |
| 65 | + } |
| 66 | + |
| 67 | + public static function convertResponse(IResponse $response): ResponseInterface { |
| 68 | + return new Response( |
| 69 | + $response->getStatusCode(), |
| 70 | + $response->getHeaders(), |
| 71 | + $response->getBody(), |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @return array<string, mixed> |
| 77 | + */ |
| 78 | + protected static function buildOptions(RequestInterface $request): array { |
| 79 | + return [ |
| 80 | + 'headers' => $request->getHeaders(), |
| 81 | + 'body' => (string)$request->getBody(), |
| 82 | + // Push services report expired subscriptions and rate limits with error |
| 83 | + // status codes, they have to be inspected instead of being thrown |
| 84 | + 'http_errors' => false, |
| 85 | + ]; |
| 86 | + } |
| 87 | +} |
0 commit comments