Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   1  <?php
   2  
   3  namespace GuzzleHttp;
   4  
   5  use GuzzleHttp\Exception\GuzzleException;
   6  use GuzzleHttp\Promise\PromiseInterface;
   7  use Psr\Http\Message\ResponseInterface;
   8  use Psr\Http\Message\UriInterface;
   9  
  10  /**
  11   * Client interface for sending HTTP requests.
  12   */
  13  trait ClientTrait
  14  {
  15      /**
  16       * Create and send an HTTP request.
  17       *
  18       * Use an absolute path to override the base path of the client, or a
  19       * relative path to append to the base path of the client. The URL can
  20       * contain the query string as well.
  21       *
  22       * @param string              $method  HTTP method.
  23       * @param string|UriInterface $uri     URI object or string.
  24       * @param array               $options Request options to apply.
  25       *
  26       * @throws GuzzleException
  27       */
  28      abstract public function request(string $method, $uri, array $options = []): ResponseInterface;
  29  
  30      /**
  31       * Create and send an HTTP GET request.
  32       *
  33       * Use an absolute path to override the base path of the client, or a
  34       * relative path to append to the base path of the client. The URL can
  35       * contain the query string as well.
  36       *
  37       * @param string|UriInterface $uri     URI object or string.
  38       * @param array               $options Request options to apply.
  39       *
  40       * @throws GuzzleException
  41       */
  42      public function get($uri, array $options = []): ResponseInterface
  43      {
  44          return $this->request('GET', $uri, $options);
  45      }
  46  
  47      /**
  48       * Create and send an HTTP HEAD request.
  49       *
  50       * Use an absolute path to override the base path of the client, or a
  51       * relative path to append to the base path of the client. The URL can
  52       * contain the query string as well.
  53       *
  54       * @param string|UriInterface $uri     URI object or string.
  55       * @param array               $options Request options to apply.
  56       *
  57       * @throws GuzzleException
  58       */
  59      public function head($uri, array $options = []): ResponseInterface
  60      {
  61          return $this->request('HEAD', $uri, $options);
  62      }
  63  
  64      /**
  65       * Create and send an HTTP PUT request.
  66       *
  67       * Use an absolute path to override the base path of the client, or a
  68       * relative path to append to the base path of the client. The URL can
  69       * contain the query string as well.
  70       *
  71       * @param string|UriInterface $uri     URI object or string.
  72       * @param array               $options Request options to apply.
  73       *
  74       * @throws GuzzleException
  75       */
  76      public function put($uri, array $options = []): ResponseInterface
  77      {
  78          return $this->request('PUT', $uri, $options);
  79      }
  80  
  81      /**
  82       * Create and send an HTTP POST request.
  83       *
  84       * Use an absolute path to override the base path of the client, or a
  85       * relative path to append to the base path of the client. The URL can
  86       * contain the query string as well.
  87       *
  88       * @param string|UriInterface $uri     URI object or string.
  89       * @param array               $options Request options to apply.
  90       *
  91       * @throws GuzzleException
  92       */
  93      public function post($uri, array $options = []): ResponseInterface
  94      {
  95          return $this->request('POST', $uri, $options);
  96      }
  97  
  98      /**
  99       * Create and send an HTTP PATCH request.
 100       *
 101       * Use an absolute path to override the base path of the client, or a
 102       * relative path to append to the base path of the client. The URL can
 103       * contain the query string as well.
 104       *
 105       * @param string|UriInterface $uri     URI object or string.
 106       * @param array               $options Request options to apply.
 107       *
 108       * @throws GuzzleException
 109       */
 110      public function patch($uri, array $options = []): ResponseInterface
 111      {
 112          return $this->request('PATCH', $uri, $options);
 113      }
 114  
 115      /**
 116       * Create and send an HTTP DELETE request.
 117       *
 118       * Use an absolute path to override the base path of the client, or a
 119       * relative path to append to the base path of the client. The URL can
 120       * contain the query string as well.
 121       *
 122       * @param string|UriInterface $uri     URI object or string.
 123       * @param array               $options Request options to apply.
 124       *
 125       * @throws GuzzleException
 126       */
 127      public function delete($uri, array $options = []): ResponseInterface
 128      {
 129          return $this->request('DELETE', $uri, $options);
 130      }
 131  
 132      /**
 133       * Create and send an asynchronous HTTP request.
 134       *
 135       * Use an absolute path to override the base path of the client, or a
 136       * relative path to append to the base path of the client. The URL can
 137       * contain the query string as well. Use an array to provide a URL
 138       * template and additional variables to use in the URL template expansion.
 139       *
 140       * @param string              $method  HTTP method
 141       * @param string|UriInterface $uri     URI object or string.
 142       * @param array               $options Request options to apply.
 143       */
 144      abstract public function requestAsync(string $method, $uri, array $options = []): PromiseInterface;
 145  
 146      /**
 147       * Create and send an asynchronous HTTP GET request.
 148       *
 149       * Use an absolute path to override the base path of the client, or a
 150       * relative path to append to the base path of the client. The URL can
 151       * contain the query string as well. Use an array to provide a URL
 152       * template and additional variables to use in the URL template expansion.
 153       *
 154       * @param string|UriInterface $uri     URI object or string.
 155       * @param array               $options Request options to apply.
 156       */
 157      public function getAsync($uri, array $options = []): PromiseInterface
 158      {
 159          return $this->requestAsync('GET', $uri, $options);
 160      }
 161  
 162      /**
 163       * Create and send an asynchronous HTTP HEAD request.
 164       *
 165       * Use an absolute path to override the base path of the client, or a
 166       * relative path to append to the base path of the client. The URL can
 167       * contain the query string as well. Use an array to provide a URL
 168       * template and additional variables to use in the URL template expansion.
 169       *
 170       * @param string|UriInterface $uri     URI object or string.
 171       * @param array               $options Request options to apply.
 172       */
 173      public function headAsync($uri, array $options = []): PromiseInterface
 174      {
 175          return $this->requestAsync('HEAD', $uri, $options);
 176      }
 177  
 178      /**
 179       * Create and send an asynchronous HTTP PUT request.
 180       *
 181       * Use an absolute path to override the base path of the client, or a
 182       * relative path to append to the base path of the client. The URL can
 183       * contain the query string as well. Use an array to provide a URL
 184       * template and additional variables to use in the URL template expansion.
 185       *
 186       * @param string|UriInterface $uri     URI object or string.
 187       * @param array               $options Request options to apply.
 188       */
 189      public function putAsync($uri, array $options = []): PromiseInterface
 190      {
 191          return $this->requestAsync('PUT', $uri, $options);
 192      }
 193  
 194      /**
 195       * Create and send an asynchronous HTTP POST request.
 196       *
 197       * Use an absolute path to override the base path of the client, or a
 198       * relative path to append to the base path of the client. The URL can
 199       * contain the query string as well. Use an array to provide a URL
 200       * template and additional variables to use in the URL template expansion.
 201       *
 202       * @param string|UriInterface $uri     URI object or string.
 203       * @param array               $options Request options to apply.
 204       */
 205      public function postAsync($uri, array $options = []): PromiseInterface
 206      {
 207          return $this->requestAsync('POST', $uri, $options);
 208      }
 209  
 210      /**
 211       * Create and send an asynchronous HTTP PATCH request.
 212       *
 213       * Use an absolute path to override the base path of the client, or a
 214       * relative path to append to the base path of the client. The URL can
 215       * contain the query string as well. Use an array to provide a URL
 216       * template and additional variables to use in the URL template expansion.
 217       *
 218       * @param string|UriInterface $uri     URI object or string.
 219       * @param array               $options Request options to apply.
 220       */
 221      public function patchAsync($uri, array $options = []): PromiseInterface
 222      {
 223          return $this->requestAsync('PATCH', $uri, $options);
 224      }
 225  
 226      /**
 227       * Create and send an asynchronous HTTP DELETE request.
 228       *
 229       * Use an absolute path to override the base path of the client, or a
 230       * relative path to append to the base path of the client. The URL can
 231       * contain the query string as well. Use an array to provide a URL
 232       * template and additional variables to use in the URL template expansion.
 233       *
 234       * @param string|UriInterface $uri     URI object or string.
 235       * @param array               $options Request options to apply.
 236       */
 237      public function deleteAsync($uri, array $options = []): PromiseInterface
 238      {
 239          return $this->requestAsync('DELETE', $uri, $options);
 240      }
 241  }