changeset 5016:c89ffa13173e

Lua: new "SetHttpTimeout" function
author Alain Mazy <am@osimis.io>
date Mon, 13 Jun 2022 16:55:22 +0200
parents 5b41a8a77cf1
children 0d61efc6256c 4be5a8b0af1d
files NEWS OrthancFramework/Sources/Lua/LuaContext.cpp OrthancFramework/Sources/Lua/LuaContext.h TODO
diffstat 4 files changed, 35 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Fri Jun 10 06:37:48 2022 +0200
+++ b/NEWS	Mon Jun 13 16:55:22 2022 +0200
@@ -1,6 +1,12 @@
 Pending changes in the mainline
 ===========================
 
+General
+-------
+
+* Lua: new "SetHttpTimeout" function
+
+
 Maintenance
 -----------
 
--- a/OrthancFramework/Sources/Lua/LuaContext.cpp	Fri Jun 10 06:37:48 2022 +0200
+++ b/OrthancFramework/Sources/Lua/LuaContext.cpp	Mon Jun 13 16:55:22 2022 +0200
@@ -183,6 +183,32 @@
 
 
 #if ORTHANC_ENABLE_CURL == 1
+  int LuaContext::SetHttpTimeout(lua_State *state)
+  {
+    LuaContext& that = GetLuaContext(state);
+
+    // Check the types of the arguments
+    int nArgs = lua_gettop(state);
+    if (nArgs != 1 ||
+        !lua_isnumber(state, 1))    // Timeout
+    {
+      LOG(ERROR) << "Lua: Bad parameters to SetHttpTimeout()";
+    }
+    else
+    {
+      // Configure the HTTP client
+      // Convert to "int" if truncation does not loose precision
+      long timeout = static_cast<long>(lua_tonumber(state, 1));
+
+      that.httpClient_.SetTimeout(timeout);
+    }
+
+    return 0;
+  }
+#endif
+
+
+#if ORTHANC_ENABLE_CURL == 1
   bool LuaContext::AnswerHttpQuery(lua_State* state)
   {
     std::string str;
@@ -566,6 +592,7 @@
     lua_register(lua_, "HttpPut", CallHttpPut);
     lua_register(lua_, "HttpDelete", CallHttpDelete);
     lua_register(lua_, "SetHttpCredentials", SetHttpCredentials);
+    lua_register(lua_, "SetHttpTimeout", SetHttpTimeout);
 #endif
 
     SetGlobalVariable("_LuaContext", this);
@@ -620,15 +647,6 @@
   }
 
 
-#if ORTHANC_ENABLE_CURL == 1
-  void LuaContext::SetHttpCredentials(const char* username,
-                                      const char* password)
-  {
-    httpClient_.SetCredentials(username, password);
-  }
-#endif
-
-
   void LuaContext::Execute(Json::Value& output,
                            const std::string& command)
   {
--- a/OrthancFramework/Sources/Lua/LuaContext.h	Fri Jun 10 06:37:48 2022 +0200
+++ b/OrthancFramework/Sources/Lua/LuaContext.h	Mon Jun 13 16:55:22 2022 +0200
@@ -70,6 +70,7 @@
 
 #if ORTHANC_ENABLE_CURL == 1
     static int SetHttpCredentials(lua_State *state);
+    static int SetHttpTimeout(lua_State *state);
     static int CallHttpPostOrPut(lua_State *state,
                                  HttpMethod method);
     static int CallHttpGet(lua_State *state);
@@ -105,11 +106,6 @@
 
     bool IsExistingFunction(const char* name);
 
-#if ORTHANC_ENABLE_CURL == 1
-    void SetHttpCredentials(const char* username,
-                            const char* password);
-#endif
-
     void RegisterFunction(const char* name,
                           lua_CFunction func);
 
--- a/TODO	Fri Jun 10 06:37:48 2022 +0200
+++ b/TODO	Mon Jun 13 16:55:22 2022 +0200
@@ -244,8 +244,7 @@
 Lua
 ===
 
-* Configure HTTP headers from Lua (in HttpGet(), HttpPost(),
-  HttpPut(), HttpDelete(), RestApiGet(), RestApiPost(), RestApiPut()
+* Configure HTTP headers from Lua (in RestApiPost(), RestApiPut()
   and RestApiDelete().
   https://groups.google.com/forum/#!msg/orthanc-users/WNnW187OILM/6XX_bm96BwAJ