From 748c85ae5db3b20d32aff4d9effe58153719e8f3 Mon Sep 17 00:00:00 2001 From: abdulmohsen Date: Sun, 28 Apr 2024 12:49:29 +0300 Subject: [PATCH] Added backend info/sessions/users/version API endpoint --- src/API/Backends/Info.php | 59 ++++++++++++++++++++++++++++++++ src/API/Backends/Sessions.php | 59 ++++++++++++++++++++++++++++++++ src/API/Backends/Users.php | 63 +++++++++++++++++++++++++++++++++++ src/API/Backends/Version.php | 59 ++++++++++++++++++++++++++++++++ 4 files changed, 240 insertions(+) create mode 100644 src/API/Backends/Info.php create mode 100644 src/API/Backends/Sessions.php create mode 100644 src/API/Backends/Users.php create mode 100644 src/API/Backends/Version.php diff --git a/src/API/Backends/Info.php b/src/API/Backends/Info.php new file mode 100644 index 00000000..f8faaa76 --- /dev/null +++ b/src/API/Backends/Info.php @@ -0,0 +1,59 @@ +getClient(name: $name); + } catch (InvalidArgumentException $e) { + return api_error($e->getMessage(), HTTP_STATUS::HTTP_NOT_FOUND); + } + + $opts = []; + $params = DataUtil::fromRequest($request, true); + + if (true === (bool)$params->get('raw', false)) { + $opts[Options::RAW_RESPONSE] = true; + } + + try { + $data = $client->getInfo($opts); + } catch (Throwable $e) { + return api_error($e->getMessage(), HTTP_STATUS::HTTP_INTERNAL_SERVER_ERROR); + } + + $apiUrl = $request->getUri()->withHost('')->withPort(0)->withScheme(''); + + $response = [ + 'data' => $data, + 'links' => [ + 'self' => (string)$apiUrl, + 'list' => (string)$apiUrl->withPath(parseConfigValue(Index::URL)), + ], + ]; + + return api_response(HTTP_STATUS::HTTP_OK, $response); + } +} diff --git a/src/API/Backends/Sessions.php b/src/API/Backends/Sessions.php new file mode 100644 index 00000000..1dd8417e --- /dev/null +++ b/src/API/Backends/Sessions.php @@ -0,0 +1,59 @@ +getClient(name: $name); + } catch (InvalidArgumentException $e) { + return api_error($e->getMessage(), HTTP_STATUS::HTTP_NOT_FOUND); + } + + $opts = []; + $params = DataUtil::fromRequest($request, true); + + if (true === (bool)$params->get('raw', false)) { + $opts[Options::RAW_RESPONSE] = true; + } + + try { + $sessions = $client->getSessions($opts); + } catch (Throwable $e) { + return api_error($e->getMessage(), HTTP_STATUS::HTTP_INTERNAL_SERVER_ERROR); + } + + $apiUrl = $request->getUri()->withHost('')->withPort(0)->withScheme(''); + + $response = [ + 'sessions' => ag($sessions, 'sessions', []), + 'links' => [ + 'self' => (string)$apiUrl, + 'list' => (string)$apiUrl->withPath(parseConfigValue(Index::URL)), + ], + ]; + + return api_response(HTTP_STATUS::HTTP_OK, $response); + } +} diff --git a/src/API/Backends/Users.php b/src/API/Backends/Users.php new file mode 100644 index 00000000..92efab7f --- /dev/null +++ b/src/API/Backends/Users.php @@ -0,0 +1,63 @@ +getClient(name: $name); + } catch (InvalidArgumentException $e) { + return api_error($e->getMessage(), HTTP_STATUS::HTTP_NOT_FOUND); + } + + $opts = []; + $params = DataUtil::fromRequest($request, true); + + if (true === (bool)$params->get('tokens', false)) { + $opts['tokens'] = true; + } + + if (true === (bool)$params->get('raw', false)) { + $opts[Options::RAW_RESPONSE] = true; + } + + try { + $users = $client->getUsersList($opts); + } catch (Throwable $e) { + return api_error($e->getMessage(), HTTP_STATUS::HTTP_INTERNAL_SERVER_ERROR); + } + + $apiUrl = $request->getUri()->withHost('')->withPort(0)->withScheme(''); + + $response = [ + 'users' => $users, + 'links' => [ + 'self' => (string)$apiUrl, + 'list' => (string)$apiUrl->withPath(parseConfigValue(Index::URL)), + ], + ]; + + return api_response(HTTP_STATUS::HTTP_OK, $response); + } +} diff --git a/src/API/Backends/Version.php b/src/API/Backends/Version.php new file mode 100644 index 00000000..1581c0e5 --- /dev/null +++ b/src/API/Backends/Version.php @@ -0,0 +1,59 @@ +getClient(name: $name); + } catch (InvalidArgumentException $e) { + return api_error($e->getMessage(), HTTP_STATUS::HTTP_NOT_FOUND); + } + + $opts = []; + $params = DataUtil::fromRequest($request, true); + + if (true === (bool)$params->get('raw', false)) { + $opts[Options::RAW_RESPONSE] = true; + } + + try { + $version = $client->getVersion($opts); + } catch (Throwable $e) { + return api_error($e->getMessage(), HTTP_STATUS::HTTP_INTERNAL_SERVER_ERROR); + } + + $apiUrl = $request->getUri()->withHost('')->withPort(0)->withScheme(''); + + $response = [ + 'version' => $version, + 'links' => [ + 'self' => (string)$apiUrl, + 'list' => (string)$apiUrl->withPath(parseConfigValue(Index::URL)), + ], + ]; + + return api_response(HTTP_STATUS::HTTP_OK, $response); + } +}