-
Notifications
You must be signed in to change notification settings - Fork 1
/
gm_webapi_http.lua
219 lines (176 loc) · 6.05 KB
/
gm_webapi_http.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
--Localizations
local isstring = isstring
local isnumber = isnumber
local istable = istable
local isfunction = isfunction
local ErrorNoHalt = ErrorNoHalt
local CurTime = CurTime
local httpPost = http.Post
local utilJSONToTable = util.JSONToTable
local apiUrl = ""
local apiHeaders = {}
local methods = {}
--[[
Doccumentation: https://github.com/The-Lord-of-Owls/gm_webapi/tree/main#apiadd-id-route-cback-onerror-cachettl-customheaders-
]]
local function Add( id, route, cback, onError, cacheTTL, customHeaders )
--Check if an id was provided
if not id or not isstring( id ) then
return ErrorNoHalt( "API ERROR: You must provide an id when setting up a new API method!\n" )
end
--Check if a route was provided
if not route or not isstring( route ) then
return ErrorNoHalt( "API ERROR: You must specify a route when setting up a new API method!\n" )
end
--Check if a callback function was provided
if not cback or not isfunction( cback ) then
return ErrorNoHalt( "API ERROR: You must provide a callback function when setting up a new API method!\n" )
end
--Setup method object
local method = {
route = route,
cback = cback
}
--Specify optional custom headers for method
if customHeaders and istable( customHeaders ) then
method.headers = customHeaders
elseif customHeaders then
ErrorNoHalt( "API WARNING: Custom route headers must be in a keyvalue table format. The API method has been created but custom headers have not been set for it!\n" )
end
--Optional error response
if onError and isfunction( onError ) then
method.onError = onError
elseif onError then
ErrorNoHalt( "API WARNING: Custom onError must be a function, the API method has been created without custom error handling!\n" )
end
--Handle if we will cache the value
if cacheTTL and isnumber( cacheTTL ) then
method.cacheTTL = cacheTTL
method.nextCache = 0
method.waitReq = false
elseif cacheTTL then
ErrorNoHalt( "API WARNING: cacheTTL for API methods must be a number value, the API method has been created with caching disabled!\n" )
end
--Make the method object accessible
methods[ id ] = method
end
--[[
Doccumentation: https://github.com/The-Lord-of-Owls/gm_webapi/tree/main#apiremove-id-
]]
local function Remove( id )
if not id or not isstring( id ) then
return ErrorNoHalt( "API WARNING: Please provide a id string for the method you want to remove!\n" )
end
methods[ id ] = nil
end
--[[
Doccumentation: https://github.com/The-Lord-of-Owls/gm_webapi/tree/main#apicall-id-params-
]]
local function Call( id, params )
--Check if the API url has been set
if apiUrl == "" then
return ErrorNoHalt( "API ERROR: API URL has not been set, aborting API call! Please make sure to run api.SetURL before calling any methods!\n" )
end
--Check if the API method exists
if not methods[ id ] then
return ErrorNoHalt( "API ERROR: The API method '" .. id .. "' does not exist!\n" )
end
--The API method
local apiMethod = methods[ id ]
--Check if the API has a proper callback function
if not apiMethod.cback then
return ErrorNoHalt( "API ERROR: The API method '" .. id .. "' does not have a callback function!\n" )
end
local curTime = CurTime()
--Check if should return cached value
if apiMethod.waitReq or ( apiMethod.nextCache and apiMethod.nextCache >= curTime and apiMethod.cacheRes ~= "" ) then
apiMethod.cback( apiMethod.cacheRes )
return
end
apiMethod.waitReq = true
httpPost( apiUrl .. apiMethod.route, params or {}, function( res, size, headers, code )
local parsed = utilJSONToTable( res )
--Update the cache
if apiMethod.cacheTTL and curTime > apiMethod.nextCache then
apiMethod.nextCache = curTime + apiMethod.cacheTTL
apiMethod.cacheRes = parsed.resData
apiMethod.waitReq = false
end
apiMethod.cback( parsed.resData )
end, apiMethod.onError or ErrorNoHalt, apiMethod.headers or apiHeaders )
end
--[[
Doccumentation: https://github.com/The-Lord-of-Owls/gm_webapi/tree/main#apigettable
]]
local function GetTable()
return methods or {}
end
--[[
Doccumentation: https://github.com/The-Lord-of-Owls/gm_webapi/tree/main#apiseturl-url-
]]
local function SetUrl( url )
if not url and isstring( url ) then
return ErrorNoHalt( "API ERROR: Please provide a valid url string for the API!\n" )
end
apiUrl = url
end
--[[
Doccumentation: https://github.com/The-Lord-of-Owls/gm_webapi/tree/main#apiaddheader-header-value-
]]
local function AddHeader( header, value )
if not header or not isstring( header ) then
return ErrorNoHalt( "API ERROR: Please provide a proper header string!\n" )
end
if not value or not isstring( value ) then
return ErrorNoHalt( "API ERROR: Please provide a proper header string value!\n" )
end
apiHeaders[ header ] = value
end
--[[
Doccumentation: https://github.com/The-Lord-of-Owls/gm_webapi/tree/main#apisetheader-header-value-
]]
local function SetHeader( header, value )
if not header or not isstring( header ) then
return ErrorNoHalt( "API ERROR: Please provide a proper header string!\n" )
end
if not value or not isstring( value ) then
return ErrorNoHalt( "API ERROR: Please provide a proper header string value!\n" )
end
apiHeaders[ header ] = value
end
--[[
Doccumentation: https://github.com/The-Lord-of-Owls/gm_webapi/tree/main#apiremoveheader-header-
]]
local function RemoveHeader( header )
if not header or not isstring( header ) then
return ErrorNoHalt( "API ERROR: Please provide a proper header string!\n" )
end
apiHeaders[ header ] = nil
end
--[[
Setting up our API library table
Doccumentation: https://github.com/The-Lord-of-Owls/gm_webapi/blob/main/README.md#apicall-id-params-
]]
api = setmetatable( {
Add = Add,
Remove = Remove,
Call = Call,
GetTable = GetTable,
SetUrl = SetUrl,
AddHeader = AddHeader,
SetHeader = SetHeader,
RemoveHeader = RemoveHeader
}, {
__metatable = "WebAPI Handler",
__call = function( self, ... )
Call( ... )
end
} )
--[[Was used during testing]]
SetUrl( "http://localhost:5000/api" )
Add( "test", "/test", function( res )
PrintTable(res)
end, nil, 10 )
timer.Create( "testing-api", 1, 0, function()
Call( "test", { steamId = "00000000000000001" } )
end )