add: generic response makers

This commit is contained in:
Tobias Reisinger 2020-05-31 00:23:57 +02:00
parent 2d992cfe3c
commit 6178ef3c7b
13 changed files with 200 additions and 503 deletions

View file

@ -16,11 +16,7 @@ api_v1_schedules_POST(struct http_message *hm, endpoint_args_t *args, endpoint_r
if(json == NULL)
{
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;
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
@ -31,11 +27,7 @@ api_v1_schedules_POST(struct http_message *hm, endpoint_args_t *args, endpoint_r
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;
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
cJSON *json_period;
@ -66,11 +58,7 @@ api_v1_schedules_POST(struct http_message *hm, endpoint_args_t *args, endpoint_r
schedule_free(new_schedule);
static const char content[] = "one period is missing a start";
response->status_code = 400;
response->content_type = "text/plain";
response->content_length = STRLEN(content);;
response->content = content;
response->alloced_content = false;
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
if(!cJSON_IsString(json_period_end) || (json_period_end->valuestring == NULL))
@ -80,11 +68,7 @@ api_v1_schedules_POST(struct http_message *hm, endpoint_args_t *args, endpoint_r
schedule_free(new_schedule);
static const char content[] = "one period is missing an end";
response->status_code = 400;
response->content_type = "text/plain";
response->content_length = STRLEN(content);;
response->content = content;
response->alloced_content = false;
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
@ -97,11 +81,7 @@ api_v1_schedules_POST(struct http_message *hm, endpoint_args_t *args, endpoint_r
schedule_free(new_schedule);
static const char content[] = "the start for one period is invalid";
response->status_code = 400;
response->content_type = "text/plain";
response->content_length = STRLEN(content);;
response->content = content;
response->alloced_content = false;
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
if(period_helper_parse_hhmm(json_period_end->valuestring, &end))
@ -111,11 +91,7 @@ api_v1_schedules_POST(struct http_message *hm, endpoint_args_t *args, endpoint_r
schedule_free(new_schedule);
static const char content[] = "the end for one period is invalid";
response->status_code = 400;
response->content_type = "text/plain";
response->content_length = STRLEN(content);;
response->content = content;
response->alloced_content = false;
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
@ -151,26 +127,7 @@ api_v1_schedules_POST(struct http_message *hm, endpoint_args_t *args, endpoint_r
cJSON_Delete(json);
json = schedule_to_json(new_schedule);
char *json_str = cJSON_Print(json);
if (json_str == NULL)
{
LOG_ERROR("failed to print schedule json\n");
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
{
response->status_code = 201;
response->content_type = "application/json";
response->content_length = strlen(json_str);
response->content = json_str;
response->alloced_content = true;
}
endpoint_response_json(response, 201, json);
cJSON_Delete(json);
schedule_free(new_schedule);
}
@ -191,26 +148,7 @@ api_v1_schedules_GET(struct http_message *hm, endpoint_args_t *args, endpoint_re
cJSON_AddItemToArray(json, json_schedule);
}
char *json_str = cJSON_Print(json);
if (json_str == NULL)
{
LOG_ERROR("failed to print schedules json\n");
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
{
response->status_code = 200;
response->content_type = "application/json";
response->content_length = strlen(json_str);
response->content = json_str;
response->alloced_content = true;
}
endpoint_response_json(response, 200, json);
cJSON_Delete(json);
schedule_free_list(all_schedules);
}