add: generic response makers
This commit is contained in:
		
							parent
							
								
									2d992cfe3c
								
							
						
					
					
						commit
						6178ef3c7b
					
				
					 13 changed files with 200 additions and 503 deletions
				
			
		| 
						 | 
				
			
			@ -23,26 +23,7 @@ api_v1_controllers_GET(struct http_message *hm, endpoint_args_t *args, endpoint_
 | 
			
		|||
        cJSON_AddItemToArray(json, json_controller);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    char *json_str = cJSON_Print(json);
 | 
			
		||||
    if (json_str == NULL)
 | 
			
		||||
    {
 | 
			
		||||
        LOG_ERROR("failed to print controllers json\n");
 | 
			
		||||
 | 
			
		||||
        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
 | 
			
		||||
    {
 | 
			
		||||
        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);
 | 
			
		||||
    controller_free_list(all_controllers);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -18,11 +18,7 @@ api_v1_controllers_STR_GET(struct http_message *hm, endpoint_args_t *args, endpo
 | 
			
		|||
        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;
 | 
			
		||||
        endpoint_response_text(response, 400, content, STRLEN(content));
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -33,37 +29,13 @@ api_v1_controllers_STR_GET(struct http_message *hm, endpoint_args_t *args, endpo
 | 
			
		|||
        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;
 | 
			
		||||
        endpoint_response_text(response, 404, content, STRLEN(content));
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    cJSON *json = controller_to_json(controller);
 | 
			
		||||
 | 
			
		||||
    char *json_str = cJSON_Print(json);
 | 
			
		||||
    if (json_str == NULL)
 | 
			
		||||
    {
 | 
			
		||||
        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
 | 
			
		||||
    {
 | 
			
		||||
        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);
 | 
			
		||||
    controller_free(controller);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -79,11 +51,7 @@ api_v1_controllers_STR_PUT(struct http_message *hm, endpoint_args_t *args, endpo
 | 
			
		|||
        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;
 | 
			
		||||
        endpoint_response_text(response, 400, content, STRLEN(content));
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -94,11 +62,7 @@ api_v1_controllers_STR_PUT(struct http_message *hm, endpoint_args_t *args, endpo
 | 
			
		|||
        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;
 | 
			
		||||
        endpoint_response_text(response, 404, content, STRLEN(content));
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -107,11 +71,7 @@ api_v1_controllers_STR_PUT(struct http_message *hm, endpoint_args_t *args, endpo
 | 
			
		|||
    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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -17,11 +17,7 @@ api_v1_controllers_STR_relays_GET(struct http_message *hm, endpoint_args_t *args
 | 
			
		|||
        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;
 | 
			
		||||
        endpoint_response_text(response, 400, content, STRLEN(content));
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -32,11 +28,7 @@ api_v1_controllers_STR_relays_GET(struct http_message *hm, endpoint_args_t *args
 | 
			
		|||
        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;
 | 
			
		||||
        endpoint_response_text(response, 404, content, STRLEN(content));
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
        
 | 
			
		||||
| 
						 | 
				
			
			@ -51,26 +43,7 @@ api_v1_controllers_STR_relays_GET(struct http_message *hm, endpoint_args_t *args
 | 
			
		|||
        cJSON_AddItemToArray(json, json_relay);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    char *json_str = cJSON_Print(json);
 | 
			
		||||
    if (json_str == NULL)
 | 
			
		||||
    {
 | 
			
		||||
        LOG_ERROR("failed to print relays json\n");
 | 
			
		||||
 | 
			
		||||
        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
 | 
			
		||||
    {
 | 
			
		||||
        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);
 | 
			
		||||
    relay_free_list(all_relays);
 | 
			
		||||
    controller_free(controller);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -20,11 +20,7 @@ api_v1_controllers_STR_relays_INT_GET(struct http_message *hm, endpoint_args_t *
 | 
			
		|||
        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;
 | 
			
		||||
        endpoint_response_text(response, 400, content, STRLEN(content));
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -35,11 +31,7 @@ api_v1_controllers_STR_relays_INT_GET(struct http_message *hm, endpoint_args_t *
 | 
			
		|||
        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;
 | 
			
		||||
        endpoint_response_text(response, 404, content, STRLEN(content));
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -50,36 +42,13 @@ api_v1_controllers_STR_relays_INT_GET(struct http_message *hm, endpoint_args_t *
 | 
			
		|||
        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;
 | 
			
		||||
        endpoint_response_text(response, 404, content, STRLEN(content));
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    cJSON *json = relay_to_json(relay);
 | 
			
		||||
 | 
			
		||||
    char *json_str = cJSON_Print(json);
 | 
			
		||||
    if (json_str == NULL)
 | 
			
		||||
    {
 | 
			
		||||
        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
 | 
			
		||||
    {
 | 
			
		||||
        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);
 | 
			
		||||
    relay_free(relay);
 | 
			
		||||
    controller_free(controller);
 | 
			
		||||
| 
						 | 
				
			
			@ -96,11 +65,7 @@ api_v1_controllers_STR_relays_INT_PUT(struct http_message *hm, endpoint_args_t *
 | 
			
		|||
        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;
 | 
			
		||||
        endpoint_response_text(response, 400, content, STRLEN(content));
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -111,11 +76,7 @@ api_v1_controllers_STR_relays_INT_PUT(struct http_message *hm, endpoint_args_t *
 | 
			
		|||
        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;
 | 
			
		||||
        endpoint_response_text(response, 404, content, STRLEN(content));
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -148,11 +109,7 @@ api_v1_controllers_STR_relays_INT_PUT(struct http_message *hm, endpoint_args_t *
 | 
			
		|||
    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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -179,11 +136,7 @@ api_v1_controllers_STR_relays_INT_PUT(struct http_message *hm, endpoint_args_t *
 | 
			
		|||
                cJSON_Delete(json);
 | 
			
		||||
 | 
			
		||||
                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;
 | 
			
		||||
                endpoint_response_text(response, 400, content, STRLEN(content));
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
            uuid_t target_uid;
 | 
			
		||||
| 
						 | 
				
			
			@ -193,11 +146,7 @@ api_v1_controllers_STR_relays_INT_PUT(struct http_message *hm, endpoint_args_t *
 | 
			
		|||
                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;
 | 
			
		||||
                endpoint_response_text(response, 400, content, STRLEN(content));
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -227,11 +176,7 @@ api_v1_controllers_STR_relays_INT_PUT(struct http_message *hm, endpoint_args_t *
 | 
			
		|||
                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;
 | 
			
		||||
                endpoint_response_text(response, 400, content, STRLEN(content));
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
            relay->schedules[day_of_week] = schedule_get_by_uid_or_off(target_uid);
 | 
			
		||||
| 
						 | 
				
			
			@ -245,12 +190,7 @@ api_v1_controllers_STR_relays_INT_PUT(struct http_message *hm, endpoint_args_t *
 | 
			
		|||
        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;
 | 
			
		||||
        endpoint_response_text(response, 500, content, STRLEN(content));
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -282,26 +222,7 @@ api_v1_controllers_STR_relays_INT_PUT(struct http_message *hm, endpoint_args_t *
 | 
			
		|||
 | 
			
		||||
    command_set_relay_schedule(relay);
 | 
			
		||||
 | 
			
		||||
    char *json_str = cJSON_Print(json);
 | 
			
		||||
    if (json_str == NULL)
 | 
			
		||||
    {
 | 
			
		||||
        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
 | 
			
		||||
    {
 | 
			
		||||
        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);
 | 
			
		||||
    relay_free(relay);
 | 
			
		||||
    controller_free(controller);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -23,26 +23,7 @@ api_v1_relays_GET(struct http_message *hm, endpoint_args_t *args, endpoint_respo
 | 
			
		|||
        cJSON_AddItemToArray(json, json_relay);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    char *json_str = cJSON_Print(json);
 | 
			
		||||
    if (json_str == NULL)
 | 
			
		||||
    {
 | 
			
		||||
        LOG_ERROR("failed to print relays json\n");
 | 
			
		||||
 | 
			
		||||
        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
 | 
			
		||||
    {
 | 
			
		||||
        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);
 | 
			
		||||
    relay_free_list(all_relays);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -19,11 +19,7 @@ api_v1_relays_tag_STR_GET(struct http_message *hm, endpoint_args_t *args, endpoi
 | 
			
		|||
        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;
 | 
			
		||||
        endpoint_response_text(response, 500, content, STRLEN(content));
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -44,27 +40,7 @@ api_v1_relays_tag_STR_GET(struct http_message *hm, endpoint_args_t *args, endpoi
 | 
			
		|||
        relay_free(relay);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    char *json_str = cJSON_Print(json);
 | 
			
		||||
    if (json_str == NULL)
 | 
			
		||||
    {
 | 
			
		||||
        LOG_ERROR("failed to print relays json\n");
 | 
			
		||||
 | 
			
		||||
        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
 | 
			
		||||
    {
 | 
			
		||||
        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);
 | 
			
		||||
    free(relays_ids);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -21,11 +21,7 @@ api_v1_schedules_STR_GET(struct http_message *hm, endpoint_args_t *args, endpoin
 | 
			
		|||
        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;
 | 
			
		||||
        endpoint_response_text(response, 400, content, STRLEN(content));
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -36,36 +32,13 @@ api_v1_schedules_STR_GET(struct http_message *hm, endpoint_args_t *args, endpoin
 | 
			
		|||
        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;
 | 
			
		||||
        endpoint_response_text(response, 404, content, STRLEN(content));
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    cJSON *json =  schedule_to_json(schedule);
 | 
			
		||||
    cJSON *json = schedule_to_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(schedule);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -81,11 +54,7 @@ api_v1_schedules_STR_PUT(struct http_message *hm, endpoint_args_t *args, endpoin
 | 
			
		|||
        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;
 | 
			
		||||
        endpoint_response_text(response, 400, content, STRLEN(content));
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -96,11 +65,7 @@ api_v1_schedules_STR_PUT(struct http_message *hm, endpoint_args_t *args, endpoin
 | 
			
		|||
        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;
 | 
			
		||||
        endpoint_response_text(response, 404, content, STRLEN(content));
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -109,11 +74,7 @@ api_v1_schedules_STR_PUT(struct http_message *hm, endpoint_args_t *args, endpoin
 | 
			
		|||
    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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -179,11 +140,7 @@ api_v1_schedules_STR_PUT(struct http_message *hm, endpoint_args_t *args, endpoin
 | 
			
		|||
        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;
 | 
			
		||||
        endpoint_response_text(response, 500, content, STRLEN(content));
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -219,26 +176,7 @@ api_v1_schedules_STR_PUT(struct http_message *hm, endpoint_args_t *args, endpoin
 | 
			
		|||
    cJSON_Delete(json);
 | 
			
		||||
    json = schedule_to_json(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 = 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);
 | 
			
		||||
    relay_free_list(relays);
 | 
			
		||||
    schedule_free(schedule);
 | 
			
		||||
| 
						 | 
				
			
			@ -257,11 +195,7 @@ api_v1_schedules_STR_DELETE(struct http_message *hm, endpoint_args_t *args, endp
 | 
			
		|||
        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;
 | 
			
		||||
        endpoint_response_text(response, 400, content, STRLEN(content));
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -272,22 +206,14 @@ api_v1_schedules_STR_DELETE(struct http_message *hm, endpoint_args_t *args, endp
 | 
			
		|||
        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;
 | 
			
		||||
        endpoint_response_text(response, 404, content, STRLEN(content));
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if(schedule_is_protected(schedule))
 | 
			
		||||
    {
 | 
			
		||||
        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;
 | 
			
		||||
        endpoint_response_text(response, 403, content, STRLEN(content));
 | 
			
		||||
 | 
			
		||||
        schedule_free(schedule);
 | 
			
		||||
        return;
 | 
			
		||||
| 
						 | 
				
			
			@ -298,19 +224,11 @@ api_v1_schedules_STR_DELETE(struct http_message *hm, endpoint_args_t *args, endp
 | 
			
		|||
        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;
 | 
			
		||||
        endpoint_response_text(response, 500, content, STRLEN(content));
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
        response->status_code = 200;
 | 
			
		||||
        response->content_type = "application/json";
 | 
			
		||||
        response->content_length = 0;
 | 
			
		||||
        response->content = "";
 | 
			
		||||
        response->alloced_content = false;
 | 
			
		||||
        endpoint_response_text(response, 200, "", 0);
 | 
			
		||||
    }
 | 
			
		||||
    schedule_free(schedule);
 | 
			
		||||
    return;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -19,11 +19,7 @@ api_v1_schedules_tag_STR_GET(struct http_message *hm, endpoint_args_t *args, end
 | 
			
		|||
        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;
 | 
			
		||||
        endpoint_response_text(response, 500, content, STRLEN(content));
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -44,27 +40,7 @@ api_v1_schedules_tag_STR_GET(struct http_message *hm, endpoint_args_t *args, end
 | 
			
		|||
        schedule_free(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);
 | 
			
		||||
    free(schedules_ids);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue