core-legacy/endpoints/api_v1_relays.c

49 lines
1.3 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_relays.h>
#include <logger.h>
#include <models/junction_tag.h>
#include <models/relay.h>
#include <models/tag.h>
void
2020-05-20 23:33:18 +00:00
api_v1_relays_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
{
(void)args;
(void)hm;
relay_t** all_relays = relay_get_all();
cJSON *json = cJSON_CreateArray();
for(int i = 0; all_relays[i] != NULL; ++i)
{
cJSON *json_relay = relay_to_json(all_relays[i]);
cJSON_AddItemToArray(json, json_relay);
}
char *json_str = cJSON_Print(json);
if (json_str == NULL)
{
LOG_ERROR("failed to print relays json\n");
2020-05-20 23:33:18 +00:00
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
{
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);
relay_free_list(all_relays);
}