Skip to content

Commit

Permalink
Add unpair client to API
Browse files Browse the repository at this point in the history
  • Loading branch information
salty2011 committed Jan 23, 2025
1 parent 3fd4a01 commit 73b4796
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/moonlight-server/api/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ struct PairRequest {
rfl::Description<"The PIN created by the remote Moonlight client", std::string> pin;
};

struct UnpairClientRequest {
std::string client_id;
rfl::Description<"The client ID to unpair", std::string> client_id;
};

struct GenericSuccessResponse {
bool success = true;
};
Expand Down Expand Up @@ -114,7 +119,7 @@ class UnixSocketServer {
void endpoint_Apps(const HTTPRequest &req, std::shared_ptr<UnixSocket> socket);
void endpoint_AddApp(const HTTPRequest &req, std::shared_ptr<UnixSocket> socket);
void endpoint_RemoveApp(const HTTPRequest &req, std::shared_ptr<UnixSocket> socket);

void endpoint_UnpairClient(const HTTPRequest &req, std::shared_ptr<UnixSocket> socket);
void endpoint_StreamSessions(const HTTPRequest &req, std::shared_ptr<UnixSocket> socket);
void endpoint_StreamSessionAdd(const HTTPRequest &req, std::shared_ptr<UnixSocket> socket);
void endpoint_StreamSessionStart(const HTTPRequest &req, std::shared_ptr<UnixSocket> socket);
Expand Down
26 changes: 26 additions & 0 deletions src/moonlight-server/api/endpoints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,32 @@ void UnixSocketServer::endpoint_PairedClients(const HTTPRequest &req, std::share
send_http(socket, 200, rfl::json::write(res));
}

void UnixSocketServer::endpoint_UnpairClient(const HTTPRequest &req, std::shared_ptr<UnixSocket> socket) {
try {
auto payload = rfl::json::read<UnpairClientRequest>(req.body);
if (!payload) {
auto res = GenericErrorResponse{.error = "Invalid request format"};
send_http(socket, 400, rfl::json::write(res));
return;
}

auto client = state::get_client_by_id(this->state_->app_state->config, std::stoul(payload->client_id));
if (!client) {
auto res = GenericErrorResponse{.error = "Client not found"};
send_http(socket, 404, rfl::json::write(res));
return;
}

state::unpair(this->state_->app_state->config, *client);

auto res = GenericSuccessResponse{.success = true};
send_http(socket, 200, rfl::json::write(res));
} catch (const std::exception &e) {
auto res = GenericErrorResponse{.error = e.what()};
send_http(socket, 500, rfl::json::write(res));
}
}

void UnixSocketServer::endpoint_Apps(const HTTPRequest &req, std::shared_ptr<UnixSocket> socket) {
auto res = AppListResponse{.success = true};
auto apps = state_->app_state->config->apps->load();
Expand Down

0 comments on commit 73b4796

Please sign in to comment.