Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Lint

on:
push:
branches: [master, main]
pull_request:
branches: [master, main]

jobs:
golangci:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/[email protected]

- name: Install Go
uses: actions/[email protected]
with:
go-version-file: go.mod
cache-dependency-path: go.sum

- name: Lint
uses: golangci/[email protected]
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
version: v2.2.1
33 changes: 16 additions & 17 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,27 @@ name: Testing

on:
push:
branches: [ master ]
branches: [master]
pull_request:
branches: [ master ]
branches: [master]

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install Go
uses: actions/setup-go@v5
with:
go-version: "1.20.x"

- name: Checkout Code
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.18
- name: Test
run: go test -v -timeout 30m -race ./... -coverprofile=coverage.txt -covermode=atomic

- name: Vet
run: go vet ./...
- name: Test
run: go test -v -timeout 30m -race ./... -coverprofile=coverage.txt -covermode=atomic

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
slug: wenchy/requests
35 changes: 35 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
version: "2"
linters:
default: none
enable:
- errcheck
- govet
# - gosec
- ineffassign
- staticcheck
- unused
- misspell
exclusions:
# presets:
# - comments
# - common-false-positives
# - legacy
# - std-error-handling
rules:
- linters:
- staticcheck
text: "QF1008:" # could remove embedded field "XXX" from selector (staticcheck)
- linters:
- staticcheck
text: "QF1007:" # could merge conditional assignment into variable declaration (staticcheck)
- linters:
- staticcheck
text: "QF1001:" # could apply De Morgan's law (staticcheck)
- linters:
- staticcheck
text: "QF1006:" # could lift into loop condition (staticcheck)
formatters:
enable:
- gofmt
- gofumpt
28 changes: 24 additions & 4 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,23 @@
// caches them for reuse by subsequent calls. It uses HTTP proxies as
// directed by the environment variables HTTP_PROXY, HTTPS_PROXY and
// NO_PROXY (or the lowercase versions thereof).
transport *http.Transport
transport *http.Transport
// hostRoundTrippers specify the host-specific RoundTripper to use for the
// request. If not found, http.DefaultTransport is used.
hostRoundTrippers map[string]http.RoundTripper
// interceptor intercepts each HTTP request.
interceptor InterceptorFunc
}

var env environment

func init() {
env.timeout = 60 * time.Second // default timeout
env.transport = http.DefaultTransport.(*http.Transport).Clone() // default transport
env.timeout = 60 * time.Second // default timeout
transport, ok := http.DefaultTransport.(*http.Transport)
if !ok {
panic("Ooh! http.DefaultTransport's underlying is not *http.Transport. Maybe golang team has changed it.")

Check warning on line 29 in env.go

View check run for this annotation

Codecov / codecov/patch

env.go#L29

Added line #L29 was not covered by tests
}
env.transport = transport
}

// SetEnvTimeout sets the default timeout for each HTTP request at
Expand Down Expand Up @@ -53,7 +61,7 @@
}
}

// getChainDo recursively generates the chained do.
// getChainDo generates the chained do recursively.
func getChainDo(interceptors []InterceptorFunc, curr int, finalDo Do) Do {
if curr == len(interceptors)-1 {
return finalDo
Expand All @@ -62,3 +70,15 @@
return interceptors[curr+1](ctx, r, getChainDo(interceptors, curr+1, finalDo))
}
}

// SetHostTransport sets the host-specific RoundTripper to use for the request.
//
// # Example
//
// SetHostTransport(map[string]http.RoundTripper{
// "example1.com": http.DefaultTransport,
// "example2.com": MyCustomTransport,
// })
func SetHostTransport(rts map[string]http.RoundTripper) {
env.hostRoundTrippers = rts
}
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module github.com/Wenchy/requests

go 1.18
go 1.20

require github.com/stretchr/testify v1.9.0

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.9.0
gopkg.in/yaml.v3 v3.0.1 // indirect
)
45 changes: 28 additions & 17 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,32 @@ type Options struct {
DumpRequestOut *string
DumpResponse *string

// custom interceptor
// interceptor
Interceptor InterceptorFunc
// round tripper
RoundTripper http.RoundTripper
}

// Option is the functional option type.
type Option func(*Options)

// newDefaultOptions creates a new default HTTP options.
func newDefaultOptions() *Options {
return &Options{
Headers: http.Header{},
bodyType: bodyTypeDefault,
Timeout: env.timeout,
}
}

func parseOptions(options ...Option) *Options {
opts := newDefaultOptions()
for _, setter := range options {
setter(opts)
}
return opts
}

// Context sets the HTTP request context.
//
// For outgoing client request, the context controls the entire lifetime of
Expand Down Expand Up @@ -351,27 +370,19 @@ func Dump(req, resp *string) Option {
}
}

// Interceptor specifies a custom interceptor, which is prepended to environment
// interceptors for current request only.
// Interceptor prepends an interceptor to environment interceptors for current
// request only.
func Interceptor(interceptor InterceptorFunc) Option {
return func(opts *Options) {
opts.Interceptor = interceptor
}
}

// newDefaultOptions creates a new default HTTP options.
func newDefaultOptions() *Options {
return &Options{
Headers: http.Header{},
bodyType: bodyTypeDefault,
Timeout: env.timeout,
}
}

func parseOptions(options ...Option) *Options {
opts := newDefaultOptions()
for _, setter := range options {
setter(opts)
// Transport specifies a custom RoundTripper for current request only.
//
// NOTE: If specified, then option DisableKeepAlives() will not work.
func Transport(rt http.RoundTripper) Option {
return func(opts *Options) {
opts.RoundTripper = rt
}
return opts
}
26 changes: 19 additions & 7 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,30 @@ func do(method, url string, opts *Options, body []byte) (*Response, error) {
//
// - https://stackoverflow.com/questions/57683132/turning-off-connection-pool-for-go-http-client
// - https://stackoverflow.com/questions/59656164/what-is-the-difference-between-net-dialerkeepalive-and-http-transportidletimeo
transport := env.transport
if opts.DisableKeepAlives {
// If option DisableKeepAlives set as true, then clone a new transport
// just for this one-off HTTP request.
transport = env.transport.Clone()
transport.DisableKeepAlives = true
var roundTripper http.RoundTripper
if opts.RoundTripper != nil {
roundTripper = opts.RoundTripper
} else {
if rt := env.hostRoundTrippers[req.Host]; rt != nil {
// Use the host-specific RoundTripper if set.
roundTripper = rt
} else if opts.DisableKeepAlives {
// If option DisableKeepAlives set as true, then clone a new transport
// just for this one-off HTTP request.
transport := env.transport.Clone()
transport.DisableKeepAlives = true
roundTripper = transport
} else {
// If option DisableKeepAlives not set as true, then use the default
// transport.
roundTripper = env.transport
}
}
client := &Client{
Client: &http.Client{
CheckRedirect: redirector.RedirectPolicyFunc,
Timeout: opts.Timeout,
Transport: transport,
Transport: roundTripper,
},
}
var ctx context.Context
Expand Down
Loading
Loading