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