core-legacy/endpoints/api_v1_controllers.c

49 lines
1.4 KiB
C
Raw Normal View History

#include <cJSON.h>
2020-05-20 23:33:18 +00:00
#include <macros.h>
#include <constants.h>
#include <endpoints/api_v1_controllers.h>
#include <logger.h>
#include <models/junction_tag.h>
#include <models/controller.h>
#include <models/tag.h>
void
2020-05-20 23:33:18 +00:00
api_v1_controllers_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
{
(void)args;
(void)hm;
controller_t** all_controllers = controller_get_all();
cJSON *json = cJSON_CreateArray();
for(int i = 0; all_controllers[i] != NULL; ++i)
{
cJSON *json_controller = controller_to_json(all_controllers[i]);
cJSON_AddItemToArray(json, json_controller);
}
char *json_str = cJSON_Print(json);
if (json_str == NULL)
{
LOG_ERROR("failed to print controllers json\n");
2020-05-20 23:33:18 +00:00
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
{
2020-05-20 23:33:18 +00:00
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);
}