From 85358171b3fd3c44e78f82e4dbd684d84eb4be74 Mon Sep 17 00:00:00 2001 From: Rustam Gilyazov <16064414+rusq@users.noreply.github.com> Date: Sun, 5 Feb 2023 11:21:15 +1000 Subject: [PATCH] cleanup chttp --- auth/auth.go | 2 +- cmd/slackdump/internal/diag/rawoutput.go | 2 +- internal/chttp/chttp.go | 23 ++++++++--------------- internal/edge/edge.go | 2 +- slackdump.go | 2 +- 5 files changed, 12 insertions(+), 19 deletions(-) diff --git a/auth/auth.go b/auth/auth.go index cda71596..976d6035 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -131,7 +131,7 @@ func (s simpleProvider) Test(ctx context.Context) error { ctx, task := trace.NewTask(ctx, "TestAuth") defer task.End() - httpCl := chttp.NewWithToken(s.Token, "https://slack.com", chttp.ConvertCookies(s.Cookie)) + httpCl := chttp.New("https://slack.com", chttp.ConvertCookies(s.Cookie)) cl := slack.New(s.Token, slack.OptionHTTPClient(httpCl)) diff --git a/cmd/slackdump/internal/diag/rawoutput.go b/cmd/slackdump/internal/diag/rawoutput.go index c1a73a10..c7af0a87 100644 --- a/cmd/slackdump/internal/diag/rawoutput.go +++ b/cmd/slackdump/internal/diag/rawoutput.go @@ -88,7 +88,7 @@ func run(ctx context.Context, p params) error { if err != nil { return err } - cl := chttp.New(domain, chttp.ConvertCookies(prov.Cookies()), chttp.NewTransport(nil)) + cl := chttp.NewWithTransport(domain, chttp.ConvertCookies(prov.Cookies()), chttp.NewTransport(nil)) if err := saveOutput(ctx, cl, p.output, prov.SlackToken(), sl); err != nil { return err } diff --git a/internal/chttp/chttp.go b/internal/chttp/chttp.go index c917512f..4b126f5a 100644 --- a/internal/chttp/chttp.go +++ b/internal/chttp/chttp.go @@ -1,5 +1,5 @@ -// Package chttp provides some convenience function to wrap the standard http -// Client. +// Package chttp (Cooked HTTP) provides a wrapper around http.Client with +// cookies. package chttp import ( @@ -10,8 +10,9 @@ import ( "golang.org/x/net/publicsuffix" ) -// New inits the HTTP client with cookies. -func New(cookieDomain string, cookies []*http.Cookie, rt http.RoundTripper) *http.Client { +// NewWithTransport inits the HTTP client with cookies. It allows to use +// the custom Transport. +func NewWithTransport(cookieDomain string, cookies []*http.Cookie, rt http.RoundTripper) *http.Client { jar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List}) url, err := url.Parse(cookieDomain) if err != nil { @@ -25,17 +26,9 @@ func New(cookieDomain string, cookies []*http.Cookie, rt http.RoundTripper) *htt return &cl } -// NewWithToken returns the HTTP client with cookies, that augments requests -// with slack token. -func NewWithToken(token string, cookieDomain string, cookies []*http.Cookie) *http.Client { - tr := NewTransport(nil) - tr.BeforeReq = func(req *http.Request) { - // req.V - // if req.Method == http.MethodGet { - // req.Form.Add("token", token) - // } - } - return New(cookieDomain, cookies, tr) +// New returns the HTTP client with cookies and default transport. +func New(cookieDomain string, cookies []*http.Cookie) *http.Client { + return NewWithTransport(cookieDomain, cookies, NewTransport(nil)) } func sliceOfPtr[T any](cc []T) []*T { diff --git a/internal/edge/edge.go b/internal/edge/edge.go index c393a0ca..ad51db90 100644 --- a/internal/edge/edge.go +++ b/internal/edge/edge.go @@ -21,7 +21,7 @@ type Client struct { func New(teamID string, token string, cookies []*http.Cookie) *Client { return &Client{ - cl: chttp.NewWithToken(token, "https://slack.com", cookies), + cl: chttp.New("https://slack.com", cookies), token: token, apiPath: fmt.Sprintf("https://edgeapi.slack.com/cache/%s/", teamID)} } diff --git a/slackdump.go b/slackdump.go index 42ddba4a..e4c811a1 100644 --- a/slackdump.go +++ b/slackdump.go @@ -113,7 +113,7 @@ func New(ctx context.Context, prov auth.Provider, cfg Config, opts ...Option) (* return nil, fmt.Errorf("auth provider validation error: %s", err) } - httpCl := chttp.NewWithToken(prov.SlackToken(), "https://slack.com", chttp.ConvertCookies(prov.Cookies())) + httpCl := chttp.New("https://slack.com", chttp.ConvertCookies(prov.Cookies())) cl := slack.New(prov.SlackToken(), slack.OptionHTTPClient(httpCl))