-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
request.lua
59 lines (49 loc) · 1.31 KB
/
request.lua
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
local http = require("socket.http")
local socketutil = require("socketutil")
local socket = require("socket")
local ltn12 = require("ltn12")
local logger = require("logger")
local ResponseFactory = require("libs/http/responsefactory")
local DEFAULT_TIMEOUT = 30
local DEFAULT_MAXTIME = 30
local DEFAULT_REDIRECTS = 5
local Request = {
url = nil,
method = nil,
maxtime = DEFAULT_MAXTIME,
timeout = DEFAULT_TIMEOUT,
redirects = DEFAULT_REDIRECTS,
sink = {},
}
Request.method = {
get = "GET",
post = "POST",
}
Request.scheme = {
http = "HTTP",
https = "HTTPS"
}
Request.default = {
timeout = DEFAULT_TIMEOUT,
maxtime = DEFAULT_MAXTIME,
redirects = DEFAULT_REDIRECTS,
}
function Request:new(o)
o = o or {}
self.__index = self
setmetatable(o, self)
return o
end
function Request:send()
self.sink = {}
socketutil:set_timeout(self.timeout, self.maxtime)
local code, headers, status = socket.skip(1, http.request({
url = self.url,
method = self.method,
sink = self.maxtime and socketutil.table_sink(self.sink) or ltn12.sink.table(self.sink)
}))
local content = table.concat(self.sink)
socketutil:reset_timeout()
return ResponseFactory:make(code, headers, status, content)
end
return Request