|
1 /* |
|
2 Copyright 2017-2018 Mikael Berthe |
|
3 |
|
4 Licensed under the MIT license. Please see the LICENSE file is this directory. |
|
5 */ |
|
6 |
|
7 package madon |
|
8 |
|
9 import ( |
|
10 "github.com/sendgrid/rest" |
|
11 ) |
|
12 |
|
13 // GetBlockedDomains returns the current user blocked domains |
|
14 // If lopt.All is true, several requests will be made until the API server |
|
15 // has nothing to return. |
|
16 func (mc *Client) GetBlockedDomains(lopt *LimitParams) ([]DomainName, error) { |
|
17 const endPoint = "domain_blocks" |
|
18 var links apiLinks |
|
19 var domains []DomainName |
|
20 if err := mc.apiCall("v1/"+endPoint, rest.Get, nil, lopt, &links, &domains); err != nil { |
|
21 return nil, err |
|
22 } |
|
23 if lopt != nil { // Fetch more pages to reach our limit |
|
24 var domainSlice []DomainName |
|
25 for (lopt.All || lopt.Limit > len(domains)) && links.next != nil { |
|
26 newlopt := links.next |
|
27 links = apiLinks{} |
|
28 if err := mc.apiCall("v1/"+endPoint, rest.Get, nil, newlopt, &links, &domainSlice); err != nil { |
|
29 return nil, err |
|
30 } |
|
31 domains = append(domains, domainSlice...) |
|
32 domainSlice = domainSlice[:0] // Clear struct |
|
33 } |
|
34 } |
|
35 return domains, nil |
|
36 } |
|
37 |
|
38 // BlockDomain blocks the specified domain |
|
39 func (mc *Client) BlockDomain(domain DomainName) error { |
|
40 const endPoint = "domain_blocks" |
|
41 params := make(apiCallParams) |
|
42 params["domain"] = string(domain) |
|
43 return mc.apiCall("v1/"+endPoint, rest.Post, params, nil, nil, nil) |
|
44 } |
|
45 |
|
46 // UnblockDomain unblocks the specified domain |
|
47 func (mc *Client) UnblockDomain(domain DomainName) error { |
|
48 const endPoint = "domain_blocks" |
|
49 params := make(apiCallParams) |
|
50 params["domain"] = string(domain) |
|
51 return mc.apiCall("v1/"+endPoint, rest.Delete, params, nil, nil, nil) |
|
52 } |