-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGhostClient.lua
More file actions
44 lines (37 loc) · 1.17 KB
/
GhostClient.lua
File metadata and controls
44 lines (37 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
local cjson = require("cjson")
local Object = require("classic")
local http = require("socket.http")
local ltn12 = require("ltn12")
local bitser = require("./bitser")
local log = require("./log")
local GhostClient = Object:extend()
function GhostClient:new(baseUrl, context)
self.baseUrl = baseUrl or "http://localhost:8080/api"
self.context = context or {}
end
function GhostClient:callMethod(method, ...)
local args = {...}
-- log(args)
local url = self.baseUrl
local data = {method = method, args = args, context = {}}
local bodyData = bitser.dumps(data)
-- local bodyData = cjson.encode(data)
local respbody = {} -- for the response body
local result, respcode, respheaders, respstatus = http.request(
{
url = url,
method = "POST",
headers = {
["Content-Type"] = "application/x-lua+bitser",
-- ["Content-Type"] = "application/json",
["Content-Length"] = tostring(#bodyData)
},
source = ltn12.source.string(bodyData),
sink = ltn12.sink.table(respbody)
}
)
-- return result, respcode, respheaders, respstatus, respbody
return bitser.loads(respbody[1])
end
g = GhostClient()
return GhostClient