-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhttp_client.lua
More file actions
executable file
·41 lines (35 loc) · 1.14 KB
/
http_client.lua
File metadata and controls
executable file
·41 lines (35 loc) · 1.14 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
local httpClient = {
timeout = 5000
}
local httpRequest
if pcall( require, "chttp" ) and CHTTP ~= nil then
httpRequest = CHTTP
else
httpRequest = HTTP
end
function httpClient:Request( requestMethod, endpoint, data )
return AzLink.Promise( function( onResolve, onReject )
httpRequest( {
method = requestMethod,
url = AzLink.config.url .. "/api/azlink" .. endpoint,
timeout = self.timeout,
headers = {
["Azuriom-Link-Token"] = AzLink.config.site_key,
["Accept"] = "application/json",
["Content-Type"] = "application/json",
},
type = "application/json",
body = data and util.TableToJSON( data ) or nil,
success = function( code, body )
local jsonBody = body and util.JSONToTable( body ) or body
if code >= 300 then
onReject( jsonBody.message or body, code )
return
end
onResolve( jsonBody )
end,
failed = onReject,
} )
end )
end
AzLink.HttpClient = httpClient