added read-url
This commit is contained in:
29
ml.cpp
29
ml.cpp
@@ -53,6 +53,7 @@ std::string read_file_contents(std::string filename) {
|
||||
#include <exception>
|
||||
|
||||
#include "csvparser.h"
|
||||
#include "sslclient.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// ERROR MESSAGES /////////////////////////////////////////////////////////////
|
||||
@@ -1273,6 +1274,33 @@ namespace builtin {
|
||||
return result;
|
||||
}
|
||||
|
||||
// Read URL to (code content)
|
||||
Value read_url(std::vector<Value> args, Environment &env) {
|
||||
// Is not a special form, so we can evaluate our args.
|
||||
eval_args(args, env);
|
||||
|
||||
// PERF optimize it for memory usage and performance
|
||||
// TODO handle second parameter (headers)
|
||||
if (args.size() != 1)
|
||||
throw Error(Value("read_url", write_file), env, args.size() > 1? TOO_MANY_ARGS : TOO_FEW_ARGS);
|
||||
|
||||
std::unordered_map<std::string, std::string> headers = {};
|
||||
HttpClient client;
|
||||
|
||||
if (args.size() == 2) {
|
||||
// do magick here
|
||||
// for (auto i = map.begin(); i != map.end(); ++i) {
|
||||
// headers[i->first] = i->second.getString();
|
||||
// }
|
||||
}
|
||||
|
||||
std::pair<int, std::string> result = client.doGetRequest(args[0].as_string(), headers);
|
||||
std::vector<Value> lst;
|
||||
lst.push_back(Value(result.first));
|
||||
lst.push_back(Value::string(result.second));
|
||||
return lst;
|
||||
}
|
||||
|
||||
// Read a file and execute its code
|
||||
Value include(std::vector<Value> args, Environment &env) {
|
||||
// Import is technically not a special form, it's more of a macro.
|
||||
@@ -1794,6 +1822,7 @@ Value Environment::get(std::string name) const {
|
||||
if (name == "parse-csv") return Value("parse-csv", builtin::parse_csv);
|
||||
if (name == "read-file") return Value("read-file", builtin::read_file);
|
||||
if (name == "write-file") return Value("write-file", builtin::write_file);
|
||||
if (name == "read-url") return Value("read-url", builtin::read_url);
|
||||
#endif
|
||||
|
||||
// String operations
|
||||
|
||||
Reference in New Issue
Block a user