add: response macros
add: preparation for more macro endpoints
This commit is contained in:
parent
9d2c48d645
commit
01ffb1d58d
26 changed files with 482 additions and 353 deletions
src/endpoints
|
@ -18,10 +18,7 @@ api_v1_controllers_STR_GET(struct mg_connection *nc, struct http_message *hm, en
|
|||
uuid_t target_uid;
|
||||
if(uuid_parse(args[0].value.v_str, target_uid))
|
||||
{
|
||||
LOGGER_DEBUG("failed to unparse uid\n");
|
||||
|
||||
static const char content[] = "given id was invalid";
|
||||
endpoint_response_text(response, 400, content, STRLEN(content));
|
||||
M_RESPONSE_400_NO_VALID_ID(response);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -29,10 +26,7 @@ api_v1_controllers_STR_GET(struct mg_connection *nc, struct http_message *hm, en
|
|||
|
||||
if(!controller)
|
||||
{
|
||||
LOGGER_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str);
|
||||
|
||||
static const char content[] = "no controller for id found";
|
||||
endpoint_response_text(response, 404, content, STRLEN(content));
|
||||
M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 404, "no controller found for id");
|
||||
return;
|
||||
}
|
||||
LOGGER_DEBUG("returning controller for uid '%s'\n", args[0].value.v_str);
|
||||
|
@ -53,10 +47,7 @@ api_v1_controllers_STR_PUT(struct mg_connection *nc, struct http_message *hm, en
|
|||
uuid_t target_uid;
|
||||
if(uuid_parse(args[0].value.v_str, target_uid))
|
||||
{
|
||||
LOGGER_DEBUG("failed to unparse uid\n");
|
||||
|
||||
static const char content[] = "given id was invalid";
|
||||
endpoint_response_text(response, 400, content, STRLEN(content));
|
||||
M_RESPONSE_400_NO_VALID_ID(response);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -64,10 +55,7 @@ api_v1_controllers_STR_PUT(struct mg_connection *nc, struct http_message *hm, en
|
|||
|
||||
if(!controller)
|
||||
{
|
||||
LOGGER_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str);
|
||||
|
||||
static const char content[] = "no controller for id found";
|
||||
endpoint_response_text(response, 404, content, STRLEN(content));
|
||||
M_RESPONSE_404_NO_CONTROLLER_FOUND_FOR_ID(response);
|
||||
return;
|
||||
}
|
||||
LOGGER_DEBUG("starting overwrite for controller %s\n", args[0].value.v_str);
|
||||
|
@ -76,70 +64,64 @@ api_v1_controllers_STR_PUT(struct mg_connection *nc, struct http_message *hm, en
|
|||
|
||||
if(json == NULL)
|
||||
{
|
||||
static const char content[] = "no valid json was supplied";
|
||||
endpoint_response_text(response, 400, content, STRLEN(content));
|
||||
controller_free(controller);
|
||||
|
||||
M_RESPONSE_400_NO_VALID_JSON(response);
|
||||
return;
|
||||
}
|
||||
|
||||
cJSON *json_name = cJSON_GetObjectItemCaseSensitive(json, "name");
|
||||
if(json_name)
|
||||
{
|
||||
if(cJSON_IsString(json_name) && json_name->valuestring)
|
||||
if(!cJSON_IsString(json_name) || json_name->valuestring == NULL)
|
||||
{
|
||||
strncpy(controller->name, json_name->valuestring, MAX_NAME_LENGTH);
|
||||
controller->name[MAX_NAME_LENGTH] = '\0';
|
||||
LOGGER_DEBUG("new controller name: %s\n", controller->name);
|
||||
}
|
||||
else
|
||||
{
|
||||
static const char content[] = "the given name is no valid string";
|
||||
endpoint_response_text(response, 400, content, STRLEN(content));
|
||||
cJSON_Delete(json);
|
||||
controller_free(controller);
|
||||
|
||||
M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the given name is invalid");
|
||||
return;
|
||||
}
|
||||
|
||||
strncpy(controller->name, json_name->valuestring, MAX_NAME_LENGTH);
|
||||
controller->name[MAX_NAME_LENGTH] = '\0';
|
||||
LOGGER_DEBUG("new controller name: %s\n", controller->name);
|
||||
}
|
||||
|
||||
cJSON *json_ip = cJSON_GetObjectItemCaseSensitive(json, "ip");
|
||||
if(json_ip)
|
||||
{
|
||||
if(cJSON_IsString(json_ip) && json_ip->valuestring)
|
||||
if(!cJSON_IsString(json_ip) || json_ip->valuestring == NULL)
|
||||
{
|
||||
unsigned char buf[sizeof(struct in_addr)];
|
||||
if(inet_pton(AF_INET, json_ip->valuestring, buf))
|
||||
{
|
||||
strncpy(controller->ip, json_ip->valuestring, IP_LENGTH);
|
||||
controller->ip[IP_LENGTH] = '\0';
|
||||
LOGGER_DEBUG("new controller ip: %s\n", controller->ip);
|
||||
}
|
||||
else
|
||||
{
|
||||
static const char content[] = "the given ip address is no valid IPv4 address";
|
||||
endpoint_response_text(response, 400, content, STRLEN(content));
|
||||
cJSON_Delete(json);
|
||||
controller_free(controller);
|
||||
return;
|
||||
}
|
||||
cJSON_Delete(json);
|
||||
controller_free(controller);
|
||||
|
||||
M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "no ip address given");
|
||||
return;
|
||||
}
|
||||
|
||||
unsigned char buf[sizeof(struct in_addr)];
|
||||
if(inet_pton(AF_INET, json_ip->valuestring, buf))
|
||||
{
|
||||
strncpy(controller->ip, json_ip->valuestring, IP_LENGTH);
|
||||
controller->ip[IP_LENGTH] = '\0';
|
||||
LOGGER_DEBUG("new controller ip: %s\n", controller->ip);
|
||||
}
|
||||
else
|
||||
{
|
||||
static const char content[] = "the given ip address is no valid string";
|
||||
endpoint_response_text(response, 400, content, STRLEN(content));
|
||||
cJSON_Delete(json);
|
||||
controller_free(controller);
|
||||
|
||||
M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the given ip address is not valid");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if(controller_save(controller))
|
||||
{
|
||||
LOGGER_ERR("failed to save controller\n");
|
||||
controller_free(controller);
|
||||
cJSON_Delete(json);
|
||||
|
||||
static const char content[] = "failed to save controller to database";
|
||||
endpoint_response_text(response, 500, content, STRLEN(content));
|
||||
M_RESPONSE_TEXT_STATIC(LOGGER_ERR, response, 500, "failed to save controller to database");
|
||||
return;
|
||||
}
|
||||
LOGGER_DEBUG("saved controller %s\n", args[0].value.v_str);
|
||||
|
@ -165,10 +147,7 @@ api_v1_controllers_STR_DELETE(struct mg_connection *nc, struct http_message *hm,
|
|||
uuid_t target_uid;
|
||||
if(uuid_parse(target_uid_str, target_uid))
|
||||
{
|
||||
LOGGER_DEBUG("failed to unparse uid\n");
|
||||
|
||||
static const char content[] = "given id was invalid";
|
||||
endpoint_response_text(response, 400, content, STRLEN(content));
|
||||
M_RESPONSE_400_NO_VALID_ID(response);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -176,25 +155,17 @@ api_v1_controllers_STR_DELETE(struct mg_connection *nc, struct http_message *hm,
|
|||
|
||||
if(!controller)
|
||||
{
|
||||
LOGGER_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str);
|
||||
|
||||
static const char content[] = "no controller for id found";
|
||||
endpoint_response_text(response, 404, content, STRLEN(content));
|
||||
M_RESPONSE_404_NO_CONTROLLER_FOUND_FOR_ID(response);
|
||||
return;
|
||||
}
|
||||
|
||||
if(controller_remove(controller))
|
||||
{
|
||||
LOGGER_ERR("failed to remove controller from database\n");
|
||||
|
||||
static const char content[] = "failed to remove controller from database";
|
||||
endpoint_response_text(response, 500, content, STRLEN(content));
|
||||
M_RESPONSE_TEXT_STATIC(LOGGER_ERR, response, 500, "failed to remove controller from database");
|
||||
}
|
||||
else
|
||||
{
|
||||
LOGGER_DEBUG("deleted controller %s\n", args[0].value.v_str);
|
||||
static const char content[] = "delete controller";
|
||||
endpoint_response_text(response, 200, content, STRLEN(content));
|
||||
M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 200, "deleted controller");
|
||||
}
|
||||
controller_free(controller);
|
||||
return;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue