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

Add support for Cloudflare Workers AI

ozanarmagan6963aa3ece4a
diff --git a/include/conversation_model.h b/include/conversation_model.h
index 97458384..d8ff13fe 100644
--- a/include/conversation_model.h
+++ b/include/conversation_model.h
@@ -1,16 +1,23 @@
#pragma once
#include <string>
+#include <condition_variable>
+#include <mutex>
#include <json.hpp>
#include "option.h"
+struct async_conversation_t {
+ std::string response;
+ std::mutex mutex;
+ std::condition_variable cv;
+};
+
class ConversationModel {
public:
virtual ~ConversationModel() {};
static Option<std::string> get_answer(const std::string& context, const std::string& prompt, const nlohmann::json& model_config);
- static Option<nlohmann::json> parse_conversation_history(const nlohmann::json& conversation, const nlohmann::json& model_config);
static Option<bool> validate_model(const nlohmann::json& model_config);
static Option<std::string> get_standalone_question(const nlohmann::json& conversation_history, const std::string& question, const nlohmann::json& model_config);
static Option<nlohmann::json> format_question(const std::string& message, const nlohmann::json& model_config);
@@ -18,12 +25,10 @@ class ConversationModel {
private:
};
-
class OpenAIConversationModel : public ConversationModel {
public:
static Option<std::string> get_answer(const std::string& context, const std::string& prompt, const std::string& system_prompt, const nlohmann::json& model_config);
static Option<bool> validate_model(const nlohmann::json& model_config);
- static Option<nlohmann::json> parse_conversation_history(const nlohmann::json& conversation);
static Option<std::string> get_standalone_question(const nlohmann::json& conversation_history, const std::string& question, const nlohmann::json& model_config);
static Option<nlohmann::json> format_question(const std::string& message);
static Option<nlohmann::json> format_answer(const std::string& message);
@@ -36,3 +41,21 @@ class OpenAIConversationModel : public ConversationModel {
static constexpr char* OPENAI_LIST_MODELS = "https://api.openai.com/v1/models";
static constexpr char* OPENAI_CHAT_COMPLETION = "https://api.openai.com/v1/chat/completions";
};
+
+class CFConversationModel : public ConversationModel {
+ public:
+ static Option<std::string> get_answer(const std::string& context, const std::string& prompt, const std::string& system_prompt, const nlohmann::json& model_config);
+ static Option<bool> validate_model(const nlohmann::json& model_config);
+ static Option<std::string> get_standalone_question(const nlohmann::json& conversation_history, const std::string& question, const nlohmann::json& model_config);
+ static Option<nlohmann::json> format_question(const std::string& message);
+ static Option<nlohmann::json> format_answer(const std::string& message);
+ static const inline std::string STANDALONE_QUESTION_PROMPT = R"(
+ [INST]Rewrite the follow-up question on top of a human-assistant conversation history as a standalone question that encompasses all pertinent context.[/INST]
+ )";
+ static const inline std::string INFO_PROMPT = "[INST] You are an assistant for question-answering tasks. Use the following pieces of retrieved context to answer the question. If you don't know the answer, just say that you don't know. Use three sentences maximum and keep the answer concise. [/INST]";
+ // prevent instantiation
+ CFConversationModel() = delete;
+ private:
+ static const inline std::vector<std::string> CF_MODEL_NAMES{"mistral/mistral-7b-instruct-v0.1"};
+ static const std::string get_model_url(const std::string& model_name, const std::string& account_id);
+};
diff --git a/include/http_client.h b/include/http_client.h
index 94cb636c..1624fcce 100644
--- a/include/http_client.h
+++ b/include/http_client.h
@@ -22,21 +22,25 @@ private:
static size_t curl_write_async(char *buffer, size_t size, size_t nmemb, void* context);
+ static size_t curl_write_stream(char *buffer, size_t size, size_t nmemb, void *context);
+
static size_t curl_write_async_done(void* context, curl_socket_t item);
+ static size_t curl_write_stream_done(void* context, curl_socket_t item);
+
static size_t curl_write_download(void *ptr, size_t size, size_t nmemb, FILE *stream);
static CURL* init_curl(const std::string& url, std::string& response, const size_t timeout_ms = 0);
static CURL* init_curl_async(const std::string& url, deferred_req_res_t* req_res, curl_slist*& chunk,
bool send_ts_api_header);
+ static CURL* init_curl_stream(const std::string& url, async_stream_response_t& res, long timeout_ms);
static size_t curl_req_send_callback(char* buffer, size_t size, size_t nitems, void *userdata);
static long perform_curl(CURL *curl, std::map<std::string, std::string>& res_headers,
struct curl_slist *chunk = nullptr,
bool send_ts_api_header = false);
-
public:
static HttpClient & get_instance() {
static HttpClient instance;
@@ -71,6 +75,10 @@ public:
HttpServer* server,
bool send_ts_api_header = false);
+ static long post_response_stream(const std::string &url, const std::string &body, async_stream_response_t &response,
+ std::map<std::string, std::string>& res_headers,
+ const std::unordered_map<std::string, std::string>& headers, long timeout_ms=4000);
+
static long put_response(const std::string & url, const std::string & body, std::string & response,
std::map<std::string, std::string>& res_headers, long timeout_ms=4000,
bool send_ts_api_header = false);
diff --git a/include/http_data.h b/include/http_data.h
index 9a2bedc9..46da5a49 100644
--- a/include/http_data.h
+++ b/include/http_data.h
@@ -600,3 +600,10 @@ struct http_message_dispatcher {
message_handlers.emplace(message, handler);
}
};
+
+struct async_stream_response_t {
+ std::vector<std::string> response_chunks;
+ std::mutex mutex;
+ std::condition_variable cv;
+ bool ready = false;
+};
diff --git a/src/conversation_model.cpp b/src/conversation_model.cpp
index cc6311e8..da0dd513 100644
--- a/src/conversation_model.cpp
+++ b/src/conversation_model.cpp
@@ -25,6 +25,8 @@ Option<bool> ConversationModel::validate_model(const nlohmann::json& model_confi
const std::string model_namespace = get_model_namespace(model_config["model_name"].get<std::string>());
if(model_namespace == "openai") {
return OpenAIConversationModel::validate_model(model_config);
+ } else if(model_namespace == "cf") {
+ return CFConversationModel::validate_model(model_config);
}
return Option<bool>(400, "Model namespace `" + model_namespace + "` is not supported.");
@@ -41,26 +43,20 @@ Option<std::string> ConversationModel::get_answer(const std::string& context, co
if(model_namespace == "openai") {
return OpenAIConversationModel::get_answer(context, prompt, system_prompt, model_config);
+ } else if(model_namespace == "cf") {
+ return CFConversationModel::get_answer(context, prompt, system_prompt, model_config);
}
throw Option<std::string>(400, "Model namespace " + model_namespace + " is not supported.");
}
-Option<nlohmann::json> ConversationModel::parse_conversation_history(const nlohmann::json& conversation, const nlohmann::json& model_config) {
- const std::string model_namespace = get_model_namespace(model_config["model_name"].get<std::string>());
-
- if(model_namespace == "openai") {
- return OpenAIConversationModel::parse_conversation_history(conversation);
- }
-
- throw Option<nlohmann::json>(400, "Model namespace " + model_namespace + " is not supported.");
-}
-
Option<std::string> ConversationModel::get_standalone_question(const nlohmann::json& conversation_history, const std::string& question, const nlohmann::json& model_config) {
const std::string model_namespace = get_model_namespace(model_config["model_name"].get<std::string>());
if(model_namespace == "openai") {
return OpenAIConversationModel::get_standalone_question(conversation_history, question, model_config);
+ } else if(model_namespace == "cf") {
+ return CFConversationModel::get_standalone_question(conversation_history, question, model_config);
}
throw Option<std::string>(400, "Model namespace " + model_namespace + " is not supported.");
@@ -71,6 +67,8 @@ Option<nlohmann::json> ConversationModel::format_question(const std::string& mes
if(model_namespace == "openai") {
return OpenAIConversationModel::format_question(message);
+ } else if(model_namespace == "cf") {
+ return CFConversationModel::format_question(message);
}
throw Option<nlohmann::json>(400, "Model namespace " + model_namespace + " is not supported.");
@@ -81,6 +79,8 @@ Option<nlohmann::json> ConversationModel::format_answer(const std::string& messa
if(model_namespace == "openai") {
return OpenAIConversationModel::format_answer(message);
+ } else if(model_namespace == "cf") {
+ return CFConversationModel::format_answer(message);
}
throw Option<nlohmann::json>(400, "Model namespace " + model_namespace + " is not supported.");
@@ -227,33 +227,6 @@ Option<std::string> OpenAIConversationModel::get_answer(const std::string& conte
return Option<std::string>(json_res["choices"][0]["message"]["content"].get<std::string>());
}
-Option<nlohmann::json> OpenAIConversationModel::parse_conversation_history(const nlohmann::json& conversation) {
- if(!conversation.is_array()) {
- return Option<nlohmann::json>(400, "Conversation is not an array");
- }
-
- nlohmann::json messages = nlohmann::json::array();
- for(auto& message : conversation) {
- if(!message.is_object()) {
- return Option<nlohmann::json>(400, "Message is not an object");
- }
-
- if(message.count("role") == 0 || !message["role"].is_string()) {
- return Option<nlohmann::json>(400, "Message role is not provided or not a string");
- }
-
- if(message.count("content") == 0 || !message["content"].is_string()) {
- return Option<nlohmann::json>(400, "Message content is not provided or not a string");
- }
-
- auto parsed_message = nlohmann::json::object();
- parsed_message[message["role"].get<std::string>()] = message["content"].get<std::string>();
- messages.push_back(parsed_message);
- }
-
- return Option<nlohmann::json>(messages);
-}
-
Option<std::string> OpenAIConversationModel::get_standalone_question(const nlohmann::json& conversation_history,
const std::string& question, const nlohmann::json& model_config) {
const std::string model_name = EmbedderManager::get_model_name_without_namespace(model_config["model_name"].get<std::string>());
@@ -328,3 +301,247 @@ Option<nlohmann::json> OpenAIConversationModel::format_answer(const std::string&
return Option<nlohmann::json>(json);
}
+const std::string CFConversationModel::get_model_url(const std::string& model_name, const std::string& account_id) {
+ return "https://api.cloudflare.com/client/v4/accounts/" + account_id + "/ai/run/@cf/" + model_name;
+}
+
+Option<bool> CFConversationModel::validate_model(const nlohmann::json& model_config) {
+ if(model_config.count("api_key") == 0) {
+ return Option<bool>(400, "API key is not provided");
+ }
+
+ if(!model_config["api_key"].is_string()) {
+ return Option<bool>(400, "API key is not a string");
+ }
+
+ if(model_config.count("account_id") == 0) {
+ return Option<bool>(400, "Account ID is not provided");
+ }
+
+ if(!model_config["account_id"].is_string()) {
+ return Option<bool>(400, "Account ID is not a string");
+ }
+
+ auto model_name = EmbedderManager::get_model_name_without_namespace(model_config["model_name"].get<std::string>());
+
+ bool found = false;
+
+ for(auto& cf_model_name : CF_MODEL_NAMES) {
+ if(cf_model_name == model_name) {
+ found = true;
+ break;
+ }
+ }
+
+ if(!found) {
+ return Option<bool>(400, "Model name is not a valid Cloudflare model");
+ }
+
+ std::unordered_map<std::string, std::string> headers;
+ std::map<std::string, std::string> res_headers;
+ headers["Authorization"] = "Bearer " + model_config["api_key"].get<std::string>();
+ headers["Content-Type"] = "application/json";
+ std::string res;
+ auto url = get_model_url(model_name, model_config["account_id"].get<std::string>());
+ nlohmann::json req_body;
+ req_body["messages"] = R"([
+ {
+ "role":"user",
+ "content":"hello"
+ }
+ ])"_json;
+ std::string chat_res;
+
+ auto res_code = RemoteEmbedder::call_remote_api("POST", url, req_body.dump(), chat_res, res_headers, headers);
+
+ if(res_code == 408) {
+ return Option<bool>(408, "Cloudflare API timeout.");
+ }
+
+ if (res_code != 200) {
+ nlohmann::json json_res;
+ try {
+ json_res = nlohmann::json::parse(chat_res);
+ } catch (const std::exception& e) {
+ return Option<bool>(400, "Cloudflare API error: " + chat_res);
+ }
+ if(json_res.count("errors") == 0 || json_res["errors"].size() == 0 || json_res["errors"][0].count("message") == 0) {
+ return Option<bool>(400, "Cloudflare API error: " + chat_res);
+ }
+ return Option<bool>(400, "Cloudflare API error: " + nlohmann::json::parse(res)["errors"][0]["message"].get<std::string>());
+ }
+
+ return Option<bool>(true);
+}
+
+Option<std::string> CFConversationModel::get_answer(const std::string& context, const std::string& prompt, const std::string& system_prompt, const nlohmann::json& model_config) {
+ const std::string model_name = EmbedderManager::get_model_name_without_namespace(model_config["model_name"].get<std::string>());
+ const std::string api_key = model_config["api_key"].get<std::string>();
+ const std::string account_id = model_config["account_id"].get<std::string>();
+
+ std::unordered_map<std::string, std::string> headers;
+ std::map<std::string, std::string> res_headers;
+ headers["Authorization"] = "Bearer " + api_key;
+ headers["Content-Type"] = "application/json";
+ nlohmann::json req_body;
+ req_body["stream"] = true;
+ req_body["messages"] = nlohmann::json::array();
+
+ if(!system_prompt.empty()) {
+ nlohmann::json system_message = nlohmann::json::object();
+ system_message["role"] = "system";
+ system_message["content"] = system_prompt;
+ req_body["messages"].push_back(system_message);
+ }
+
+ nlohmann::json message = nlohmann::json::object();
+ message["role"] = "system";
+ message["content"] = INFO_PROMPT;
+ req_body["messages"].push_back(message);
+ message["role"] = "user";
+ message["content"] = "[INST]Context:\n" + context + "\n\nQuestion:\n" + prompt + "\n\nAnswer:[/INST]";
+ req_body["messages"].push_back(message);
+
+ std::string res;
+ auto url = get_model_url(model_name, account_id);
+ auto res_code = RemoteEmbedder::call_remote_api("POST_STREAM", url, req_body.dump(), res, res_headers, headers);
+
+ if(res_code == 408) {
+ return Option<std::string>(400, "Cloudflare API timeout.");
+ }
+
+ if (res_code != 200) {
+ nlohmann::json json_res;
+ try {
+ json_res = nlohmann::json::parse(res);
+ json_res = nlohmann::json::parse(json_res["response"].get<std::string>());
+ } catch (const std::exception& e) {
+ throw Option<std::string>(400, "Cloudflare API error: " + res);
+ }
+
+ if(json_res.count("errors") == 0 || json_res["errors"].size() == 0 || json_res["errors"][0].count("message") == 0) {
+ return Option<std::string>(400, "Cloudflare API error: " + res);
+ }
+ return Option<std::string>(400, "Cloudflare API error: " + nlohmann::json::parse(res)["error"][0]["message"].get<std::string>());
+ }
+ try {
+ auto json_res = nlohmann::json::parse(res);
+ std::string parsed_response = "";
+ std::vector<std::string> lines = json_res["response"].get<std::vector<std::string>>();
+ for(auto& line : lines) {
+ while(line.find("data:") != std::string::npos) {
+ auto substr_line = line.substr(line.find("data:") + 6);
+ if(substr_line.find("[DONE]") != std::string::npos) {
+ break;
+ }
+ nlohmann::json json_line;
+ if(substr_line.find("\n") != std::string::npos) {
+ json_line = nlohmann::json::parse(substr_line.substr(0, substr_line.find("\n")));
+ } else {
+ json_line = nlohmann::json::parse(substr_line);
+ }
+ parsed_response += json_line["response"];
+ line = substr_line;
+ }
+ }
+ return Option<std::string>(parsed_response);
+ } catch (const std::exception& e) {
+ LOG(ERROR) << e.what();
+ return Option<std::string>(400, "Got malformed response from Cloudflare API.");
+ }
+}
+
+Option<std::string> CFConversationModel::get_standalone_question(const nlohmann::json& conversation_history,
+ const std::string& question, const nlohmann::json& model_config) {
+ const std::string model_name = EmbedderManager::get_model_name_without_namespace(model_config["model_name"].get<std::string>());
+ const std::string api_key = model_config["api_key"].get<std::string>();
+ const std::string account_id = model_config["account_id"].get<std::string>();
+
+ std::unordered_map<std::string, std::string> headers;
+ std::map<std::string, std::string> res_headers;
+ headers["Authorization"] = "Bearer " + api_key;
+ headers["Content-Type"] = "application/json";
+ nlohmann::json req_body;
+ req_body["stream"] = true;
+ req_body["messages"] = nlohmann::json::array();
+ std::string res;
+
+ std::string standalone_question = STANDALONE_QUESTION_PROMPT;
+
+ standalone_question += "\n\n<Conversation history>\n";
+ for(auto& message : conversation_history["conversation"]) {
+ if(message.count("user") == 0 && message.count("assistant") == 0) {
+ return Option<std::string>(400, "Conversation history is not valid");
+ }
+
+ standalone_question += message.dump(0) + "\n";
+ }
+
+ standalone_question += "\n\n<Question>\n" + question;
+ standalone_question += "\n\n<Standalone question>\n";
+
+ nlohmann::json message = nlohmann::json::object();
+ message["role"] = "user";
+ message["content"] = standalone_question;
+
+ req_body["messages"].push_back(message);
+
+ auto url = get_model_url(model_name, account_id);
+
+ auto res_code = RemoteEmbedder::call_remote_api("POST_STREAM", url, req_body.dump(), res, res_headers, headers);
+
+ if(res_code == 408) {
+ return Option<std::string>(400, "OpenAI API timeout.");
+ }
+
+ if (res_code != 200) {
+ nlohmann::json json_res;
+ try {
+ json_res = nlohmann::json::parse(res);
+ json_res = nlohmann::json::parse(json_res["response"].get<std::string>());
+ } catch (const std::exception& e) {
+ return Option<std::string>(400, "Cloudflare API error: " + res);
+ }
+ if(json_res.count("errors") == 0 || json_res["errors"].size() == 0 || json_res["errors"][0].count("message") == 0) {
+ return Option<std::string>(400, "Cloudflare API error: " + res);
+ }
+ return Option<std::string>(400, "Cloudflare API error: " + nlohmann::json::parse(res)["errors"][0]["message"].get<std::string>());
+ }
+
+ try {
+ auto json_res = nlohmann::json::parse(res);
+ std::string parsed_response = "";
+ std::vector<std::string> lines = json_res["response"].get<std::vector<std::string>>();
+ for(auto& line : lines) {
+ while(line.find("data:") != std::string::npos) {
+ auto substr_line = line.substr(line.find("data:") + 6);
+ if(substr_line.find("[DONE]") != std::string::npos) {
+ break;
+ }
+ nlohmann::json json_line;
+ if(substr_line.find("\n") != std::string::npos) {
+ json_line = nlohmann::json::parse(substr_line.substr(0, substr_line.find("\n")));
+ } else {
+ json_line = nlohmann::json::parse(substr_line);
+ }
+ parsed_response += json_line["response"];
+ line = substr_line;
+ }
+ }
+ return Option<std::string>(parsed_response);
+ } catch (const std::exception& e) {
+ return Option<std::string>(400, "Got malformed response from Cloudflare API.");
+ }
+}
+
+Option<nlohmann::json> CFConversationModel::format_question(const std::string& message) {
+ nlohmann::json json = nlohmann::json::object();
+ json["user"] = message;
+ return Option<nlohmann::json>(json);
+}
+
+Option<nlohmann::json> CFConversationModel::format_answer(const std::string& message) {
+ nlohmann::json json = nlohmann::json::object();
+ json["assistant"] = message;
+ return Option<nlohmann::json>(json);
+}
\ No newline at end of file
diff --git a/src/http_client.cpp b/src/http_client.cpp
index cef788c6..9db981d1 100644
--- a/src/http_client.cpp
+++ b/src/http_client.cpp
@@ -36,6 +36,35 @@ long HttpClient::post_response(const std::string &url, const std::string &body,
}
+long HttpClient::post_response_stream(const std::string &url, const std::string &body, async_stream_response_t &response,
+ std::map<std::string, std::string>& res_headers,
+ const std::unordered_map<std::string, std::string>& headers, long timeout_ms) {
+ struct curl_slist* chunk = nullptr;
+
+ CURL *curl = init_curl_stream(url, response, timeout_ms);
+ if(curl == nullptr) {
+ return 500;
+ }
+
+ for(const auto& header: headers) {
+ std::string header_str = header.first + ": " + header.second;
+ chunk = curl_slist_append(chunk, header_str.c_str());
+ }
+
+ curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
+
+ curl_easy_setopt(curl, CURLOPT_POST, 1L);
+ curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body.c_str());
+ curl_easy_perform(curl);
+
+ long status_code = 0;
+ curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status_code);
+ curl_easy_cleanup(curl);
+ curl_slist_free_all(chunk);
+
+ return status_code;
+}
+
long HttpClient::post_response_async(const std::string &url, const std::shared_ptr<http_req> request,
const std::shared_ptr<http_res> response, HttpServer* server,
bool send_ts_api_header) {
@@ -292,6 +321,24 @@ size_t HttpClient::curl_write_async(char *buffer, size_t size, size_t nmemb, voi
return res_size;
}
+size_t HttpClient::curl_write_stream(char *buffer, size_t size, size_t nmemb, void *context) {
+ size_t res_size = size * nmemb;
+ auto res = reinterpret_cast<async_stream_response_t*>(context);
+ res->response_chunks.emplace_back(std::string(buffer, res_size));
+ return res_size;
+}
+
+size_t HttpClient::curl_write_stream_done(void *context, curl_socket_t item) {
+ auto res = reinterpret_cast<async_stream_response_t*>(context);
+
+ std::unique_lock<std::mutex> lock(res->mutex);
+ res->ready = true;
+ res->cv.notify_one();
+
+ close(item);
+ return 0;
+}
+
size_t HttpClient::curl_write_async_done(void *context, curl_socket_t item) {
//LOG(INFO) << "curl_write_async_done";
deferred_req_res_t* req_res = static_cast<deferred_req_res_t *>(context);
@@ -320,6 +367,34 @@ size_t HttpClient::curl_write_async_done(void *context, curl_socket_t item) {
return 0;
}
+CURL *HttpClient::init_curl_stream(const std::string& url, async_stream_response_t& res, long timeout_ms) {
+ CURL* curl = curl_easy_init();
+
+ if(!ca_cert_path.empty()) {
+ curl_easy_setopt(curl, CURLOPT_CAINFO, ca_cert_path.c_str());
+ } else {
+ LOG(WARNING) << "Unable to locate system SSL certificates.";
+ }
+
+
+ curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
+ curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, 4000);
+ curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, timeout_ms);
+ curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE);
+
+ // to allow self-signed certs
+ curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
+ curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
+
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, HttpClient::curl_write_stream);
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, &res);
+
+ curl_easy_setopt(curl, CURLOPT_CLOSESOCKETFUNCTION, HttpClient::curl_write_stream_done);
+ curl_easy_setopt(curl, CURLOPT_CLOSESOCKETDATA, &res);
+
+ return curl;
+}
+
CURL *HttpClient::init_curl_async(const std::string& url, deferred_req_res_t* req_res, curl_slist*& chunk,
bool send_ts_api_header) {
CURL *curl = curl_easy_init();
diff --git a/src/http_proxy.cpp b/src/http_proxy.cpp
index a340f8e1..1f39f4de 100644
--- a/src/http_proxy.cpp
+++ b/src/http_proxy.cpp
@@ -22,10 +22,20 @@ http_proxy_res_t HttpProxy::call(const std::string& url, const std::string& meth
res.status_code = client.put_response(url, req_body, res.body, res.headers, timeout_ms);
} else if(method == "DELETE") {
res.status_code = client.delete_response(url, res.body, res.headers, timeout_ms);
+ } else if(method == "POST_STREAM") {
+ async_stream_response_t stream_res;
+ res.status_code = client.post_response_stream(url, req_body, stream_res, res.headers, req_headers, timeout_ms);
+ std::unique_lock lock(stream_res.mutex);
+ stream_res.cv.wait(lock, [&](){
+ return stream_res.ready;
+ });
+ nlohmann::json j;
+ j["response"] = stream_res.response_chunks;
+ res.body = j.dump();
} else {
res.status_code = 400;
nlohmann::json j;
- j["message"] = "Parameter `method` must be one of GET, POST, PUT, DELETE.";
+ j["message"] = "Parameter `method` must be one of GET, POST, POST_STREAM, PUT, DELETE.";
res.body = j.dump();
}
return res;