Add GetDomainBlocks, DomainBlock and DomainUnblock
authorMikael Berthe <mikael@lilotux.net>
Sat, 27 May 2017 19:12:01 +0200
changeset 187 e1b1f4a168b7
parent 186 eb9e257ba50c
child 188 5f4210c4921a
Add GetDomainBlocks, DomainBlock and DomainUnblock
domain.go
types.go
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/domain.go	Sat May 27 19:12:01 2017 +0200
@@ -0,0 +1,52 @@
+/*
+Copyright 2017 Mikael Berthe
+
+Licensed under the MIT license.  Please see the LICENSE file is this directory.
+*/
+
+package madon
+
+import (
+	"github.com/sendgrid/rest"
+)
+
+// GetDomainBlocks returns the current user blocked domains
+// If lopt.All is true, several requests will be made until the API server
+// has nothing to return.
+func (mc *Client) GetDomainBlocks(lopt *LimitParams) ([]DomainName, error) {
+	const endPoint = "domain_blocks"
+	var links apiLinks
+	var domains []DomainName
+	if err := mc.apiCall(endPoint, rest.Get, nil, lopt, &links, &domains); err != nil {
+		return nil, err
+	}
+	if lopt != nil { // Fetch more pages to reach our limit
+		var domainSlice []DomainName
+		for (lopt.All || lopt.Limit > len(domains)) && links.next != nil {
+			newlopt := links.next
+			links = apiLinks{}
+			if err := mc.apiCall(endPoint, rest.Get, nil, newlopt, &links, &domainSlice); err != nil {
+				return nil, err
+			}
+			domains = append(domains, domainSlice...)
+			domainSlice = domainSlice[:0] // Clear struct
+		}
+	}
+	return domains, nil
+}
+
+// DomainBlock blocks the specified domain
+func (mc *Client) DomainBlock(domain DomainName) error {
+	const endPoint = "domain_blocks"
+	params := make(apiCallParams)
+	params["domain"] = string(domain)
+	return mc.apiCall(endPoint, rest.Post, params, nil, nil, nil)
+}
+
+// DomainUnblock unblocks the specified domain
+func (mc *Client) DomainUnblock(domain DomainName) error {
+	const endPoint = "domain_blocks"
+	params := make(apiCallParams)
+	params["domain"] = string(domain)
+	return mc.apiCall(endPoint, rest.Delete, params, nil, nil, nil)
+}
--- a/types.go	Sat May 27 13:18:48 2017 +0200
+++ b/types.go	Sat May 27 19:12:01 2017 +0200
@@ -26,6 +26,9 @@
 Entities - Everything manipulated/returned by the API
 */
 
+// DomainName is a domain name string, as returned by the domain_blocks API
+type DomainName string
+
 // Account represents a Mastodon account entity
 type Account struct {
 	ID             int64     `json:"id"`