package client import ( "bytes" "encoding/json" "errors" "fmt" "io" "net/http" "net/url" "time" "git.wzray.com/homelab/hivemind/internal/types" "git.wzray.com/homelab/hivemind/internal/web/middleware" ) type client struct { http *http.Client middleware middleware.Middleware } var defaultClient *client const timeout = time.Duration(2) * time.Second func (c *client) makeRequest(method string, host string, path types.Path, data any, out any) error { var body io.Reader if data != nil { raw, err := json.Marshal(data) if err != nil { return fmt.Errorf("marshal body: %w", err) } body = bytes.NewReader(raw) } uri := (&url.URL{ Scheme: "http", Host: host, Path: path.String(), }).String() r, err := http.NewRequest(method, uri, body) if err != nil { return fmt.Errorf("build http request: %w", err) } if body != nil { r.Header.Set("Content-Type", "application/json; charset=utf-8") } if err := c.middleware.Client(r); err != nil { return fmt.Errorf("apply middleware: %w", err) } resp, err := c.http.Do(r) if err != nil { return fmt.Errorf("send request: %w", err) } if resp.StatusCode < 200 || resp.StatusCode >= 300 { b, _ := io.ReadAll(resp.Body) return fmt.Errorf("http %d: %s", resp.StatusCode, string(b)) } defer resp.Body.Close() if out != nil { if err := json.NewDecoder(resp.Body).Decode(out); err != nil { return fmt.Errorf("decode body: %w", err) } } io.Copy(io.Discard, resp.Body) return nil } func Init(mw middleware.Middleware) { if defaultClient != nil { panic("web.client: Init called twice") } defaultClient = &client{ http: &http.Client{ Timeout: timeout, }, middleware: mw, } } func request[Out any, In any](method string, host string, path types.Path, data In) (*Out, error) { out := &types.Response[Out]{} err := defaultClient.makeRequest(method, host, path, data, out) if err != nil { return nil, err } if !out.Ok { return nil, errors.New(out.Err) } return &out.Data, err } // TODO: out should not be a pointer func Get[Out any](host string, path types.Path) (*Out, error) { return request[Out, any](http.MethodGet, host, path, nil) } // TODO: out should not be a pointer func Post[Out any, In any](host string, path types.Path, data In) (*Out, error) { return request[Out](http.MethodPost, host, path, data) }