add: endpoint responses

This commit is contained in:
Tobias Reisinger 2020-05-21 01:33:18 +02:00
parent 5a19e99627
commit 5e64f5963c
18 changed files with 587 additions and 235 deletions

View file

@ -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);

View file

@ -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;

View file

@ -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);

View file

@ -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);

View file

@ -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);
}

View file

@ -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);

View file

@ -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);

View file

@ -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);

View file

@ -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;
}

View file

@ -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);