• 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/custom.http.client/main.go
1
package main
2

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

12
        "github.com/jeffotoni/quick/http/client"
13
)
14

15
// Example of creating an HTTP client using a fluent and modular approach.
16
// This allows fine-grained control over HTTP settings without requiring a full config struct.
17
//
18
// - WithTimeout: Sets the HTTP client timeout to 30 seconds.
19
// - WithContext: Injects a context for the client (context.TODO() used as placeholder).
20
// - WithHeaders: Adds custom headers (e.g., Content-Type: application/json).
21
// - WithRetry: Enables automatic retries for specific HTTP status codes (500, 502, 503, 504)
22
// - WithCustomHTTPClient: Uses a pre-configured http.Client with custom settings.
23
func main() {
×
24
        // Creating a CookieJar to manage cookies automatically.
×
25
        jar, _ := cookiejar.New(nil)
×
26

×
27
        // Creating a fully custom *http.Client.
×
28
        customHTTPClient := &http.Client{
×
29
                Timeout: 10 * time.Second, // Sets a global timeout of 10 seconds.
×
30
                Jar:     jar,              // Uses a CookieJar to store cookies.
×
31
                CheckRedirect: func(req *http.Request, via []*http.Request) error {
×
32
                        // Allows up to 3 redirects.
×
33
                        if len(via) >= 3 {
×
34
                                return http.ErrUseLastResponse
×
35
                        }
×
36
                        return nil
×
37
                },
38
                Transport: &http.Transport{
39
                        Proxy: http.ProxyFromEnvironment,
40
                        TLSClientConfig: &tls.Config{
41
                                InsecureSkipVerify: true, // Allows insecure TLS (not recommended for production).
42
                                MinVersion:         tls.VersionTLS12,
43
                        },
44
                        MaxIdleConns:        50,    // Maximum idle connections.
45
                        MaxConnsPerHost:     30,    // Max simultaneous connections per host.
46
                        MaxIdleConnsPerHost: 10,    // Max idle connections per host.
47
                        DisableKeepAlives:   false, // Enables keep-alive.
48
                },
49
        }
50

51
        // Creating a quick client using the custom *http.Client.
52
        cClient := client.New(
×
53
                client.WithCustomHTTPClient(customHTTPClient), // Uses the pre-configured HTTP client.
×
54
                client.WithContext(context.Background()),      // Sets a request context.
×
55
                client.WithHeaders(map[string]string{
×
56
                        "Content-Type":  "application/json",
×
57
                        "Authorization": "Bearer YOUR_ACCESS_TOKEN",
×
58
                }),
×
59
                // Enables retry for specific HTTP status codes using the new RetryConfig.
×
NEW
60
                client.WithRetry(client.RetryConfig{
×
NEW
61
                        MaxRetries:   3,                         // Maximum number of retries.
×
NEW
62
                        Delay:        1 * time.Second,           // Delay between attempts.
×
NEW
63
                        UseBackoff:   true,                      // Use exponential backoff.
×
NEW
64
                        Statuses:     []int{500, 502, 503, 504}, // HTTP statuses for retry.
×
NEW
65
                        FailoverURLs: []string{"https://httpbin_error.org/get", "https://httpbin.org/get"},
×
NEW
66
                        EnableLog:    true, // Enable logger.
×
NEW
67
                }),
×
NEW
68
        )
×
NEW
69

×
NEW
70
        // Creating a quick client using the custom *http.Client.
×
NEW
71
        cClient2 := client.New(
×
NEW
72
                client.WithTimeout(5*time.Second),
×
NEW
73
                client.WithHeaders(map[string]string{
×
NEW
74
                        "Content-Type":  "application/json",
×
NEW
75
                        "Authorization": "Bearer YOUR_ACCESS_TOKEN",
×
NEW
76
                }),
×
NEW
77
                // Enables retry for specific HTTP status codes using the new RetryConfig.
×
78
                client.WithRetry(client.RetryConfig{
×
79
                        MaxRetries:   3,                         // Maximum number of retries.
×
80
                        Delay:        1 * time.Second,           // Delay between attempts.
×
81
                        UseBackoff:   true,                      // Use exponential backoff.
×
82
                        Statuses:     []int{500, 502, 503, 504}, // HTTP statuses for retry.
×
83
                        FailoverURLs: []string{"https://httpbin_error.org/post", "https://httpbin.org/post"},
×
84
                        EnableLog:    true, // Enable logger.
×
85
                }),
×
86
        )
×
87

×
88
        // Performing a GET request.
×
NEW
89
        resp, err := cClient.Get("https://httpbin_error.org/get")
×
90
        if err != nil {
×
91
                log.Fatalf("GET request failed: %v", err)
×
92
        }
×
93
        fmt.Println("GET Response:", string(resp.Body))
×
94

×
95
        // Performing a POST request.
×
96
        data := map[string]string{"name": "QuickFramework", "version": "1.0"}
×
NEW
97
        resp, err = cClient2.Post("https://httpbin_error.org/post", data)
×
98
        if err != nil {
×
99
                log.Fatalf("POST request failed: %v", err)
×
100
        }
×
101
        fmt.Println("POST Response:", string(resp.Body))
×
102
}
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