IClient.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OCP\Http\Client;
  9. /**
  10. * Interface IClient
  11. *
  12. * @since 8.1.0
  13. */
  14. interface IClient {
  15. /**
  16. * Default request timeout for requests
  17. *
  18. * @since 31.0.0
  19. */
  20. public const DEFAULT_REQUEST_TIMEOUT = 30;
  21. /**
  22. * Sends a GET request
  23. * @param string $uri
  24. * @param array $options Array such as
  25. * 'query' => [
  26. * 'field' => 'abc',
  27. * 'other_field' => '123',
  28. * 'file_name' => fopen('/path/to/file', 'r'),
  29. * ],
  30. * 'headers' => [
  31. * 'foo' => 'bar',
  32. * ],
  33. * 'cookies' => [
  34. * 'foo' => 'bar',
  35. * ],
  36. * 'allow_redirects' => [
  37. * 'max' => 10, // allow at most 10 redirects.
  38. * 'strict' => true, // use "strict" RFC compliant redirects.
  39. * 'referer' => true, // add a Referer header
  40. * 'protocols' => ['https'] // only allow https URLs
  41. * ],
  42. * 'sink' => '/path/to/file', // save to a file or a stream
  43. * 'verify' => true, // bool or string to CA file
  44. * 'debug' => true,
  45. * @return IResponse
  46. * @throws \Exception If the request could not get completed
  47. * @since 8.1.0
  48. */
  49. public function get(string $uri, array $options = []): IResponse;
  50. /**
  51. * Sends a HEAD request
  52. * @param string $uri
  53. * @param array $options Array such as
  54. * 'headers' => [
  55. * 'foo' => 'bar',
  56. * ],
  57. * 'cookies' => [
  58. * 'foo' => 'bar',
  59. * ],
  60. * 'allow_redirects' => [
  61. * 'max' => 10, // allow at most 10 redirects.
  62. * 'strict' => true, // use "strict" RFC compliant redirects.
  63. * 'referer' => true, // add a Referer header
  64. * 'protocols' => ['https'] // only allow https URLs
  65. * ],
  66. * 'sink' => '/path/to/file', // save to a file or a stream
  67. * 'verify' => true, // bool or string to CA file
  68. * 'debug' => true,
  69. * @return IResponse
  70. * @throws \Exception If the request could not get completed
  71. * @since 8.1.0
  72. */
  73. public function head(string $uri, array $options = []): IResponse;
  74. /**
  75. * Sends a POST request
  76. * @param string $uri
  77. * @param array $options Array such as
  78. * 'body' => [
  79. * 'field' => 'abc',
  80. * 'other_field' => '123',
  81. * 'file_name' => fopen('/path/to/file', 'r'),
  82. * ],
  83. * 'headers' => [
  84. * 'foo' => 'bar',
  85. * ],
  86. * 'cookies' => [
  87. * 'foo' => 'bar',
  88. * ],
  89. * 'allow_redirects' => [
  90. * 'max' => 10, // allow at most 10 redirects.
  91. * 'strict' => true, // use "strict" RFC compliant redirects.
  92. * 'referer' => true, // add a Referer header
  93. * 'protocols' => ['https'] // only allow https URLs
  94. * ],
  95. * 'sink' => '/path/to/file', // save to a file or a stream
  96. * 'verify' => true, // bool or string to CA file
  97. * 'debug' => true,
  98. * @return IResponse
  99. * @throws \Exception If the request could not get completed
  100. * @since 8.1.0
  101. */
  102. public function post(string $uri, array $options = []): IResponse;
  103. /**
  104. * Sends a PUT request
  105. * @param string $uri
  106. * @param array $options Array such as
  107. * 'body' => [
  108. * 'field' => 'abc',
  109. * 'other_field' => '123',
  110. * 'file_name' => fopen('/path/to/file', 'r'),
  111. * ],
  112. * 'headers' => [
  113. * 'foo' => 'bar',
  114. * ],
  115. * 'cookies' => [
  116. * 'foo' => 'bar',
  117. * ],
  118. * 'allow_redirects' => [
  119. * 'max' => 10, // allow at most 10 redirects.
  120. * 'strict' => true, // use "strict" RFC compliant redirects.
  121. * 'referer' => true, // add a Referer header
  122. * 'protocols' => ['https'] // only allow https URLs
  123. * ],
  124. * 'sink' => '/path/to/file', // save to a file or a stream
  125. * 'verify' => true, // bool or string to CA file
  126. * 'debug' => true,
  127. * @return IResponse
  128. * @throws \Exception If the request could not get completed
  129. * @since 8.1.0
  130. */
  131. public function put(string $uri, array $options = []): IResponse;
  132. /**
  133. * Sends a PATCH request
  134. * @param string $uri
  135. * @param array $options Array such as
  136. * 'body' => [
  137. * 'field' => 'abc',
  138. * 'other_field' => '123',
  139. * 'file_name' => fopen('/path/to/file', 'r'),
  140. * ],
  141. * 'headers' => [
  142. * 'foo' => 'bar',
  143. * ],
  144. * 'cookies' => [
  145. * 'foo' => 'bar',
  146. * ],
  147. * 'allow_redirects' => [
  148. * 'max' => 10, // allow at most 10 redirects.
  149. * 'strict' => true, // use "strict" RFC compliant redirects.
  150. * 'referer' => true, // add a Referer header
  151. * 'protocols' => ['https'] // only allow https URLs
  152. * ],
  153. * 'sink' => '/path/to/file', // save to a file or a stream
  154. * 'verify' => true, // bool or string to CA file
  155. * 'debug' => true,
  156. * @return IResponse
  157. * @throws \Exception If the request could not get completed
  158. * @since 29.0.0
  159. */
  160. public function patch(string $uri, array $options = []): IResponse;
  161. /**
  162. * Sends a DELETE request
  163. * @param string $uri
  164. * @param array $options Array such as
  165. * 'body' => [
  166. * 'field' => 'abc',
  167. * 'other_field' => '123',
  168. * 'file_name' => fopen('/path/to/file', 'r'),
  169. * ],
  170. * 'headers' => [
  171. * 'foo' => 'bar',
  172. * ],
  173. * 'cookies' => [
  174. * 'foo' => 'bar',
  175. * ],
  176. * 'allow_redirects' => [
  177. * 'max' => 10, // allow at most 10 redirects.
  178. * 'strict' => true, // use "strict" RFC compliant redirects.
  179. * 'referer' => true, // add a Referer header
  180. * 'protocols' => ['https'] // only allow https URLs
  181. * ],
  182. * 'sink' => '/path/to/file', // save to a file or a stream
  183. * 'verify' => true, // bool or string to CA file
  184. * 'debug' => true,
  185. * @return IResponse
  186. * @throws \Exception If the request could not get completed
  187. * @since 8.1.0
  188. */
  189. public function delete(string $uri, array $options = []): IResponse;
  190. /**
  191. * Sends an OPTIONS request
  192. * @param string $uri
  193. * @param array $options Array such as
  194. * 'body' => [
  195. * 'field' => 'abc',
  196. * 'other_field' => '123',
  197. * 'file_name' => fopen('/path/to/file', 'r'),
  198. * ],
  199. * 'headers' => [
  200. * 'foo' => 'bar',
  201. * ],
  202. * 'cookies' => [
  203. * 'foo' => 'bar',
  204. * ],
  205. * 'allow_redirects' => [
  206. * 'max' => 10, // allow at most 10 redirects.
  207. * 'strict' => true, // use "strict" RFC compliant redirects.
  208. * 'referer' => true, // add a Referer header
  209. * 'protocols' => ['https'] // only allow https URLs
  210. * ],
  211. * 'sink' => '/path/to/file', // save to a file or a stream
  212. * 'verify' => true, // bool or string to CA file
  213. * 'debug' => true,
  214. * @return IResponse
  215. * @throws \Exception If the request could not get completed
  216. * @since 8.1.0
  217. */
  218. public function options(string $uri, array $options = []): IResponse;
  219. /**
  220. * Get the response of a Throwable thrown by the request methods when possible
  221. *
  222. * @param \Throwable $e
  223. * @return IResponse
  224. * @throws \Throwable When $e did not have a response
  225. * @since 29.0.0
  226. */
  227. public function getResponseFromThrowable(\Throwable $e): IResponse;
  228. /**
  229. * Sends a HTTP request
  230. * @param string $method The HTTP method to use
  231. * @param string $uri
  232. * @param array $options Array such as
  233. * 'query' => [
  234. * 'field' => 'abc',
  235. * 'other_field' => '123',
  236. * 'file_name' => fopen('/path/to/file', 'r'),
  237. * ],
  238. * 'headers' => [
  239. * 'foo' => 'bar',
  240. * ],
  241. * 'cookies' => [
  242. * 'foo' => 'bar',
  243. * ],
  244. * 'allow_redirects' => [
  245. * 'max' => 10, // allow at most 10 redirects.
  246. * 'strict' => true, // use "strict" RFC compliant redirects.
  247. * 'referer' => true, // add a Referer header
  248. * 'protocols' => ['https'] // only allow https URLs
  249. * ],
  250. * 'sink' => '/path/to/file', // save to a file or a stream
  251. * 'verify' => true, // bool or string to CA file
  252. * 'debug' => true,
  253. * @return IResponse
  254. * @throws \Exception If the request could not get completed
  255. * @since 29.0.0
  256. */
  257. public function request(string $method, string $uri, array $options = []): IResponse;
  258. /**
  259. * Sends an asynchronous GET request
  260. * @param string $uri
  261. * @param array $options Array such as
  262. * 'query' => [
  263. * 'field' => 'abc',
  264. * 'other_field' => '123',
  265. * 'file_name' => fopen('/path/to/file', 'r'),
  266. * ],
  267. * 'headers' => [
  268. * 'foo' => 'bar',
  269. * ],
  270. * 'cookies' => [
  271. * 'foo' => 'bar',
  272. * ],
  273. * 'allow_redirects' => [
  274. * 'max' => 10, // allow at most 10 redirects.
  275. * 'strict' => true, // use "strict" RFC compliant redirects.
  276. * 'referer' => true, // add a Referer header
  277. * 'protocols' => ['https'] // only allow https URLs
  278. * ],
  279. * 'sink' => '/path/to/file', // save to a file or a stream
  280. * 'verify' => true, // bool or string to CA file
  281. * 'debug' => true,
  282. * @return IPromise
  283. * @since 28.0.0
  284. */
  285. public function getAsync(string $uri, array $options = []): IPromise;
  286. /**
  287. * Sends an asynchronous HEAD request
  288. * @param string $uri
  289. * @param array $options Array such as
  290. * 'headers' => [
  291. * 'foo' => 'bar',
  292. * ],
  293. * 'cookies' => [
  294. * 'foo' => 'bar',
  295. * ],
  296. * 'allow_redirects' => [
  297. * 'max' => 10, // allow at most 10 redirects.
  298. * 'strict' => true, // use "strict" RFC compliant redirects.
  299. * 'referer' => true, // add a Referer header
  300. * 'protocols' => ['https'] // only allow https URLs
  301. * ],
  302. * 'sink' => '/path/to/file', // save to a file or a stream
  303. * 'verify' => true, // bool or string to CA file
  304. * 'debug' => true,
  305. * @return IPromise
  306. * @since 28.0.0
  307. */
  308. public function headAsync(string $uri, array $options = []): IPromise;
  309. /**
  310. * Sends an asynchronous POST request
  311. * @param string $uri
  312. * @param array $options Array such as
  313. * 'body' => [
  314. * 'field' => 'abc',
  315. * 'other_field' => '123',
  316. * 'file_name' => fopen('/path/to/file', 'r'),
  317. * ],
  318. * 'headers' => [
  319. * 'foo' => 'bar',
  320. * ],
  321. * 'cookies' => [
  322. * 'foo' => 'bar',
  323. * ],
  324. * 'allow_redirects' => [
  325. * 'max' => 10, // allow at most 10 redirects.
  326. * 'strict' => true, // use "strict" RFC compliant redirects.
  327. * 'referer' => true, // add a Referer header
  328. * 'protocols' => ['https'] // only allow https URLs
  329. * ],
  330. * 'sink' => '/path/to/file', // save to a file or a stream
  331. * 'verify' => true, // bool or string to CA file
  332. * 'debug' => true,
  333. * @return IPromise
  334. * @since 28.0.0
  335. */
  336. public function postAsync(string $uri, array $options = []): IPromise;
  337. /**
  338. * Sends an asynchronous PUT request
  339. * @param string $uri
  340. * @param array $options Array such as
  341. * 'body' => [
  342. * 'field' => 'abc',
  343. * 'other_field' => '123',
  344. * 'file_name' => fopen('/path/to/file', 'r'),
  345. * ],
  346. * 'headers' => [
  347. * 'foo' => 'bar',
  348. * ],
  349. * 'cookies' => [
  350. * 'foo' => 'bar',
  351. * ],
  352. * 'allow_redirects' => [
  353. * 'max' => 10, // allow at most 10 redirects.
  354. * 'strict' => true, // use "strict" RFC compliant redirects.
  355. * 'referer' => true, // add a Referer header
  356. * 'protocols' => ['https'] // only allow https URLs
  357. * ],
  358. * 'sink' => '/path/to/file', // save to a file or a stream
  359. * 'verify' => true, // bool or string to CA file
  360. * 'debug' => true,
  361. * @return IPromise
  362. * @since 28.0.0
  363. */
  364. public function putAsync(string $uri, array $options = []): IPromise;
  365. /**
  366. * Sends an asynchronous DELETE request
  367. * @param string $uri
  368. * @param array $options Array such as
  369. * 'body' => [
  370. * 'field' => 'abc',
  371. * 'other_field' => '123',
  372. * 'file_name' => fopen('/path/to/file', 'r'),
  373. * ],
  374. * 'headers' => [
  375. * 'foo' => 'bar',
  376. * ],
  377. * 'cookies' => [
  378. * 'foo' => 'bar',
  379. * ],
  380. * 'allow_redirects' => [
  381. * 'max' => 10, // allow at most 10 redirects.
  382. * 'strict' => true, // use "strict" RFC compliant redirects.
  383. * 'referer' => true, // add a Referer header
  384. * 'protocols' => ['https'] // only allow https URLs
  385. * ],
  386. * 'sink' => '/path/to/file', // save to a file or a stream
  387. * 'verify' => true, // bool or string to CA file
  388. * 'debug' => true,
  389. * @return IPromise
  390. * @since 28.0.0
  391. */
  392. public function deleteAsync(string $uri, array $options = []): IPromise;
  393. /**
  394. * Sends an asynchronous OPTIONS request
  395. * @param string $uri
  396. * @param array $options Array such as
  397. * 'body' => [
  398. * 'field' => 'abc',
  399. * 'other_field' => '123',
  400. * 'file_name' => fopen('/path/to/file', 'r'),
  401. * ],
  402. * 'headers' => [
  403. * 'foo' => 'bar',
  404. * ],
  405. * 'cookies' => [
  406. * 'foo' => 'bar',
  407. * ],
  408. * 'allow_redirects' => [
  409. * 'max' => 10, // allow at most 10 redirects.
  410. * 'strict' => true, // use "strict" RFC compliant redirects.
  411. * 'referer' => true, // add a Referer header
  412. * 'protocols' => ['https'] // only allow https URLs
  413. * ],
  414. * 'sink' => '/path/to/file', // save to a file or a stream
  415. * 'verify' => true, // bool or string to CA file
  416. * 'debug' => true,
  417. * @return IPromise
  418. * @since 28.0.0
  419. */
  420. public function optionsAsync(string $uri, array $options = []): IPromise;
  421. }