Skip to content

Commit c531931

Browse files
committed
feat: filter params to query support
1 parent 53e2a09 commit c531931

File tree

1 file changed

+56
-5
lines changed

1 file changed

+56
-5
lines changed

src/Classes/APIContractBuilds.lua

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,16 @@ local EndpointType = {
4949
---@field name string
5050
---@field fallbackVersion integer
5151
---@field endpointType EndpointType
52+
---@field supportedVersion? integer
5253
---@field baseAPIPath? string
5354
---@field league_filter? boolean
5455
---@field gem_filter? boolean
5556

57+
--- Build version 1 Filter option
58+
---@class BuildVersion1Filter
59+
---@field league string
60+
---@field gem string
61+
5662
--- This primarily exists for the lua language server
5763
---@param buildInfo BuildInfo
5864
---@param source APICapabilities
@@ -67,6 +73,25 @@ local function buildInfoToCache(buildInfo, source)
6773
}
6874
end
6975

76+
---@param t table
77+
local function tableToQueryParams(t)
78+
if #t == 0 then
79+
return ""
80+
end
81+
local query = "?"
82+
for key, value in pairs(t) do
83+
if value then
84+
query = query .. key .. "=" .. value .. "&"
85+
end
86+
end
87+
-- remove trailing &
88+
if #t > 0 then
89+
query = query:sub(1, #query - 1)
90+
end
91+
92+
return query
93+
end
94+
7095
---@class APIContractBuilds
7196
---@field buildList table<string,BuildInfoCache>
7297
---@field apiCapabilities table<string, APICapabilities>
@@ -81,11 +106,36 @@ local APIContractBuilds = newClass("APIContractBuilds",
81106
local getBuildVersions = {
82107
[0] = function(...) return APIContractBuilds:GetBuildsVersion1(...) end,
83108
}
109+
-- Switch case for GET Endpoint for Builds
110+
local getBuildFilterVersions = {
111+
[0] = function(data) return APIContractBuilds:GetBuildVersion1Filter(data) end,
112+
}
113+
114+
---@param data table
115+
function APIContractBuilds:GetBuildVersion1Filter(data)
116+
return {
117+
league = data.league,
118+
gem = data.gem
119+
}
120+
end
84121

85122
--- Gets the builds from the source
86-
---@param source APISourceInfo
87-
function APIContractBuilds:GetBuilds(source)
88-
getBuildVersions[source.fallbackVersion](source.endpointType)
123+
---@param source APICapabilities
124+
---@param data table
125+
function APIContractBuilds:GetBuilds(source, data)
126+
local getBuildsFunction = nil
127+
local getBuildsFilterFunction = nil
128+
if not source.supportedVersion then
129+
getBuildsFunction = getBuildVersions[source.fallbackVersion]
130+
getBuildsFilterFunction = getBuildFilterVersions[source.fallbackVersion]
131+
else
132+
getBuildsFunction = getBuildVersions[source.supportedVersion]
133+
getBuildsFilterFunction = getBuildFilterVersions[source.supportedVersion]
134+
end
135+
136+
if getBuildsFunction and getBuildsFilterFunction then
137+
getBuildsFunction(source.endpointType, getBuildsFilterFunction(data), source)
138+
end
89139
end
90140

91141
---@param source APISourceInfo
@@ -136,13 +186,14 @@ end
136186

137187
--- Version 1 of the API Contract for Builds
138188
---@param source APICapabilities
139-
function APIContractBuilds:GetBuildsVersion1(source)
189+
---@param params BuildVersion1Filter
190+
function APIContractBuilds:GetBuildsVersion1(source, params)
140191
if source.endpointType == EndpointType.CSV then
141192
-- TODO: Implement CSV Parsing and Format
142193
return
143194
end
144195

145-
launch:DownloadPage(source.baseAPIPath + "/v1/builds", function(...)
196+
launch:DownloadPage(source.baseAPIPath .. "/v1/builds" .. tableToQueryParams(params), function(...)
146197
self:BuildsVersion1Callback(source, ...)
147198
end, {})
148199
end

0 commit comments

Comments
 (0)