Mercurial > hg > orthanc
diff 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 |
line wrap: on
line diff
--- a/OrthancServer/LuaScripting.cpp Tue Jun 30 17:19:26 2015 +0200 +++ b/OrthancServer/LuaScripting.cpp Tue Jun 30 18:41:33 2015 +0200 @@ -36,6 +36,7 @@ #include "ServerContext.h" #include "OrthancInitialization.h" #include "../Core/Lua/LuaFunctionCall.h" +#include "../Core/HttpServer/StringHttpOutput.h" #include "Scheduler/DeleteInstanceCommand.h" #include "Scheduler/StoreScuCommand.h" @@ -50,6 +51,49 @@ namespace Orthanc { + OrthancRestApi* LuaScripting::GetRestApi(lua_State *state) + { + const void* value = LuaContext::GetGlobalVariable(state, "_RestApi"); + return const_cast<OrthancRestApi*>(reinterpret_cast<const OrthancRestApi*>(value)); + } + + + int LuaScripting::RestApiGet(lua_State *state) + { + OrthancRestApi* restApi = GetRestApi(state); + if (restApi == NULL) + { + LOG(ERROR) << "Lua: The REST API is unavailable"; + lua_pushnil(state); + return 1; + } + + // Check the types of the arguments + int nArgs = lua_gettop(state); + if (nArgs != 1 || !lua_isstring(state, 1)) // URI + { + LOG(ERROR) << "Lua: Bad parameters to RestApiGet()"; + lua_pushnil(state); + return 1; + } + + const char* uri = lua_tostring(state, 1); + + std::string str; + if (restApi->SimpleGet(str, uri)) + { + lua_pushstring(state, str.c_str()); + } + else + { + LOG(ERROR) << "Lua: Error in RestApiGet() for URI " << uri; + lua_pushnil(state); + } + + return 1; + } + + IServerCommand* LuaScripting::ParseOperation(const std::string& operation, const Json::Value& parameters) { @@ -200,13 +244,27 @@ } - LuaScripting::LuaScripting(ServerContext& context) : context_(context) + LuaScripting::LuaScripting(ServerContext& context) : context_(context), restApi_(NULL) { + lua_.RegisterFunction("RestApiGet", RestApiGet); + lua_.Execute(Orthanc::EmbeddedResources::LUA_TOOLBOX); lua_.SetHttpProxy(Configuration::GetGlobalStringParameter("HttpProxy", "")); } + void LuaScripting::SetOrthancRestApi(OrthancRestApi& restApi) + { + lua_.SetGlobalVariable("_RestApi", &restApi); + } + + + void LuaScripting::ResetOrthancRestApi() + { + lua_.SetGlobalVariable("_RestApi", NULL); + } + + void LuaScripting::ApplyOnStoredInstance(const std::string& instanceId, const Json::Value& simplifiedTags, const Json::Value& metadata,