doRequestWithCredentialsAndClient lets SSE streams opt out of the JSON request timeout.
( ctx context.Context, method string, path string, query url.Values, requestBody any, lastEventID string, credentials agentidentity.Credentials, client *http.Client, )
| 5522 | |
| 5523 | // doRequestWithCredentialsAndClient lets SSE streams opt out of the JSON request timeout. |
| 5524 | func (c *unixSocketClient) doRequestWithCredentialsAndClient( |
| 5525 | ctx context.Context, |
| 5526 | method string, |
| 5527 | path string, |
| 5528 | query url.Values, |
| 5529 | requestBody any, |
| 5530 | lastEventID string, |
| 5531 | credentials agentidentity.Credentials, |
| 5532 | client *http.Client, |
| 5533 | ) (*http.Response, error) { |
| 5534 | if ctx == nil { |
| 5535 | return nil, errors.New("cli: context is required") |
| 5536 | } |
| 5537 | if client == nil { |
| 5538 | return nil, errors.New("cli: http client is required") |
| 5539 | } |
| 5540 | |
| 5541 | target := baseURL + path |
| 5542 | if len(query) > 0 { |
| 5543 | target += "?" + query.Encode() |
| 5544 | } |
| 5545 | |
| 5546 | var body io.Reader |
| 5547 | if requestBody != nil { |
| 5548 | payload, err := json.Marshal(requestBody) |
| 5549 | if err != nil { |
| 5550 | return nil, fmt.Errorf("cli: encode %s %s request: %w", method, path, err) |
| 5551 | } |
| 5552 | body = bytes.NewReader(payload) |
| 5553 | } |
| 5554 | |
| 5555 | req, err := http.NewRequestWithContext(ctx, method, target, body) |
| 5556 | if err != nil { |
| 5557 | return nil, fmt.Errorf("cli: build %s %s request: %w", method, path, err) |
| 5558 | } |
| 5559 | req.Header.Set("User-Agent", defaultUserAgentName) |
| 5560 | if requestBody != nil { |
| 5561 | req.Header.Set("Content-Type", "application/json") |
| 5562 | } |
| 5563 | if strings.TrimSpace(lastEventID) != "" { |
| 5564 | req.Header.Set("Last-Event-ID", strings.TrimSpace(lastEventID)) |
| 5565 | } |
| 5566 | setAgentIdentityHeaders(req, credentials) |
| 5567 | |
| 5568 | response, err := client.Do(req) |
| 5569 | if err != nil { |
| 5570 | if isDaemonUnavailableTransportError(err) { |
| 5571 | return nil, newDaemonUnavailableError(c.socketPath, method, path, err) |
| 5572 | } |
| 5573 | return nil, fmt.Errorf("cli: %s %s via %s: %w", method, path, c.socketPath, err) |
| 5574 | } |
| 5575 | return response, nil |
| 5576 | } |
| 5577 | |
| 5578 | func newDaemonUnavailableError(socketPath string, method string, path string, err error) error { |
| 5579 | item := diagnosticspkg.NewItem( |
no test coverage detected