httpclient

package
v0.1.17 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 19, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

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
}

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

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

func (cli *HttpClient) Delete(url string, token string, body any) (*resty.Response, error)

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

func (cli *HttpClient) Get(url string, token string, body any) (*resty.Response, error)

Get sends an HTTP GET request to the given URL with the provided bearer token and request body, returning the response or an error.

func (*HttpClient) Post

func (cli *HttpClient) Post(url string, token string, body any) (*resty.Response, error)

Post sends an HTTP POST request to the given URL with the provided bearer token and JSON body, returning the response or an error.

func (*HttpClient) Put

func (cli *HttpClient) Put(url string, token string, body any) (*resty.Response, error)

Put sends an HTTP PUT request to the given URL with the provided bearer token and JSON body, returning the response or an error.

Directories

Path Synopsis
Package retryable wraps hashicorp/go-retryablehttp and exposes it as a standard *http.Client with configurable retry and timeout.
Package retryable wraps hashicorp/go-retryablehttp and exposes it as a standard *http.Client with configurable retry and timeout.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL