MCPcopy Create free account
hub / github.com/1Panel-dev/1Panel / HandleRequestWithClient

Function HandleRequestWithClient

agent/utils/req_helper/request.go:56–85  ·  view source on GitHub ↗
(client *http.Client, url, method string, timeout int)

Source from the content-addressed store, hash-verified

54}
55
56func HandleRequestWithClient(client *http.Client, url, method string, timeout int) (int, []byte, error) {
57 defer func() {
58 if r := recover(); r != nil {
59 global.LOG.Errorf("handle request failed, error message: %v", r)
60 return
61 }
62 }()
63
64 ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
65 defer cancel()
66 request, err := http.NewRequestWithContext(ctx, method, url, nil)
67 if err != nil {
68 return 0, nil, err
69 }
70 request.Header.Set("Content-Type", "application/json")
71 resp, err := client.Do(request)
72 if err != nil {
73 return 0, nil, err
74 }
75 defer resp.Body.Close()
76 if resp.StatusCode != http.StatusOK {
77 return 0, nil, errors.New(resp.Status)
78 }
79 body, err := io.ReadAll(resp.Body)
80 if err != nil {
81 return 0, nil, err
82 }
83
84 return resp.StatusCode, body, nil
85}
86
87type RequestResponse struct {
88 StatusCode int

Callers 1

HandleRequestFunction · 0.85

Calls 2

CloseMethod · 0.65
SetMethod · 0.45

Tested by

no test coverage detected