Skip to content

Commit

Permalink
cleanup chttp
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Feb 5, 2023
1 parent 06a9360 commit 8535817
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 19 deletions.
2 changes: 1 addition & 1 deletion auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
2 changes: 1 addition & 1 deletion cmd/slackdump/internal/diag/rawoutput.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
23 changes: 8 additions & 15 deletions internal/chttp/chttp.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/edge/edge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
}
Expand Down
2 changes: 1 addition & 1 deletion slackdump.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down

0 comments on commit 8535817

Please sign in to comment.