Skip to content

Commit

Permalink
Add appropriate user-agent string to requests
Browse files Browse the repository at this point in the history
  • Loading branch information
danlamanna committed Mar 25, 2024
1 parent a6c77f6 commit 41a65b1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
20 changes: 20 additions & 0 deletions zip_streamer/build_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package zip_streamer

import (
"runtime/debug"
)

func getVcsRevision() string {
buildInfo, ok := debug.ReadBuildInfo()
if !ok {
return "dev"
}

for _, setting := range buildInfo.Settings {
if setting.Key == "vcs.revision" {
return setting.Value[:8]
}
}

return "dev"
}
1 change: 1 addition & 0 deletions zip_streamer/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func retrieveZipDescriptorFromUrl(listfileUrl string, listfileBasicAuth string)
return nil, err
}
req.SetBasicAuth("", listfileBasicAuth)
req.Header.Set("User-Agent", fmt.Sprintf("isic-zipstreamer/%s", getVcsRevision()))
listfileResp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
Expand Down
8 changes: 7 additions & 1 deletion zip_streamer/zip_streamer.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ func retryableGet(url string) (*http.Response, error) {
for i := 0; i < NUM_RETRIES; i++ {
sleepDuration = time.Duration(math.Min(math.Pow(float64(2), float64(i)), float64(30))) * time.Second

resp, err := http.Get(url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", fmt.Sprintf("isic-zipstreamer/%s", getVcsRevision()))
resp, err := http.DefaultClient.Do(req)

if err != nil {
time.Sleep(sleepDuration)
} else if slices.Contains(retryableStatusCodes, resp.StatusCode) {
Expand Down

0 comments on commit 41a65b1

Please sign in to comment.