#include "ground_lib.h" #include #include #include GroundValue simpleRequest(GroundValue* args, int arg_count) { VALIDATE_ARGS_1(GROUND_STRING); cpr::Response r = cpr::Get(cpr::Url(GET_STRING(args[0]))); if (!(r.status_code >= 200 && r.status_code < 300)) { return ground_string_val("Error code " + std::to_string(r.status_code)); } return ground_string_val(r.text); } GroundValue saveContents(GroundValue* args, int arg_count) { VALIDATE_ARGS_2(GROUND_STRING, GROUND_STRING); std::ofstream file(GET_STRING(args[1]), std::ios::binary); if (file.good()) { cpr::Response r = cpr::Download(file, cpr::Url{GET_STRING(args[0])}); if (r.status_code >= 200 && r.status_code < 300) { return GROUND_BOOL_VAL(false); } } else { return GROUND_BOOL_VAL(false); } return GROUND_BOOL_VAL(true); } GROUND_LIBRARY_INTERFACE() GROUND_LIBRARY_INIT() REGISTER_GROUND_FUNCTION(simpleRequest); REGISTER_GROUND_FUNCTION(saveContents); GROUND_LIBRARY_INIT_END() GROUND_LIBRARY_CLEANUP() GROUND_LIBRARY_CLEANUP_END()