This repository has been archived by the owner on Dec 27, 2024. It is now read-only.
forked from The-Lord-of-Owls/gm_webapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgm_webapi.lua
201 lines (163 loc) · 5.42 KB
/
gm_webapi.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
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!" )
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!" )
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!" )
end
--Setup method object
local method = {
route = route,
method.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!" )
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!" )
end
--Handle if we will cache the value
if cacheTTL and isnumber( cacheTTL ) then
method.cacheRes = ""
method.cacheTTL = cacheTTL
method.lastCache = 0
elseif cacheTTL then
ErrorNoHalt( "API WARNING: cacheTTL for API methods must be a number value, the API method has been created with caching disabled!" )
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!" )
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!" )
end
--Check if the API method exists
if not methods[ id ] then
return ErrorNoHalt( "API ERROR: The API method '" .. id .. "' does not exist!" )
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!")
end
--Check if should return cached value
if apiMethod.cacheTTL and apiMethod.cacheTTL < CurTime() then
apiMethod.cback( method.cacheRes )
return
end
HTTP( {
failed = method.onError or ErrorNoHalt,
success = function( code, res, headers )
--Update the cache
if apiMethod.cacheTTL then
apiMethod.lastCache = CurTime() + apiMethod.cacheTTL
apiMethod.cacheRes = res
end
apiMethod.cback( res )
end,
method = "POST",
url = apiUrl .. apiMethod.route,
parameters = params or {},
headers = apiMethod.headers or apiHeaders,
timeoute = apiMethod.timeoute or 60
} )
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!" )
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!" )
end
if not value or not isstring( value ) then
return ErrorNoHalt( "API ERROR: Please provide a proper header string value!" )
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!" )
end
if not value or not isstring( value ) then
return ErrorNoHalt( "API ERROR: Please provide a proper header string value!" )
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!" )
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
} )