MCPcopy Create free account
hub / github.com/APIParkLab/APIPark / callHttp

Function callHttp

gateway/admin/http.go:22–58  ·  view source on GitHub ↗
(ctx context.Context, address []string, method string, path string, body []byte)

Source from the content-addressed store, hash-verified

20)
21
22func callHttp[M any](ctx context.Context, address []string, method string, path string, body []byte) (*M, error) {
23 if len(address) == 0 {
24 return nil, ErrorInvalidAdminAddress
25 }
26 method = strings.ToUpper(method)
27 var response *http.Response
28 var err error
29 for _, addr := range address {
30 url := fmt.Sprint(addr, strings.TrimPrefix(path, "/"))
31 response, err = doRequest(ctx, method, url, body)
32 if err != nil {
33 continue
34 }
35 if response.StatusCode != 200 {
36 continue
37 }
38 break
39 }
40 if err != nil {
41 return nil, err
42 }
43
44 if response.StatusCode != 200 {
45 return nil, fmt.Errorf("http status code is %d", response.StatusCode)
46 }
47 data, err := io.ReadAll(response.Body)
48 if err != nil {
49 return nil, err
50 }
51 m := new(M)
52 err = json.Unmarshal(data, m)
53 if err != nil {
54 return nil, err
55 }
56 return m, nil
57
58}
59func doRequest(ctx context.Context, method string, url string, body []byte) (*http.Response, error) {
60
61 request, err := http.NewRequestWithContext(ctx, method, url, bytes.NewReader(body))

Callers

nothing calls this directly

Calls 1

doRequestFunction · 0.85

Tested by

no test coverage detected