From 9891276246b96ca1bc43383b3e434a7d8f0ed91f Mon Sep 17 00:00:00 2001 From: OpenGnSys Support Team Date: Wed, 12 Jun 2024 11:37:36 +0200 Subject: rest: Add /center/info Add URI to allow a GET request to obtain info about a center (name, id and comment of the center as of now). To use this uri, simply send a GET request with a json containing the id of the center whose info needs to be consulted: curl -X GET -H "Authorization: $API_KEY" http://127.0.0.1:8888/center/info -d '{"id":1}' based on work from Javier Hernandez. --- src/dbi.c | 33 ++++++++++++++++++++++++++++ src/dbi.h | 8 +++++++ src/rest.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/rest.h | 1 + 4 files changed, 115 insertions(+) diff --git a/src/dbi.c b/src/dbi.c index 3d2608d..a3bee37 100644 --- a/src/dbi.c +++ b/src/dbi.c @@ -129,6 +129,39 @@ int og_dbi_get_computer_info(struct og_dbi *dbi, struct og_computer *computer, return 0; } +int og_dbi_get_center_info(struct og_dbi *dbi, struct og_center *center) +{ + const char *msglog; + dbi_result result; + + result = dbi_conn_queryf(dbi->conn, + "SELECT nombrecentro, idcentro, comentarios " + "FROM centros WHERE idcentro=%d", + center->id); + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", + __func__, __LINE__, msglog); + return -1; + } + + if (!dbi_result_next_row(result)) { + syslog(LOG_ERR, "center does not exist in database (%s:%d)\n", + __func__, __LINE__); + dbi_result_free(result); + return -1; + } + + snprintf(center->name, sizeof(center->name), "%s", + dbi_result_get_string(result, "nombrecentro")); + snprintf(center->comment, sizeof(center->comment), "%s", + dbi_result_get_string(result, "comentarios")); + + dbi_result_free(result); + + return 0; +} + int og_dbi_get_room_info(struct og_dbi *dbi, struct og_room *room, uint32_t room_id) { diff --git a/src/dbi.h b/src/dbi.h index 7286fe3..d6de474 100644 --- a/src/dbi.h +++ b/src/dbi.h @@ -41,6 +41,7 @@ void og_dbi_close(struct og_dbi *db); #define OG_DB_MAC_MAXLEN 15 #define OG_DB_IP_MAXLEN 15 #define OG_DB_SMALLINT_MAXLEN 6 +#define OG_DB_COMMENT_MAXLEN 250 struct og_image_legacy { char software_id[OG_DB_INT_MAXLEN + 1]; @@ -110,6 +111,12 @@ struct og_room { bool remote; }; +struct og_center { + uint32_t id; + char name[OG_DB_CENTER_NAME_MAXLEN + 1]; + char comment[OG_DB_COMMENT_MAXLEN + 1]; +}; + struct og_folder { unsigned int id; unsigned int room; @@ -128,6 +135,7 @@ int og_dbi_get_computer_info(struct og_dbi *dbi, struct og_computer *computer, struct in_addr addr); int og_dbi_get_room_info(struct og_dbi *dbi, struct og_room *room, uint32_t room_id); +int og_dbi_get_center_info(struct og_dbi *dbi, struct og_center *center); bool og_dbi_get_image(struct og_dbi *dbi, struct og_image *image); int og_dbi_add_image(struct og_dbi *dbi, struct og_image *image); diff --git a/src/rest.c b/src/rest.c index 4b85701..ec1dda6 100644 --- a/src/rest.c +++ b/src/rest.c @@ -1670,6 +1670,64 @@ static int og_cmd_post_client_server(json_t *element, return 0; } +static int og_cmd_get_center_info(json_t *element, + struct og_msg_params *params, + char *buffer_reply) +{ + struct og_center center = {}; + json_t *value, *root; + struct og_dbi *dbi; + 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, ¢er.id); + params->flags |= OG_REST_PARAM_CENTER; + } + if (err < 0) + return err; + } + if (!og_msg_params_validate(params, OG_REST_PARAM_CENTER)) + 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_center_info(dbi, ¢er)) { + og_dbi_close(dbi); + return -1; + } + og_dbi_close(dbi); + + root = json_object(); + if (!root) + return -1; + + json_object_set_new(root, "comment", + json_string(center.comment)); + json_object_set_new(root, "id", + json_integer(center.id)); + json_object_set_new(root, "name", + json_string(center.name)); + + 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_get_client_info(json_t *element, struct og_msg_params *params, char *buffer_reply) @@ -7510,6 +7568,7 @@ struct { [OG_URI_CENTER_ADD] = { "center/add", }, [OG_URI_CENTER_UPDATE] = { "center/update", }, [OG_URI_CENTER_DELETE] = { "center/delete", }, + [OG_URI_CENTER_INFO] = { "center/info", }, [OG_URI_ROOM_ADD] = { "room/add", }, [OG_URI_ROOM_UPDATE] = { "room/update", }, [OG_URI_ROOM_DELETE] = { "room/delete", }, @@ -8222,6 +8281,20 @@ int og_client_state_process_payload_rest(struct og_client *cli) goto err_process_rest_payload; } err = og_cmd_post_center_delete(root, ¶ms); + } else if (!strncmp(cmd, "center/info", + strlen("center/info"))) { + if (method != OG_METHOD_GET) { + err = og_client_method_not_found(cli); + goto err_process_rest_payload; + } + if (!root) { + syslog(LOG_ERR, + "command client info with no payload\n"); + err = og_client_bad_request(cli); + goto err_process_rest_payload; + } + + err = og_cmd_get_center_info(root, ¶ms, buf_reply); } else if (!strncmp(cmd, "center/update", strlen("center/update"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); diff --git a/src/rest.h b/src/rest.h index 1c5cc47..86cb763 100644 --- a/src/rest.h +++ b/src/rest.h @@ -131,6 +131,7 @@ enum og_rest_uri { OG_URI_CENTER_ADD, OG_URI_CENTER_UPDATE, OG_URI_CENTER_DELETE, + OG_URI_CENTER_INFO, OG_URI_ROOM_ADD, OG_URI_ROOM_UPDATE, OG_URI_ROOM_DELETE, -- cgit v1.2.3-18-g5258