comparison OrthancServer/LuaScripting.cpp @ 1437:02f5a3f5c0a0

access to the REST API from Lua
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 30 Jun 2015 18:41:33 +0200
parents 0a3e3be59094
children 3567503c00a7
comparison
equal deleted inserted replaced
1436:0a3e3be59094 1437:02f5a3f5c0a0
34 #include "LuaScripting.h" 34 #include "LuaScripting.h"
35 35
36 #include "ServerContext.h" 36 #include "ServerContext.h"
37 #include "OrthancInitialization.h" 37 #include "OrthancInitialization.h"
38 #include "../Core/Lua/LuaFunctionCall.h" 38 #include "../Core/Lua/LuaFunctionCall.h"
39 #include "../Core/HttpServer/StringHttpOutput.h"
39 40
40 #include "Scheduler/DeleteInstanceCommand.h" 41 #include "Scheduler/DeleteInstanceCommand.h"
41 #include "Scheduler/StoreScuCommand.h" 42 #include "Scheduler/StoreScuCommand.h"
42 #include "Scheduler/StorePeerCommand.h" 43 #include "Scheduler/StorePeerCommand.h"
43 #include "Scheduler/ModifyInstanceCommand.h" 44 #include "Scheduler/ModifyInstanceCommand.h"
48 #include <EmbeddedResources.h> 49 #include <EmbeddedResources.h>
49 50
50 51
51 namespace Orthanc 52 namespace Orthanc
52 { 53 {
54 OrthancRestApi* LuaScripting::GetRestApi(lua_State *state)
55 {
56 const void* value = LuaContext::GetGlobalVariable(state, "_RestApi");
57 return const_cast<OrthancRestApi*>(reinterpret_cast<const OrthancRestApi*>(value));
58 }
59
60
61 int LuaScripting::RestApiGet(lua_State *state)
62 {
63 OrthancRestApi* restApi = GetRestApi(state);
64 if (restApi == NULL)
65 {
66 LOG(ERROR) << "Lua: The REST API is unavailable";
67 lua_pushnil(state);
68 return 1;
69 }
70
71 // Check the types of the arguments
72 int nArgs = lua_gettop(state);
73 if (nArgs != 1 || !lua_isstring(state, 1)) // URI
74 {
75 LOG(ERROR) << "Lua: Bad parameters to RestApiGet()";
76 lua_pushnil(state);
77 return 1;
78 }
79
80 const char* uri = lua_tostring(state, 1);
81
82 std::string str;
83 if (restApi->SimpleGet(str, uri))
84 {
85 lua_pushstring(state, str.c_str());
86 }
87 else
88 {
89 LOG(ERROR) << "Lua: Error in RestApiGet() for URI " << uri;
90 lua_pushnil(state);
91 }
92
93 return 1;
94 }
95
96
53 IServerCommand* LuaScripting::ParseOperation(const std::string& operation, 97 IServerCommand* LuaScripting::ParseOperation(const std::string& operation,
54 const Json::Value& parameters) 98 const Json::Value& parameters)
55 { 99 {
56 if (operation == "delete") 100 if (operation == "delete")
57 { 101 {
198 job.SetDescription(description); 242 job.SetDescription(description);
199 context_.GetScheduler().Submit(job); 243 context_.GetScheduler().Submit(job);
200 } 244 }
201 245
202 246
203 LuaScripting::LuaScripting(ServerContext& context) : context_(context) 247 LuaScripting::LuaScripting(ServerContext& context) : context_(context), restApi_(NULL)
204 { 248 {
249 lua_.RegisterFunction("RestApiGet", RestApiGet);
250
205 lua_.Execute(Orthanc::EmbeddedResources::LUA_TOOLBOX); 251 lua_.Execute(Orthanc::EmbeddedResources::LUA_TOOLBOX);
206 lua_.SetHttpProxy(Configuration::GetGlobalStringParameter("HttpProxy", "")); 252 lua_.SetHttpProxy(Configuration::GetGlobalStringParameter("HttpProxy", ""));
253 }
254
255
256 void LuaScripting::SetOrthancRestApi(OrthancRestApi& restApi)
257 {
258 lua_.SetGlobalVariable("_RestApi", &restApi);
259 }
260
261
262 void LuaScripting::ResetOrthancRestApi()
263 {
264 lua_.SetGlobalVariable("_RestApi", NULL);
207 } 265 }
208 266
209 267
210 void LuaScripting::ApplyOnStoredInstance(const std::string& instanceId, 268 void LuaScripting::ApplyOnStoredInstance(const std::string& instanceId,
211 const Json::Value& simplifiedTags, 269 const Json::Value& simplifiedTags,