|
1
|
|
|
package client |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"context" |
|
5
|
|
|
"encoding/json" |
|
6
|
|
|
"net/http" |
|
7
|
|
|
) |
|
8
|
|
|
|
|
9
|
|
|
// contactService is the API client for the `/contacts` endpoint |
|
10
|
|
|
type contactService service |
|
11
|
|
|
|
|
12
|
|
|
// Create a new contact or update existing (upsert by email) |
|
13
|
|
|
// |
|
14
|
|
|
// API Docs: https://next-wiki.useplunk.com/api-reference/contacts/createContact |
|
15
|
|
|
func (service *contactService) Create(ctx context.Context, params *ContactCreateRequest) (*ContactCreateResponse, *Response, error) { |
|
16
|
|
|
request, err := service.client.newRequestWithSecretKey(ctx, http.MethodPost, "/contacts", params) |
|
17
|
|
|
if err != nil { |
|
18
|
|
|
return nil, nil, err |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
response, err := service.client.do(request) |
|
22
|
|
|
if err != nil { |
|
23
|
|
|
return nil, response, err |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
contact := new(ContactCreateResponse) |
|
27
|
|
|
if err = json.Unmarshal(*response.Body, contact); err != nil { |
|
28
|
|
|
return nil, response, err |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
return contact, response, nil |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
// Delete a contact permanently by ID |
|
35
|
|
|
// |
|
36
|
|
|
// API Docs: https://next-wiki.useplunk.com/api-reference/contacts/deleteContact |
|
37
|
|
|
func (service *contactService) Delete(ctx context.Context, contactID string) (*Response, error) { |
|
38
|
|
|
request, err := service.client.newRequestWithSecretKey(ctx, http.MethodDelete, "/contacts/"+contactID, nil) |
|
39
|
|
|
if err != nil { |
|
40
|
|
|
return nil, err |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
response, err := service.client.do(request) |
|
44
|
|
|
if err != nil { |
|
45
|
|
|
return response, err |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return response, nil |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
// List a paginated list of contacts with cursor-based pagination |
|
52
|
|
|
// |
|
53
|
|
|
// API Docs: https://next-wiki.useplunk.com/api-reference/contacts/listContacts |
|
54
|
|
|
func (service *contactService) List(ctx context.Context, params map[string]string) (*ContactListResponse, *Response, error) { |
|
55
|
|
|
request, err := service.client.newRequestWithSecretKey(ctx, http.MethodGet, "/contacts", nil) |
|
56
|
|
|
if err != nil { |
|
57
|
|
|
return nil, nil, err |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
response, err := service.client.do(service.client.addURLParams(request, params)) |
|
61
|
|
|
if err != nil { |
|
62
|
|
|
return nil, response, err |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
contacts := new(ContactListResponse) |
|
66
|
|
|
if err = json.Unmarshal(*response.Body, contacts); err != nil { |
|
67
|
|
|
return nil, response, err |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return contacts, response, nil |
|
71
|
|
|
} |
|
72
|
|
|
|