From edac32bb986e8dab23a14a79317c2109bc523270 Mon Sep 17 00:00:00 2001 From: OpenGnSys Support Team Date: Thu, 30 Nov 2023 20:06:07 +0100 Subject: rest: add GET /room/info Add GET /room/info to obtain room information, this includes the name, gateway and netmask. curl -X GET -H "Authorization: $API_KEY" http://127.0.0.1:8888/room/info -d '{ "id" : 1 } --- src/rest.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) (limited to 'src/rest.c') diff --git a/src/rest.c b/src/rest.c index f177e4c..bd28b67 100644 --- a/src/rest.c +++ b/src/rest.c @@ -2038,6 +2038,67 @@ static int og_cmd_post_client_delete(json_t *element, return 0; } +static int og_cmd_get_room_info(json_t *element, + struct og_msg_params *params, + char *buffer_reply) +{ + struct og_room room = {}; + json_t *value, *root; + struct og_dbi *dbi; + uint32_t room_id; + const char *key; + int err = 0; + + struct og_buffer og_buffer = { + .data = buffer_reply + }; + + json_object_foreach(element, key, value) { + if (!strcmp(key, "id")) { + err = og_json_parse_uint(value, &room_id); + params->flags |= OG_REST_PARAM_ID; + } + + if (err < 0) + return err; + } + + if (!og_msg_params_validate(params, OG_REST_PARAM_ID)) + return -1; + + dbi = og_dbi_open(&ogconfig.db); + if (!dbi) { + syslog(LOG_ERR, "cannot open conection database (%s:%d)\n", + __func__, __LINE__); + return -1; + } + + if (og_dbi_get_room_info(dbi, &room, room_id)) { + og_dbi_close(dbi); + return -1; + } + + og_dbi_close(dbi); + + root = json_object(); + if (!root) + return -1; + + json_object_set_new(root, "id", json_integer(room.id)); + json_object_set_new(root, "name", json_string(room.name)); + json_object_set_new(root, "gateway", json_string(room.gateway)); + json_object_set_new(root, "netmask", json_string(room.netmask)); + + if (json_dump_callback(root, og_json_dump_clients, &og_buffer, 0)) { + json_decref(root); + return -1; + } + + json_decref(root); + return 0; +} + + static int og_cmd_stop(json_t *element, struct og_msg_params *params) { const char *key; @@ -6516,6 +6577,7 @@ struct { [OG_URI_CENTER_DELETE] = { "center/delete", }, [OG_URI_ROOM_ADD] = { "room/add", }, [OG_URI_ROOM_DELETE] = { "room/delete", }, + [OG_URI_ROOM_INFO] = { "room/info", }, [OG_URI_PROC_ADD] = { "procedure/add", }, [OG_URI_PROC_UPDATE] = { "procedure/update", }, [OG_URI_PROC_RUN] = { "procedure/run", }, @@ -7164,6 +7226,19 @@ int og_client_state_process_payload_rest(struct og_client *cli) goto err_process_rest_payload; } err = og_cmd_post_room_delete(root, ¶ms); + } else if (!strncmp(cmd, "room/info", strlen("room/info"))) { + if (method != OG_METHOD_GET) { + err = og_client_method_not_found(cli); + goto err_process_rest_payload; + } + if (!root) { + syslog(LOG_ERR, + "command room info with no payload\n"); + err = og_client_bad_request(cli); + goto err_process_rest_payload; + } + + err = og_cmd_get_room_info(root, ¶ms, buf_reply); } else if (!strncmp(cmd, "procedure/add", strlen("procedure/add"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); -- cgit v1.2.3-18-g5258