Skip to content

Commit

Permalink
Rework cookie usage
Browse files Browse the repository at this point in the history
  • Loading branch information
jdeppe-pivotal committed Sep 29, 2017
1 parent 9c45421 commit 69673a1
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions src/fly-utils/fly-log/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"net/http"
"github.com/donovanhide/eventsource"
"net/http/cookiejar"
"fly-utils/flyrc"
"io/ioutil"
"encoding/json"
Expand Down Expand Up @@ -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)
}
Expand All @@ -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))
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 69673a1

Please sign in to comment.