Skip to content

Commit

Permalink
Set a user agent on all requests (#2)
Browse files Browse the repository at this point in the history
In case Bahnhof wants to block us, now they can.
  • Loading branch information
daenney authored Mar 30, 2022
1 parent 2f2eae6 commit f980001
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions bahnhof.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ import (

const base = "https://bahnhof.se/kundservice/driftinfo"
const api = "https://bahnhof.se/ajax/kundservice/driftinfo"
const userAgent = "bahnboom (+https://github.com/daenney/bahnboom)"

type transport struct{}

func (*transport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Set("User-Agent", userAgent)
return http.DefaultTransport.RoundTrip(req)
}

var client = &http.Client{Transport: &transport{}}

type response struct {
Status string `json:"status"`
Expand Down Expand Up @@ -120,7 +130,7 @@ func tokens(ctx context.Context) (e error, cookie *http.Cookie, csrf string) {
return err, cookie, csrf
}

resp, err := http.DefaultClient.Do(req)
resp, err := client.Do(req)
if err != nil {
return err, cookie, csrf
}
Expand Down Expand Up @@ -190,7 +200,7 @@ func issues(ctx context.Context, cookie *http.Cookie, csrf string) (error, []ent
req.Header.Set("X-CSRF-TOKEN", csrf)
req.Header.Set("X-Requested-With", "XMLHttpRequest")

resp, err := http.DefaultClient.Do(req)
resp, err := client.Do(req)
if err != nil {
return err, nil
}
Expand Down

0 comments on commit f980001

Please sign in to comment.