| 1 |
package proton |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"strconv" |
| 6 |
|
| 7 |
"github.com/go-resty/resty/v2" |
| 8 |
) |
| 9 |
|
| 10 |
func (c *Client) GetContact(ctx context.Context, contactID string) (Contact, error) { |
| 11 |
var res struct { |
| 12 |
Contact Contact |
| 13 |
} |
| 14 |
|
| 15 |
if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) { |
| 16 |
return r.SetResult(&res).Get("/contacts/v4/" + contactID) |
| 17 |
}); err != nil { |
| 18 |
return Contact{}, err |
| 19 |
} |
| 20 |
|
| 21 |
return res.Contact, nil |
| 22 |
} |
| 23 |
|
| 24 |
func (c *Client) CountContacts(ctx context.Context) (int, error) { |
| 25 |
var res struct { |
| 26 |
Total int |
| 27 |
} |
| 28 |
|
| 29 |
if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) { |
| 30 |
return r.SetResult(&res).Get("/contacts/v4") |
| 31 |
}); err != nil { |
| 32 |
return 0, err |
| 33 |
} |
| 34 |
|
| 35 |
return res.Total, nil |
| 36 |
} |
| 37 |
|
| 38 |
func (c *Client) CountContactEmails(ctx context.Context, email string) (int, error) { |
| 39 |
var res struct { |
| 40 |
Total int |
| 41 |
} |
| 42 |
|
| 43 |
if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) { |
| 44 |
return r.SetResult(&res).SetQueryParam("Email", email).Get("/contacts/v4/emails") |
| 45 |
}); err != nil { |
| 46 |
return 0, err |
| 47 |
} |
| 48 |
|
| 49 |
return res.Total, nil |
| 50 |
} |
| 51 |
|
| 52 |
func (c *Client) GetContacts(ctx context.Context, page, pageSize int) ([]Contact, error) { |
| 53 |
var res struct { |
| 54 |
Contacts []Contact |
| 55 |
} |
| 56 |
|
| 57 |
if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) { |
| 58 |
return r.SetQueryParams(map[string]string{ |
| 59 |
"Page": strconv.Itoa(page), |
| 60 |
"PageSize": strconv.Itoa(pageSize), |
| 61 |
}).SetResult(&res).Get("/contacts/v4") |
| 62 |
}); err != nil { |
| 63 |
return nil, err |
| 64 |
} |
| 65 |
|
| 66 |
return res.Contacts, nil |
| 67 |
} |
| 68 |
|
| 69 |
func (c *Client) GetAllContacts(ctx context.Context) ([]Contact, error) { |
| 70 |
total, err := c.CountContacts(ctx) |
| 71 |
if err != nil { |
| 72 |
return nil, err |
| 73 |
} |
| 74 |
|
| 75 |
return fetchPaged(ctx, total, maxPageSize, c, func(ctx context.Context, page, pageSize int) ([]Contact, error) { |
| 76 |
return c.GetContacts(ctx, page, pageSize) |
| 77 |
}) |
| 78 |
} |
| 79 |
|
| 80 |
func (c *Client) GetContactEmails(ctx context.Context, email string, page, pageSize int) ([]ContactEmail, error) { |
| 81 |
var res struct { |
| 82 |
ContactEmails []ContactEmail |
| 83 |
} |
| 84 |
|
| 85 |
if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) { |
| 86 |
return r.SetQueryParams(map[string]string{ |
| 87 |
"Page": strconv.Itoa(page), |
| 88 |
"PageSize": strconv.Itoa(pageSize), |
| 89 |
"Email": email, |
| 90 |
}).SetResult(&res).Get("/contacts/v4/emails") |
| 91 |
}); err != nil { |
| 92 |
return nil, err |
| 93 |
} |
| 94 |
|
| 95 |
return res.ContactEmails, nil |
| 96 |
} |
| 97 |
|
| 98 |
func (c *Client) GetAllContactEmails(ctx context.Context, email string) ([]ContactEmail, error) { |
| 99 |
total, err := c.CountContactEmails(ctx, email) |
| 100 |
if err != nil { |
| 101 |
return nil, err |
| 102 |
} |
| 103 |
|
| 104 |
return fetchPaged(ctx, total, maxPageSize, c, func(ctx context.Context, page, pageSize int) ([]ContactEmail, error) { |
| 105 |
return c.GetContactEmails(ctx, email, page, pageSize) |
| 106 |
}) |
| 107 |
} |
| 108 |
|
| 109 |
func (c *Client) CreateContacts(ctx context.Context, req CreateContactsReq) ([]CreateContactsRes, error) { |
| 110 |
var res struct { |
| 111 |
Responses []CreateContactsRes |
| 112 |
} |
| 113 |
|
| 114 |
if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) { |
| 115 |
return r.SetBody(req).SetResult(&res).Post("/contacts/v4") |
| 116 |
}); err != nil { |
| 117 |
return nil, err |
| 118 |
} |
| 119 |
|
| 120 |
return res.Responses, nil |
| 121 |
} |
| 122 |
|
| 123 |
func (c *Client) UpdateContact(ctx context.Context, contactID string, req UpdateContactReq) (Contact, error) { |
| 124 |
var res struct { |
| 125 |
Contact Contact |
| 126 |
} |
| 127 |
|
| 128 |
if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) { |
| 129 |
return r.SetBody(req).SetResult(&res).Put("/contacts/v4/" + contactID) |
| 130 |
}); err != nil { |
| 131 |
return Contact{}, err |
| 132 |
} |
| 133 |
|
| 134 |
return res.Contact, nil |
| 135 |
} |
| 136 |
|
| 137 |
func (c *Client) DeleteContacts(ctx context.Context, req DeleteContactsReq) error { |
| 138 |
return c.do(ctx, func(r *resty.Request) (*resty.Response, error) { |
| 139 |
return r.SetBody(req).Put("/contacts/v4/delete") |
| 140 |
}) |
| 141 |
} |