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

Function HandleRequest

agent/utils/version/version.go:172–203  ·  view source on GitHub ↗
(url, method string, timeout int)

Source from the content-addressed store, hash-verified

170}
171
172func HandleRequest(url, method string, timeout int) (int, []byte, error) {
173 defer func() {
174 if r := recover(); r != nil {
175 global.LOG.Errorf("handle request failed, error message: %v", r)
176 return
177 }
178 }()
179
180 transport := loadRequestTransport()
181 client := http.Client{Timeout: time.Duration(timeout) * time.Second, Transport: transport}
182 ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
183 defer cancel()
184 request, err := http.NewRequestWithContext(ctx, method, url, nil)
185 if err != nil {
186 return 0, nil, err
187 }
188 request.Header.Set("Content-Type", "application/json")
189 resp, err := client.Do(request)
190 if err != nil {
191 return 0, nil, err
192 }
193 defer resp.Body.Close()
194 if resp.StatusCode != http.StatusOK {
195 return 0, nil, errors.New(resp.Status)
196 }
197 body, err := io.ReadAll(resp.Body)
198 if err != nil {
199 return 0, nil, err
200 }
201
202 return resp.StatusCode, body, nil
203}
204
205func loadRequestTransport() *http.Transport {
206 return &http.Transport{

Callers 2

loadVersionFunction · 0.70
loadReleaseNotesFunction · 0.70

Calls 3

loadRequestTransportFunction · 0.70
CloseMethod · 0.65
SetMethod · 0.45

Tested by

no test coverage detected