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

3
import (
4
        "context"
5
        "crypto/tls"
6
        "encoding/json"
7
        "fmt"
8
        "log"
9
        "time"
10

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

14
// Example of creating an HTTP client using a fluent and modular approach.
15
// This allows fine-grained control over HTTP settings without requiring a full config struct.
16
//
17
// - WithContext: Injects a context for the client (context.TODO() used as placeholder).
18
// - WithHeaders: Adds custom headers (e.g., Content-Type: application/xml).
19
// - WithRetry: Enables automatic retries for specific HTTP status codes (500, 502, 503, 504)
20
// - WithHTTPClientConfig: Defines advanced transport settings like connection pooling.
21
func main() {
×
22
        cfg := &client.HTTPClientConfig{
×
23
                Timeout:             20 * time.Second,
×
24
                DisableKeepAlives:   false,
×
25
                MaxIdleConns:        20,
×
26
                MaxConnsPerHost:     20,
×
27
                MaxIdleConnsPerHost: 20,
×
28
                TLSClientConfig: &tls.Config{
×
29
                        InsecureSkipVerify: false,
×
30
                        MinVersion:         tls.VersionTLS12,
×
31
                },
×
32
        }
×
33

×
34
        // Creating an HTTP client with the pre-defined configuration.
×
35
        //
×
36
        // - WithContext: Sets a custom context for handling request cancellation and deadlines.
×
37
        // - WithHeaders: Adds a map of default headers (e.g., "Content-Type: application/xml").
×
38
        // - WithHTTPClientConfig: Applies the entire configuration object (cfg) to the client.
×
39
        cClient := client.New(
×
40
                client.WithContext(context.TODO()),
×
41
                client.WithHeaders(map[string]string{"Content-Type": "application/xml"}),
×
42
                client.WithHTTPClientConfig(cfg),
×
43
                client.WithRetry(
×
44
                        client.RetryConfig{
×
45
                                MaxRetries: 2,
×
46
                                Delay:      1 * time.Second,
×
47
                                UseBackoff: true,
×
48
                                Statuses:   []int{500},
×
49
                                FailoverURLs: []string{
×
50
                                        "http://backup1",
×
51
                                        "https://httpbin_error.org/post",
×
NEW
52
                                        "https://httpbin.org/post"},
×
53
                                EnableLog: true,
×
54
                        }),
×
55
        )
×
56

×
57
        // Define a struct to send as JSON
×
58
        data := struct {
×
59
                Message string `json:"message"`
×
60
        }{
×
61
                Message: "Hello, POST!",
×
62
        }
×
63

×
NEW
64
        resp, err := cClient.Post("https://httpbin_error.org/post ", data)
×
65
        if err != nil {
×
66
                log.Fatal(err)
×
67
        }
×
68

69
        // Unmarshal the JSON response (if applicable)
70
        var result map[string]string
×
71
        if err := json.Unmarshal(resp.Body, &result); err != nil {
×
72
                log.Fatal(err)
×
73
        }
×
74
        fmt.Println("POST response:", result["message"])
×
75
}
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