Skip to content

Commit

Permalink
misc: allow user agent to be passed to the globalping client (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
radulucut authored Aug 9, 2024
1 parent cf74203 commit e87cb79
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 20 deletions.
5 changes: 5 additions & 0 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/icza/backscanner"
"github.com/jsdelivr/globalping-cli/globalping"
"github.com/jsdelivr/globalping-cli/version"
"github.com/shirou/gopsutil/process"
)

Expand Down Expand Up @@ -321,3 +322,7 @@ func silenceUsageOnCreateMeasurementError(err error) bool {
}
return true
}

func getUserAgent() string {
return fmt.Sprintf("globalping-cli/v%s (https://github.com/jsdelivr/globalping-cli)", version.Version)
}
6 changes: 6 additions & 0 deletions cmd/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"
"testing"

"github.com/jsdelivr/globalping-cli/version"
"github.com/jsdelivr/globalping-cli/view"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -200,3 +201,8 @@ func Test_FindAndRemoveResolver_ResolverOnly(t *testing.T) {
assert.Equal(t, "1.1.1.1", resolver)
assert.Equal(t, []string{"example.com"}, argsWithoutResolver)
}

func TestUserAgent(t *testing.T) {
version.Version = "x.y.z"
assert.Equal(t, "globalping-cli/vx.y.z (https://github.com/jsdelivr/globalping-cli)", getUserAgent())
}
5 changes: 3 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ func Execute() {
}
t := time.NewTicker(10 * time.Second)
globalpingClient := globalping.NewClientWithCacheCleanup(globalping.Config{
APIURL: config.GlobalpingAPIURL,
APIToken: config.GlobalpingToken,
APIURL: config.GlobalpingAPIURL,
APIToken: config.GlobalpingToken,
UserAgent: getUserAgent(),
}, t, 30)
globalpingProbe := probe.NewProbe()
viewer := view.NewViewer(ctx, printer, utime, globalpingClient)
Expand Down
13 changes: 8 additions & 5 deletions globalping/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ type Client interface {
}

type Config struct {
APIURL string
APIToken string
APIURL string
APIToken string
UserAgent string
}

type CacheEntry struct {
Expand All @@ -40,6 +41,7 @@ type client struct {
apiURL string
apiToken string
apiResponseCacheExpireSeconds int64
userAgent string
}

// NewClient creates a new client with the given configuration.
Expand All @@ -50,9 +52,10 @@ func NewClient(config Config) Client {
http: &http.Client{
Timeout: 30 * time.Second,
},
apiURL: config.APIURL,
apiToken: config.APIToken,
cache: map[string]*CacheEntry{},
apiURL: config.APIURL,
apiToken: config.APIToken,
userAgent: config.UserAgent,
cache: map[string]*CacheEntry{},
}
}

Expand Down
9 changes: 2 additions & 7 deletions globalping/globalping.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/andybalholm/brotli"
"github.com/jsdelivr/globalping-cli/utils"
"github.com/jsdelivr/globalping-cli/version"
)

var (
Expand All @@ -30,7 +29,7 @@ func (c *client) CreateMeasurement(measurement *MeasurementCreate) (*Measurement
if err != nil {
return nil, &MeasurementError{Message: "failed to create request - please report this bug"}
}
req.Header.Set("User-Agent", userAgent())
req.Header.Set("User-Agent", c.userAgent)
req.Header.Set("Accept-Encoding", "br")
req.Header.Set("Content-Type", "application/json")

Expand Down Expand Up @@ -146,7 +145,7 @@ func (c *client) GetMeasurementRaw(id string) ([]byte, error) {
return nil, &MeasurementError{Message: "failed to create request"}
}

req.Header.Set("User-Agent", userAgent())
req.Header.Set("User-Agent", c.userAgent)
req.Header.Set("Accept-Encoding", "br")

etag := c.getETag(id)
Expand Down Expand Up @@ -292,7 +291,3 @@ func DecodeHTTPTLS(tls json.RawMessage) (*HTTPTLSCertificate, error) {
}
return t, nil
}

func userAgent() string {
return fmt.Sprintf("globalping-cli/v%s (https://github.com/jsdelivr/globalping-cli)", version.Version)
}
6 changes: 0 additions & 6 deletions globalping/globalping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"testing"

"github.com/andybalholm/brotli"
"github.com/jsdelivr/globalping-cli/version"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -859,11 +858,6 @@ func TestFetchWithBrotli(t *testing.T) {
assert.Equal(t, id, m.ID)
}

func TestUserAgent(t *testing.T) {
version.Version = "x.y.z"
assert.Equal(t, "globalping-cli/vx.y.z (https://github.com/jsdelivr/globalping-cli)", userAgent())
}

// Generate server for testing
func generateServer(json string, statusCode int) *httptest.Server {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit e87cb79

Please sign in to comment.