Skip to content

Commit 316c0ca

Browse files
authored
upgrade go, functional and safety enhancements, misc updates (#30)
* upgrade go, functional and safety enhancements, misc updates * too soon i guess * revert function removal
1 parent 346e080 commit 316c0ca

22 files changed

Lines changed: 321 additions & 129 deletions

.golangci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ formatters:
4141
extra-rules: true
4242
goimports:
4343
local-prefixes:
44-
- github.com/theopenlane/httpsling
44+
- github.com/theopenlane/httpsling

.pre-commit-config.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ default_language_version:
55

66
repos:
77
- repo: https://github.com/pre-commit/pre-commit-hooks
8-
rev: v5.0.0
8+
rev: v6.0.0
99
hooks:
1010
- id: trailing-whitespace
1111
- id: detect-private-key
1212
- repo: https://github.com/google/yamlfmt
13-
rev: v0.15.0
13+
rev: v0.17.2
1414
hooks:
1515
- id: yamlfmt
16-
- repo: https://github.com/crate-ci/typos
17-
rev: v1.29.4
16+
- repo: https://github.com/adhtruong/mirrors-typos
17+
rev: v1.38.1
1818
hooks:
1919
- id: typos

README.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[![Build status](https://badge.buildkite.com/f74a461120ffcadbf7796d5aac8ae8c03a1cbcfda142220074.svg)](https://buildkite.com/theopenlane/httpsling)
1+
[![Build status](https://badge.buildkite.com/f74a461120ffcadbf7796d5aac8ae8c03a1cbcfda142220074.svg)](https://buildkite.com/theopenlane/httpsling?branch=main)
22
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=theopenlane_httpsling&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=theopenlane_httpsling)
33
[![Go Report Card](https://goreportcard.com/badge/github.com/theopenlane/httpsling)](https://goreportcard.com/report/github.com/theopenlane/httpsling)
44
[![Go Reference](https://pkg.go.dev/badge/github.com/theopenlane/httpsling.svg)](https://pkg.go.dev/github.com/theopenlane/httpsling)
@@ -33,7 +33,7 @@ func main() {
3333
}
3434

3535
// Perform a GET request
36-
var out map[string]interface{}
36+
var out map[string]any
3737
resp, err := requester.Receive(&out, httpsling.Get("resource"))
3838
if err != nil {
3939
log.Fatal(err)
@@ -56,9 +56,17 @@ RequestWithContext(context.Context, ...Option) (*http.Request, error)
5656
Send(...Option) (*http.Response, error)
5757
SendWithContext(context.Context, ...Option) (*http.Response, error)
5858

59-
// build and send the request and parse the response into an interface
60-
Receive(interface{}, ...Option) (*http.Response, []byte, error)
61-
ReceiveWithContext(context.Context, interface{}, ...Option) (*http.Response, error)
59+
// build and send the request and parse the response into a value
60+
Receive(any, ...Option) (*http.Response, error)
61+
ReceiveWithContext(context.Context, any, ...Option) (*http.Response, error)
62+
63+
// typed helper using generics
64+
ReceiveInto[T any](...Option) (*http.Response, T, error)
65+
ReceiveIntoWithContext[T any](context.Context, ...Option) (*http.Response, T, error)
66+
67+
// stream response body into an io.Writer
68+
ReceiveTo(io.Writer, ...Option) (*http.Response, int64, error)
69+
ReceiveToWithContext(context.Context, io.Writer, ...Option) (*http.Response, int64, error)
6270
```
6371

6472
### Configuring BaseURL
@@ -130,7 +138,7 @@ The library provides a `Receive` to construct and dispatch HTTP. Here are exampl
130138
```go
131139
resp, err := requester.ReceiveWithContext(context.Background(), &out,
132140
httpsling.Post("/path"),
133-
httpsling.Body(map[string]interface{}{"key": "value"})
141+
httpsling.Body(map[string]any{"key": "value"})
134142
)
135143
```
136144

@@ -139,7 +147,7 @@ The library provides a `Receive` to construct and dispatch HTTP. Here are exampl
139147
```go
140148
resp, err := requester.ReceiveWithContext(context.Background(), &out,
141149
httpsling.Put("/path/123456"),
142-
httpsling.Body(map[string]interface{}{"key": "newValue"})
150+
httpsling.Body(map[string]any{"key": "newValue"})
143151
)
144152
```
145153

@@ -187,6 +195,8 @@ log.Printf("Status Code: %d\n", resp.StatusCode)
187195
log.Printf("Response Data: %s\n", out.Data)
188196
```
189197

198+
Note: Receive fully reads the response into memory to unmarshal, and restores `resp.Body` so it remains readable by callers.
199+
190200
### Evaluating Response Success
191201

192202
To assess whether the HTTP request was successful:
@@ -210,4 +220,4 @@ This library was inspired by and built upon the work of several other HTTP clien
210220

211221
## Contributing
212222

213-
See [contributing](.github/CONTRIBUTING.md) for details.
223+
See [contributing](.github/CONTRIBUTING.md) for details.

echo/context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ func (a *EchoContextAdapter) Err() error {
3939

4040
// Value implements the Value method of the context.Context interface
4141
// used to retrieve a value associated with a specific key from the context
42-
func (a *EchoContextAdapter) Value(key interface{}) interface{} {
42+
func (a *EchoContextAdapter) Value(key any) any {
4343
return a.c.Get(fmt.Sprintf("%v", key))
4444
}

example_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@ import (
1313
)
1414

1515
func Example() {
16-
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
17-
w.Header().Set("Content-Type", "application/json")
18-
w.WriteHeader(200)
19-
w.Write([]byte(`{"color":"red"}`))
20-
}))
21-
defer s.Close()
22-
23-
var out map[string]string
24-
resp, _ := Receive(
25-
out,
26-
Get(s.URL),
27-
)
28-
29-
fmt.Println(resp.StatusCode)
30-
fmt.Printf("%s", out)
16+
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
17+
w.Header().Set("Content-Type", "application/json")
18+
w.WriteHeader(200)
19+
w.Write([]byte(`{"color":"red"}`))
20+
}))
21+
defer s.Close()
22+
23+
var out map[string]string
24+
resp, _ := Receive(
25+
&out,
26+
Get(s.URL),
27+
)
28+
29+
fmt.Println(resp.StatusCode)
30+
fmt.Printf("%v", out)
3131
}
3232

3333
func Example_receive() {

files.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func MimeTypeValidator(validMimeTypes ...string) ValidationFunc {
6565
}
6666
}
6767

68-
// ChainValidators returns a validator that accepts multiple validating criteras
68+
// ChainValidators returns a validator that accepts multiple validating criteria
6969
func ChainValidators(validators ...ValidationFunc) ValidationFunc {
7070
return func(f File) error {
7171
for _, validator := range validators {

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
module github.com/theopenlane/httpsling
22

3-
go 1.23.5
3+
go 1.25.1
44

55
require (
66
github.com/felixge/httpsnoop v1.0.4
77
github.com/google/go-querystring v1.1.0
88
github.com/stretchr/testify v1.11.1
9-
github.com/theopenlane/utils v0.4.4
9+
github.com/theopenlane/utils v0.5.2
1010
)
1111

1212
require (
@@ -20,6 +20,6 @@ require (
2020
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
2121
github.com/mazrean/formstream v1.1.2
2222
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
23-
github.com/theopenlane/echox v0.2.1
23+
github.com/theopenlane/echox v0.2.4
2424
gopkg.in/yaml.v3 v3.0.1 // indirect
2525
)

go.sum

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@ github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRI
1313
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1414
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
1515
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
16-
github.com/theopenlane/echox v0.2.1 h1:ZhVkimmWxpKITf67oM57SrLWeIdnV8+dNXlC+VzlRaQ=
17-
github.com/theopenlane/echox v0.2.1/go.mod h1:4j/Hx0uoLk5gVzdA83Qqz7xBEmqpoEP+OnzVaw2p6/o=
18-
github.com/theopenlane/utils v0.4.4 h1:4Xb2T+4bjMtf4OL73bWQ1a8zllTt43ryVflRzVaUgmU=
19-
github.com/theopenlane/utils v0.4.4/go.mod h1:lNzPjqQoDM5565s5FRqkmBGO77twAkY3Hxgd38ESo6I=
16+
github.com/theopenlane/echox v0.2.4 h1:bocz1Dfs7d2fkNa8foQqdmeTtkMTQNwe1v20bIGIDps=
17+
github.com/theopenlane/echox v0.2.4/go.mod h1:0cPOHe4SSQHmqP0/n2LsIEzRSogkxSX653bE+PIOVZ8=
18+
github.com/theopenlane/utils v0.5.2 h1:5Hpg+lgSGxBZwirh9DQumTHCBU9Wgopjp7Oug2FA+1c=
19+
github.com/theopenlane/utils v0.5.2/go.mod h1:d7F811pRS817S9wo9SmsSghS5GDgN32BFn6meMM9PM0=
2020
go.uber.org/mock v0.5.0 h1:KAMbZvZPyBPWgD14IrIQ38QCyjwpvVVV6K/bHl1IwQU=
2121
go.uber.org/mock v0.5.0/go.mod h1:ge71pBPLYDk7QIi1LupWxdAykm7KIEFchiOqd6z7qMM=
2222
golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0=
2323
golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
24-
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
25-
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
24+
golang.org/x/net v0.43.0 h1:lat02VYK2j4aLzMzecihNvTlJNQUq316m2Mr9rnM6YE=
25+
golang.org/x/net v0.43.0/go.mod h1:vhO1fvI4dGsIjh73sWfUVjj3N7CA9WkKJNQm2svM6Jg=
2626
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
2727
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
28-
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
29-
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
28+
golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0=
29+
golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU=
3030
golang.org/x/tools v0.22.0 h1:gqSGLZqv+AI9lIQzniJ0nZDRG5GBPsSi+DRNHWNz6yA=
3131
golang.org/x/tools v0.22.0/go.mod h1:aCwcsjqvq7Yqt6TNyX7QMU2enbQ/Gt0bo6krSeEri+c=
3232
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

httpclient/client.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,17 @@ func Apply(c *http.Client, opts ...Option) error {
2727
}
2828

2929
func newDefaultTransport() *http.Transport {
30-
return &http.Transport{
31-
Proxy: http.ProxyFromEnvironment,
32-
DialContext: (&net.Dialer{
33-
Timeout: 30 * time.Second, // nolint: mnd
34-
KeepAlive: 30 * time.Second, // nolint: mnd
35-
DualStack: true,
36-
}).DialContext,
37-
MaxIdleConns: 100, // nolint: mnd
38-
IdleConnTimeout: 90 * time.Second, // nolint: mnd
39-
TLSHandshakeTimeout: 10 * time.Second, // nolint: mnd
40-
ExpectContinueTimeout: 1 * time.Second, // nolint: mnd
41-
}
30+
return &http.Transport{
31+
Proxy: http.ProxyFromEnvironment,
32+
DialContext: (&net.Dialer{
33+
Timeout: 30 * time.Second, // nolint: mnd
34+
KeepAlive: 30 * time.Second, // nolint: mnd
35+
}).DialContext,
36+
MaxIdleConns: 100, // nolint: mnd
37+
IdleConnTimeout: 90 * time.Second, // nolint: mnd
38+
TLSHandshakeTimeout: 10 * time.Second, // nolint: mnd
39+
ExpectContinueTimeout: 1 * time.Second, // nolint: mnd
40+
}
4241
}
4342

4443
// Option is a configuration option for building an http.Client

httptestutil/dump.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func DumpToStdout(ts *httptest.Server) {
6464
Dump(ts, os.Stdout)
6565
}
6666

67-
type logFunc func(a ...interface{})
67+
type logFunc func(a ...any)
6868

6969
// Write implements io.Writer
7070
func (f logFunc) Write(p []byte) (n int, err error) {
@@ -74,6 +74,6 @@ func (f logFunc) Write(p []byte) (n int, err error) {
7474
}
7575

7676
// DumpToLog writes requests and responses to a logging function
77-
func DumpToLog(ts *httptest.Server, logf func(a ...interface{})) {
77+
func DumpToLog(ts *httptest.Server, logf func(a ...any)) {
7878
Dump(ts, logFunc(logf))
7979
}

0 commit comments

Comments
 (0)