Skip to content

Commit 6a95df0

Browse files
committed
Fix the response according to API V2
1 parent 0ac1ebd commit 6a95df0

File tree

4 files changed

+47
-68
lines changed

4 files changed

+47
-68
lines changed

contact.go

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,17 @@ type ContactListRequest struct {
2929

3030
// ContactCreateResponse represents the response from creating or updating a contact
3131
type ContactCreateResponse struct {
32-
Success bool `json:"success"`
33-
Data Contact `json:"data"`
34-
}
35-
36-
// ContactDeleteResponse represents the response from deleting a contact
37-
type ContactDeleteResponse struct {
38-
Success bool `json:"success"`
39-
Data struct {
40-
Message string `json:"message"`
41-
} `json:"data"`
32+
Contact
33+
Meta struct {
34+
IsNew bool `json:"isNew"`
35+
IsUpdate bool `json:"isUpdate"`
36+
} `json:"_meta"`
4237
}
4338

4439
// ContactListResponse represents the response from listing contacts
4540
type ContactListResponse struct {
46-
Success bool `json:"success"`
47-
Data struct {
48-
Items []Contact `json:"items"`
49-
NextCursor string `json:"nextCursor"`
50-
HasMore bool `json:"hasMore"`
51-
Total int `json:"total"`
52-
} `json:"data"`
41+
Contacts []Contact `json:"contacts"`
42+
Total int `json:"total"`
43+
HasMore bool `json:"hasMore"`
44+
NextCursor *string `json:"nextCursor"`
5345
}

contact_service.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,18 @@ func (service *contactService) Create(ctx context.Context, params *ContactCreate
3434
// Delete a contact permanently by ID
3535
//
3636
// API Docs: https://next-wiki.useplunk.com/api-reference/contacts/deleteContact
37-
func (service *contactService) Delete(ctx context.Context, contactID string) (*ContactDeleteResponse, *Response, error) {
37+
func (service *contactService) Delete(ctx context.Context, contactID string) (*Response, error) {
3838
request, err := service.client.newRequest(ctx, http.MethodDelete, "/contacts/"+contactID, nil)
3939
if err != nil {
40-
return nil, nil, err
40+
return nil, err
4141
}
4242

4343
response, err := service.client.do(request)
4444
if err != nil {
45-
return nil, response, err
46-
}
47-
48-
status := new(ContactDeleteResponse)
49-
if err = json.Unmarshal(*response.Body, status); err != nil {
50-
return nil, response, err
45+
return response, err
5146
}
5247

53-
return status, response, nil
48+
return response, nil
5449
}
5550

5651
// List a paginated list of contacts with cursor-based pagination

contact_service_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,15 @@ func TestContactService_CreateWithError(t *testing.T) {
6666
func TestContactService_Delete(t *testing.T) {
6767
// Arrange
6868
apiKey := "test-api-key"
69-
server := helpers.MakeTestServer(http.StatusOK, stubs.ContactsDeleteResponse())
69+
server := helpers.MakeTestServer(http.StatusNoContent, nil)
7070
client := New(WithBaseURL(server.URL), WithSecretKey(apiKey))
7171

7272
// Act
73-
contact, response, err := client.Contacts.Delete(context.Background(), "contact-id")
73+
response, err := client.Contacts.Delete(context.Background(), "contact-id")
7474

7575
// Assert
7676
assert.Nil(t, err)
77-
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode)
78-
jsonContent, _ := json.Marshal(contact)
79-
assert.JSONEq(t, string(stubs.ContactsDeleteResponse()), string(jsonContent))
77+
assert.Equal(t, http.StatusNoContent, response.HTTPResponse.StatusCode)
8078

8179
// Teardown
8280
server.Close()
@@ -89,7 +87,7 @@ func TestContactService_DeleteWithError(t *testing.T) {
8987
client := New(WithBaseURL(server.URL), WithSecretKey(apiKey))
9088

9189
// Act
92-
_, response, err := client.Contacts.Delete(context.Background(), "contact-id")
90+
response, err := client.Contacts.Delete(context.Background(), "contact-id")
9391

9492
// Assert
9593
assert.NotNil(t, err)

internal/stubs/contact.go

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,20 @@ package stubs
44
func ContactsCreateResponse() []byte {
55
return []byte(`
66
{
7-
"success": true,
8-
"data": {
9-
"id": "string",
10-
"email": "[email protected]",
11-
"subscribed": true,
12-
"data": {},
13-
"createdAt": "2019-08-24T14:15:22Z",
14-
"updatedAt": "2019-08-24T14:15:22Z"
15-
}
16-
}
17-
`)
18-
}
19-
20-
// ContactsDeleteResponse returns a stubbed response for deleting a contact
21-
func ContactsDeleteResponse() []byte {
22-
return []byte(`
23-
{
24-
"success": true,
25-
"data": {
26-
"message": "string"
27-
}
7+
"id": "string",
8+
"email": "[email protected]",
9+
"subscribed": true,
10+
"data": {
11+
"firstName": "John",
12+
"lastName": "Doe",
13+
"plan": "premium"
14+
},
15+
"createdAt": "2019-08-24T14:15:22Z",
16+
"updatedAt": "2019-08-24T14:15:22Z",
17+
"_meta": {
18+
"isNew":false,
19+
"isUpdate":false
20+
}
2821
}
2922
`)
3023
}
@@ -33,22 +26,23 @@ func ContactsDeleteResponse() []byte {
3326
func ContactsListResponse() []byte {
3427
return []byte(`
3528
{
36-
"success": true,
37-
"data": {
38-
"items": [
39-
{
40-
"id": "string",
41-
"email": "[email protected]",
42-
"subscribed": true,
43-
"data": {},
44-
"createdAt": "2019-08-24T14:15:22Z",
45-
"updatedAt": "2019-08-24T14:15:22Z"
46-
}
29+
"contacts": [
30+
{
31+
"id": "string",
32+
"email": "[email protected]",
33+
"subscribed": true,
34+
"data": {
35+
"firstName": "John",
36+
"lastName": "Doe",
37+
"plan": "premium"
38+
},
39+
"createdAt": "2019-08-24T14:15:22Z",
40+
"updatedAt": "2019-08-24T14:15:22Z"
41+
}
4742
],
48-
"nextCursor": "string",
49-
"hasMore": true,
50-
"total": 0
51-
}
43+
"nextCursor": null,
44+
"hasMore": false,
45+
"total": 1
5246
}
5347
`)
5448
}

0 commit comments

Comments
 (0)