IClient.php 18 KB

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