Skip to content

Commit

Permalink
Replace httpx with Go's standard HTTP client
Browse files Browse the repository at this point in the history
Signed-off-by: James Pond <james@cipher.host>
  • Loading branch information
jamesponddotco committed Jan 25, 2024
1 parent 1c094c1 commit 2e293f6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
25 changes: 14 additions & 11 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ import (
"path/filepath"
"strings"

"git.sr.ht/~jamesponddotco/httpx-go"
"git.sr.ht/~jamesponddotco/xstd-go/xerrors"
"git.sr.ht/~jamesponddotco/xstd-go/xnet/xhttp"
"git.sr.ht/~jamesponddotco/xstd-go/xstrings"
"golang.org/x/time/rate"
)

const (
Expand All @@ -26,7 +25,7 @@ type (
// Client is the LanguageTool API client.
Client struct {
// httpc is the underlying HTTP client used by the API client.
httpc *httpx.Client
httpc *http.Client

// cfg specifies the configuration used by the API client.
cfg *Config
Expand All @@ -45,12 +44,16 @@ func NewClient(cfg *Config) (*Client, error) {
return nil, err
}

retryPolicy := &xhttp.RetryPolicy{
IsRetryable: xhttp.DefaultIsRetryable,
MaxRetries: cfg.MaxRetries,
MinRetryDelay: xhttp.DefaultMinRetryDelay,
MaxRetryDelay: xhttp.DefaultMaxRetryDelay,
}

return &Client{
httpc: &httpx.Client{
RateLimiter: rate.NewLimiter(rate.Limit(2), 1),
RetryPolicy: httpx.DefaultRetryPolicy(),
},
cfg: cfg,
httpc: xhttp.NewRetryingClient(cfg.Timeout, retryPolicy, cfg.Logger),
cfg: cfg,
}, nil
}

Expand Down Expand Up @@ -160,14 +163,14 @@ func (c *Client) Delete(ctx context.Context, path, filename string) (*Response,
}

// do performs an HTTP request using the underlying HTTP client.
func (c *Client) do(ctx context.Context, req *http.Request) (*Response, error) {
ret, err := c.httpc.Do(ctx, req)
func (c *Client) do(_ context.Context, req *http.Request) (*Response, error) {
ret, err := c.httpc.Do(req)
if err != nil {
return nil, fmt.Errorf("%w", err)
}

defer func() {
if err = httpx.DrainResponseBody(ret); err != nil {
if err = xhttp.DrainResponseBody(ret); err != nil {
log.Fatal(err)
}
}()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.21

require (
git.sr.ht/~jamesponddotco/httpx-go v0.0.0-20230427215504-7c26a7f028e7
git.sr.ht/~jamesponddotco/xstd-go v0.0.0-20230709232003-22489c0e7382
git.sr.ht/~jamesponddotco/xstd-go v0.7.1
golang.org/x/time v0.3.0
)

Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ git.sr.ht/~jamesponddotco/recache-go v1.0.1 h1:O9S7SdGyMh4mD+Vom0WOkY45EhJJHDRBl
git.sr.ht/~jamesponddotco/recache-go v1.0.1/go.mod h1:oF6LkAuwZYQqHe8+G/4hP9ZSNyDjAk6J8qhuy44wXw0=
git.sr.ht/~jamesponddotco/xstd-go v0.0.0-20230709232003-22489c0e7382 h1:3LjkxT6zVDvIlaOc5riiRAnAKVgpTWHxWgmzpzOuR8A=
git.sr.ht/~jamesponddotco/xstd-go v0.0.0-20230709232003-22489c0e7382/go.mod h1:0tqdK5/MZYSPxAiwtG4LlVfdQ+iaFoksU/FTIGQ/v/Y=
git.sr.ht/~jamesponddotco/xstd-go v0.7.0 h1:fwcjXnmTzEsrHXPXMvFFyz+6d/xyDSNShbyKBdXz0eM=
git.sr.ht/~jamesponddotco/xstd-go v0.7.0/go.mod h1:L0SjmhDqcj/gR7oeNof+ed6l9VPk6oHPeNQSoaFBRFk=
git.sr.ht/~jamesponddotco/xstd-go v0.7.1 h1:MqkIWlzLwBOVLf9W2qLVF5m/QnJzuNiKA/4P+ditvnk=
git.sr.ht/~jamesponddotco/xstd-go v0.7.1/go.mod h1:L0SjmhDqcj/gR7oeNof+ed6l9VPk6oHPeNQSoaFBRFk=
golang.org/x/crypto v0.8.0 h1:pd9TJtTueMTVQXzk8E2XESSMQDj/U7OUu0PqJqPXQjQ=
golang.org/x/crypto v0.8.0/go.mod h1:mRqEX+O9/h5TFCrQhkgjo2yKi0yYA+9ecGkdQoHrywE=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
Expand Down

0 comments on commit 2e293f6

Please sign in to comment.