ssl client bit of work
This commit is contained in:
22
ml.cpp
22
ml.cpp
@@ -1202,13 +1202,16 @@ MlValue read_url(std::vector<MlValue> args, MlEnvironment &env) {
|
||||
eval_args(args, env);
|
||||
|
||||
// PERF optimize it for memory usage and performance
|
||||
if (args.empty() || args.size() > 2)
|
||||
if (args.empty() || args.size() > 4)
|
||||
throw MlError(MlValue("read_url", read_url), env, args.empty() ? TOO_FEW_ARGS : TOO_MANY_ARGS);
|
||||
|
||||
std::unordered_map<std::string, std::string> headers = {};
|
||||
HttpClient client;
|
||||
std::string url = args[0].as_string();
|
||||
std::map<std::string, std::string> headers = {};
|
||||
std::string method = "GET";
|
||||
std::string body;
|
||||
|
||||
if (args.size() == 2) {
|
||||
// headers
|
||||
if (args.size() > 1) {
|
||||
for (const auto &hdr_val_pair: args[1].as_list()) {
|
||||
// TODO check its 2 string elements list
|
||||
const auto &pair = hdr_val_pair.as_list();
|
||||
@@ -1216,7 +1219,16 @@ MlValue read_url(std::vector<MlValue> args, MlEnvironment &env) {
|
||||
}
|
||||
}
|
||||
|
||||
std::pair<long, std::string> result = client.doGetRequest(args[0].as_string(), headers);
|
||||
// body
|
||||
if (args.size() > 2)
|
||||
body = args[2].as_string();
|
||||
|
||||
// method
|
||||
if (args.size() > 3)
|
||||
method = args[3].as_string();
|
||||
|
||||
|
||||
std::pair<long, std::string> result = HttpClient{}.doRequest(method, url, headers, body);
|
||||
|
||||
return std::vector<MlValue>{MlValue(result.first), MlValue::string(result.second)};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user