ApiBase.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace OC\Remote\Api;
  22. use OCP\Http\Client\IClientService;
  23. use OCP\Remote\ICredentials;
  24. use OCP\Remote\IInstance;
  25. class ApiBase {
  26. /** @var IInstance */
  27. private $instance;
  28. /** @var ICredentials */
  29. private $credentials;
  30. /** @var IClientService */
  31. private $clientService;
  32. public function __construct(IInstance $instance, ICredentials $credentials, IClientService $clientService) {
  33. $this->instance = $instance;
  34. $this->credentials = $credentials;
  35. $this->clientService = $clientService;
  36. }
  37. protected function getHttpClient() {
  38. return $this->clientService->newClient();
  39. }
  40. protected function addDefaultHeaders(array $headers) {
  41. return array_merge([
  42. 'OCS-APIREQUEST' => 'true',
  43. 'Accept' => 'application/json'
  44. ], $headers);
  45. }
  46. /**
  47. * @param string $method
  48. * @param string $url
  49. * @param array $body
  50. * @param array $query
  51. * @param array $headers
  52. * @return resource|string
  53. * @throws \InvalidArgumentException
  54. */
  55. protected function request($method, $url, array $body = [], array $query = [], array $headers = []) {
  56. $fullUrl = trim($this->instance->getFullUrl(), '/') . '/' . $url;
  57. $options = [
  58. 'query' => $query,
  59. 'headers' => $this->addDefaultHeaders($headers),
  60. 'auth' => [$this->credentials->getUsername(), $this->credentials->getPassword()]
  61. ];
  62. if ($body) {
  63. $options['body'] = $body;
  64. }
  65. $client = $this->getHttpClient();
  66. switch ($method) {
  67. case 'get':
  68. $response = $client->get($fullUrl, $options);
  69. break;
  70. case 'post':
  71. $response = $client->post($fullUrl, $options);
  72. break;
  73. case 'put':
  74. $response = $client->put($fullUrl, $options);
  75. break;
  76. case 'delete':
  77. $response = $client->delete($fullUrl, $options);
  78. break;
  79. case 'options':
  80. $response = $client->options($fullUrl, $options);
  81. break;
  82. default:
  83. throw new \InvalidArgumentException('Invalid method ' . $method);
  84. }
  85. return $response->getBody();
  86. }
  87. }