Skip to content

Commit

Permalink
chore: add tests and chnage to upsert
Browse files Browse the repository at this point in the history
  • Loading branch information
robgordon89 committed Dec 19, 2024
1 parent 6387815 commit a6e5cf6
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 13 deletions.
11 changes: 6 additions & 5 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func main() {
}
```

### Create a subscriber
### Create/Upsert a subscriber

```go
package main
Expand All @@ -192,14 +192,14 @@ func main() {

ctx := context.TODO()

subscriber := &mailerlite.Subscriber{
subscriber := &mailerlite.UpsertSubscriber{
Email: "[email protected]",
Fields: map[string]interface{}{
"city": "Vilnius",
},
}

newSubscriber, _, err := client.Subscriber.Create(ctx, subscriber)
newSubscriber, _, err := client.Subscriber.Upsert(ctx, subscriber)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -227,14 +227,15 @@ func main() {

ctx := context.TODO()

subscriber := &mailerlite.Subscriber{
subscriber := &mailerlite.UpdateSubscriber{
ID: "1",
Email: "[email protected]",
Fields: map[string]interface{}{
"company": "MailerLite",
},
}

newSubscriber, _, err := client.Subscriber.Create(ctx, subscriber)
newSubscriber, _, err := client.Subscriber.Update(ctx, subscriber)
if err != nil {
log.Fatal(err)
}
Expand Down
30 changes: 23 additions & 7 deletions subscribers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ type SubscriberService interface {
List(ctx context.Context, options *ListSubscriberOptions) (*RootSubscribers, *Response, error)
Count(ctx context.Context) (*Count, *Response, error)
Get(ctx context.Context, options *GetSubscriberOptions) (*RootSubscriber, *Response, error)
Create(ctx context.Context, subscriber *SubscriberToCreate) (*RootSubscriber, *Response, error)
Update(ctx context.Context, subscriber *Subscriber) (*RootSubscriber, *Response, error)
Create(ctx context.Context, subscriber *Subscriber) (*RootSubscriber, *Response, error)
Upsert(ctx context.Context, subscriber *UpsertSubscriber) (*RootSubscriber, *Response, error)
Update(ctx context.Context, subscriber *UpdateSubscriber) (*RootSubscriber, *Response, error)
Delete(ctx context.Context, subscriberID string) (*Response, error)
Forget(ctx context.Context, subscriberID string) (*RootSubscriber, *Response, error)
}
Expand Down Expand Up @@ -61,7 +62,10 @@ type Subscriber struct {
OptinIP string `json:"optin_ip,omitempty"`
}

type SubscriberToCreate struct {
type UpdateSubscriber UpsertSubscriber

type UpsertSubscriber struct {
ID string `json:"id,omitempty"`
Email string `json:"email,omitempty"`
Status string `json:"status,omitempty"`
IPAddress interface{} `json:"ip_address,omitempty"`
Expand Down Expand Up @@ -139,7 +143,8 @@ func (s *subscriberService) Get(ctx context.Context, options *GetSubscriberOptio
return root, res, nil
}

func (s *subscriberService) Create(ctx context.Context, subscriber *SubscriberToCreate) (*RootSubscriber, *Response, error) {
// Deprecated: use Upsert instead
func (s *subscriberService) Create(ctx context.Context, subscriber *Subscriber) (*RootSubscriber, *Response, error) {
req, err := s.client.newRequest(http.MethodPost, subscriberEndpoint, subscriber)
if err != nil {
return nil, nil, err
Expand All @@ -154,11 +159,22 @@ func (s *subscriberService) Create(ctx context.Context, subscriber *SubscriberTo
return root, res, nil
}

func (s *subscriberService) Upsert(ctx context.Context, subscriber *SubscriberToCreate) (*RootSubscriber, *Response, error) {
return s.Create(ctx, subscriber)
func (s *subscriberService) Upsert(ctx context.Context, subscriber *UpsertSubscriber) (*RootSubscriber, *Response, error) {
req, err := s.client.newRequest(http.MethodPost, subscriberEndpoint, subscriber)
if err != nil {
return nil, nil, err
}

root := new(RootSubscriber)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}

return root, res, nil
}

func (s *subscriberService) Update(ctx context.Context, subscriber *Subscriber) (*RootSubscriber, *Response, error) {
func (s *subscriberService) Update(ctx context.Context, subscriber *UpdateSubscriber) (*RootSubscriber, *Response, error) {
path := fmt.Sprintf("%s/%s", subscriberEndpoint, subscriber.ID)

req, err := s.client.newRequest(http.MethodPut, path, subscriber)
Expand Down
65 changes: 64 additions & 1 deletion subscribers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func TestCanForgetSubscrber(t *testing.T) {
assert.Equal(t, res.StatusCode, http.StatusOK)
}

func TestCanCreateSubscrberWithGroup(t *testing.T) {
func TestCanCreateSubscrberWithGroupDeprecated(t *testing.T) {
client := mailerlite.NewClient(testKey)

testClient := NewTestClient(func(req *http.Request) *http.Response {
Expand Down Expand Up @@ -198,3 +198,66 @@ func TestCanCreateSubscrberWithGroup(t *testing.T) {

assert.Equal(t, res.StatusCode, http.StatusOK)
}

func TestCanUpsertSubscrberWithGroup(t *testing.T) {
client := mailerlite.NewClient(testKey)

testClient := NewTestClient(func(req *http.Request) *http.Response {
assert.Equal(t, req.Method, http.MethodPost)
assert.Equal(t, req.URL.String(), "https://connect.mailerlite.com/api/subscribers")
b, _ := io.ReadAll(req.Body)
assert.Equal(t, strings.TrimRight(string(b), "\r\n"), `{"email":"[email protected]","groups":["1234"]}`)
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(`OK`)),
}
})

ctx := context.TODO()

client.SetHttpClient(testClient)

options := &mailerlite.UpsertSubscriber{
Email: "[email protected]",
Groups: []string{"1234"},
}

_, res, err := client.Subscriber.Upsert(ctx, options)
if err != nil {
return
}

assert.Equal(t, res.StatusCode, http.StatusOK)
}

func TestCanUpdateSubscrberWithGroup(t *testing.T) {
client := mailerlite.NewClient(testKey)

testClient := NewTestClient(func(req *http.Request) *http.Response {
assert.Equal(t, req.Method, http.MethodPut)
assert.Equal(t, req.URL.String(), "https://connect.mailerlite.com/api/subscribers/1")
b, _ := io.ReadAll(req.Body)
assert.Equal(t, strings.TrimRight(string(b), "\r\n"), `{"id":"1","email":"[email protected]","groups":["1234"]}`)
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(`OK`)),
}
})

ctx := context.TODO()

client.SetHttpClient(testClient)

options := &mailerlite.UpdateSubscriber{
ID: "1",
Email: "[email protected]",
Groups: []string{"1234"},
}

_, res, err := client.Subscriber.Update(ctx, options)
if err != nil {
return
}

assert.Equal(t, res.StatusCode, http.StatusOK)
}

0 comments on commit a6e5cf6

Please sign in to comment.