From 69673a1c69c497aca8e4b63989317b550c94249d Mon Sep 17 00:00:00 2001 From: Jens Deppe Date: Thu, 28 Sep 2017 20:12:05 -0700 Subject: [PATCH] Rework cookie usage --- src/fly-utils/fly-log/main.go | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/fly-utils/fly-log/main.go b/src/fly-utils/fly-log/main.go index 020e900..4d63a13 100644 --- a/src/fly-utils/fly-log/main.go +++ b/src/fly-utils/fly-log/main.go @@ -7,7 +7,6 @@ import ( "fmt" "net/http" "github.com/donovanhide/eventsource" - "net/http/cookiejar" "fly-utils/flyrc" "io/ioutil" "encoding/json" @@ -57,22 +56,12 @@ func main() { log.Fatal(err) } - cookies := []*http.Cookie{ - { - Name: "ATC-Authorization", - Value: fmt.Sprintf("Bearer %s", token), - }, + cookie := &http.Cookie{ + Name: "ATC-Authorization", + Value: fmt.Sprintf("Bearer %s", token), } - baseUrl, _ := url.Parse(fmt.Sprintf("%s://%s/", u.Scheme, u.Host)) - cookieJar, _ := cookiejar.New(nil) - cookieJar.SetCookies(baseUrl, cookies) - - client := &http.Client{ - Jar: cookieJar, - } - - build, err := getBuild(u, client) + build, err := getBuild(u, cookie) if err != nil { log.Fatal(err) } @@ -87,6 +76,9 @@ func main() { log.Fatal(err) } + request.AddCookie(cookie) + var client = &http.Client{} + stream, err := eventsource.SubscribeWith("", client, request) if err != nil { log.Fatal(fmt.Sprintf("Unable to subscribe to event source: %s", err)) @@ -120,11 +112,20 @@ func main() { } } -func getBuild(u *url.URL, client *http.Client) (*Build, error) { +func getBuild(u *url.URL, cookie *http.Cookie) (*Build, error) { apiUrl := u apiUrl.Path = fmt.Sprintf("/api/v1%s", apiUrl.Path) - resp, err := client.Get(apiUrl.String()) + req, err := http.NewRequest("GET", apiUrl.String(), nil) + if err != nil { + log.Fatal(err) + } + + req.AddCookie(cookie) + req.Header.Set("Accept", "*/*") + + var client = &http.Client{} + resp, err := client.Do(req) defer resp.Body.Close() if err != nil { log.Fatal(err)