Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func runAPIKey(cfg *config.Config) (*api.APIKeyResponse, error) {
return api.NewClient(cfg.EndpointURL, cfg.APIKey, cfg.Debug).GetAPIKey()
return newAPIClient(cfg).GetAPIKey()
}

var apiKeyCmd = &cobra.Command{
Expand Down
2 changes: 1 addition & 1 deletion cmd/auth_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func runAuthLogin(apiKey, name string, skipVerify bool) (*api.APIKeyResponse, er
}
return nil, nil
}
result, err := api.NewClient(config.EndpointURL(), apiKey, debugFlag).GetAPIKey()
result, err := newAPIClient(&config.Config{EndpointURL: config.EndpointURL(), APIKey: apiKey, Debug: debugFlag}).GetAPIKey()
if err != nil {
return nil, fmt.Errorf("API key verification failed: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/auth_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func runAuthStatus() (*config.Config, *api.APIKeyResponse, *config.PersistentCon
if err != nil {
return nil, nil, nil, err
}
keyResp, err := api.NewClient(cfg.EndpointURL, cfg.APIKey, cfg.Debug).GetAPIKey()
keyResp, err := newAPIClient(cfg).GetAPIKey()
if err != nil {
return nil, nil, nil, fmt.Errorf("API key verification failed: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/contact_properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
)

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

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

var contactPropertiesCmd = &cobra.Command{
Expand Down
8 changes: 4 additions & 4 deletions cmd/contacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func contactFieldParamsFromCmd(cmd *cobra.Command) (contactFieldParams, error) {
// find

func runContactsFind(cfg *config.Config, email, userID string) ([]api.Contact, error) {
return api.NewClient(cfg.EndpointURL, cfg.APIKey, cfg.Debug).FindContacts(api.FindContactParams{
return newAPIClient(cfg).FindContacts(api.FindContactParams{
Email: email,
UserID: userID,
})
Expand Down Expand Up @@ -149,7 +149,7 @@ type contactCreateResult struct {
}

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

var contactsCreateCmd = &cobra.Command{
Expand Down Expand Up @@ -196,7 +196,7 @@ var contactsCreateCmd = &cobra.Command{
// update

func runContactsUpdate(cfg *config.Config, req api.UpdateContactRequest) error {
return api.NewClient(cfg.EndpointURL, cfg.APIKey, cfg.Debug).UpdateContact(req)
return newAPIClient(cfg).UpdateContact(req)
}

var contactsUpdateCmd = &cobra.Command{
Expand Down Expand Up @@ -244,7 +244,7 @@ var contactsUpdateCmd = &cobra.Command{
// delete

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

var contactsDeleteCmd = &cobra.Command{
Expand Down
2 changes: 1 addition & 1 deletion cmd/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func parseMailingLists(pairs []string) (map[string]bool, error) {
}

func runEventsSend(cfg *config.Config, req api.SendEventRequest) error {
return api.NewClient(cfg.EndpointURL, cfg.APIKey, cfg.Debug).SendEvent(req)
return newAPIClient(cfg).SendEvent(req)
}

var eventsCmd = &cobra.Command{
Expand Down
2 changes: 1 addition & 1 deletion cmd/lists.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

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

var listsCmd = &cobra.Command{
Expand Down
6 changes: 6 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"os"

"github.com/loops-so/cli/internal/api"
"github.com/loops-so/cli/internal/config"
"github.com/spf13/cobra"
)
Expand All @@ -15,6 +16,11 @@ var outputFormat outputFlag = "text"
var teamFlag string
var debugFlag bool

func newAPIClient(cfg *config.Config) *api.Client {
return api.NewClient(cfg.EndpointURL, cfg.APIKey, cfg.Debug).
WithUserAgent("loops-cli/" + version)
}

func loadConfig() (*config.Config, error) {
cfg, err := config.Load(teamFlag)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/transactional.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func attachmentFromPath(path string) (api.Attachment, error) {
}

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

func runTransactionalSend(cfg *config.Config, req api.SendTransactionalRequest) error {
return api.NewClient(cfg.EndpointURL, cfg.APIKey, cfg.Debug).SendTransactional(req)
return newAPIClient(cfg).SendTransactional(req)
}

var transactionalCmd = &cobra.Command{
Expand Down
8 changes: 8 additions & 0 deletions internal/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Client struct {
apiKey string
httpClient *http.Client
debug bool
userAgent string
}

func NewClient(baseURL, apiKey string, debug bool) *Client {
Expand All @@ -44,9 +45,15 @@ func NewClient(baseURL, apiKey string, debug bool) *Client {
apiKey: apiKey,
httpClient: &http.Client{Timeout: 5 * time.Second},
debug: debug,
userAgent: "loops-go/dev",
}
}

func (c *Client) WithUserAgent(ua string) *Client {
c.userAgent = ua
return c
}

func errorFromResponse(resp *http.Response) *APIError {
var body struct {
Error string `json:"error"`
Expand Down Expand Up @@ -119,6 +126,7 @@ func (c *Client) newRequest(method, path string, body io.Reader) (*http.Request,
return nil, err
}
req.Header.Set("Authorization", "Bearer "+c.apiKey)
req.Header.Set("User-Agent", c.userAgent)
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
Expand Down
15 changes: 15 additions & 0 deletions internal/api/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,25 @@ func TestNewRequest(t *testing.T) {
if got := req.Header.Get("Authorization"); got != wantAuth {
t.Errorf("Authorization = %q, want %q", got, wantAuth)
}

if got := req.Header.Get("User-Agent"); got != "loops-go/dev" {
t.Errorf("User-Agent = %q, want %q", got, "loops-go/dev")
}
})
}
}

func TestWithUserAgent(t *testing.T) {
client := NewClient("https://example.com/api/v1", "test-key", false).WithUserAgent("loops-cli/1.2.3")
req, err := client.newRequest(http.MethodGet, "/api-key", nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got := req.Header.Get("User-Agent"); got != "loops-cli/1.2.3" {
t.Errorf("User-Agent = %q, want %q", got, "loops-cli/1.2.3")
}
}

func TestNewRequest_InvalidURL(t *testing.T) {
client := NewClient("://bad-url", "test-key", false)
_, err := client.newRequest(http.MethodGet, "/path", nil)
Expand Down