doHTTPRequest executes an HTTP request and returns the response body.
(_ context.Context, httpReq *http.Request, provider string)
| 691 | |
| 692 | // doHTTPRequest executes an HTTP request and returns the response body. |
| 693 | func doHTTPRequest(_ context.Context, httpReq *http.Request, provider string) ([]byte, error) { |
| 694 | client := &http.Client{} |
| 695 | httpResp, err := client.Do(httpReq) |
| 696 | if err != nil { |
| 697 | return nil, errors.Errorf("failed to send HTTP request to %s: %s", provider, err) |
| 698 | } |
| 699 | defer httpResp.Body.Close() |
| 700 | |
| 701 | body, err := io.ReadAll(httpResp.Body) |
| 702 | if err != nil { |
| 703 | return nil, errors.Errorf("failed to read %s response body: %s", provider, err) |
| 704 | } |
| 705 | |
| 706 | if httpResp.StatusCode != http.StatusOK { |
| 707 | return nil, errors.Errorf("%s API returned status %d: %s", provider, httpResp.StatusCode, string(body)) |
| 708 | } |
| 709 | |
| 710 | return body, nil |
| 711 | } |
| 712 | |
| 713 | // chatMessageRoleToOpenAI converts a proto role enum to OpenAI role string. |
| 714 | func chatMessageRoleToOpenAI(role v1pb.AIChatMessageRole) string { |
no test coverage detected