-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHttp.ttslua
183 lines (156 loc) · 6.36 KB
/
Http.ttslua
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
require('ge_tts.License')
local Json = require('ge_tts.Json')
local TableUtils = require('ge_tts.TableUtils')
---@shape ge_tts__Http_Response<B>
---@field statusCode number
---@field headers table<string, string>
---@field body B
---@alias ge_tts__Http_Callback<B> fun(response: nil | ge_tts__Http_Response<B>, error: nil | string): void
---@type nil | string
local decodeJsonContentType = 'application/json'
---@class ge_tts__Http
local Http = {}
---@param type nil | string
function Http.setDecodeJsonContentType(type)
decodeJsonContentType = type
end
---@return nil | string
function Http.getDecodeJsonContentType()
return decodeJsonContentType
end
---@param headers table<string, string>
---@param name string
---@return nil | string, nil | string @value, headerName - `headerName` being the case-sensitive variant of `name` found within headers
function Http.getHeader(headers, name)
name = name:lower()
return TableUtils.detect(headers, function(_, key)
return key:lower() == name
end)
end
---@generic B
---@param method string
---@param url string
---@param body nil | table | string @If provided as a table, it will be JSON encoded. Otherwise, the body must be provided as a string. Tabletop Simulator can only handle UTF-8 text. If a HTTP endpoint you wish to call requires application/octet-stream, then you can Base64 encode the data and send it via your own HTTP proxy.
---@param headersOrNil nil | table<string, string>
---@param callback ge_tts__Http_Callback<B>
---@return fun(): void
function Http.submit(method, url, body, headersOrNil, callback)
---@type table<string, string>
local headers = headersOrNil and TableUtils.copy(headersOrNil) or {}
local canceled = false
---@type nil | fun(): void
local cancelEncoding
---@type nil | fun(): void
local cancelDecoding
---@param body string
local function performRequest(body)
WebRequest.custom(url, method, true, --[[---@type nil | string]] body, headers, function(request)
if canceled then
return
end
if request.is_error then
callback(nil, request.error)
elseif request.is_done then
local statusCode = request.response_code
local responseHeaders = request.getResponseHeaders()
local contentType = Http.getHeader(responseHeaders, 'Content-Type')
if contentType and (--[[---@not nil]] contentType):lower() == decodeJsonContentType then
cancelDecoding = Json.decodeAsync(request.text, {
---@param value B
onCompletion = function(value)
callback({
body = value,
headers = responseHeaders,
statusCode = statusCode,
}, nil)
end,
onError = function(message)
callback(nil, "Failed to parse JSON response body: " .. message)
end,
})
else
callback({
body = --[[---@type B]] request.text,
headers = responseHeaders,
statusCode = statusCode,
outbound = body,
}, nil)
end
end
end)
end
if type(body) == 'table' then
local contentType, contentTypeHeader = Http.getHeader(headers, 'Content-Type')
contentTypeHeader = contentTypeHeader or 'Content-Type'
if not (contentType and (--[[---@not nil]] contentType):sub(-4) == 'json') then
headers = TableUtils.copy(headers)
headers[--[[---@not nil]] contentTypeHeader] = 'application/json'
end
cancelEncoding = Json.encodeAsync(body, {
onCompletion = function(json)
cancelEncoding = nil
performRequest(json)
end,
onError = function(error)
callback(nil, error)
end,
})
else
performRequest(--[[---@type string]] body)
end
return function()
if not canceled then
canceled = true
if cancelEncoding then
(--[[---@not nil]] cancelEncoding)()
end
if cancelDecoding then
(--[[---@not nil]] cancelDecoding)()
end
end
end
end
---@generic B
---@param url string
---@param headers nil | table<string, string>
---@param callback ge_tts__Http_Callback<B>
---@return fun(): void
function Http.delete(url, headers, callback)
return Http.submit('DELETE', url, nil, headers, callback)
end
---@generic B
---@param url string
---@param headers nil | table<string, string>
---@param callback ge_tts__Http_Callback<B>
---@return fun(): void
function Http.get(url, headers, callback)
return Http.submit('GET', url, nil, headers, callback)
end
---@generic B
---@param url string
---@param body nil | table | string @If provided as a table, it will be JSON encoded. If provided as a number array, numbers are assumed to be [0, 255] and Base64 encoded. Otherwise, the body is a string.
---@param headers nil | table<string, string>
---@param callback ge_tts__Http_Callback<B>
---@return fun(): void
function Http.patch(url, body, headers, callback)
return Http.submit('PATCH', url, body, headers, callback)
end
---@generic B
---@param url string
---@param body nil | table | string @If provided as a table, it will be JSON encoded. If provided as a number array, numbers are assumed to be [0, 255] and Base64 encoded. Otherwise, the body is a string.
---@param headers nil | table<string, string>
---@param callback ge_tts__Http_Callback<B>
---@return fun(): void
function Http.post(url, body, headers, callback)
return Http.submit('POST', url, body, headers, callback)
end
---@generic B
---@param url string
---@param body nil | table | string @If provided as a table, it will be JSON encoded. If provided as a number array, numbers are assumed to be [0, 255] and Base64 encoded. Otherwise, the body is a string.
---@param headers nil | table<string, string>
---@param callback ge_tts__Http_Callback<B>
---@return fun(): void
function Http.put(url, body, headers, callback)
return Http.submit('PUT', url, body, headers, callback)
end
return Http