• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jeffotoni / quick / 233

07 Mar 2025 07:31PM UTC coverage: 56.021% (-0.4%) from 56.44%
233

push

circleci

jeffotoni
Merge remote-tracking branch 'origin/example/http'

0 of 131 new or added lines in 11 files covered. (0.0%)

4 existing lines in 4 files now uncovered.

2191 of 3911 relevant lines covered (56.02%)

2552.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/example/quick.http.client/full.retray/main.go
1
package main
2

3
import (
4
        "crypto/tls"
5
        "fmt"
6
        "log"
7
        "net/http"
8
        "time"
9

10
        "github.com/jeffotoni/quick/http/client"
11
)
12

13
// Example of creating an HTTP client using a fluent and modular approach.
14
// This allows fine-grained control over HTTP settings without requiring a full config struct.
15
//
16
// - WithTimeout: Sets the HTTP client timeout to 30 seconds.
17
// - WithHeaders: Adds custom headers (e.g., Content-Type: application/json).
18
// - WithRetry: Enables automatic retries for specific HTTP status codes (500, 502, 503, 504)
19
func main() {
×
20

×
NEW
21
        // If both WithRetry RoundTripper and WithRetry are declared,
×
NEW
22
        // WithRetry RoundTripper takes precedence and is executed.
×
23
        cClient := client.New(
×
NEW
24
                client.WithTimeout(5*time.Second), // Sets a global timeout for all requests
×
NEW
25
                client.WithHeaders(map[string]string{
×
NEW
26
                        "Content-Type": "application/json", // Defines the content type for requests
×
NEW
27
                }),
×
28

×
NEW
29
                // Configures an advanced HTTP transport for connection optimization
×
30
                client.WithTransportConfig(&http.Transport{
×
NEW
31
                        Proxy:               http.ProxyFromEnvironment,             // Uses system proxy settings
×
NEW
32
                        TLSClientConfig:     &tls.Config{InsecureSkipVerify: true}, // Disables SSL verification (not recommended for production)
×
NEW
33
                        ForceAttemptHTTP2:   true,                                  // Enables HTTP/2 if the server supports it
×
NEW
34
                        MaxIdleConns:        20,                                    // Limits the total number of idle connections
×
NEW
35
                        MaxIdleConnsPerHost: 10,                                    // Limits idle connections per host
×
NEW
36
                        MaxConnsPerHost:     20,                                    // Limits concurrent connections per host
×
NEW
37
                        DisableKeepAlives:   false,                                 // Enables persistent connections (Keep-Alive)
×
38
                }),
×
39

×
NEW
40
                // Enables automatic retries for failed requests
×
41
                client.WithRetry(
×
42
                        client.RetryConfig{
×
NEW
43
                                MaxRetries: 2,               // Maximum number of retry attempts
×
NEW
44
                                Delay:      1 * time.Second, // Wait time before retrying
×
NEW
45
                                UseBackoff: true,            // Enables exponential backoff for retries
×
NEW
46
                                Statuses:   []int{500},      // Retries only on HTTP 500 errors
×
NEW
47
                                FailoverURLs: []string{ // Backup URLs in case the primary request fails
×
48
                                        "http://backup1",
×
49
                                        "https://reqres.in/api/users",
×
NEW
50
                                        "https://httpbin_error.org/post",
×
NEW
51
                                },
×
NEW
52
                                EnableLog: true, // Enables logging for debugging retries
×
53
                        }),
×
54
        )
×
55

×
NEW
56
        // Sends a POST request to the primary URL
×
NEW
57
        resp, err := cClient.Post("https://httpbin_error.org/post", map[string]string{"name": "jeffotoni"})
×
58
        if err != nil {
×
NEW
59
                log.Fatalf("POST request failed: %v", err) // Logs an error if the request fails
×
60
        }
×
61

62
        // Print the response body
UNCOV
63
        fmt.Println("POST Form Response:", string(resp.Body))
×
64
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc