comparison Core/Lua/LuaContext.cpp @ 1052:cc4ff680e2a0

http requests in lua
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 23 Jul 2014 15:08:09 +0200
parents 92f4bf2c5d73
children 6f923d52a46c
comparison
equal deleted inserted replaced
1051:92f4bf2c5d73 1052:cc4ff680e2a0
91 that.log_.append("\n"); 91 that.log_.append("\n");
92 92
93 return 0; 93 return 0;
94 } 94 }
95 95
96 96
97 int LuaContext::CallHttpGet(lua_State *state) 97 bool LuaContext::DoHttpQuery(lua_State* state,
98 { 98 bool isJson)
99 LuaContext& that = GetLuaContext(state); 99 {
100
101 // Check that there is 1 string argument
102 int nArgs = lua_gettop(state);
103 if ((nArgs != 1 && nArgs != 2) ||
104 !lua_isstring(state, 1) ||
105 (nArgs == 2 && !lua_isboolean(state, 2)))
106 {
107 LOG(ERROR) << "Lua: Bad URL in HttpGet";
108
109 lua_pushstring(state, "ERROR");
110 return 1;
111 }
112
113 // Configure the HTTP client class
114 const char* url = lua_tostring(state, 1);
115 bool isJson = (nArgs == 2 && lua_toboolean(state, 2));
116 that.httpClient_.SetMethod(HttpMethod_Get);
117 that.httpClient_.SetUrl(url);
118
119 // Do the HTTP GET request
120 std::string str; 100 std::string str;
121 Json::Value json; 101 Json::Value json;
122 102
123 try 103 try
124 { 104 {
125 if (isJson) 105 if (isJson)
126 { 106 {
127 that.httpClient_.Apply(json); 107 httpClient_.Apply(json);
128 } 108 }
129 else 109 else
130 { 110 {
131 that.httpClient_.Apply(str); 111 httpClient_.Apply(str);
132 } 112 }
133 } 113 }
134 catch (OrthancException& e) 114 catch (OrthancException& e)
135 { 115 {
136 LOG(ERROR) << "Lua: Error in HttpGet for URL " << url << ": " << e.What(); 116 return false;
137 117 }
118
119 // Return the result of the HTTP request
120 if (isJson)
121 {
122 PushJson(json);
123 }
124 else
125 {
126 lua_pushstring(state, str.c_str());
127 }
128
129 return true;
130 }
131
132
133 int LuaContext::CallHttpGet(lua_State *state)
134 {
135 LuaContext& that = GetLuaContext(state);
136
137 // Check the types of the arguments
138 int nArgs = lua_gettop(state);
139 if ((nArgs != 1 && nArgs != 2) ||
140 !lua_isstring(state, 1) || // URL
141 (nArgs >= 2 && !lua_isboolean(state, 2))) // Interpret result as JSON
142 {
143 LOG(ERROR) << "Lua: Bad parameters to HttpGet()";
138 lua_pushstring(state, "ERROR"); 144 lua_pushstring(state, "ERROR");
139 return 1; 145 return 1;
140 } 146 }
141 147
142 // Return the result of the HTTP GET 148 // Configure the HTTP client class
143 if (isJson) 149 const char* url = lua_tostring(state, 1);
144 { 150 bool isJson = (nArgs >= 2 && lua_toboolean(state, 2));
145 that.PushJson(json); 151 that.httpClient_.SetMethod(HttpMethod_Get);
152 that.httpClient_.SetUrl(url);
153
154 // Do the HTTP GET request
155 if (!that.DoHttpQuery(state, isJson))
156 {
157 LOG(ERROR) << "Lua: Error in HttpGet() for URL " << url;
158 lua_pushstring(state, "ERROR");
159 }
160
161 return 1;
162 }
163
164
165 int LuaContext::CallHttpPostOrPut(lua_State *state,
166 HttpMethod method)
167 {
168 LuaContext& that = GetLuaContext(state);
169
170 // Check the types of the arguments
171 int nArgs = lua_gettop(state);
172 if ((nArgs != 1 && nArgs != 2 && nArgs != 3) ||
173 !lua_isstring(state, 1) || // URL
174 (nArgs >= 2 && !lua_isstring(state, 2)) || // Body data
175 (nArgs >= 3 && !lua_isboolean(state, 3))) // Interpret result as JSON
176 {
177 LOG(ERROR) << "Lua: Bad parameters to HttpPost() or HttpPut()";
178 lua_pushstring(state, "ERROR");
179 return 1;
180 }
181
182 // Configure the HTTP client class
183 const char* url = lua_tostring(state, 1);
184 bool isJson = (nArgs >= 3 && lua_toboolean(state, 3));
185 that.httpClient_.SetMethod(method);
186 that.httpClient_.SetUrl(url);
187
188 if (nArgs >= 2)
189 {
190 that.httpClient_.SetPostData(lua_tostring(state, 2));
146 } 191 }
147 else 192 else
148 { 193 {
149 lua_pushstring(state, str.c_str()); 194 that.httpClient_.AccessPostData().clear();
195 }
196
197 // Do the HTTP POST/PUT request
198 if (!that.DoHttpQuery(state, isJson))
199 {
200 LOG(ERROR) << "Lua: Error in HttpPost() or HttpPut() for URL " << url;
201 lua_pushstring(state, "ERROR");
202 }
203
204 return 1;
205 }
206
207
208 int LuaContext::CallHttpPost(lua_State *state)
209 {
210 return CallHttpPostOrPut(state, HttpMethod_Post);
211 }
212
213
214 int LuaContext::CallHttpPut(lua_State *state)
215 {
216 return CallHttpPostOrPut(state, HttpMethod_Put);
217 }
218
219
220 int LuaContext::CallHttpDelete(lua_State *state)
221 {
222 LuaContext& that = GetLuaContext(state);
223
224 // Check the types of the arguments
225 int nArgs = lua_gettop(state);
226 if (nArgs != 1 || !lua_isstring(state, 1)) // URL
227 {
228 LOG(ERROR) << "Lua: Bad parameters to HttpDelete()";
229 lua_pushstring(state, "ERROR");
230 return 1;
231 }
232
233 // Configure the HTTP client class
234 const char* url = lua_tostring(state, 1);
235 that.httpClient_.SetMethod(HttpMethod_Delete);
236 that.httpClient_.SetUrl(url);
237
238 // Do the HTTP DELETE request
239 std::string s;
240 if (!that.httpClient_.Apply(s))
241 {
242 LOG(ERROR) << "Lua: Error in HttpPost() for URL " << url;
243 lua_pushstring(state, "ERROR");
244 }
245 else
246 {
247 lua_pushstring(state, "SUCCESS");
150 } 248 }
151 249
152 return 1; 250 return 1;
153 } 251 }
154 252
231 } 329 }
232 330
233 luaL_openlibs(lua_); 331 luaL_openlibs(lua_);
234 lua_register(lua_, "print", PrintToLog); 332 lua_register(lua_, "print", PrintToLog);
235 lua_register(lua_, "HttpGet", CallHttpGet); 333 lua_register(lua_, "HttpGet", CallHttpGet);
334 lua_register(lua_, "HttpPost", CallHttpPost);
335 lua_register(lua_, "HttpPut", CallHttpPut);
336 lua_register(lua_, "HttpDelete", CallHttpDelete);
236 337
237 lua_pushlightuserdata(lua_, this); 338 lua_pushlightuserdata(lua_, this);
238 lua_setglobal(lua_, "_LuaContext"); 339 lua_setglobal(lua_, "_LuaContext");
239 } 340 }
240 341