1
0

IClient.php 17 KB

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