Skip to content

Commit c806037

Browse files
authored
chore(loo-4798): set user-agent header (#31)
1 parent b3b16d2 commit c806037

11 files changed

Lines changed: 42 additions & 13 deletions

File tree

cmd/api_key.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
func runAPIKey(cfg *config.Config) (*api.APIKeyResponse, error) {
12-
return api.NewClient(cfg.EndpointURL, cfg.APIKey, cfg.Debug).GetAPIKey()
12+
return newAPIClient(cfg).GetAPIKey()
1313
}
1414

1515
var apiKeyCmd = &cobra.Command{

cmd/auth_login.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func runAuthLogin(apiKey, name string, skipVerify bool) (*api.APIKeyResponse, er
6666
}
6767
return nil, nil
6868
}
69-
result, err := api.NewClient(config.EndpointURL(), apiKey, debugFlag).GetAPIKey()
69+
result, err := newAPIClient(&config.Config{EndpointURL: config.EndpointURL(), APIKey: apiKey, Debug: debugFlag}).GetAPIKey()
7070
if err != nil {
7171
return nil, fmt.Errorf("API key verification failed: %w", err)
7272
}

cmd/auth_status.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func runAuthStatus() (*config.Config, *api.APIKeyResponse, *config.PersistentCon
4747
if err != nil {
4848
return nil, nil, nil, err
4949
}
50-
keyResp, err := api.NewClient(cfg.EndpointURL, cfg.APIKey, cfg.Debug).GetAPIKey()
50+
keyResp, err := newAPIClient(cfg).GetAPIKey()
5151
if err != nil {
5252
return nil, nil, nil, fmt.Errorf("API key verification failed: %w", err)
5353
}

cmd/contact_properties.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import (
99
)
1010

1111
func runContactPropertiesList(cfg *config.Config, customOnly bool) ([]api.ContactProperty, error) {
12-
return api.NewClient(cfg.EndpointURL, cfg.APIKey, cfg.Debug).ListContactProperties(customOnly)
12+
return newAPIClient(cfg).ListContactProperties(customOnly)
1313
}
1414

1515
func runContactPropertiesCreate(cfg *config.Config, name, propType string) error {
16-
return api.NewClient(cfg.EndpointURL, cfg.APIKey, cfg.Debug).CreateContactProperty(name, propType)
16+
return newAPIClient(cfg).CreateContactProperty(name, propType)
1717
}
1818

1919
var contactPropertiesCmd = &cobra.Command{

cmd/contacts.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func contactFieldParamsFromCmd(cmd *cobra.Command) (contactFieldParams, error) {
7777
// find
7878

7979
func runContactsFind(cfg *config.Config, email, userID string) ([]api.Contact, error) {
80-
return api.NewClient(cfg.EndpointURL, cfg.APIKey, cfg.Debug).FindContacts(api.FindContactParams{
80+
return newAPIClient(cfg).FindContacts(api.FindContactParams{
8181
Email: email,
8282
UserID: userID,
8383
})
@@ -149,7 +149,7 @@ type contactCreateResult struct {
149149
}
150150

151151
func runContactsCreate(cfg *config.Config, req api.CreateContactRequest) (string, error) {
152-
return api.NewClient(cfg.EndpointURL, cfg.APIKey, cfg.Debug).CreateContact(req)
152+
return newAPIClient(cfg).CreateContact(req)
153153
}
154154

155155
var contactsCreateCmd = &cobra.Command{
@@ -196,7 +196,7 @@ var contactsCreateCmd = &cobra.Command{
196196
// update
197197

198198
func runContactsUpdate(cfg *config.Config, req api.UpdateContactRequest) error {
199-
return api.NewClient(cfg.EndpointURL, cfg.APIKey, cfg.Debug).UpdateContact(req)
199+
return newAPIClient(cfg).UpdateContact(req)
200200
}
201201

202202
var contactsUpdateCmd = &cobra.Command{
@@ -244,7 +244,7 @@ var contactsUpdateCmd = &cobra.Command{
244244
// delete
245245

246246
func runContactsDelete(cfg *config.Config, email, userID string) error {
247-
return api.NewClient(cfg.EndpointURL, cfg.APIKey, cfg.Debug).DeleteContact(email, userID)
247+
return newAPIClient(cfg).DeleteContact(email, userID)
248248
}
249249

250250
var contactsDeleteCmd = &cobra.Command{

cmd/events.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func parseMailingLists(pairs []string) (map[string]bool, error) {
3535
}
3636

3737
func runEventsSend(cfg *config.Config, req api.SendEventRequest) error {
38-
return api.NewClient(cfg.EndpointURL, cfg.APIKey, cfg.Debug).SendEvent(req)
38+
return newAPIClient(cfg).SendEvent(req)
3939
}
4040

4141
var eventsCmd = &cobra.Command{

cmd/lists.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
func runListsList(cfg *config.Config) ([]api.MailingList, error) {
12-
return api.NewClient(cfg.EndpointURL, cfg.APIKey, cfg.Debug).ListMailingLists()
12+
return newAPIClient(cfg).ListMailingLists()
1313
}
1414

1515
var listsCmd = &cobra.Command{

cmd/root.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"os"
99

10+
"github.com/loops-so/cli/internal/api"
1011
"github.com/loops-so/cli/internal/config"
1112
"github.com/spf13/cobra"
1213
)
@@ -15,6 +16,11 @@ var outputFormat outputFlag = "text"
1516
var teamFlag string
1617
var debugFlag bool
1718

19+
func newAPIClient(cfg *config.Config) *api.Client {
20+
return api.NewClient(cfg.EndpointURL, cfg.APIKey, cfg.Debug).
21+
WithUserAgent("loops-cli/" + version)
22+
}
23+
1824
func loadConfig() (*config.Config, error) {
1925
cfg, err := config.Load(teamFlag)
2026
if err != nil {

cmd/transactional.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func attachmentFromPath(path string) (api.Attachment, error) {
6464
}
6565

6666
func runTransactionalList(cfg *config.Config, params api.PaginationParams) ([]api.TransactionalEmail, error) {
67-
client := api.NewClient(cfg.EndpointURL, cfg.APIKey, cfg.Debug)
67+
client := newAPIClient(cfg)
6868
if params.Cursor != "" {
6969
emails, _, err := client.ListTransactional(params)
7070
return emails, err
@@ -78,7 +78,7 @@ func runTransactionalList(cfg *config.Config, params api.PaginationParams) ([]ap
7878
}
7979

8080
func runTransactionalSend(cfg *config.Config, req api.SendTransactionalRequest) error {
81-
return api.NewClient(cfg.EndpointURL, cfg.APIKey, cfg.Debug).SendTransactional(req)
81+
return newAPIClient(cfg).SendTransactional(req)
8282
}
8383

8484
var transactionalCmd = &cobra.Command{

internal/api/client.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type Client struct {
3636
apiKey string
3737
httpClient *http.Client
3838
debug bool
39+
userAgent string
3940
}
4041

4142
func NewClient(baseURL, apiKey string, debug bool) *Client {
@@ -44,9 +45,15 @@ func NewClient(baseURL, apiKey string, debug bool) *Client {
4445
apiKey: apiKey,
4546
httpClient: &http.Client{Timeout: 5 * time.Second},
4647
debug: debug,
48+
userAgent: "loops-go/dev",
4749
}
4850
}
4951

52+
func (c *Client) WithUserAgent(ua string) *Client {
53+
c.userAgent = ua
54+
return c
55+
}
56+
5057
func errorFromResponse(resp *http.Response) *APIError {
5158
var body struct {
5259
Error string `json:"error"`
@@ -119,6 +126,7 @@ func (c *Client) newRequest(method, path string, body io.Reader) (*http.Request,
119126
return nil, err
120127
}
121128
req.Header.Set("Authorization", "Bearer "+c.apiKey)
129+
req.Header.Set("User-Agent", c.userAgent)
122130
if body != nil {
123131
req.Header.Set("Content-Type", "application/json")
124132
}

0 commit comments

Comments
 (0)