add: endpoint responses
This commit is contained in:
parent
5a19e99627
commit
5e64f5963c
18 changed files with 587 additions and 235 deletions
|
@ -1,4 +1,5 @@
|
|||
#include <cJSON.h>
|
||||
#include <macros.h>
|
||||
#include <constants.h>
|
||||
#include <endpoints/api_v1_controllers.h>
|
||||
#include <logger.h>
|
||||
|
@ -7,7 +8,7 @@
|
|||
#include <models/tag.h>
|
||||
|
||||
void
|
||||
api_v1_controllers_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
|
||||
api_v1_controllers_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
|
||||
{
|
||||
(void)args;
|
||||
(void)hm;
|
||||
|
@ -26,14 +27,21 @@ api_v1_controllers_GET(struct mg_connection *c, endpoint_args_t *args, struct ht
|
|||
if (json_str == NULL)
|
||||
{
|
||||
LOG_ERROR("failed to print controllers json\n");
|
||||
mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "[]");
|
||||
|
||||
static const char content[] = "failed to print json for controllers";
|
||||
response->status_code = 500;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "%s", json_str);
|
||||
free(json_str);
|
||||
response->status_code = 200;
|
||||
response->content_type = "application/json";
|
||||
response->content_length = strlen(json_str);
|
||||
response->content = json_str;
|
||||
response->alloced_content = true;
|
||||
}
|
||||
cJSON_Delete(json);
|
||||
controller_free_list(all_controllers);
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include <cJSON.h>
|
||||
#include <macros.h>
|
||||
#include <command.h>
|
||||
#include <constants.h>
|
||||
#include <endpoints/api_v1_controllers.h>
|
||||
|
@ -7,16 +8,21 @@
|
|||
#include <models/tag.h>
|
||||
|
||||
void
|
||||
api_v1_controllers_STR_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
|
||||
api_v1_controllers_STR_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
|
||||
{
|
||||
(void)hm;
|
||||
|
||||
uuid_t target_uid;
|
||||
if(uuid_parse(args[0].value.v_str, target_uid))
|
||||
{
|
||||
LOG_ERROR("failed to unparse uid\n");
|
||||
mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
LOG_DEBUG("failed to unparse uid\n");
|
||||
|
||||
static const char content[] = "given id was invalid";
|
||||
response->status_code = 400;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -24,42 +30,60 @@ api_v1_controllers_STR_GET(struct mg_connection *c, endpoint_args_t *args, struc
|
|||
|
||||
if(!controller)
|
||||
{
|
||||
LOG_ERROR("could not find a controller for uid '%s'\n", args[0].value.v_str);
|
||||
mg_send_head(c, 404, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
LOG_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str);
|
||||
|
||||
static const char content[] = "no controller for id found";
|
||||
response->status_code = 404;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
return;
|
||||
}
|
||||
|
||||
cJSON *json = controller_to_json(controller);
|
||||
cJSON *json = controller_to_json(controller);
|
||||
|
||||
char *json_str = cJSON_Print(json);
|
||||
if (json_str == NULL)
|
||||
{
|
||||
LOG_ERROR("failed to print controllers json\n");
|
||||
mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "[]");
|
||||
LOG_ERROR("failed to print controller json\n");
|
||||
|
||||
static const char content[] = "failed to print json for controller";
|
||||
response->status_code = 500;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "%s", json_str);
|
||||
free(json_str);
|
||||
response->status_code = 200;
|
||||
response->content_type = "application/json";
|
||||
response->content_length = strlen(json_str);
|
||||
response->content = json_str;
|
||||
response->alloced_content = true;
|
||||
}
|
||||
cJSON_Delete(json);
|
||||
controller_free(controller);
|
||||
}
|
||||
|
||||
void
|
||||
api_v1_controllers_STR_PUT(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
|
||||
api_v1_controllers_STR_PUT(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
|
||||
{
|
||||
(void)hm;
|
||||
|
||||
uuid_t target_uid;
|
||||
if(uuid_parse(args[0].value.v_str, target_uid))
|
||||
{
|
||||
LOG_ERROR("failed to unparse uid\n");
|
||||
mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
LOG_DEBUG("failed to unparse uid\n");
|
||||
|
||||
static const char content[] = "given id was invalid";
|
||||
response->status_code = 400;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -67,9 +91,14 @@ api_v1_controllers_STR_PUT(struct mg_connection *c, endpoint_args_t *args, struc
|
|||
|
||||
if(!controller)
|
||||
{
|
||||
LOG_ERROR("could not find a controller for uid '%s'\n", args[0].value.v_str);
|
||||
mg_send_head(c, 404, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
LOG_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str);
|
||||
|
||||
static const char content[] = "no controller for id found";
|
||||
response->status_code = 404;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -80,11 +109,16 @@ api_v1_controllers_STR_PUT(struct mg_connection *c, endpoint_args_t *args, struc
|
|||
const char *error_ptr = cJSON_GetErrorPtr();
|
||||
if (error_ptr != NULL)
|
||||
{
|
||||
LOG_ERROR("error before: %s\n", error_ptr);
|
||||
LOG_DEBUG("error before: %s\n", error_ptr);
|
||||
}
|
||||
cJSON_Delete(json);
|
||||
mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
|
||||
static const char content[] = "no valid json was supplied";
|
||||
response->status_code = 400;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -105,43 +139,49 @@ api_v1_controllers_STR_PUT(struct mg_connection *c, endpoint_args_t *args, struc
|
|||
if(controller_save(controller))
|
||||
{
|
||||
LOG_ERROR("failed to save controller\n");
|
||||
mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
free(controller);
|
||||
cJSON_Delete(json);
|
||||
|
||||
static const char content[] = "failed to save controller to database";
|
||||
response->status_code = 500;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
return;
|
||||
}
|
||||
|
||||
cJSON_Delete(json);
|
||||
json = controller_to_json(controller);
|
||||
|
||||
int result = command_set_controller_name(controller);
|
||||
int status_code = 200;
|
||||
|
||||
if(result)
|
||||
{
|
||||
status_code = 504;
|
||||
}
|
||||
command_set_controller_name(controller);
|
||||
|
||||
char *json_str = cJSON_Print(json);
|
||||
if (json_str == NULL)
|
||||
{
|
||||
LOG_ERROR("failed to print controller json\n");
|
||||
mg_send_head(c, status_code, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
|
||||
static const char content[] = "failed to print json for controller";
|
||||
response->status_code = 500;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
mg_send_head(c, status_code, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "%s", json_str);
|
||||
free(json_str);
|
||||
response->status_code = 200;
|
||||
response->content_type = "application/json";
|
||||
response->content_length = strlen(json_str);
|
||||
response->content = json_str;
|
||||
response->alloced_content = true;
|
||||
}
|
||||
cJSON_Delete(json);
|
||||
controller_free(controller);
|
||||
}
|
||||
|
||||
void
|
||||
api_v1_controllers_STR_DELETE(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
|
||||
api_v1_controllers_STR_DELETE(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
|
||||
{
|
||||
(void)hm;
|
||||
|
||||
|
@ -150,9 +190,14 @@ api_v1_controllers_STR_DELETE(struct mg_connection *c, endpoint_args_t *args, st
|
|||
uuid_t target_uid;
|
||||
if(uuid_parse(target_uid_str, target_uid))
|
||||
{
|
||||
LOG_ERROR("failed to unparse uid\n");
|
||||
mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
LOG_DEBUG("failed to unparse uid\n");
|
||||
|
||||
static const char content[] = "given id was invalid";
|
||||
response->status_code = 400;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -160,21 +205,35 @@ api_v1_controllers_STR_DELETE(struct mg_connection *c, endpoint_args_t *args, st
|
|||
|
||||
if(!controller)
|
||||
{
|
||||
LOG_ERROR("could not find a controller for uid '%s'\n", target_uid_str);
|
||||
mg_send_head(c, 404, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
LOG_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str);
|
||||
|
||||
static const char content[] = "no controller for id found";
|
||||
response->status_code = 404;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if(controller_remove(controller))
|
||||
{
|
||||
mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
LOG_ERROR("failed to remove controller from database\n");
|
||||
|
||||
static const char content[] = "failed to remove controller from database";
|
||||
response->status_code = 500;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
mg_send_head(c, 200, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
response->status_code = 200;
|
||||
response->content_type = "application/json";
|
||||
response->content_length = 0;
|
||||
response->content = "";
|
||||
response->alloced_content = false;
|
||||
}
|
||||
controller_free(controller);
|
||||
return;
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include <cJSON.h>
|
||||
#include <macros.h>
|
||||
#include <constants.h>
|
||||
#include <endpoints/api_v1_controllers.h>
|
||||
#include <logger.h>
|
||||
|
@ -6,16 +7,21 @@
|
|||
#include <models/tag.h>
|
||||
|
||||
void
|
||||
api_v1_controllers_STR_relays_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
|
||||
api_v1_controllers_STR_relays_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
|
||||
{
|
||||
(void)hm;
|
||||
|
||||
uuid_t target_uid;
|
||||
if(uuid_parse(args[0].value.v_str, target_uid))
|
||||
{
|
||||
LOG_ERROR("failed to unparse uid\n");
|
||||
mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
LOG_DEBUG("failed to unparse uid\n");
|
||||
|
||||
static const char content[] = "given id was invalid";
|
||||
response->status_code = 400;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -23,9 +29,14 @@ api_v1_controllers_STR_relays_GET(struct mg_connection *c, endpoint_args_t *args
|
|||
|
||||
if(!controller)
|
||||
{
|
||||
LOG_ERROR("could not find a controller for uid '%s'\n", args[0].value.v_str);
|
||||
mg_send_head(c, 404, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
LOG_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str);
|
||||
|
||||
static const char content[] = "no controller for id found";
|
||||
response->status_code = 404;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -44,14 +55,21 @@ api_v1_controllers_STR_relays_GET(struct mg_connection *c, endpoint_args_t *args
|
|||
if (json_str == NULL)
|
||||
{
|
||||
LOG_ERROR("failed to print relays json\n");
|
||||
mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "[]");
|
||||
|
||||
static const char content[] = "failed to print json for relays";
|
||||
response->status_code = 500;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "%s", json_str);
|
||||
free(json_str);
|
||||
response->status_code = 200;
|
||||
response->content_type = "application/json";
|
||||
response->content_length = strlen(json_str);
|
||||
response->content = json_str;
|
||||
response->alloced_content = true;
|
||||
}
|
||||
cJSON_Delete(json);
|
||||
relay_free_list(all_relays);
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include <cJSON.h>
|
||||
#include <macros.h>
|
||||
#include <command.h>
|
||||
#include <constants.h>
|
||||
#include <endpoints/api_v1_controllers.h>
|
||||
|
@ -9,16 +10,21 @@
|
|||
#include <models/tag.h>
|
||||
|
||||
void
|
||||
api_v1_controllers_STR_relays_INT_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
|
||||
api_v1_controllers_STR_relays_INT_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
|
||||
{
|
||||
(void)hm;
|
||||
|
||||
uuid_t target_uid;
|
||||
if(uuid_parse(args[0].value.v_str, target_uid))
|
||||
{
|
||||
LOG_ERROR("failed to unparse uid\n");
|
||||
mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
LOG_DEBUG("failed to unparse uid\n");
|
||||
|
||||
static const char content[] = "given id was invalid";
|
||||
response->status_code = 400;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -26,9 +32,14 @@ api_v1_controllers_STR_relays_INT_GET(struct mg_connection *c, endpoint_args_t *
|
|||
|
||||
if(!controller)
|
||||
{
|
||||
LOG_ERROR("could not find a controller for uid '%s'\n", args[0].value.v_str);
|
||||
mg_send_head(c, 404, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
LOG_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str);
|
||||
|
||||
static const char content[] = "no controller for id found";
|
||||
response->status_code = 404;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -36,9 +47,14 @@ api_v1_controllers_STR_relays_INT_GET(struct mg_connection *c, endpoint_args_t *
|
|||
|
||||
if(!relay)
|
||||
{
|
||||
LOG_ERROR("could not find a relay with num %d for controller '%s'\n", args[1].value.v_int, args[0].value.v_str);
|
||||
mg_send_head(c, 404, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
LOG_DEBUG("could not find a relay with num %d for controller '%s'\n", args[1].value.v_int, args[0].value.v_str);
|
||||
|
||||
static const char content[] = "no relay for this controller found";
|
||||
response->status_code = 404;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -47,15 +63,22 @@ api_v1_controllers_STR_relays_INT_GET(struct mg_connection *c, endpoint_args_t *
|
|||
char *json_str = cJSON_Print(json);
|
||||
if (json_str == NULL)
|
||||
{
|
||||
LOG_ERROR("failed to print relays json\n");
|
||||
mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "[]");
|
||||
LOG_ERROR("failed to print relay json\n");
|
||||
|
||||
static const char content[] = "failed to print json for relay";
|
||||
response->status_code = 500;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "%s", json_str);
|
||||
free(json_str);
|
||||
response->status_code = 200;
|
||||
response->content_type = "application/json";
|
||||
response->content_length = strlen(json_str);
|
||||
response->content = json_str;
|
||||
response->alloced_content = true;
|
||||
}
|
||||
cJSON_Delete(json);
|
||||
relay_free(relay);
|
||||
|
@ -63,16 +86,21 @@ api_v1_controllers_STR_relays_INT_GET(struct mg_connection *c, endpoint_args_t *
|
|||
}
|
||||
|
||||
void
|
||||
api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
|
||||
api_v1_controllers_STR_relays_INT_PUT(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
|
||||
{
|
||||
(void)hm;
|
||||
|
||||
uuid_t target_uid;
|
||||
if(uuid_parse(args[0].value.v_str, target_uid))
|
||||
{
|
||||
LOG_ERROR("failed to unparse uid\n");
|
||||
mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
LOG_DEBUG("failed to unparse uid\n");
|
||||
|
||||
static const char content[] = "given id was invalid";
|
||||
response->status_code = 400;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -80,9 +108,14 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *c, endpoint_args_t *
|
|||
|
||||
if(!controller)
|
||||
{
|
||||
LOG_ERROR("could not find a controller for uid '%s'\n", args[0].value.v_str);
|
||||
mg_send_head(c, 404, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
LOG_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str);
|
||||
|
||||
static const char content[] = "no controller for id found";
|
||||
response->status_code = 404;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -120,8 +153,13 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *c, endpoint_args_t *
|
|||
LOG_ERROR("error before: %s\n", error_ptr);
|
||||
}
|
||||
cJSON_Delete(json);
|
||||
mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
|
||||
static const char content[] = "no valid json was supplied";
|
||||
response->status_code = 400;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -146,16 +184,27 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *c, endpoint_args_t *
|
|||
{
|
||||
LOG_DEBUG("schedules[%d] is missing uid\n", schedule_position);
|
||||
cJSON_Delete(json);
|
||||
mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
|
||||
static const char content[] = "at least one schedule is missing an id";
|
||||
response->status_code = 400;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
return;
|
||||
}
|
||||
uuid_t target_uid;
|
||||
if(schedule_uid_parse(json_schedule_uid->valuestring, target_uid))
|
||||
{
|
||||
LOG_ERROR("failed to unparse uid\n");
|
||||
mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
LOG_DEBUG("schedules[%d] has bad uid\n", schedule_position);
|
||||
cJSON_Delete(json);
|
||||
|
||||
static const char content[] = "at least one schedule has a bad id";
|
||||
response->status_code = 400;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -181,9 +230,15 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *c, endpoint_args_t *
|
|||
uuid_t target_uid;
|
||||
if(schedule_uid_parse(json_active_schedule_uid->valuestring, target_uid))
|
||||
{
|
||||
LOG_ERROR("failed to unparse uid\n");
|
||||
mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
LOG_DEBUG("active_schedule has bad uid\n");
|
||||
cJSON_Delete(json);
|
||||
|
||||
static const char content[] = "active_schedule has a bad id";
|
||||
response->status_code = 400;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
return;
|
||||
}
|
||||
relay->schedules[day_of_week] = schedule_get_by_uid_or_off(target_uid);
|
||||
|
@ -193,10 +248,16 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *c, endpoint_args_t *
|
|||
if(relay_save(relay))
|
||||
{
|
||||
LOG_ERROR("failed to save relay\n");
|
||||
mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
free(relay);
|
||||
free(controller);
|
||||
cJSON_Delete(json);
|
||||
|
||||
static const char content[] = "failed to save relay to database";
|
||||
response->status_code = 500;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
return;
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -232,14 +293,21 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *c, endpoint_args_t *
|
|||
if (json_str == NULL)
|
||||
{
|
||||
LOG_ERROR("failed to print relay json\n");
|
||||
mg_send_head(c, 200, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
|
||||
static const char content[] = "failed to print json for relay";
|
||||
response->status_code = 500;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "%s", json_str);
|
||||
free(json_str);
|
||||
response->status_code = 200;
|
||||
response->content_type = "application/json";
|
||||
response->content_length = strlen(json_str);
|
||||
response->content = json_str;
|
||||
response->alloced_content = true;
|
||||
}
|
||||
cJSON_Delete(json);
|
||||
relay_free(relay);
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include <cJSON.h>
|
||||
#include <macros.h>
|
||||
#include <constants.h>
|
||||
#include <endpoints/api_v1_controllers.h>
|
||||
#include <models/controller.h>
|
||||
|
@ -104,15 +105,20 @@ send_udp_broadcast(const char *addr, uint16_t port, void *message, size_t length
|
|||
}
|
||||
|
||||
void
|
||||
api_v1_controllers_discover_POST(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
|
||||
api_v1_controllers_discover_POST(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
|
||||
{
|
||||
int discover_server_socket = bind_tcp_server("0.0.0.0", "0", 20);
|
||||
int discover_server_port = get_server_port(discover_server_socket);
|
||||
|
||||
if(discover_server_port == -1)
|
||||
{
|
||||
mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "[]");
|
||||
LOG_ERROR("failed to get server port for discovery\n");
|
||||
static const char content[] = "";
|
||||
response->status_code = 500;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -121,8 +127,13 @@ api_v1_controllers_discover_POST(struct mg_connection *c, endpoint_args_t *args,
|
|||
|
||||
if(send_udp_broadcast("255.255.255.255", global_config.discovery_port, payload, sizeof(payload)) < 0)
|
||||
{
|
||||
mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "[]");
|
||||
LOG_ERROR("failed to send UDP broadcast\n");
|
||||
static const char content[] = "";
|
||||
response->status_code = 500;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -292,5 +303,5 @@ api_v1_controllers_discover_POST(struct mg_connection *c, endpoint_args_t *args,
|
|||
}
|
||||
controller_free_list(known_controllers);
|
||||
|
||||
api_v1_controllers_GET(c, args, hm);
|
||||
api_v1_controllers_GET(hm, args, response);
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include <cJSON.h>
|
||||
#include <macros.h>
|
||||
#include <constants.h>
|
||||
#include <endpoints/api_v1_relays.h>
|
||||
#include <logger.h>
|
||||
|
@ -7,7 +8,7 @@
|
|||
#include <models/tag.h>
|
||||
|
||||
void
|
||||
api_v1_relays_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
|
||||
api_v1_relays_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
|
||||
{
|
||||
(void)args;
|
||||
(void)hm;
|
||||
|
@ -26,14 +27,21 @@ api_v1_relays_GET(struct mg_connection *c, endpoint_args_t *args, struct http_me
|
|||
if (json_str == NULL)
|
||||
{
|
||||
LOG_ERROR("failed to print relays json\n");
|
||||
mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "[]");
|
||||
|
||||
static const char content[] = "failed to print json for relays";
|
||||
response->status_code = 500;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "%s", json_str);
|
||||
free(json_str);
|
||||
response->status_code = 200;
|
||||
response->content_type = "application/json";
|
||||
response->content_length = strlen(json_str);
|
||||
response->content = json_str;
|
||||
response->alloced_content = true;
|
||||
}
|
||||
cJSON_Delete(json);
|
||||
relay_free_list(all_relays);
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include <cJSON.h>
|
||||
#include <macros.h>
|
||||
#include <constants.h>
|
||||
#include <endpoints/api_v1_relays.h>
|
||||
#include <logger.h>
|
||||
|
@ -7,7 +8,7 @@
|
|||
#include <models/tag.h>
|
||||
|
||||
void
|
||||
api_v1_relays_tag_STR_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
|
||||
api_v1_relays_tag_STR_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
|
||||
{
|
||||
(void)hm;
|
||||
|
||||
|
@ -15,9 +16,14 @@ api_v1_relays_tag_STR_GET(struct mg_connection *c, endpoint_args_t *args, struct
|
|||
int *relays_ids = junction_tag_get_relays_for_tag_id(tag_id);
|
||||
if(relays_ids == NULL)
|
||||
{
|
||||
LOG_ERROR("failed to print relays json\n");
|
||||
mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "[]");
|
||||
LOG_ERROR("failed to load relays for tag from database\n");
|
||||
|
||||
static const char content[] = "failed to load relays for tag from database";
|
||||
response->status_code = 500;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -42,14 +48,21 @@ api_v1_relays_tag_STR_GET(struct mg_connection *c, endpoint_args_t *args, struct
|
|||
if (json_str == NULL)
|
||||
{
|
||||
LOG_ERROR("failed to print relays json\n");
|
||||
mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "[]");
|
||||
|
||||
static const char content[] = "failed to print json for relays";
|
||||
response->status_code = 500;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "%s", json_str);
|
||||
free(json_str);
|
||||
response->status_code = 200;
|
||||
response->content_type = "application/json";
|
||||
response->content_length = strlen(json_str);
|
||||
response->content = json_str;
|
||||
response->alloced_content = true;
|
||||
}
|
||||
|
||||
cJSON_Delete(json);
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include <cJSON.h>
|
||||
#include <macros.h>
|
||||
#include <constants.h>
|
||||
#include <endpoints/api_v1_schedules.h>
|
||||
#include <logger.h>
|
||||
|
@ -7,7 +8,7 @@
|
|||
#include <models/tag.h>
|
||||
|
||||
void
|
||||
api_v1_schedules_POST(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
|
||||
api_v1_schedules_POST(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
|
||||
{
|
||||
(void)args;
|
||||
cJSON *json = cJSON_ParseWithLength(hm->body.p, hm->body.len);
|
||||
|
@ -20,16 +21,28 @@ api_v1_schedules_POST(struct mg_connection *c, endpoint_args_t *args, struct htt
|
|||
LOG_ERROR("error before: %s\n", error_ptr);
|
||||
}
|
||||
cJSON_Delete(json);
|
||||
|
||||
static const char content[] = "no valid json was supplied";
|
||||
response->status_code = 400;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
return;
|
||||
}
|
||||
|
||||
cJSON *json_name = cJSON_GetObjectItemCaseSensitive(json, "name");
|
||||
if(!cJSON_IsString(json_name) || (json_name->valuestring == NULL))
|
||||
{
|
||||
LOG_DEBUG("no name for schedule provided\n");
|
||||
mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
|
||||
cJSON_Delete(json);
|
||||
|
||||
static const char content[] = "no name for schedule provided";
|
||||
response->status_code = 400;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
return;
|
||||
}
|
||||
cJSON *json_period;
|
||||
|
@ -113,21 +126,28 @@ api_v1_schedules_POST(struct mg_connection *c, endpoint_args_t *args, struct htt
|
|||
if (json_str == NULL)
|
||||
{
|
||||
LOG_ERROR("failed to print schedule json\n");
|
||||
mg_send_head(c, 201, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
|
||||
static const char content[] = "failed to print json for schedule";
|
||||
response->status_code = 500;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
mg_send_head(c, 201, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "%s", json_str);
|
||||
free(json_str);
|
||||
response->status_code = 201;
|
||||
response->content_type = "application/json";
|
||||
response->content_length = strlen(json_str);
|
||||
response->content = json_str;
|
||||
response->alloced_content = true;
|
||||
}
|
||||
cJSON_Delete(json);
|
||||
schedule_free(new_schedule);
|
||||
}
|
||||
|
||||
void
|
||||
api_v1_schedules_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
|
||||
api_v1_schedules_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
|
||||
{
|
||||
(void)args;
|
||||
(void)hm;
|
||||
|
@ -146,14 +166,21 @@ api_v1_schedules_GET(struct mg_connection *c, endpoint_args_t *args, struct http
|
|||
if (json_str == NULL)
|
||||
{
|
||||
LOG_ERROR("failed to print schedules json\n");
|
||||
mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "[]");
|
||||
|
||||
static const char content[] = "failed to print json for schedules";
|
||||
response->status_code = 500;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "%s", json_str);
|
||||
free(json_str);
|
||||
response->status_code = 200;
|
||||
response->content_type = "application/json";
|
||||
response->content_length = strlen(json_str);
|
||||
response->content = json_str;
|
||||
response->alloced_content = true;
|
||||
}
|
||||
cJSON_Delete(json);
|
||||
schedule_free_list(all_schedules);
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include <cJSON.h>
|
||||
#include <macros.h>
|
||||
#include <constants.h>
|
||||
#include <endpoints/api_v1_schedules.h>
|
||||
#include <logger.h>
|
||||
|
@ -10,16 +11,21 @@
|
|||
#include <models/tag.h>
|
||||
|
||||
void
|
||||
api_v1_schedules_STR_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
|
||||
api_v1_schedules_STR_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
|
||||
{
|
||||
(void)hm;
|
||||
|
||||
uuid_t target_uid;
|
||||
if(schedule_uid_parse(args[0].value.v_str, target_uid))
|
||||
{
|
||||
LOG_ERROR("failed to unparse uid\n");
|
||||
mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
LOG_DEBUG("failed to unparse uid\n");
|
||||
|
||||
static const char content[] = "given id was invalid";
|
||||
response->status_code = 400;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -27,9 +33,14 @@ api_v1_schedules_STR_GET(struct mg_connection *c, endpoint_args_t *args, struct
|
|||
|
||||
if(!schedule)
|
||||
{
|
||||
LOG_ERROR("could not find a schedule for uid '%s'\n", args[0].value.v_str);
|
||||
mg_send_head(c, 404, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
LOG_DEBUG("could not find a schedule for uid '%s'\n", args[0].value.v_str);
|
||||
|
||||
static const char content[] = "no schedule for id found";
|
||||
response->status_code = 404;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -39,30 +50,42 @@ api_v1_schedules_STR_GET(struct mg_connection *c, endpoint_args_t *args, struct
|
|||
if (json_str == NULL)
|
||||
{
|
||||
LOG_ERROR("failed to print schedules json\n");
|
||||
mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "[]");
|
||||
|
||||
static const char content[] = "failed to print json for schedules";
|
||||
response->status_code = 500;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "%s", json_str);
|
||||
free(json_str);
|
||||
response->status_code = 200;
|
||||
response->content_type = "application/json";
|
||||
response->content_length = strlen(json_str);
|
||||
response->content = json_str;
|
||||
response->alloced_content = true;
|
||||
}
|
||||
cJSON_Delete(json);
|
||||
schedule_free(schedule);
|
||||
}
|
||||
|
||||
void
|
||||
api_v1_schedules_STR_PUT(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
|
||||
api_v1_schedules_STR_PUT(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
|
||||
{
|
||||
(void)hm;
|
||||
|
||||
uuid_t target_uid;
|
||||
if(schedule_uid_parse(args[0].value.v_str, target_uid))
|
||||
{
|
||||
LOG_ERROR("failed to unparse uid\n");
|
||||
mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
LOG_DEBUG("failed to unparse uid\n");
|
||||
|
||||
static const char content[] = "given id was invalid";
|
||||
response->status_code = 400;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -70,9 +93,14 @@ api_v1_schedules_STR_PUT(struct mg_connection *c, endpoint_args_t *args, struct
|
|||
|
||||
if(!schedule)
|
||||
{
|
||||
LOG_ERROR("could not find a schedule for uid '%s'\n", args[0].value.v_str);
|
||||
mg_send_head(c, 404, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
LOG_DEBUG("could not find a schedule for uid '%s'\n", args[0].value.v_str);
|
||||
|
||||
static const char content[] = "no schedule for id found";
|
||||
response->status_code = 404;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -83,11 +111,16 @@ api_v1_schedules_STR_PUT(struct mg_connection *c, endpoint_args_t *args, struct
|
|||
const char *error_ptr = cJSON_GetErrorPtr();
|
||||
if (error_ptr != NULL)
|
||||
{
|
||||
LOG_ERROR("error before: %s\n", error_ptr);
|
||||
LOG_DEBUG("error before: %s\n", error_ptr);
|
||||
}
|
||||
cJSON_Delete(json);
|
||||
mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
|
||||
static const char content[] = "no valid json was supplied";
|
||||
response->status_code = 400;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -149,10 +182,15 @@ api_v1_schedules_STR_PUT(struct mg_connection *c, endpoint_args_t *args, struct
|
|||
if(schedule_save(schedule))
|
||||
{
|
||||
LOG_ERROR("failed to save schedule\n");
|
||||
mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
free(schedule);
|
||||
cJSON_Delete(json);
|
||||
|
||||
static const char content[] = "failed to save schedule to database";
|
||||
response->status_code = 500;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -192,21 +230,28 @@ api_v1_schedules_STR_PUT(struct mg_connection *c, endpoint_args_t *args, struct
|
|||
if (json_str == NULL)
|
||||
{
|
||||
LOG_ERROR("failed to print schedule json\n");
|
||||
mg_send_head(c, 200, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
|
||||
static const char content[] = "failed to print json for schedule";
|
||||
response->status_code = 500;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "%s", json_str);
|
||||
free(json_str);
|
||||
response->status_code = 200;
|
||||
response->content_type = "application/json";
|
||||
response->content_length = strlen(json_str);
|
||||
response->content = json_str;
|
||||
response->alloced_content = true;
|
||||
}
|
||||
cJSON_Delete(json);
|
||||
schedule_free(schedule);
|
||||
}
|
||||
|
||||
void
|
||||
api_v1_schedules_STR_DELETE(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
|
||||
api_v1_schedules_STR_DELETE(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
|
||||
{
|
||||
(void)hm;
|
||||
|
||||
|
@ -215,36 +260,62 @@ api_v1_schedules_STR_DELETE(struct mg_connection *c, endpoint_args_t *args, stru
|
|||
uuid_t target_uid;
|
||||
if(schedule_uid_parse(target_uid_str, target_uid))
|
||||
{
|
||||
LOG_ERROR("failed to unparse uid\n");
|
||||
mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
LOG_DEBUG("failed to unparse uid\n");
|
||||
|
||||
static const char content[] = "given id was invalid";
|
||||
response->status_code = 400;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
return;
|
||||
}
|
||||
|
||||
schedule_t* schedule = schedule_get_by_uid(target_uid);
|
||||
|
||||
if(schedule_is_protected(schedule))
|
||||
if(!schedule)
|
||||
{
|
||||
mg_send_head(c, 403, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
LOG_DEBUG("could not find a schedule for uid '%s'\n", args[0].value.v_str);
|
||||
|
||||
static const char content[] = "no schedule for id found";
|
||||
response->status_code = 404;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if(!schedule)
|
||||
if(schedule_is_protected(schedule))
|
||||
{
|
||||
LOG_ERROR("could not find a schedule for uid '%s'\n", target_uid_str);
|
||||
mg_send_head(c, 404, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
static const char content[] = "target schedule is protected";
|
||||
response->status_code = 403;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if(schedule_remove(schedule))
|
||||
{
|
||||
mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
return;
|
||||
LOG_ERROR("failed to remove schedule from database\n");
|
||||
|
||||
static const char content[] = "failed to remove schedule from database";
|
||||
response->status_code = 500;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
}
|
||||
mg_send_head(c, 200, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
else
|
||||
{
|
||||
response->status_code = 200;
|
||||
response->content_type = "application/json";
|
||||
response->content_length = 0;
|
||||
response->content = "";
|
||||
response->alloced_content = false;
|
||||
}
|
||||
schedule_free(schedule);
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include <cJSON.h>
|
||||
#include <macros.h>
|
||||
#include <constants.h>
|
||||
#include <endpoints/api_v1_schedules.h>
|
||||
#include <logger.h>
|
||||
|
@ -7,7 +8,7 @@
|
|||
#include <models/tag.h>
|
||||
|
||||
void
|
||||
api_v1_schedules_tag_STR_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
|
||||
api_v1_schedules_tag_STR_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
|
||||
{
|
||||
(void)hm;
|
||||
|
||||
|
@ -15,9 +16,14 @@ api_v1_schedules_tag_STR_GET(struct mg_connection *c, endpoint_args_t *args, str
|
|||
int *schedules_ids = junction_tag_get_schedules_for_tag_id(tag_id);
|
||||
if(schedules_ids == NULL)
|
||||
{
|
||||
LOG_ERROR("failed to print schedules json\n");
|
||||
mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "[]");
|
||||
LOG_ERROR("failed to load schedules for tag from database\n");
|
||||
|
||||
static const char content[] = "failed to load schedules for tag from database";
|
||||
response->status_code = 500;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -42,14 +48,21 @@ api_v1_schedules_tag_STR_GET(struct mg_connection *c, endpoint_args_t *args, str
|
|||
if (json_str == NULL)
|
||||
{
|
||||
LOG_ERROR("failed to print schedules json\n");
|
||||
mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "[]");
|
||||
|
||||
static const char content[] = "failed to print json for schedules";
|
||||
response->status_code = 500;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "%s", json_str);
|
||||
free(json_str);
|
||||
response->status_code = 200;
|
||||
response->content_type = "application/json";
|
||||
response->content_length = strlen(json_str);
|
||||
response->content = json_str;
|
||||
response->alloced_content = true;
|
||||
}
|
||||
|
||||
cJSON_Delete(json);
|
||||
|
|
|
@ -1,9 +1,20 @@
|
|||
#include <string.h>
|
||||
|
||||
#include <constants.h>
|
||||
#include <mongoose.h>
|
||||
#include <logger.h>
|
||||
#include <router.h>
|
||||
#include <handlers.h>
|
||||
|
||||
#define STD_HEADERS "Access-Control-Allow-Origin: *\r\nAccess-Control-Allow-Headers: *\r\nAccess-Control-Allow-Methods: *\r\n"
|
||||
#define HEADERS_FMT STD_HEADERS "Content-Type: %s"
|
||||
|
||||
// -2 for "%s" -1 for \0
|
||||
#define HEADERS_FMT_LEN (sizeof(HEADERS_FMT) - 3)
|
||||
|
||||
#define STD_RESPONSE_CONTENT "the server did not create a response"
|
||||
#define STD_RESPONSE_CONTENT_LEN (sizeof(STD_RESPONSE_CONTENT) - 1)
|
||||
|
||||
void
|
||||
handler_connection(struct mg_connection *c, int ev, void *p)
|
||||
{
|
||||
|
@ -18,7 +29,27 @@ handler_connection(struct mg_connection *c, int ev, void *p)
|
|||
{
|
||||
if(endpoint->func)
|
||||
{
|
||||
endpoint->func(c, endpoint->args, p);
|
||||
endpoint_response_t response;
|
||||
response.status_code = 500;
|
||||
response.content_type = "text/plain";
|
||||
response.content_length = STD_RESPONSE_CONTENT_LEN;
|
||||
response.content = STD_RESPONSE_CONTENT;
|
||||
response.alloced_content = false;
|
||||
|
||||
endpoint->func(p, endpoint->args, &response);
|
||||
|
||||
char *response_headers = malloc(sizeof(char) * (HEADERS_FMT_LEN + strlen(response.content_type) + 1));
|
||||
sprintf(response_headers, HEADERS_FMT, response.content_type);
|
||||
|
||||
mg_send_head(c, response.status_code, response.content_length, response_headers);
|
||||
mg_printf(c, "%s", response.content);
|
||||
|
||||
free(response_headers);
|
||||
|
||||
if(response.alloced_content)
|
||||
{
|
||||
free((char*)response.content);
|
||||
}
|
||||
|
||||
for(int i = 0; i < endpoint->args_count; ++i)
|
||||
{
|
||||
|
@ -33,11 +64,7 @@ handler_connection(struct mg_connection *c, int ev, void *p)
|
|||
if(endpoint->method == HTTP_METHOD_OPTIONS)
|
||||
{
|
||||
char options_header[256]; // TODO make more generic
|
||||
sprintf(options_header, "Allow: OPTIONS%s%s%s%s\r\nAccess-Control-Allow-Methods: OPTIONS%s%s%s%s\r\n" STANDARD_HEADERS,
|
||||
endpoint->options & HTTP_METHOD_GET ? ", GET" : "",
|
||||
endpoint->options & HTTP_METHOD_POST ? ", POST" : "",
|
||||
endpoint->options & HTTP_METHOD_PUT ? ", PUT" : "",
|
||||
endpoint->options & HTTP_METHOD_DELETE ? ", DELETE" : "",
|
||||
sprintf(options_header, STD_HEADERS "Allow: OPTIONS%s%s%s%s",
|
||||
endpoint->options & HTTP_METHOD_GET ? ", GET" : "",
|
||||
endpoint->options & HTTP_METHOD_POST ? ", POST" : "",
|
||||
endpoint->options & HTTP_METHOD_PUT ? ", PUT" : "",
|
||||
|
|
|
@ -28,6 +28,4 @@
|
|||
|
||||
#define PIFACE_GPIO_BASE 200
|
||||
|
||||
#define STANDARD_HEADERS "Access-Control-Allow-Origin: *\r\nAccess-Control-Allow-Headers: *"
|
||||
|
||||
#endif /* CORE_CONTANTS_H */
|
||||
|
|
|
@ -4,27 +4,27 @@
|
|||
#include <router.h>
|
||||
|
||||
void
|
||||
api_v1_controllers_discover_POST(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
|
||||
api_v1_controllers_discover_POST(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
|
||||
|
||||
void
|
||||
api_v1_controllers_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
|
||||
api_v1_controllers_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
|
||||
|
||||
void
|
||||
api_v1_controllers_STR_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
|
||||
api_v1_controllers_STR_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
|
||||
|
||||
void
|
||||
api_v1_controllers_STR_PUT(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
|
||||
api_v1_controllers_STR_PUT(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
|
||||
|
||||
void
|
||||
api_v1_controllers_STR_DELETE(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
|
||||
api_v1_controllers_STR_DELETE(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
|
||||
|
||||
void
|
||||
api_v1_controllers_STR_relays_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
|
||||
api_v1_controllers_STR_relays_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
|
||||
|
||||
void
|
||||
api_v1_controllers_STR_relays_INT_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
|
||||
api_v1_controllers_STR_relays_INT_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
|
||||
|
||||
void
|
||||
api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
|
||||
api_v1_controllers_STR_relays_INT_PUT(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
|
||||
|
||||
#endif /* CORE_ENDPOINTS_API_V1_CONTROLLERS_H */
|
||||
|
|
|
@ -4,9 +4,9 @@
|
|||
#include <router.h>
|
||||
|
||||
void
|
||||
api_v1_relays_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
|
||||
api_v1_relays_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
|
||||
|
||||
void
|
||||
api_v1_relays_tag_STR_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
|
||||
api_v1_relays_tag_STR_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
|
||||
|
||||
#endif /* CORE_ENDPOINTS_API_V1_RELAYS_H */
|
||||
|
|
|
@ -4,21 +4,21 @@
|
|||
#include <router.h>
|
||||
|
||||
void
|
||||
api_v1_schedules_POST(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
|
||||
api_v1_schedules_POST(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
|
||||
|
||||
void
|
||||
api_v1_schedules_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
|
||||
api_v1_schedules_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
|
||||
|
||||
void
|
||||
api_v1_schedules_STR_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
|
||||
api_v1_schedules_STR_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
|
||||
|
||||
void
|
||||
api_v1_schedules_STR_PUT(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
|
||||
api_v1_schedules_STR_PUT(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
|
||||
|
||||
void
|
||||
api_v1_schedules_STR_DELETE(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
|
||||
api_v1_schedules_STR_DELETE(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
|
||||
|
||||
void
|
||||
api_v1_schedules_tag_STR_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
|
||||
api_v1_schedules_tag_STR_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
|
||||
|
||||
#endif /* CORE_ENDPOINTS_API_V1_SCHEDULES_H */
|
||||
|
|
1
include/macros.h
Normal file
1
include/macros.h
Normal file
|
@ -0,0 +1 @@
|
|||
#define STRLEN(s) ((sizeof(s)/sizeof(s[0])) - sizeof(s[0]))
|
|
@ -30,7 +30,16 @@ typedef struct
|
|||
} value;
|
||||
} endpoint_args_t;
|
||||
|
||||
typedef void (*endpoint_func_f)(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
|
||||
typedef struct
|
||||
{
|
||||
int status_code;
|
||||
const char *content_type;
|
||||
size_t content_length;
|
||||
const char *content;
|
||||
int alloced_content;
|
||||
} endpoint_response_t;
|
||||
|
||||
typedef void (*endpoint_func_f)(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
|
||||
|
||||
typedef struct
|
||||
{
|
||||
|
|
45
router.c
45
router.c
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include <logger.h>
|
||||
#include <router.h>
|
||||
#include <macros.h>
|
||||
|
||||
#include <endpoints/api_v1_schedules.h>
|
||||
#include <endpoints/api_v1_controllers.h>
|
||||
|
@ -14,21 +15,31 @@ static int endpoints_registered = 0;
|
|||
static const char delimiter[2] = "/";
|
||||
|
||||
static void
|
||||
endpoint_index_func(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
|
||||
{
|
||||
(void)args;
|
||||
mg_send_head(c, 200, hm->body.len, "Content-Type: text/plain");
|
||||
//mg_printf(c, "%.*s", (int)hm->message.len, hm->message.p);
|
||||
mg_printf(c, "%.*s", (int)hm->body.len, hm->body.p);
|
||||
}
|
||||
|
||||
static void
|
||||
endpoint_not_found_func(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
|
||||
endpoint_index_func(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
|
||||
{
|
||||
(void)args;
|
||||
(void)hm;
|
||||
mg_send_head(c, 404, 9, "Content-Type: text/plain");
|
||||
mg_printf(c, "not found");
|
||||
|
||||
static const char content[] = "Emgauwa";
|
||||
response->status_code = 200;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
}
|
||||
|
||||
static void
|
||||
endpoint_not_found_func(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
|
||||
{
|
||||
(void)args;
|
||||
(void)hm;
|
||||
|
||||
static const char content[] = "404 - NOT FOUND";
|
||||
response->status_code = 404;
|
||||
response->content_type = "text/plain";
|
||||
response->content_length = STRLEN(content);;
|
||||
response->content = content;
|
||||
response->alloced_content = false;
|
||||
}
|
||||
|
||||
static struct mg_str
|
||||
|
@ -107,6 +118,16 @@ router_register_endpoint(const char *route, int method, endpoint_func_f func)
|
|||
{
|
||||
options_endpoint = router_register_endpoint(route, HTTP_METHOD_OPTIONS, NULL);
|
||||
}
|
||||
else
|
||||
{
|
||||
for(int i = 0; i < options_endpoint->args_count; ++i)
|
||||
{
|
||||
if(options_endpoint->args[i].type == ENDPOINT_ARG_TYPE_STR)
|
||||
{
|
||||
free((char*)options_endpoint->args[i].value.v_str);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(endpoints_registered >= ENDPOINTS_MAX_COUNT)
|
||||
|
|
Loading…
Reference in a new issue