Skip to content

Commit

Permalink
API to list paired clients
Browse files Browse the repository at this point in the history
  • Loading branch information
xanderfrangos committed Jan 20, 2024
1 parent 223fdfa commit 4b9f9d0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/confighttp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,28 @@ namespace confighttp {
outputTree.put("status", true);
}

void
listClients(resp_https_t response, req_https_t request) {
if (!authenticate(response, request)) return;

print_req(request);

pt::ptree named_certs = nvhttp::get_all_clients();

pt::ptree outputTree;

outputTree.put("status", false);

auto g = util::fail_guard([&]() {
std::ostringstream data;
pt::write_json(data, outputTree);
response->write(data.str());
});

outputTree.add_child("named_certs", named_certs);
outputTree.put("status", true);
}

void
closeApp(resp_https_t response, req_https_t request) {
if (!authenticate(response, request)) return;
Expand Down Expand Up @@ -746,6 +768,7 @@ namespace confighttp {
server.resource["^/api/password$"]["POST"] = savePassword;
server.resource["^/api/apps/([0-9]+)$"]["DELETE"] = deleteApp;
server.resource["^/api/clients/unpair$"]["POST"] = unpairAll;
server.resource["^/api/clients/list$"]["GET"] = listClients;
server.resource["^/api/apps/close$"]["POST"] = closeApp;
server.resource["^/api/covers/upload$"]["POST"] = uploadCover;
server.resource["^/images/sunshine.ico$"]["GET"] = getFaviconImage;
Expand Down
15 changes: 15 additions & 0 deletions src/nvhttp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,21 @@ namespace nvhttp {
response->close_connection_after_response = true;
}

pt::ptree
get_all_clients() {
pt::ptree named_cert_nodes;
for (auto &[_, client] : map_id_client) {
for (auto &named_cert : client.named_certs) {
pt::ptree named_cert_node;
named_cert_node.put("name"s, named_cert.name);
named_cert_node.put("uniqueID"s, named_cert.uniqueID);
named_cert_nodes.push_back(std::make_pair(""s, named_cert_node));
}
}

return named_cert_nodes;
}

void
applist(resp_https_t response, req_https_t request) {
print_req<SimpleWeb::HTTPS>(request);
Expand Down
5 changes: 5 additions & 0 deletions src/nvhttp.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
// standard includes
#include <string>

// lib includes
#include <boost/property_tree/ptree.hpp>

// local includes
#include "thread_safe.h"

Expand Down Expand Up @@ -44,6 +47,8 @@ namespace nvhttp {
start();
bool
pin(std::string pin, std::string name);
boost::property_tree::ptree
get_all_clients();
void
erase_all_clients();
} // namespace nvhttp

0 comments on commit 4b9f9d0

Please sign in to comment.