Skip to content

Commit c29ddaf

Browse files
committed
feat: api capabilities and matching APIContract.md
1 parent e30825b commit c29ddaf

File tree

1 file changed

+48
-10
lines changed

1 file changed

+48
-10
lines changed

src/Classes/APIContractBuilds.lua

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ local EndpointType = {
2121
-- {"baseUrl": "https://mypoebuilds.gg/pob-builds.csv", "name": "MyPoEBuilds CSV", "endpointType": 1, "fallbackVersion": 1}
2222
-- baseUrl means here the full URL to the actual file or endpoint that returns a CSV formatted file from a GET endpoint
2323

24-
--- This is the required information from the enduser
24+
--- APISourceInfo is the configurated data for a source from the enduser
2525
---@class APISourceInfo
2626
---@field name string
2727
---@field baseUrl string
@@ -43,9 +43,18 @@ local EndpointType = {
4343
---@field buildId string
4444
---@field sourceName string
4545

46+
--- API Capabilities returned by the source or defaulted to APISourceInfo
47+
---@class APICapabilities
48+
---@field name string
49+
---@field fallbackVersion integer
50+
---@field endpointType EndpointType
51+
---@field baseAPIPath string
52+
---@field league_filter boolean
53+
---@field gem_filter boolean
54+
4655
---This primarily exists for the lua language server
4756
---@param buildInfo BuildInfo
48-
---@param source APISourceInfo
57+
---@param source APICapabilities
4958
---@return BuildInfoCache
5059
local function buildInfoToCache(buildInfo, source)
5160
return {
@@ -58,10 +67,12 @@ local function buildInfoToCache(buildInfo, source)
5867
end
5968

6069
---@class APIContractBuilds
61-
---@field buildList BuildInfoCache[]
70+
---@field buildList table<string,BuildInfoCache>
71+
---@field apiCapabilities table<string, APICapabilities>
6272
local APIContractBuilds = newClass("APIContractBuilds",
6373
function(self)
6474
self.buildList = {}
75+
self.apiCapabilities = {}
6576
end
6677
)
6778

@@ -83,13 +94,38 @@ function APIContractBuilds:GetAPICapabilities(source)
8394
-- What is the latest version that is supported?
8495
-- Which endpoints are supported
8596
-- Is Querying supported? (CSV won't support this probably)
97+
98+
if source.endpointType ~= EndpointType.REST then
99+
return
100+
end
101+
102+
launch:DownloadPage(source.baseUrl + "/.well-known/pathofbuilding",
103+
function(...) return APIContractBuilds:APICapabilitiesCallback(source, ...) end, {})
86104
end
87105

88-
---@param response table | BuildInfo[]
106+
---@param response table
89107
---@param errMsg any
90108
---@param source APISourceInfo
109+
function APIContractBuilds:APICapabilitiesCallback(source, response, errMsg)
110+
local parsedResponse = dkjson.decode(response.body)
111+
---@cast parsedResponse APICapabilities
112+
if errMsg or not parsedResponse.baseAPIPath then
113+
parsedResponse.baseAPIPath = source.baseUrl
114+
end
115+
parsedResponse.name = source.name
116+
parsedResponse.fallbackVersion = source.fallbackVersion
117+
parsedResponse.endpointType = source.endpointType
118+
119+
self.apiCapabilities[source.name] = parsedResponse
120+
end
121+
122+
---@param response table
123+
---@param errMsg any
124+
---@param source APICapabilities
91125
function APIContractBuilds:BuildsVersion1Callback(source, response, errMsg)
92-
for _, build in ipairs(response) do
126+
local parsedResponse = dkjson.decode(response.body)
127+
---@cast parsedResponse BuildInfo[]
128+
for _, build in ipairs(parsedResponse) do
93129
-- source and buildId will be used as a caching key
94130
-- to avoid buildId collissions
95131
self.buildList[common.sha1(source.name + "-" + build.buildId)] = buildInfoToCache(build, source)
@@ -98,11 +134,13 @@ end
98134

99135
---Version 1 of the API Contract for Builds
100136
---@param path string
101-
---@param source APISourceInfo
137+
---@param source APICapabilities
102138
function APIContractBuilds:GetBuildsVersion1(path, source)
103-
if source.endpointType == EndpointType.REST then
104-
launch:DownloadPage(source.baseUrl + path, function(...)
105-
self:BuildsVersion1Callback(source, ...)
106-
end, {})
139+
if source.endpointType ~= EndpointType.REST then
140+
return
107141
end
142+
143+
launch:DownloadPage(source.baseAPIPath + path, function(...)
144+
self:BuildsVersion1Callback(source, ...)
145+
end, {})
108146
end

0 commit comments

Comments
 (0)