Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alesr/httpclient
A Go package for easy configuration of HTTP client settings including timeouts, connection options, and HTTP/2 configuration.
https://github.com/alesr/httpclient
Last synced: about 2 months ago
JSON representation
A Go package for easy configuration of HTTP client settings including timeouts, connection options, and HTTP/2 configuration.
- Host: GitHub
- URL: https://github.com/alesr/httpclient
- Owner: alesr
- License: mit
- Created: 2023-08-02T19:54:51.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2023-11-17T23:49:10.000Z (about 1 year ago)
- Last Synced: 2024-05-21T04:15:55.329Z (8 months ago)
- Language: Go
- Size: 6.84 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# HTTP Client
This package is designed to simplify the configuration of timeouts, connection options, and other settings for Go's HTTP client. It supports a variety of options, including dialer timeout, keep-alive duration, TLS handshake timeout, response header timeout, idle connection timeout, maximum idle connections, and HTTP/2 configuration.
## Installation
You can import this package into your Go project using go get:
```bash
go get github.com/alesr/httpclient
```## Usage
First, import the package into your Go file:
```go
import "github.com/alesr/httpclient"
```You can create a new HTTP client with default settings as follows:
```go
client := httpclient.New()
```You can modify the default configuration using the options provided by the library:
```go
client := httpclient.New(
httpclient.WithTimeout(10 * time.Second),
httpclient.WithDialerTimeout(5 * time.Second),
httpclient.WithDialerKeepAlive(15 * time.Second),
httpclient.WithTLSHandshakeTimeout(5 * time.Second),
httpclient.WithResponseHeaderTimeout(10 * time.Second),
httpclient.WithIdleConnTimeout(15 * time.Second),
httpclient.WithMaxIdleConns(50),
httpclient.WithForceHTTP2Disabled(),
)
```Each option modifies a different part of the HTTP client configuration:
- `WithTimeout`: sets the request timeout duration.
- `WithDialerTimeout`: sets the dialer timeout duration.
- `WithDialerKeepAlive`: sets the dialer keep-alive duration.
- `WithTLSHandshakeTimeout`: sets the TLS handshake timeout duration.
- `WithResponseHeaderTimeout`: sets the response header timeout duration.
- `WithIdleConnTimeout`: sets the idle connection timeout duration.
- `WithMaxIdleConns`: sets the maximum number of idle connections.
- `WithForceHTTP2Disabled`: disables HTTP2 for the transport of the HTTP client.### Custom Transport
To use a custom transport, provide it using the WithTransport option. Note that when using a custom transport, other transport-specific options (like WithTLSHandshakeTimeout) will not apply. Ensure that your custom transport is fully configured before passing it:
```go
customTransport := &http.Transport{
// Custom transport settings...
}
client := httpclient.New(
httpclient.WithTransport(customTransport),
)
```### Using the Client
After creating the client, you can use it to make HTTP requests as you would with the standard `net/http` package:
```go
resp, err := client.Get("http://example.com")
// handle error, use response...
```