1
0
Fork 0

refactor: move http api to a new transport layer

This commit is contained in:
Arthur K. 2026-01-23 09:56:01 +03:00
parent 476c4b056f
commit 0448f66ab2
Signed by: wzray
GPG key ID: B97F30FDC4636357
41 changed files with 822 additions and 390 deletions

View file

@ -10,20 +10,18 @@ import (
"net/url"
"time"
"git.wzray.com/homelab/hivemind/internal/types"
"git.wzray.com/homelab/hivemind/internal/web"
"git.wzray.com/homelab/hivemind/internal/web/middleware"
)
type client struct {
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 {
func (c *Client) Call(host string, path string, data any, out any) error {
var body io.Reader
if data != nil {
raw, err := json.Marshal(data)
@ -36,10 +34,10 @@ func (c *client) makeRequest(method string, host string, path types.Path, data a
uri := (&url.URL{
Scheme: "http",
Host: host,
Path: path.String(),
Path: path,
}).String()
r, err := http.NewRequest(method, uri, body)
r, err := http.NewRequest("POST", uri, body)
if err != nil {
return fmt.Errorf("build http request: %w", err)
}
@ -52,60 +50,41 @@ func (c *client) makeRequest(method string, host string, path types.Path, data a
return fmt.Errorf("apply middleware: %w", err)
}
resp, err := c.http.Do(r)
httpResponse, err := c.http.Do(r)
if err != nil {
return fmt.Errorf("send request: %w", err)
}
defer httpResponse.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
b, _ := io.ReadAll(resp.Body)
return fmt.Errorf("http %d: %s", resp.StatusCode, string(b))
if httpResponse.StatusCode < 200 || httpResponse.StatusCode >= 300 {
b, _ := io.ReadAll(httpResponse.Body)
return fmt.Errorf("http %d: %s", httpResponse.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)
var resp web.Response[json.RawMessage]
if err := json.NewDecoder(httpResponse.Body).Decode(&resp); err != nil {
return fmt.Errorf("decode response wrapper: %w", err)
}
if !resp.Ok {
return fmt.Errorf("error on the remote: %w", errors.New(resp.Err))
}
if err := json.Unmarshal(resp.Data, out); err != nil {
return fmt.Errorf("decode response body: %w", err)
}
}
io.Copy(io.Discard, resp.Body)
io.Copy(io.Discard, httpResponse.Body)
return nil
}
func Init(mw middleware.Middleware) {
if defaultClient != nil {
panic("web.client: Init called twice")
}
defaultClient = &client{
func New(middleware middleware.Middleware) *Client {
return &Client{
http: &http.Client{
Timeout: timeout,
},
middleware: mw,
middleware: middleware,
}
}
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)
}