|
| 1 | +-- Path of Building |
| 2 | +-- |
| 3 | +-- Class: API Contract Builds |
| 4 | +-- API contract proposal for builds |
| 5 | +-- |
| 6 | + |
| 7 | +local dkjson = require "dkjson" |
| 8 | + |
| 9 | + |
| 10 | +---@enum EndpointType |
| 11 | +local EndpointType = { |
| 12 | + REST = 0, |
| 13 | + CSV = 1 |
| 14 | +} |
| 15 | + |
| 16 | +--example for REST vs CSV |
| 17 | +-- REST: |
| 18 | +-- {"baseUrl": "https://mypoebuilds.gg/pob-api/", "name": "MyPoEBuilds REST", "endpointType": 0, "fallbackVersion": 1} |
| 19 | +-- baseUrl meaning the actual baseUrl |
| 20 | +-- CSV: |
| 21 | +-- {"baseUrl": "https://mypoebuilds.gg/pob-builds.csv", "name": "MyPoEBuilds CSV", "endpointType": 1, "fallbackVersion": 1} |
| 22 | +-- baseUrl means here the full URL to the actual file or endpoint that returns a CSV formatted file from a GET endpoint |
| 23 | + |
| 24 | +--- This is the required information from the enduser |
| 25 | +---@class APISourceInfo |
| 26 | +---@field name string |
| 27 | +---@field baseUrl string |
| 28 | +---@field endpointType EndpointType |
| 29 | +---@field fallbackVersion integer |
| 30 | + |
| 31 | +--- This data should be returned by the source |
| 32 | +---@class BuildInfo |
| 33 | +---@field pobdata string |
| 34 | +---@field name string |
| 35 | +---@field lastModified integer |
| 36 | +---@field buildId string |
| 37 | + |
| 38 | +--- This data is saved internally for caching |
| 39 | +---@class BuildInfoCache |
| 40 | +---@field pobdata string |
| 41 | +---@field name string |
| 42 | +---@field lastModified integer |
| 43 | +---@field buildId string |
| 44 | +---@field sourceName string |
| 45 | + |
| 46 | +---This primarily exists for the lua language server |
| 47 | +---@param buildInfo BuildInfo |
| 48 | +---@param source APISourceInfo |
| 49 | +---@return BuildInfoCache |
| 50 | +local function buildInfoToCache(buildInfo, source) |
| 51 | + return { |
| 52 | + pobdata = buildInfo.pobdata, |
| 53 | + name = buildInfo.name, |
| 54 | + lastModified = buildInfo.lastModified, |
| 55 | + buildId = buildInfo.buildId, |
| 56 | + sourceName = source.name |
| 57 | + } |
| 58 | +end |
| 59 | + |
| 60 | +---@class APIContractBuilds |
| 61 | +---@field buildList BuildInfoCache[] |
| 62 | +local APIContractBuilds = newClass("APIContractBuilds", |
| 63 | + function(self) |
| 64 | + self.buildList = {} |
| 65 | + end |
| 66 | +) |
| 67 | + |
| 68 | +-- Switch case for GET Endpoint for Builds |
| 69 | +local getBuildVersions = { |
| 70 | + [0] = function(...) return APIContractBuilds:GetBuildsVersion1(...) end, |
| 71 | +} |
| 72 | + |
| 73 | +---comments |
| 74 | +---@param path string |
| 75 | +---@param source APISourceInfo |
| 76 | +function APIContractBuilds:GetBuilds(path, source) |
| 77 | + local builds = getBuildVersions[source.fallbackVersion](path, source.endpointType) |
| 78 | +end |
| 79 | + |
| 80 | +---@param source APISourceInfo |
| 81 | +function APIContractBuilds:GetAPICapabilities(source) |
| 82 | + -- TODO: Which features are supported? |
| 83 | + -- What is the latest version that is supported? |
| 84 | + -- Which endpoints are supported |
| 85 | + -- Is Querying supported? (CSV won't support this probably) |
| 86 | +end |
| 87 | + |
| 88 | +---@param response table | BuildInfo[] |
| 89 | +---@param errMsg any |
| 90 | +---@param source APISourceInfo |
| 91 | +function APIContractBuilds:BuildsVersion1Callback(source, response, errMsg) |
| 92 | + for _, build in ipairs(response) do |
| 93 | + -- source and buildId will be used as a caching key |
| 94 | + -- to avoid buildId collissions |
| 95 | + self.buildList[common.sha1(source.name + "-" + build.buildId)] = buildInfoToCache(build, source) |
| 96 | + end |
| 97 | +end |
| 98 | + |
| 99 | +---Version 1 of the API Contract for Builds |
| 100 | +---@param path string |
| 101 | +---@param source APISourceInfo |
| 102 | +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, {}) |
| 107 | + end |
| 108 | +end |
0 commit comments