MCPcopy Create free account
hub / github.com/LumePart/Explo / MakeRequest

Method MakeRequest

src/util/http.go:35–70  ·  view source on GitHub ↗
(method, url string, payload io.Reader, headers map[string]string)

Source from the content-addressed store, hash-verified

33}
34
35func (c *HttpClient) MakeRequest(method, url string, payload io.Reader, headers map[string]string) ([]byte, error) {
36 req, err := http.NewRequest(method, url, payload)
37 if err != nil {
38 return nil, fmt.Errorf("failed to initialize request: %s", err.Error())
39 }
40 req.Header.Add("Content-Type", "application/json")
41 req.Header.Add("Accept", "application/json")
42 req.Header.Add("User-Agent", c.UserAgent)
43
44 for key, value := range headers {
45 req.Header.Add(key, value)
46 }
47
48 resp, err := c.Client.Do(req)
49 if err != nil {
50 return nil, fmt.Errorf("failed to make request: %s", err.Error())
51 }
52
53 defer func() {
54 if err := resp.Body.Close(); err != nil {
55 slog.Warn("response body close failed", "context", err.Error())
56 }
57 }()
58
59 body, err := io.ReadAll(resp.Body)
60 if err != nil {
61 return nil, fmt.Errorf("failed to read response body: %s", err.Error())
62 }
63
64 if resp.StatusCode < 200 || resp.StatusCode >= 300 {
65 slog.Debug("response info", logging.RuntimeAttr(string(body)))
66 return nil, fmt.Errorf("got %d from %s", resp.StatusCode, url)
67 }
68
69 return body, nil
70}
71
72func ParseResp[T any](body []byte, target *T) error {
73

Callers 15

subsonicRequestMethod · 0.80
GetLibraryMethod · 0.80
AddLibraryMethod · 0.80
RefreshLibraryMethod · 0.80
SearchSongsMethod · 0.80
SearchPlaylistMethod · 0.80
CreatePlaylistMethod · 0.80
UpdatePlaylistMethod · 0.80
DeletePlaylistMethod · 0.80
ResolveUserIDMethod · 0.80
GetLibraryMethod · 0.80
AddLibraryMethod · 0.80

Calls 1

RuntimeAttrFunction · 0.92

Tested by

no test coverage detected