Skip to content

Commit

Permalink
Merge pull request #89 from TimRots/BMA-6273/add-user-agent
Browse files Browse the repository at this point in the history
BMA-6273: add default User-Agent to sdk
  • Loading branch information
majidkarimizadeh committed Jul 13, 2023
2 parents dcb2014 + 2889989 commit 314097f
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ import (
var lswClient *leasewebClient

const DEFAULT_BASE_URL = "https://api.leaseweb.com"
const DEFAULT_VERSION = "dev"
const DEFAULT_USER_AGENT = "leaseweb-go-sdk/" + DEFAULT_VERSION

type leasewebClient struct {
client *http.Client
apiKey string
baseUrl string
apiKey string
baseUrl string
client *http.Client
userAgent string
}

type ApiContext struct {
Expand Down Expand Up @@ -59,14 +62,24 @@ func (erre *EncodingError) Error() string {

func InitLeasewebClient(key string) {
lswClient = &leasewebClient{
client: &http.Client{},
apiKey: key,
client: &http.Client{},
apiKey: key,
userAgent: DEFAULT_USER_AGENT,
}
}
func SetUserAgent(userAgent string) {
lswClient.userAgent = userAgent
}

func SetBaseUrl(baseUrl string) {
lswClient.baseUrl = baseUrl
}
func getUserAgent() string {
if lswClient.userAgent != "" {
return DEFAULT_USER_AGENT + " " + lswClient.userAgent
}
return DEFAULT_USER_AGENT
}

func getBaseUrl() string {
if lswClient.baseUrl != "" {
Expand Down Expand Up @@ -104,6 +117,7 @@ func doRequest(ctx context.Context, method, path, query string, args ...interfac
}

req.Header.Add("x-lsw-auth", lswClient.apiKey)
req.Header.Set("User-Agent", getUserAgent())
resp, err := lswClient.client.Do(req)
if err != nil {
return err
Expand Down

0 comments on commit 314097f

Please sign in to comment.