apiRequest makes a request to the workspace agent's HTTP API server.
(ctx context.Context, method, path string, body interface{})
| 1332 | |
| 1333 | // apiRequest makes a request to the workspace agent's HTTP API server. |
| 1334 | func (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. |
no test coverage detected