From 60bab8b2d7b61383188c10f5d931dc7b5522d042 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Sat, 6 Jun 2020 17:17:08 +0100 Subject: Add HTTP API to main menu (#9998) --- src/script/lua_api/l_http.cpp | 44 ++++++++++++++++++++++++++++++++++++++- src/script/lua_api/l_http.h | 6 ++++++ src/script/scripting_mainmenu.cpp | 2 ++ 3 files changed, 51 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/script/lua_api/l_http.cpp b/src/script/lua_api/l_http.cpp index 2ff651cb5..73b4586e0 100644 --- a/src/script/lua_api/l_http.cpp +++ b/src/script/lua_api/l_http.cpp @@ -83,6 +83,24 @@ void ModApiHttp::push_http_fetch_result(lua_State *L, HTTPFetchResult &res, bool setstringfield(L, -1, "data", res.data); } +// http_api.fetch_sync(HTTPRequest definition) +int ModApiHttp::l_http_fetch_sync(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + + HTTPFetchRequest req; + read_http_fetch_request(L, req); + + infostream << "Mod performs HTTP request with URL " << req.url << std::endl; + + HTTPFetchResult res; + httpfetch_sync(req, res); + + push_http_fetch_result(L, res, true); + + return 1; +} + // http_api.fetch_async(HTTPRequest definition) int ModApiHttp::l_http_fetch_async(lua_State *L) { @@ -180,11 +198,35 @@ int ModApiHttp::l_request_http_api(lua_State *L) return 1; } + +int ModApiHttp::l_get_http_api(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + + lua_newtable(L); + HTTP_API(fetch_async); + HTTP_API(fetch_async_get); + HTTP_API(fetch_sync); + + return 1; +} + #endif void ModApiHttp::Initialize(lua_State *L, int top) { #if USE_CURL - API_FCT(request_http_api); + + bool isMainmenu = false; +#ifndef SERVER + isMainmenu = ModApiBase::getGuiEngine(L) != nullptr; +#endif + + if (isMainmenu) { + API_FCT(get_http_api); + } else { + API_FCT(request_http_api); + } + #endif } diff --git a/src/script/lua_api/l_http.h b/src/script/lua_api/l_http.h index 3d9cdad29..c665235a9 100644 --- a/src/script/lua_api/l_http.h +++ b/src/script/lua_api/l_http.h @@ -32,6 +32,9 @@ private: static void read_http_fetch_request(lua_State *L, HTTPFetchRequest &req); static void push_http_fetch_result(lua_State *L, HTTPFetchResult &res, bool completed = true); + // http_fetch_sync({url=, timeout=, post_data=}) + static int l_http_fetch_sync(lua_State *L); + // http_fetch_async({url=, timeout=, post_data=}) static int l_http_fetch_async(lua_State *L); @@ -40,6 +43,9 @@ private: // request_http_api() static int l_request_http_api(lua_State *L); + + // get_http_api() + static int l_get_http_api(lua_State *L); #endif public: diff --git a/src/script/scripting_mainmenu.cpp b/src/script/scripting_mainmenu.cpp index b6068439a..08858b1a5 100644 --- a/src/script/scripting_mainmenu.cpp +++ b/src/script/scripting_mainmenu.cpp @@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "content/mods.h" #include "cpp_api/s_internal.h" #include "lua_api/l_base.h" +#include "lua_api/l_http.h" #include "lua_api/l_mainmenu.h" #include "lua_api/l_sound.h" #include "lua_api/l_util.h" @@ -67,6 +68,7 @@ void MainMenuScripting::initializeModApi(lua_State *L, int top) ModApiMainMenu::Initialize(L, top); ModApiUtil::Initialize(L, top); ModApiSound::Initialize(L, top); + ModApiHttp::Initialize(L, top); asyncEngine.registerStateInitializer(registerLuaClasses); asyncEngine.registerStateInitializer(ModApiMainMenu::InitializeAsync); -- cgit v1.2.3