-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest.lua
114 lines (87 loc) · 3.38 KB
/
rest.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
--[[
Resource: MultiRPG
Type: Serverside
Developers: Inder00 <admin@multirpg.pl>
(©) 2023 <admin@multirpg.pl>. All rights reserved.
]]--
-- Content type parsers
local contentTypeParsers = {
-- JSON Content-Type parser
["application/json"] = function( response )
return fromJSON( response )
end,
}
-- HTTP Request function
local function httpRequest( url, endpoint, method )
-- return request wrapper
return {
-- data
baseUrl = url,
path = endpoint,
method = method,
headerList = {},
bodyData = nil,
-- add header
addHeader = function( self, name, value )
assert( type( name ) == "string", "Bad argument 1 @ addHeader [string expected, got " .. type( name ) .. "]" )
assert( type( value ) == "string", "Bad argument 2 @ addHeader [string expected, got " .. type( value ) .. "]" )
self.headerList[ name ] = value
return self
end,
-- set body data
setBody = function( self, data )
assert( type( data ) == "string" or type( data ) == "table", "Bad argument @ setBody [string/table expected, got " .. type( data ) .. "]" )
if type( data ) == "table" then
local jsonData = toJSON( data )
self.bodyData = utf8.sub( jsonData, math.min( utf8.len( jsonData ), 3 ), math.max( 1, utf8.len( jsonData ) - 2 ) )
else
self.bodyData = data
end
return self
end,
-- execute
execute = function( self, callbackFunction, ... )
-- send request
fetchRemote ( self.baseUrl .. self.path, {
queueName = string.format("RestClient[%s]", self.path),
postData = self.bodyData,
method = self.method,
headers = self.headerList
}, function( response, responseData, ... )
-- content type parser
local contentType = (type(responseData) == "table" and type(responseData.headers) == "table" and responseData.headers["Content-Type"]) and responseData.headers["Content-Type"] or "text/plain"
local responseParser = contentTypeParsers[ contentType ]
-- execute callback
callbackFunction( type(responseParser) == "function" and responseParser( response ) or response, responseData.statusCode, ... )
end, {...} )
end,
}
end
-- Create rest client wrapper
function createRestClient( url )
-- return rest client
return {
-- data
baseUrl = url,
-- HTTP GET Request
get = function( self, endpoint )
return httpRequest( self.baseUrl, endpoint, "GET" )
end,
-- HTTP POST Request
post = function( self, endpoint )
return httpRequest( self.baseUrl, endpoint, "POST" )
end,
-- HTTP PUT Request
put = function( self, endpoint )
return httpRequest( self.baseUrl, endpoint, "PUT" )
end,
-- HTTP DELETE Request
delete = function( self, endpoint )
return httpRequest( self.baseUrl, endpoint, "DELETE" )
end,
-- HTTP PATCH Request
patch = function( self, endpoint )
return httpRequest( self.baseUrl, endpoint, "PATCH" )
end,
}
end