Skip to content

Commit

Permalink
Moving UserAgent creation to clients initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
soareswallace committed Jan 23, 2025
1 parent 32ccb88 commit db1566d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 34 deletions.
39 changes: 20 additions & 19 deletions incognia.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Client struct {
tokenProvider TokenProvider
netClient *http.Client
endpoints *endpoints
UserAgent string
}

type IncogniaClientConfig struct {
Expand Down Expand Up @@ -131,14 +132,31 @@ func New(config *IncogniaClientConfig) (*Client, error) {
Timeout: tokenRouteTimeout,
})

libVersion := "unknown"
if buildInfo, ok := debug.ReadBuildInfo(); ok {
for _, dep := range buildInfo.Deps {
if dep.Path == "repo.incognia.com/go/incognia" {
libVersion = dep.Version
}
}
}

userAgent := fmt.Sprintf(
"incognia-api-go/%s (%s %s) Go/%s",
libVersion,
runtime.GOOS,
runtime.GOARCH,
runtime.Version(),
)

tokenProvider := config.TokenProvider
if tokenProvider == nil {
tokenProvider = NewAutoRefreshTokenProvider(tokenClient)
}

endpoints := getEndpoints()

return &Client{clientID: config.ClientID, clientSecret: config.ClientSecret, tokenProvider: tokenProvider, netClient: netClient, endpoints: &endpoints}, nil
return &Client{clientID: config.ClientID, clientSecret: config.ClientSecret, tokenProvider: tokenProvider, netClient: netClient, endpoints: &endpoints, UserAgent: userAgent}, nil
}

func (c *Client) RegisterSignup(installationID string, address *Address) (ret *SignupAssessment, err error) {
Expand Down Expand Up @@ -386,25 +404,8 @@ func (c *Client) registerLogin(login *Login) (*TransactionAssessment, error) {
}

func (c *Client) doRequest(request *http.Request, response interface{}) error {
libVersion := "unknown"
if buildInfo, ok := debug.ReadBuildInfo(); ok {
for _, dep := range buildInfo.Deps {
if dep.Path == "repo.incognia.com/go/incognia" {
libVersion = dep.Version
}
}
}

userAgent := fmt.Sprintf(
"incognia-api-go/%s (%s %s) Go/%s",
libVersion,
runtime.GOOS,
runtime.GOARCH,
runtime.Version(),
)

request.Header.Add("Content-Type", "application/json")
request.Header.Add("User-agent", userAgent)
request.Header.Add("User-Agent", c.UserAgent)

err := c.authorizeRequest(request)
if err != nil {
Expand Down
32 changes: 17 additions & 15 deletions token_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type TokenClient struct {
ClientSecret string
netClient *http.Client
tokenEndpoint string
UserAgent string
}

type TokenClientConfig struct {
Expand All @@ -40,20 +41,6 @@ func NewTokenClient(config *TokenClientConfig) *TokenClient {
timeout = tokenNetClientTimeout
}

return &TokenClient{
ClientID: config.ClientID,
ClientSecret: config.ClientSecret,
netClient: &http.Client{Timeout: timeout},
tokenEndpoint: incogniaEndpoints.Token,
}
}

func (tm TokenClient) requestToken() (Token, error) {
req, err := http.NewRequest("POST", tm.tokenEndpoint, nil)
if err != nil {
return nil, err
}

libVersion := "unknown"
if buildInfo, ok := debug.ReadBuildInfo(); ok {
for _, dep := range buildInfo.Deps {
Expand All @@ -71,9 +58,24 @@ func (tm TokenClient) requestToken() (Token, error) {
runtime.Version(),
)

return &TokenClient{
ClientID: config.ClientID,
ClientSecret: config.ClientSecret,
netClient: &http.Client{Timeout: timeout},
tokenEndpoint: incogniaEndpoints.Token,
UserAgent: userAgent,
}
}

func (tm TokenClient) requestToken() (Token, error) {
req, err := http.NewRequest("POST", tm.tokenEndpoint, nil)
if err != nil {
return nil, err
}

req.SetBasicAuth(tm.ClientID, tm.ClientSecret)
req.Header.Add("content-type", "application/x-www-form-urlencoded")
req.Header.Add("User-agent", userAgent)
req.Header.Add("User-Agent", tm.UserAgent)

res, err := tm.netClient.Do(req)
if err != nil {
Expand Down

0 comments on commit db1566d

Please sign in to comment.