diff options
author | René 'Necoro' Neumann <necoro@necoro.eu> | 2022-01-08 20:08:36 +0100 |
---|---|---|
committer | René 'Necoro' Neumann <necoro@necoro.eu> | 2022-01-08 20:08:36 +0100 |
commit | 5c5ea3caa6770c89c9c3b8e8be6916e74e29f27e (patch) | |
tree | 130f59f0b659f9e23d6cd79a5dbc54e7910ae0b7 /internal/http | |
parent | 180aa45d40e5d58d579fcaba44b94bcb8e3e08be (diff) | |
download | feed2imap-go-5c5ea3caa6770c89c9c3b8e8be6916e74e29f27e.tar.gz feed2imap-go-5c5ea3caa6770c89c9c3b8e8be6916e74e29f27e.tar.bz2 feed2imap-go-5c5ea3caa6770c89c9c3b8e8be6916e74e29f27e.zip |
Introduce http.Context to bundle http specific parameters
Diffstat (limited to 'internal/http')
-rw-r--r-- | internal/http/client.go | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/internal/http/client.go b/internal/http/client.go index 230c333..17eb0cc 100644 --- a/internal/http/client.go +++ b/internal/http/client.go @@ -20,6 +20,11 @@ type Error struct { Status string } +type Context struct { + Timeout int + DisableTLS bool +} + func (err Error) Error() string { return fmt.Sprintf("http error: %s", err.Status) } @@ -35,8 +40,8 @@ func init() { unsafeClient = &http.Client{Transport: transport} } -func Context(timeout int) (ctxt.Context, ctxt.CancelFunc) { - return ctxt.WithTimeout(ctxt.Background(), time.Duration(timeout)*time.Second) +func (ctx Context) StdContext() (ctxt.Context, ctxt.CancelFunc) { + return ctxt.WithTimeout(ctxt.Background(), time.Duration(ctx.Timeout)*time.Second) } func client(disableTLS bool) *http.Client { @@ -48,9 +53,9 @@ func client(disableTLS bool) *http.Client { var noop ctxt.CancelFunc = func() {} -func Get(url string, timeout int, disableTLS bool) (resp *http.Response, cancel ctxt.CancelFunc, err error) { +func Get(url string, ctx Context) (resp *http.Response, cancel ctxt.CancelFunc, err error) { prematureExit := true - ctx, ctxCancel := Context(timeout) + stdCtx, ctxCancel := ctx.StdContext() cancel = func() { if resp != nil { @@ -65,13 +70,13 @@ func Get(url string, timeout int, disableTLS bool) (resp *http.Response, cancel } }() - req, err := http.NewRequestWithContext(ctx, "GET", url, nil) + req, err := http.NewRequestWithContext(stdCtx, "GET", url, nil) if err != nil { return nil, noop, err } req.Header.Set("User-Agent", "Feed2Imap-Go/1.0") - resp, err = client(disableTLS).Do(req) + resp, err = client(ctx.DisableTLS).Do(req) if err != nil { return nil, noop, err } |