api help is shown

This commit is contained in:
Florian Hoss 2022-05-02 18:01:32 +02:00
parent 0c5ef2e939
commit bbebb4060e
2 changed files with 39 additions and 22 deletions

View file

@ -29,6 +29,15 @@ namespace fw
this->server->handleClient(); this->server->handleClient();
} }
String API::get_url_base()
{
#ifdef ESP32
return "http://" + this->server_ip + ":" + this->server_port;
#elif defined(ESP8266)
return "https://" + this->server_ip + ":" + this->server_port;
#endif
}
ok_t API::setup_auth(const char *username, const char *password) ok_t API::setup_auth(const char *username, const char *password)
{ {
if (!username || *username == 0x00 || strlen(username) > CREDENTIALS_LENGTH) if (!username || *username == 0x00 || strlen(username) > CREDENTIALS_LENGTH)
@ -77,17 +86,14 @@ namespace fw
this->server->on(UriRegex("/firewall/([0-9]+)"), HTTP_DELETE, std::bind(&API::delete_firewall_handler, this)); this->server->on(UriRegex("/firewall/([0-9]+)"), HTTP_DELETE, std::bind(&API::delete_firewall_handler, this));
add_api_endpoint("/firewall/1", "DELETE", "Delete Firewall Rule by key"); add_api_endpoint("/firewall/1", "DELETE", "Delete Firewall Rule by key");
this->server->on("/api", HTTP_GET, std::bind(&API::api_endpoints_handler, this));
this->server->onNotFound(std::bind(&API::not_found_handler, this)); this->server->onNotFound(std::bind(&API::not_found_handler, this));
} }
void API::add_api_endpoint(const String uri, const char *method, const char *description) void API::add_api_endpoint(const String uri, const char *method, const char *description)
{ {
api_endpoint_t *temp; api_endpoint_t *temp;
#ifdef ESP32 const String url = get_url_base() + uri;
const String url = "http://" + this->server_ip + ":" + this->server_port + uri;
#elif defined(ESP8266)
const String url = "https://" + this->server_ip + ":" + this->server_port + uri;
#endif
api_endpoint_t *api_ptr = (api_endpoint_t *)malloc(sizeof(api_endpoint_t)); api_endpoint_t *api_ptr = (api_endpoint_t *)malloc(sizeof(api_endpoint_t));
strncpy(api_ptr->uri, url.c_str(), sizeof(api_ptr->uri)); strncpy(api_ptr->uri, url.c_str(), sizeof(api_ptr->uri));
@ -112,8 +118,14 @@ namespace fw
void API::not_found_handler() void API::not_found_handler()
{ {
uint16_t response_code = 404; this->json_message_response(
this->json_generic_response(this->construct_json_api(response_code), response_code); "see " + get_url_base() + "/api for available routes",
404);
}
void API::api_endpoints_handler()
{
this->json_generic_response(this->construct_json_api(), 200);
} }
void API::get_firewall_rule_handler() void API::get_firewall_rule_handler()
@ -126,7 +138,7 @@ namespace fw
if (rule_ptr == NULL) if (rule_ptr == NULL)
this->json_message_response("rule does not exist", 404); this->json_message_response("rule does not exist", 404);
else else
this->json_generic_response(construct_json_firewall_rule(rule_ptr), 200); this->json_generic_response(this->construct_json_firewall_rule(rule_ptr), 200);
} }
void API::get_firewall_rules_handler() void API::get_firewall_rules_handler()
@ -196,13 +208,15 @@ namespace fw
void API::json_generic_response(String serialized_string, const uint16_t response_code) void API::json_generic_response(String serialized_string, const uint16_t response_code)
{ {
this->server->send(response_code, "application/json; charset=utf-8", serialized_string); this->server->send(
response_code,
"application/json; charset=utf-8",
construct_json_begin(response_code) + "\"result\": [" + serialized_string + "]}");
} }
void API::json_message_response(String message, const uint16_t response_code) void API::json_message_response(String message, const uint16_t response_code)
{ {
String serialized_string = "{"; String serialized_string = construct_json_begin(response_code);
serialized_string += json_new_attribute("status", response_code_to_string(response_code));
serialized_string += json_new_attribute("message", message, true); serialized_string += json_new_attribute("message", message, true);
serialized_string += "}"; serialized_string += "}";
this->server->send(response_code, "application/json; charset=utf-8", serialized_string); this->server->send(response_code, "application/json; charset=utf-8", serialized_string);
@ -223,9 +237,7 @@ namespace fw
String API::construct_json_firewall() String API::construct_json_firewall()
{ {
firewall_rule_t *rule_ptr = rule_head; firewall_rule_t *rule_ptr = rule_head;
String serialized_string = "{"; String serialized_string;
serialized_string += json_new_attribute("status", response_code_to_string(200));
serialized_string += "\"rules\": [";
while (rule_ptr != NULL) while (rule_ptr != NULL)
{ {
serialized_string += construct_json_firewall_rule(rule_ptr); serialized_string += construct_json_firewall_rule(rule_ptr);
@ -233,7 +245,6 @@ namespace fw
if (rule_ptr != NULL) if (rule_ptr != NULL)
serialized_string += ","; serialized_string += ",";
} }
serialized_string += "]}";
return serialized_string; return serialized_string;
} }
@ -247,12 +258,10 @@ namespace fw
return serialized_string; return serialized_string;
} }
String API::construct_json_api(const uint16_t response_code) String API::construct_json_api()
{ {
api_endpoint_t *api_ptr = this->endpoint_head; api_endpoint_t *api_ptr = this->endpoint_head;
String serialized_string = "{"; String serialized_string;
serialized_string += json_new_attribute("status", response_code_to_string(response_code));
serialized_string += "\"endpoints\": [";
while (api_ptr != NULL) while (api_ptr != NULL)
{ {
serialized_string += construct_json_api_endpoint(api_ptr); serialized_string += construct_json_api_endpoint(api_ptr);
@ -260,7 +269,13 @@ namespace fw
if (api_ptr != NULL) if (api_ptr != NULL)
serialized_string += ","; serialized_string += ",";
} }
serialized_string += "]}"; return serialized_string;
}
String API::construct_json_begin(const uint16_t response_code)
{
String serialized_string = "{";
serialized_string += json_new_attribute("status", response_code_to_string(response_code));
return serialized_string; return serialized_string;
} }
} }

View file

@ -44,6 +44,7 @@ namespace fw
void post_firewall_handler(); void post_firewall_handler();
void delete_firewall_handler(); void delete_firewall_handler();
void not_found_handler(); void not_found_handler();
void api_endpoints_handler();
bool request_has_firewall_parameter(); bool request_has_firewall_parameter();
String json_new_attribute(String key, String value, bool last = false); String json_new_attribute(String key, String value, bool last = false);
@ -53,13 +54,14 @@ namespace fw
String construct_json_firewall_rule(firewall_rule_t *); String construct_json_firewall_rule(firewall_rule_t *);
String construct_json_firewall(); String construct_json_firewall();
String construct_json_api_endpoint(api_endpoint_t *); String construct_json_api_endpoint(api_endpoint_t *);
String construct_json_api(const uint16_t response_code); String construct_json_api();
String construct_json_array(String message, String array_name); String construct_json_begin(const uint16_t response_code);
protected: protected:
String server_ip; String server_ip;
uint16_t server_port; uint16_t server_port;
void handle_client(); void handle_client();
String get_url_base();
public: public:
API(const char *cert, const char *key, const char *username, const char *password, const String ip, const uint16_t port); API(const char *cert, const char *key, const char *username, const char *password, const String ip, const uint16_t port);