Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Refactor api action string generation.

Kishore Nallanda663087fac6
diff --git a/include/http_data.h b/include/http_data.h
index 3ffaa6ad..fda4413b 100644
--- a/include/http_data.h
+++ b/include/http_data.h
@@ -485,63 +485,7 @@ struct route_path {
return (hash > 100) ? hash : (hash + 100); // [0-99] reserved for special codes
}
- std::string _get_action() {
- // `resource:operation` forms an action
- // operations: create, get, list, delete, search, import, export
-
- std::string resource;
- std::string operation;
-
- size_t resource_index = 0;
- size_t identifier_index = 0;
-
- for(size_t i = 0; i < path_parts.size(); i++) {
- if(path_parts[i][0] == ':') {
- identifier_index = i;
- }
- }
-
- if(identifier_index == 0) {
- // means that no identifier found, so set the last part as resource
- resource_index = path_parts.size() - 1;
- } else if(identifier_index == (path_parts.size() - 1)) {
- // is already last position
- resource_index = identifier_index - 1;
- } else {
- resource_index = identifier_index + 1;
- }
-
- resource = path_parts[resource_index];
-
- // special case to maintain semantics and backward compatibility
- if(resource == "multi_search") {
- return "documents:search";
- }
-
- if(resource_index != path_parts.size() - 1 && path_parts[resource_index+1][0] != ':') {
- // e.g. /collections/:collection/documents/search
- operation = path_parts[resource_index+1];
- } else {
- // e.g /collections or /collections/:collection/foo or /collections/:collection
-
- if(http_method == "GET") {
- // GET can be a `get` or `list`
- operation = (resource_index == path_parts.size()-1) ? "list" : "get";
- } else if(http_method == "POST") {
- operation = "create";
- } else if(http_method == "PUT") {
- operation = "upsert";
- } else if(http_method == "DELETE") {
- operation = "delete";
- } else if(http_method == "PATCH") {
- operation = "update";
- } else {
- operation = "unknown";
- }
- }
-
- return resource + ":" + operation;
- }
+ std::string _get_action();
};
struct h2o_custom_res_message_t {
diff --git a/src/http_data.cpp b/src/http_data.cpp
new file mode 100644
index 00000000..7924b653
--- /dev/null
+++ b/src/http_data.cpp
@@ -0,0 +1,55 @@
+#include "http_data.h"
+
+std::string route_path::_get_action() {
+ // `resource:operation` forms an action
+ // operations: create, get, list, delete, search, import, export
+
+ std::string resource_path;
+ std::string operation;
+ size_t identifier_index = 0;
+
+ for(size_t i = 0; i < path_parts.size(); i++) {
+ if(i == 0 && path_parts.size() > 2 && path_parts[i] == "collections") {
+ // sub-resource of a collection, e.g. /collections/:name/overrides should be treated as
+ // top-level resource to maintain backward compatibility
+ continue;
+ }
+
+ if(path_parts[i][0] == ':') {
+ identifier_index = i;
+ } else if(resource_path.empty()){
+ resource_path = path_parts[i];
+ } else {
+ resource_path = resource_path + "/" + path_parts[i];
+ }
+ }
+
+ // special cases to maintain semantics and backward compatibility
+ if(resource_path == "multi_search" || resource_path == "documents/search") {
+ return "documents:search";
+ }
+
+ if(resource_path == "documents/import" || resource_path == "documents/export") {
+ StringUtils::replace_all(resource_path, "documents/", "");
+ return "documents:" + resource_path;
+ }
+
+ // e.g /collections or /collections/:collection/foo or /collections/:collection
+
+ if(http_method == "GET") {
+ // GET can be a `get` or `list`
+ operation = (identifier_index != 0) ? "get" : "list";
+ } else if(http_method == "POST") {
+ operation = "create";
+ } else if(http_method == "PUT") {
+ operation = "upsert";
+ } else if(http_method == "DELETE") {
+ operation = "delete";
+ } else if(http_method == "PATCH") {
+ operation = "update";
+ } else {
+ operation = "unknown";
+ }
+
+ return resource_path + ":" + operation;
+}
diff --git a/src/http_server.cpp b/src/http_server.cpp
index 6981b7df..baba7d14 100644
--- a/src/http_server.cpp
+++ b/src/http_server.cpp
@@ -538,6 +538,8 @@ int HttpServer::catch_all_handler(h2o_handler_t *_h2o_handler, h2o_req_t *req) {
request->metadata = multi_search_key;
}
+ LOG(INFO) << "action: " << rpath->action;
+
// routes match and is an authenticated request
// do any additional pre-request middleware operations here
if(rpath->action == "keys:create") {
diff --git a/test/auth_manager_test.cpp b/test/auth_manager_test.cpp
index e2359514..ee35d3e2 100644
--- a/test/auth_manager_test.cpp
+++ b/test/auth_manager_test.cpp
@@ -266,20 +266,34 @@ TEST_F(AuthManagerTest, GenerationOfAPIAction) {
route_path rpath_coll_create = route_path("POST", {"collections"}, nullptr, false, false);
route_path rpath_coll_get = route_path("GET", {"collections", ":collection"}, nullptr, false, false);
route_path rpath_coll_list = route_path("GET", {"collections"}, nullptr, false, false);
+ route_path rpath_coll_import = route_path("POST", {"collections", ":collection", "documents", "import"}, nullptr, false, false);
+ route_path rpath_coll_export = route_path("GET", {"collections", ":collection", "documents", "export"}, nullptr, false, false);
route_path rpath_keys_post = route_path("POST", {"keys"}, nullptr, false, false);
route_path rpath_doc_delete = route_path("DELETE", {"collections", ":collection", "documents", ":id"}, nullptr, false, false);
route_path rpath_override_upsert = route_path("PUT", {"collections", ":collection", "overrides", ":id"}, nullptr, false, false);
route_path rpath_doc_patch = route_path("PATCH", {"collections", ":collection", "documents", ":id"}, nullptr, false, false);
+ route_path rpath_analytics_rules_list = route_path("GET", {"analytics", "rules"}, nullptr, false, false);
+ route_path rpath_analytics_rules_get = route_path("GET", {"analytics", "rules", ":id"}, nullptr, false, false);
+ route_path rpath_analytics_rules_put = route_path("PUT", {"analytics", "rules", ":id"}, nullptr, false, false);
+ route_path rpath_ops_cache_clear_post = route_path("POST", {"operations", "cache", "clear"}, nullptr, false, false);
+ route_path rpath_conv_models_list = route_path("GET", {"conversations", "models"}, nullptr, false, false);
ASSERT_STREQ("documents:search", rpath_search._get_action().c_str());
ASSERT_STREQ("documents:search", rpath_multi_search._get_action().c_str());
ASSERT_STREQ("collections:create", rpath_coll_create._get_action().c_str());
ASSERT_STREQ("collections:get", rpath_coll_get._get_action().c_str());
+ ASSERT_STREQ("documents:import", rpath_coll_import._get_action().c_str());
+ ASSERT_STREQ("documents:export", rpath_coll_export._get_action().c_str());
ASSERT_STREQ("collections:list", rpath_coll_list._get_action().c_str());
ASSERT_STREQ("keys:create", rpath_keys_post._get_action().c_str());
ASSERT_STREQ("documents:delete", rpath_doc_delete._get_action().c_str());
ASSERT_STREQ("overrides:upsert", rpath_override_upsert._get_action().c_str());
ASSERT_STREQ("documents:update", rpath_doc_patch._get_action().c_str());
+ ASSERT_STREQ("analytics/rules:list", rpath_analytics_rules_list._get_action().c_str());
+ ASSERT_STREQ("analytics/rules:get", rpath_analytics_rules_get._get_action().c_str());
+ ASSERT_STREQ("analytics/rules:upsert", rpath_analytics_rules_put._get_action().c_str());
+ ASSERT_STREQ("operations/cache/clear:create", rpath_ops_cache_clear_post._get_action().c_str());
+ ASSERT_STREQ("conversations/models:list", rpath_conv_models_list._get_action().c_str());
}
TEST_F(AuthManagerTest, ScopedAPIKeys) {