MCPcopy Index your code
hub / github.com/coder/coder / apiRequest

Method apiRequest

codersdk/workspacesdk/agentconn.go:1334–1376  ·  view source on GitHub ↗

apiRequest makes a request to the workspace agent's HTTP API server.

(ctx context.Context, method, path string, body interface{})

Source from the content-addressed store, hash-verified

1332
1333// apiRequest makes a request to the workspace agent's HTTP API server.
1334func (c *agentConn) apiRequest(ctx context.Context, method, path string, body interface{}) (*http.Response, error) {
1335 ctx, span := tracing.StartSpan(ctx)
1336 defer span.End()
1337
1338 host := net.JoinHostPort(c.agentAddress().String(), strconv.Itoa(AgentHTTPAPIServerPort))
1339 url := fmt.Sprintf("http://%s%s", host, path)
1340
1341 var r io.Reader
1342 if body != nil {
1343 switch data := body.(type) {
1344 case io.Reader:
1345 r = data
1346 case []byte:
1347 r = bytes.NewReader(data)
1348 default:
1349 // Assume JSON in all other cases.
1350 buf := bytes.NewBuffer(nil)
1351 enc := json.NewEncoder(buf)
1352 enc.SetEscapeHTML(false)
1353 err := enc.Encode(body)
1354 if err != nil {
1355 return nil, xerrors.Errorf("encode body: %w", err)
1356 }
1357 r = buf
1358 }
1359 }
1360
1361 req, err := http.NewRequestWithContext(ctx, method, url, r)
1362 if err != nil {
1363 return nil, xerrors.Errorf("new http api request to %q: %w", url, err)
1364 }
1365
1366 c.headersMu.RLock()
1367 extraHeaders := c.extraHeaders.Clone()
1368 c.headersMu.RUnlock()
1369 for key, values := range extraHeaders {
1370 for _, value := range values {
1371 req.Header.Add(key, value)
1372 }
1373 }
1374
1375 return c.apiClient().Do(req)
1376}
1377
1378// apiClient returns an HTTP client that can be used to make
1379// requests to the workspace agent's HTTP API server.

Callers 15

ListeningPortsMethod · 0.95
NetcheckMethod · 0.95
DebugMagicsockMethod · 0.95
DebugManifestMethod · 0.95
DebugLogsMethod · 0.95
PrometheusMetricsMethod · 0.95
ListContainersMethod · 0.95
StartDesktopRecordingMethod · 0.95
StopDesktopRecordingMethod · 0.95
DeleteDevcontainerMethod · 0.95
RecreateDevcontainerMethod · 0.95
LSMethod · 0.95

Calls 9

agentAddressMethod · 0.95
apiClientMethod · 0.95
StartSpanFunction · 0.92
EncodeMethod · 0.80
AddMethod · 0.65
DoMethod · 0.65
StringMethod · 0.45
ErrorfMethod · 0.45
CloneMethod · 0.45

Tested by

no test coverage detected