GuzzlePromiseAdapter.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023, Joas Schilling <coding@schilljs.com>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OC\Http\Client;
  23. use Exception;
  24. use GuzzleHttp\Exception\RequestException;
  25. use GuzzleHttp\Promise\PromiseInterface;
  26. use LogicException;
  27. use OCP\Http\Client\IPromise;
  28. use OCP\Http\Client\IResponse;
  29. use Psr\Http\Message\ResponseInterface;
  30. use Psr\Log\LoggerInterface;
  31. /**
  32. * A wrapper around Guzzle's PromiseInterface
  33. *
  34. * @see \GuzzleHttp\Promise\PromiseInterface
  35. * @since 28.0.0
  36. */
  37. class GuzzlePromiseAdapter implements IPromise {
  38. public function __construct(
  39. protected PromiseInterface $promise,
  40. protected LoggerInterface $logger,
  41. ) {
  42. }
  43. /**
  44. * Appends fulfillment and rejection handlers to the promise, and returns
  45. * a new promise resolving to the return value of the called handler.
  46. *
  47. * @param ?callable(IResponse): void $onFulfilled Invoked when the promise fulfills. Gets an \OCP\Http\Client\IResponse passed in as argument
  48. * @param ?callable(Exception): void $onRejected Invoked when the promise is rejected. Gets an \Exception passed in as argument
  49. *
  50. * @return IPromise
  51. * @since 28.0.0
  52. */
  53. public function then(
  54. ?callable $onFulfilled = null,
  55. ?callable $onRejected = null,
  56. ): IPromise {
  57. if ($onFulfilled !== null) {
  58. $wrappedOnFulfilled = static function (ResponseInterface $response) use ($onFulfilled) {
  59. $onFulfilled(new Response($response));
  60. };
  61. } else {
  62. $wrappedOnFulfilled = null;
  63. }
  64. if ($onRejected !== null) {
  65. $wrappedOnRejected = static function (RequestException $e) use ($onRejected) {
  66. $onRejected($e);
  67. };
  68. } else {
  69. $wrappedOnRejected = null;
  70. }
  71. $this->promise->then($wrappedOnFulfilled, $wrappedOnRejected);
  72. return $this;
  73. }
  74. /**
  75. * Get the state of the promise ("pending", "rejected", or "fulfilled").
  76. *
  77. * The three states can be checked against the constants defined:
  78. * STATE_PENDING, STATE_FULFILLED, and STATE_REJECTED.
  79. *
  80. * @return IPromise::STATE_*
  81. * @since 28.0.0
  82. */
  83. public function getState(): string {
  84. $state = $this->promise->getState();
  85. if ($state === PromiseInterface::FULFILLED) {
  86. return self::STATE_FULFILLED;
  87. }
  88. if ($state === PromiseInterface::REJECTED) {
  89. return self::STATE_REJECTED;
  90. }
  91. if ($state === PromiseInterface::PENDING) {
  92. return self::STATE_PENDING;
  93. }
  94. $this->logger->error('Unexpected promise state "{state}" returned by Guzzle', [
  95. 'state' => $state,
  96. ]);
  97. return self::STATE_PENDING;
  98. }
  99. /**
  100. * Cancels the promise if possible.
  101. *
  102. * @link https://github.com/promises-aplus/cancellation-spec/issues/7
  103. * @since 28.0.0
  104. */
  105. public function cancel(): void {
  106. $this->promise->cancel();
  107. }
  108. /**
  109. * Waits until the promise completes if possible.
  110. *
  111. * Pass $unwrap as true to unwrap the result of the promise, either
  112. * returning the resolved value or throwing the rejected exception.
  113. *
  114. * If the promise cannot be waited on, then the promise will be rejected.
  115. *
  116. * @param bool $unwrap
  117. *
  118. * @return mixed
  119. *
  120. * @throws LogicException if the promise has no wait function or if the
  121. * promise does not settle after waiting.
  122. * @since 28.0.0
  123. */
  124. public function wait(bool $unwrap = true): mixed {
  125. return $this->promise->wait($unwrap);
  126. }
  127. }