-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_util.go
137 lines (112 loc) · 3.16 KB
/
http_util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package ai
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"sync"
"time"
)
// RequestDetails holds the details for an HTTP request
type RequestDetails struct {
URL string
APIKey string
RequestBody interface{}
AdditionalHeaders map[string]string
}
// ClientOptions holds options for customizing the HTTP client
type ClientOptions struct {
Timeout time.Duration
RetryAttempts int
RetryDelay time.Duration
}
var (
httpClient *http.Client
clientOnce sync.Once
)
// initClient initializes the HTTP client with default options
func initClient() {
httpClient = &http.Client{
Timeout: 30 * time.Second,
}
}
// SetClientOptions allows customization of the HTTP client
func SetClientOptions(options ClientOptions) {
clientOnce.Do(func() {
httpClient = &http.Client{
Timeout: options.Timeout,
}
})
}
func drainAndCloseBody(body io.ReadCloser) error {
_, err := io.Copy(io.Discard, body)
if err != nil {
return fmt.Errorf("error draining body: %w", err)
}
if err := body.Close(); err != nil {
return fmt.Errorf("error closing body: %w", err)
}
return nil
}
func createRequest(ctx context.Context, details RequestDetails) (*http.Request, error) {
jsonBody, err := json.Marshal(details.RequestBody)
if err != nil {
return nil, fmt.Errorf("error marshaling request: %w", err)
}
req, err := http.NewRequestWithContext(ctx, "POST", details.URL, bytes.NewBuffer(jsonBody))
if err != nil {
return nil, fmt.Errorf("error creating request for URL %s: %w", details.URL, err)
}
req.Header.Set("Content-Type", "application/json")
if details.APIKey != "" {
req.Header.Set("Authorization", "Bearer "+details.APIKey)
}
for key, value := range details.AdditionalHeaders {
req.Header.Set(key, value)
}
return req, nil
}
func executeRequest(req *http.Request, options ClientOptions) ([]byte, error) {
clientOnce.Do(initClient)
var resp *http.Response
var err error
var body []byte
for attempt := 0; attempt <= options.RetryAttempts; attempt++ {
if attempt > 0 {
time.Sleep(options.RetryDelay)
}
ctx, cancel := context.WithTimeout(req.Context(), options.Timeout)
defer cancel()
reqWithTimeout := req.WithContext(ctx)
resp, err = httpClient.Do(reqWithTimeout)
if err != nil {
log.Printf("Attempt %d: error sending request to %s: %v", attempt+1, req.URL, err)
continue
}
defer func() {
if err := drainAndCloseBody(resp.Body); err != nil {
log.Printf("Error closing response body: %v", err)
}
}()
body, err = io.ReadAll(resp.Body)
if err != nil {
log.Printf("Attempt %d: error reading response from %s: %v", attempt+1, req.URL, err)
continue
}
if resp.StatusCode == http.StatusOK {
return body, nil
}
log.Printf("Attempt %d: API request to %s failed with status code %d: %s", attempt+1, req.URL, resp.StatusCode, string(body))
}
return nil, fmt.Errorf("API request to %s failed after %d attempts", req.URL, options.RetryAttempts+1)
}
func sendRequest(ctx context.Context, details RequestDetails, options ClientOptions) ([]byte, error) {
req, err := createRequest(ctx, details)
if err != nil {
return nil, err
}
return executeRequest(req, options)
}