MCPcopy
hub / github.com/SurgeDM/Surge / doRequest

Method doRequest

internal/core/remote_service.go:53–86  ·  view source on GitHub ↗
(method, path string, body interface{})

Source from the content-addressed store, hash-verified

51}
52
53func (s *RemoteDownloadService) doRequest(method, path string, body interface{}) (*http.Response, error) {
54 var bodyReader io.Reader
55 if body != nil {
56 jsonBody, err := json.Marshal(body)
57 if err != nil {
58 return nil, err
59 }
60 bodyReader = bytes.NewBuffer(jsonBody)
61 }
62
63 req, err := http.NewRequestWithContext(s.ctx, method, s.BaseURL+path, bodyReader)
64 if err != nil {
65 return nil, err
66 }
67
68 req.Header.Set("Authorization", "Bearer "+s.Token)
69 if body != nil {
70 req.Header.Set("Content-Type", "application/json")
71 }
72
73 resp, err := s.Client.Do(req)
74 if err != nil {
75 return nil, err
76 }
77
78 if resp.StatusCode >= 400 {
79 defer func() { _, _ = io.Copy(io.Discard, resp.Body); _ = resp.Body.Close() }()
80 // Limit error body read to 1KB to prevent DoS
81 bodyBytes, _ := io.ReadAll(io.LimitReader(resp.Body, types.KB))
82 return nil, fmt.Errorf("API error %d: %s", resp.StatusCode, string(bodyBytes))
83 }
84
85 return resp, nil
86}
87
88// List returns the status of all active and completed downloads.
89func (s *RemoteDownloadService) List() ([]types.DownloadStatus, error) {

Callers 15

ListMethod · 0.95
HistoryMethod · 0.95
GetStatusMethod · 0.95
AddMethod · 0.95
AddWithIDMethod · 0.95
PauseMethod · 0.95
ResumeMethod · 0.95
UpdateURLMethod · 0.95
DeleteMethod · 0.95
PurgeMethod · 0.95
SetRateLimitMethod · 0.95
ClearRateLimitMethod · 0.95

Calls 1

CloseMethod · 0.45

Tested by

no test coverage detected