comparison OrthancFramework/Sources/Lua/LuaContext.cpp @ 5016:c89ffa13173e

Lua: new "SetHttpTimeout" function
author Alain Mazy <am@osimis.io>
date Mon, 13 Jun 2022 16:55:22 +0200
parents 43e613a7756b
children 0ea402b4d901
comparison
equal deleted inserted replaced
5015:5b41a8a77cf1 5016:c89ffa13173e
181 } 181 }
182 #endif 182 #endif
183 183
184 184
185 #if ORTHANC_ENABLE_CURL == 1 185 #if ORTHANC_ENABLE_CURL == 1
186 int LuaContext::SetHttpTimeout(lua_State *state)
187 {
188 LuaContext& that = GetLuaContext(state);
189
190 // Check the types of the arguments
191 int nArgs = lua_gettop(state);
192 if (nArgs != 1 ||
193 !lua_isnumber(state, 1)) // Timeout
194 {
195 LOG(ERROR) << "Lua: Bad parameters to SetHttpTimeout()";
196 }
197 else
198 {
199 // Configure the HTTP client
200 // Convert to "int" if truncation does not loose precision
201 long timeout = static_cast<long>(lua_tonumber(state, 1));
202
203 that.httpClient_.SetTimeout(timeout);
204 }
205
206 return 0;
207 }
208 #endif
209
210
211 #if ORTHANC_ENABLE_CURL == 1
186 bool LuaContext::AnswerHttpQuery(lua_State* state) 212 bool LuaContext::AnswerHttpQuery(lua_State* state)
187 { 213 {
188 std::string str; 214 std::string str;
189 215
190 try 216 try
564 lua_register(lua_, "HttpGet", CallHttpGet); 590 lua_register(lua_, "HttpGet", CallHttpGet);
565 lua_register(lua_, "HttpPost", CallHttpPost); 591 lua_register(lua_, "HttpPost", CallHttpPost);
566 lua_register(lua_, "HttpPut", CallHttpPut); 592 lua_register(lua_, "HttpPut", CallHttpPut);
567 lua_register(lua_, "HttpDelete", CallHttpDelete); 593 lua_register(lua_, "HttpDelete", CallHttpDelete);
568 lua_register(lua_, "SetHttpCredentials", SetHttpCredentials); 594 lua_register(lua_, "SetHttpCredentials", SetHttpCredentials);
595 lua_register(lua_, "SetHttpTimeout", SetHttpTimeout);
569 #endif 596 #endif
570 597
571 SetGlobalVariable("_LuaContext", this); 598 SetGlobalVariable("_LuaContext", this);
572 } 599 }
573 600
616 { 643 {
617 lua_settop(lua_, 0); 644 lua_settop(lua_, 0);
618 lua_getglobal(lua_, name); 645 lua_getglobal(lua_, name);
619 return lua_type(lua_, -1) == LUA_TFUNCTION; 646 return lua_type(lua_, -1) == LUA_TFUNCTION;
620 } 647 }
621
622
623 #if ORTHANC_ENABLE_CURL == 1
624 void LuaContext::SetHttpCredentials(const char* username,
625 const char* password)
626 {
627 httpClient_.SetCredentials(username, password);
628 }
629 #endif
630 648
631 649
632 void LuaContext::Execute(Json::Value& output, 650 void LuaContext::Execute(Json::Value& output,
633 const std::string& command) 651 const std::string& command)
634 { 652 {