app_api.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace OCA\AppAPI\Service {
  3. use OCP\IRequest;
  4. class AppAPIService {
  5. /**
  6. * @param IRequest $request
  7. * @param bool $isDav
  8. *
  9. * @return bool
  10. */
  11. public function validateExAppRequestToNC(IRequest $request, bool $isDav = false): bool {}
  12. }
  13. }
  14. namespace OCA\AppAPI {
  15. use OCP\IRequest;
  16. use OCP\Http\Client\IPromise;
  17. use OCP\Http\Client\IResponse;
  18. class PublicFunctions {
  19. public function __construct(
  20. private readonly ExAppService $exAppService,
  21. private readonly AppAPIService $service,
  22. ) {
  23. }
  24. /**
  25. * Request to ExApp with AppAPI auth headers
  26. */
  27. public function exAppRequest(
  28. string $appId,
  29. string $route,
  30. ?string $userId = null,
  31. string $method = 'POST',
  32. array $params = [],
  33. array $options = [],
  34. ?IRequest $request = null,
  35. ): array|IResponse {
  36. $exApp = $this->exAppService->getExApp($appId);
  37. if ($exApp === null) {
  38. return ['error' => sprintf('ExApp `%s` not found', $appId)];
  39. }
  40. return $this->service->requestToExApp($exApp, $route, $userId, $method, $params, $options, $request);
  41. }
  42. /**
  43. * Async request to ExApp with AppAPI auth headers
  44. *
  45. * @throws \Exception if ExApp not found
  46. */
  47. public function asyncExAppRequest(
  48. string $appId,
  49. string $route,
  50. ?string $userId = null,
  51. string $method = 'POST',
  52. array $params = [],
  53. array $options = [],
  54. ?IRequest $request = null,
  55. ): IPromise {
  56. $exApp = $this->exAppService->getExApp($appId);
  57. if ($exApp === null) {
  58. throw new \Exception(sprintf('ExApp `%s` not found', $appId));
  59. }
  60. return $this->service->requestToExAppAsync($exApp, $route, $userId, $method, $params, $options, $request);
  61. }
  62. /**
  63. * Get basic ExApp info by appid
  64. *
  65. * @param string $appId
  66. *
  67. * @return array|null ExApp info (appid, version, name, enabled) or null if no ExApp found
  68. */
  69. public function getExApp(string $appId): ?array {
  70. $exApp = $this->exAppService->getExApp($appId);
  71. if ($exApp !== null) {
  72. $info = $exApp->jsonSerialize();
  73. return [
  74. 'appid' => $info['appid'],
  75. 'version' => $info['version'],
  76. 'name' => $info['name'],
  77. 'enabled' => $info['enabled'],
  78. ];
  79. }
  80. return null;
  81. }
  82. }
  83. }