Documentation
¶
Index ¶
- type Config
- type HttpClient
- func (cli *HttpClient) Client() *resty.Client
- func (cli *HttpClient) Delete(url string, token string, body any) (*resty.Response, error)
- func (cli *HttpClient) Get(url string, token string, body any) (*resty.Response, error)
- func (cli *HttpClient) Post(url string, token string, body any) (*resty.Response, error)
- func (cli *HttpClient) Put(url string, token string, body any) (*resty.Response, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
Timeout time.Duration // default: 10s
RetryMax int // default: 3
RetryWaitTime time.Duration // default: 1s
RetryMaxWaitTime time.Duration // default: 30s
InsecureSkipVerify bool // skip TLS certificate verification; use only for internal services with self-signed certs
// RetryOnServerErrors also retries on HTTP 429 and 5xx responses, not just
// transport-level errors (connection refused, timeout, DNS failure, etc).
// resty's own default retry condition only looks at the error returned by
// the underlying http.Client.Do — a well-formed 500/503/429 response is
// not a Go error, so without this flag RetryMax never fires for those.
// Defaults to false: existing callers that only expect transport-level
// retries keep that behavior; turning this on can noticeably lengthen a
// call against a genuinely-down downstream (RetryMax attempts, each
// waiting up to RetryMaxWaitTime) instead of failing fast.
RetryOnServerErrors bool
}
Config holds all configuration for HttpClient (resty-based). Zero-value duration and count fields fall back to the package defaults above. The caller is responsible for reading values from env (or any other source) at the composition root so this package has no dependency on env or log.
type HttpClient ¶
type HttpClient struct {
// contains filtered or unexported fields
}
HttpClient wraps a resty.Client with pre-configured timeout, retry, and TLS settings.
func NewHttpClient ¶
func NewHttpClient(cfg ...Config) *HttpClient
NewHttpClient creates and returns a new HttpClient. Pass an optional Config to override defaults; omit for all-default settings. Timeout, retry count, and wait times are configurable. TLS certificate verification can be disabled via InsecureSkipVerify (only for internal services with self-signed certificates). Log output is routed through slog.Default(), which is the project logger after log.InitLog() has been called.
Example ¶
ExampleNewHttpClient shows how to create an HttpClient with default settings. Log output is routed through slog.Default() — call log.InitLog() first to integrate with the project's structured logger.
package main
import (
"fmt"
"github.com/phcp-tech/common-library-golang/httpclient"
)
func main() {
cli := httpclient.NewHttpClient()
fmt.Println(cli != nil)
}
Output: true
Example (CustomConfig) ¶
ExampleNewHttpClient_customConfig shows how to override default timeout and retry settings. Pass a Config with only the fields you want to change; zero-value fields fall back to the package defaults (Timeout=10s, RetryMax=3).
package main
import (
"fmt"
"time"
"github.com/phcp-tech/common-library-golang/httpclient"
)
func main() {
cli := httpclient.NewHttpClient(httpclient.Config{
Timeout: 15 * time.Second,
RetryMax: 5,
RetryWaitTime: 2 * time.Second,
RetryMaxWaitTime: 60 * time.Second,
})
fmt.Println(cli != nil)
}
Output: true
Example (InsecureSkipVerify) ¶
ExampleNewHttpClient_insecureSkipVerify shows how to disable TLS certificate verification for internal services that use self-signed certificates. Do NOT use this in production for external services.
package main
import (
"fmt"
"github.com/phcp-tech/common-library-golang/httpclient"
)
func main() {
cli := httpclient.NewHttpClient(httpclient.Config{
InsecureSkipVerify: true, // only for internal services with self-signed certs
})
fmt.Println(cli != nil)
}
Output: true
Example (RetryOnServerErrors) ¶
ExampleNewHttpClient_retryOnServerErrors shows how to make RetryMax also retry HTTP 429/5xx responses, not just transport-level errors (connection refused, timeout, etc). resty's own default retry condition only looks at the error returned by the underlying round trip — a well-formed 500/503 response is not a Go error, so without this flag RetryMax never fires for those. Defaults to false to avoid silently lengthening existing callers' requests against a genuinely-down downstream.
package main
import (
"fmt"
"github.com/phcp-tech/common-library-golang/httpclient"
)
func main() {
cli := httpclient.NewHttpClient(httpclient.Config{
RetryMax: 3,
RetryOnServerErrors: true,
})
fmt.Println(cli != nil)
}
Output: true
func (*HttpClient) Client ¶
func (cli *HttpClient) Client() *resty.Client
Client returns the underlying resty.Client for advanced configuration or direct use.
Example ¶
ExampleHttpClient_Client shows how to obtain the underlying resty.Client for advanced configuration not exposed by Config.
package main
import (
"fmt"
"github.com/phcp-tech/common-library-golang/httpclient"
)
func main() {
cli := httpclient.NewHttpClient()
restyClient := cli.Client() // *resty.Client
fmt.Println(restyClient != nil)
}
Output: true
func (*HttpClient) Delete ¶
Delete sends an HTTP DELETE request to the given URL with the provided bearer token and JSON body, returning the response or an error.
func (*HttpClient) Get ¶
Get sends an HTTP GET request to the given URL with the provided bearer token and request body, returning the response or an error.