Skip to content

Commit f5e8562

Browse files
committed
feat: initial work towards a PoB API contract
1 parent 18fcd61 commit f5e8562

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed

docs/APIContract.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Versions
2+
3+
So far there is only one version
4+
5+
The base URL for an API should be: _baseURL_/_apiVersion_/ meaning even something such as sometestbuildsforpoe.gg/something/a/b/c/ would be valid as a base URL, as long as it has afterwards the per version required endpoints.
6+
7+
## Metadata Endpoint
8+
9+
In order to provide a consistent API a metadata endpoint should be provided under _baseURL_/.well-known/pathofbuilding this endpoint should provide information about the API and the version of the API.
10+
Furthermore this may allow having custom endpoints for the API.
11+
12+
## Version 1
13+
14+
### Response and Request types
15+
16+
#### BuildInfo
17+
18+
| Field | Type | Description |
19+
| ------------ | ------- | ----------------------------------- |
20+
| pobdata | string | The Path of Building data |
21+
| name | string | The name of the build |
22+
| lastModified | integer | The last modification timestamp |
23+
| buildId | string | The unique identifier for the build |
24+
25+
### Endpoints
26+
27+
<details>
28+
<summary><code>GET</code> <code><b>/v1/builds</b></code> <code>(Lists multiple builds)</code></summary>
29+
30+
#### Parameters
31+
32+
| name | type | data type | description |
33+
| ---- | -------- | --------- | ------------------- |
34+
| page | optional | integer | Used for pagination |
35+
36+
#### Responses
37+
38+
| http code | content-type | response |
39+
| --------- | ------------------ | ------------------ |
40+
| `200` | `application/json` | `BuildInfo object` |
41+
42+
</details>
43+

src/Classes/APIContractBuilds.lua

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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

Comments
 (0)