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