domain.go
author Mikael Berthe <mikael@lilotux.net>
Mon, 29 Jul 2019 21:57:47 +0200
changeset 253 0e8c8026cf40
parent 238 1c0042e76902
permissions -rw-r--r--
Update dependencies
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
187
e1b1f4a168b7 Add GetDomainBlocks, DomainBlock and DomainUnblock
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     1
/*
207
301d5b94be3f Update copyrights
Mikael Berthe <mikael@lilotux.net>
parents: 191
diff changeset
     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
b3f9331551b7 Rename Domain methods
Mikael Berthe <mikael@lilotux.net>
parents: 187
diff changeset
    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
b3f9331551b7 Rename Domain methods
Mikael Berthe <mikael@lilotux.net>
parents: 187
diff changeset
    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
238
1c0042e76902 Do not use a global API version
Mikael Berthe <mikael@lilotux.net>
parents: 207
diff changeset
    20
	if err := mc.apiCall("v1/"+endPoint, rest.Get, nil, lopt, &links, &domains); err != nil {
187
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{}
238
1c0042e76902 Do not use a global API version
Mikael Berthe <mikael@lilotux.net>
parents: 207
diff changeset
    28
			if err := mc.apiCall("v1/"+endPoint, rest.Get, nil, newlopt, &links, &domainSlice); err != nil {
187
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
b3f9331551b7 Rename Domain methods
Mikael Berthe <mikael@lilotux.net>
parents: 187
diff changeset
    38
// BlockDomain blocks the specified domain
b3f9331551b7 Rename Domain methods
Mikael Berthe <mikael@lilotux.net>
parents: 187
diff changeset
    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)
238
1c0042e76902 Do not use a global API version
Mikael Berthe <mikael@lilotux.net>
parents: 207
diff changeset
    43
	return mc.apiCall("v1/"+endPoint, rest.Post, params, nil, nil, nil)
187
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
b3f9331551b7 Rename Domain methods
Mikael Berthe <mikael@lilotux.net>
parents: 187
diff changeset
    46
// UnblockDomain unblocks the specified domain
b3f9331551b7 Rename Domain methods
Mikael Berthe <mikael@lilotux.net>
parents: 187
diff changeset
    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)
238
1c0042e76902 Do not use a global API version
Mikael Berthe <mikael@lilotux.net>
parents: 207
diff changeset
    51
	return mc.apiCall("v1/"+endPoint, rest.Delete, params, nil, nil, nil)
187
e1b1f4a168b7 Add GetDomainBlocks, DomainBlock and DomainUnblock
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    52
}