You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.7 KiB

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