hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/internal/fabric
diff options
context:
space:
mode:
authorhachem <im@hachem.wtf>2026-08-29 11:26:09 +0200
committerhachem <im@hachem.wtf>2026-08-29 11:26:09 +0200
commit4806ee7ef95f0f04079856f7753399b7b7aa6330 (patch)
tree5766e1f8cd714ae76b2f81da4ab3045e7193b256 /internal/fabric
parentd8b2d27b1f1ccccfe26fe0d45dbb624d453272d3 (diff)
feat: recommended option filters
Diffstat (limited to 'internal/fabric')
-rw-r--r--internal/fabric/fabric.go79
1 files changed, 79 insertions, 0 deletions
diff --git a/internal/fabric/fabric.go b/internal/fabric/fabric.go
new file mode 100644
index 0000000..3e52e6f
--- /dev/null
+++ b/internal/fabric/fabric.go
@@ -0,0 +1,79 @@
+package fabric
+
+import (
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "time"
+)
+
+const (
+ apiBase = "https://meta.fabricmc.net/v2"
+ userAgent = "benson/2.0 (github.com/hachem-wtf/benson)"
+)
+
+type GameVersion struct {
+ Version string `json:"version"`
+ Stable bool `json:"stable"`
+}
+
+type LoaderVersion struct {
+ Version string `json:"version"`
+ Stable bool `json:"stable"`
+}
+
+type InstallerVersion struct {
+ Version string `json:"version"`
+ Stable bool `json:"stable"`
+}
+
+type Client struct {
+ http *http.Client
+}
+
+func NewClient() *Client {
+ return &Client{http: &http.Client{Timeout: 30 * time.Second}}
+}
+
+func (c *Client) get(path string, out any) error {
+ req, err := http.NewRequest(http.MethodGet, apiBase+path, nil)
+ if err != nil {
+ return err
+ }
+ req.Header.Set("User-Agent", userAgent)
+
+ resp, err := c.http.Do(req)
+ if err != nil {
+ return err
+ }
+ defer resp.Body.Close()
+
+ if resp.StatusCode != http.StatusOK {
+ return fmt.Errorf("fabric: %s returned %s", path, resp.Status)
+ }
+ return json.NewDecoder(resp.Body).Decode(out)
+}
+
+func (c *Client) GameVersions() ([]GameVersion, error) {
+ var versions []GameVersion
+ if err := c.get("/versions/game", &versions); err != nil {
+ return nil, err
+ }
+ return versions, nil
+}
+
+func (c *Client) LoaderVersions() ([]LoaderVersion, error) {
+ var versions []LoaderVersion
+ if err := c.get("/versions/loader", &versions); err != nil {
+ return nil, err
+ }
+ return versions, nil
+}
+
+func (c *Client) InstallerVersions() ([]InstallerVersion, error) {
+ var versions []InstallerVersion
+ if err := c.get("/versions/installer", &versions); err != nil {
+ return nil, err
+ }
+ return versions, nil
+}