-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_generic_client_test.go
More file actions
366 lines (304 loc) · 10.3 KB
/
example_generic_client_test.go
File metadata and controls
366 lines (304 loc) · 10.3 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package httpx_test
import (
"fmt"
"log"
"net/http"
"time"
"github.com/slashdevops/httpx"
)
// User represents a user in the API
type User struct {
ID int `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
}
// Post represents a blog post
type Post struct {
ID int `json:"id"`
Title string `json:"title"`
Body string `json:"body"`
UserID int `json:"userId"`
}
// ExampleNewGenericClient demonstrates basic usage of the generic client
func ExampleNewGenericClient() {
// Create a typed client for User responses with configuration
client := httpx.NewGenericClient[User](
httpx.WithTimeout[User](10*time.Second),
httpx.WithMaxRetries[User](3),
httpx.WithRetryStrategy[User](httpx.ExponentialBackoffStrategy),
)
// Make a GET request with full URL
response, err := client.Get("https://api.example.com/users/1")
if err != nil {
log.Fatal(err)
}
fmt.Printf("User: %s (%s)\n", response.Data.Name, response.Data.Email)
}
// ExampleNewGenericClient_allOptions demonstrates using all configuration options
func ExampleNewGenericClient_allOptions() {
// Create a fully configured client
client := httpx.NewGenericClient[User](
// Timeout configuration
httpx.WithTimeout[User](15*time.Second),
// Retry configuration
httpx.WithMaxRetries[User](5),
httpx.WithRetryStrategy[User](httpx.JitterBackoffStrategy),
httpx.WithRetryBaseDelay[User](500*time.Millisecond),
httpx.WithRetryMaxDelay[User](10*time.Second),
// Connection pooling
httpx.WithMaxIdleConns[User](100),
httpx.WithMaxIdleConnsPerHost[User](10),
httpx.WithIdleConnTimeout[User](90*time.Second),
// TLS and handshake timeouts
httpx.WithTLSHandshakeTimeout[User](10*time.Second),
httpx.WithExpectContinueTimeout[User](1*time.Second),
// Keep-alive
httpx.WithDisableKeepAlive[User](false),
)
response, err := client.Get("https://api.example.com/users/1")
if err != nil {
log.Fatal(err)
}
fmt.Printf("User: %s\n", response.Data.Name)
}
// ExampleNewGenericClient_withPreConfiguredClient demonstrates using WithHTTPClient
func ExampleNewGenericClient_withPreConfiguredClient() {
// Build a custom HTTP client using ClientBuilder
httpClient := httpx.NewClientBuilder().
WithMaxRetries(3).
WithRetryStrategy(httpx.ExponentialBackoffStrategy).
WithRetryBaseDelay(500 * time.Millisecond).
WithTimeout(30 * time.Second).
WithMaxIdleConns(50).
Build()
// Use it with GenericClient (takes precedence over other options)
client := httpx.NewGenericClient[User](
httpx.WithHTTPClient[User](httpClient),
)
response, err := client.Get("https://api.example.com/users/1")
if err != nil {
log.Fatal(err)
}
fmt.Printf("User: %s\n", response.Data.Name)
}
// ExampleGenericClient_Execute demonstrates using Execute with RequestBuilder
func ExampleGenericClient_Execute() {
// Create a typed client for Post responses
client := httpx.NewGenericClient[Post]()
// Build a request with RequestBuilder
req, err := httpx.NewRequestBuilder("https://jsonplaceholder.typicode.com").
WithMethodGET().
WithPath("/posts/1").
WithAccept("application/json").
Build()
if err != nil {
log.Fatal(err)
}
// Execute the request
response, err := client.Execute(req)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Post: %s\n", response.Data.Title)
}
// ExampleGenericClient_Post demonstrates making a POST request
func ExampleGenericClient_Post() {
client := httpx.NewGenericClient[Post]()
// Create a new post using RequestBuilder
newPost := Post{
Title: "My New Post",
Body: "This is the post content",
UserID: 1,
}
req, err := httpx.NewRequestBuilder("https://jsonplaceholder.typicode.com").
WithMethodPOST().
WithPath("/posts").
WithContentType("application/json").
WithJSONBody(newPost).
Build()
if err != nil {
log.Fatal(err)
}
response, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Created post with ID: %d\n", response.Data.ID)
}
// ExampleGenericClient_withRetry demonstrates using generic client with retry logic
func ExampleGenericClient_withRetry() {
// Option 1: Use ClientBuilder and pass to GenericClient
retryClient := httpx.NewClientBuilder().
WithMaxRetries(3).
WithRetryStrategy(httpx.ExponentialBackoffStrategy).
WithRetryBaseDelay(500 * time.Millisecond).
WithTimeout(30 * time.Second).
Build()
client := httpx.NewGenericClient[User](
httpx.WithHTTPClient[User](retryClient),
)
// Option 2: Configure retry directly in GenericClient
client2 := httpx.NewGenericClient[User](
httpx.WithMaxRetries[User](3),
httpx.WithRetryStrategy[User](httpx.ExponentialBackoffStrategy),
httpx.WithRetryBaseDelay[User](500*time.Millisecond),
httpx.WithTimeout[User](30*time.Second),
)
// Make requests - they will automatically retry on failures
response, err := client.Get("https://api.example.com/users/1")
if err != nil {
log.Fatal(err)
}
fmt.Printf("User: %s\n", response.Data.Name)
// Using second client
response2, err := client2.Get("https://api.example.com/users/2")
if err != nil {
log.Fatal(err)
}
fmt.Printf("User: %s\n", response2.Data.Name)
}
// ExampleGenericClient_multipleClients demonstrates using multiple typed clients
func ExampleGenericClient_multipleClients() {
baseURL := "https://api.example.com"
// Create a client for User responses with specific configuration
userClient := httpx.NewGenericClient[User](
httpx.WithTimeout[User](10*time.Second),
httpx.WithMaxRetries[User](3),
)
// Create a client for Post responses with different configuration
postClient := httpx.NewGenericClient[Post](
httpx.WithTimeout[Post](15*time.Second),
httpx.WithMaxRetries[Post](5),
httpx.WithRetryStrategy[Post](httpx.JitterBackoffStrategy),
)
// Fetch user
userResp, err := userClient.Get(baseURL + "/users/1")
if err != nil {
log.Fatal(err)
}
// Fetch posts by that user
postResp, err := postClient.Get(fmt.Sprintf("%s/users/%d/posts", baseURL, userResp.Data.ID))
if err != nil {
log.Fatal(err)
}
fmt.Printf("User %s has %d posts\n", userResp.Data.Name, len(postResp.Data.Title))
}
// ExampleGenericClient_ExecuteRaw demonstrates using ExecuteRaw for non-JSON responses
func ExampleGenericClient_ExecuteRaw() {
client := httpx.NewGenericClient[User]()
req, err := http.NewRequest(http.MethodGet, "https://example.com/image.png", nil)
if err != nil {
log.Fatal(err)
}
// Get raw response for binary data
response, err := client.ExecuteRaw(req)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
fmt.Printf("Content-Type: %s\n", response.Header.Get("Content-Type"))
fmt.Printf("Status: %d\n", response.StatusCode)
}
// ExampleGenericClient_errorHandling demonstrates error handling
func ExampleGenericClient_errorHandling() {
client := httpx.NewGenericClient[User]()
response, err := client.Get("https://api.example.com/users/999999")
if err != nil {
// Check if it's an ErrorResponse
if apiErr, ok := err.(*httpx.ErrorResponse); ok {
fmt.Printf("API Error: Status %d - %s\n", apiErr.StatusCode, apiErr.Message)
return
}
// Other errors (network, parsing, etc.)
log.Fatal(err)
}
fmt.Printf("User: %s\n", response.Data.Name)
}
// ExampleGenericClient_withCustomHeaders demonstrates adding custom headers per request
func ExampleGenericClient_withCustomHeaders() {
client := httpx.NewGenericClient[User]()
// Build request with headers using RequestBuilder
req, err := httpx.NewRequestBuilder("https://api.example.com").
WithMethodGET().
WithPath("/users/1").
WithHeader("Authorization", "Bearer token").
WithHeader("X-Request-ID", "unique-id-123").
WithHeader("X-Trace-ID", "trace-456").
Build()
if err != nil {
log.Fatal(err)
}
// Execute the request
response, err := client.Execute(req)
if err != nil {
log.Fatal(err)
}
fmt.Printf("User: %s\n", response.Data.Name)
}
// ExampleGenericClient_productionConfiguration demonstrates a production-ready configuration
func ExampleGenericClient_productionConfiguration() {
// Configure client with production-ready settings
client := httpx.NewGenericClient[User](
// Aggressive retry for resilience
httpx.WithMaxRetries[User](5),
httpx.WithRetryStrategy[User](httpx.ExponentialBackoffStrategy),
httpx.WithRetryBaseDelay[User](500*time.Millisecond),
httpx.WithRetryMaxDelay[User](30*time.Second),
// Reasonable timeout
httpx.WithTimeout[User](30*time.Second),
// Connection pooling for performance
httpx.WithMaxIdleConns[User](100),
httpx.WithMaxIdleConnsPerHost[User](10),
httpx.WithIdleConnTimeout[User](90*time.Second),
// TLS optimization
httpx.WithTLSHandshakeTimeout[User](10*time.Second),
)
// Build request with headers
req, err := httpx.NewRequestBuilder("https://api.example.com").
WithMethodGET().
WithPath("/users/1").
WithHeader("Authorization", "Bearer prod-token").
WithHeader("X-Request-ID", "req-123").
WithUserAgent("MyApp/1.0.0").
Build()
if err != nil {
log.Fatal(err)
}
// Execute with automatic retries and error handling
response, err := client.Execute(req)
if err != nil {
if apiErr, ok := err.(*httpx.ErrorResponse); ok {
log.Printf("API Error: %d - %s", apiErr.StatusCode, apiErr.Message)
return
}
log.Fatal(err)
}
fmt.Printf("User: %s (%s)\n", response.Data.Name, response.Data.Email)
}
// ExampleGenericClient_retryStrategies demonstrates different retry strategies
func ExampleGenericClient_retryStrategies() {
// Fixed delay - predictable retry timing
fixedClient := httpx.NewGenericClient[User](
httpx.WithRetryStrategyAsString[User]("fixed"),
httpx.WithMaxRetries[User](3),
httpx.WithRetryBaseDelay[User](1*time.Second),
)
// Exponential backoff - doubles delay each time
exponentialClient := httpx.NewGenericClient[User](
httpx.WithRetryStrategy[User](httpx.ExponentialBackoffStrategy),
httpx.WithMaxRetries[User](5),
httpx.WithRetryBaseDelay[User](500*time.Millisecond),
httpx.WithRetryMaxDelay[User](10*time.Second),
)
// Jitter backoff - random delays to prevent thundering herd
jitterClient := httpx.NewGenericClient[User](
httpx.WithRetryStrategy[User](httpx.JitterBackoffStrategy),
httpx.WithMaxRetries[User](3),
httpx.WithRetryBaseDelay[User](500*time.Millisecond),
httpx.WithRetryMaxDelay[User](5*time.Second),
)
// Use different clients based on use case
_, _ = fixedClient.Get("https://api.example.com/users/1")
_, _ = exponentialClient.Get("https://api.example.com/users/2")
_, _ = jitterClient.Get("https://api.example.com/users/3")
}