package api import ( "bytes" "encoding/json" "fmt" "io" "net/http" "time" "git.wisehodl.dev/jay/aicli/config" ) var httpClient = &http.Client{Timeout: 5 * time.Minute} // executeHTTP sends the payload to the API endpoint and returns the response body. func executeHTTP(cfg config.ConfigData, payload map[string]interface{}) ([]byte, error) { body, err := json.Marshal(payload) if err != nil { return nil, fmt.Errorf("marshal payload: %w", err) } req, err := http.NewRequest("POST", cfg.URL, bytes.NewReader(body)) if err != nil { return nil, fmt.Errorf("create request: %w", err) } req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", cfg.APIKey)) resp, err := httpClient.Do(req) if err != nil { return nil, fmt.Errorf("execute request: %w", err) } defer resp.Body.Close() respBody, err := io.ReadAll(resp.Body) if err != nil { return nil, fmt.Errorf("read response: %w", err) } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("HTTP %d: %s", resp.StatusCode, string(respBody)) } return respBody, nil }