package fed import ( "bytes" "context" "encoding/json" "fmt" "io" "net/http" ) // Get fetches a peer path with the peer token and decodes the JSON answer. func (h *Hub) Get(ctx context.Context, p Peer, path string, out any) error { _, err := h.get(ctx, p, path, out) return err } // PostJSON sends v as JSON to a peer path with the peer token. func (h *Hub) PostJSON(ctx context.Context, p Peer, path string, v any) error { b, err := json.Marshal(v) if err != nil { return err } req, err := http.NewRequestWithContext(ctx, http.MethodPost, p.URL+path, bytes.NewReader(b)) if err != nil { return err } tok := h.Token if p.Token != "" { tok = p.Token } if tok != "" { req.Header.Set("Authorization", "Bearer "+tok) } req.Header.Set("Content-Type", "application/json") resp, err := h.client.Do(req) if err != nil { return err } defer resp.Body.Close() io.Copy(io.Discard, io.LimitReader(resp.Body, 4096)) if resp.StatusCode/100 != 2 { return fmt.Errorf("HTTP %d", resp.StatusCode) } return nil }