lists.go
author Mikael Berthe <mikael@lilotux.net>
Mon, 29 Jul 2019 21:57:47 +0200
changeset 253 0e8c8026cf40
parent 243 7386c6a454a8
permissions -rw-r--r--
Update dependencies
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
209
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     1
/*
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     2
Copyright 2018 Mikael Berthe
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     3
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     4
Licensed under the MIT license.  Please see the LICENSE file is this directory.
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
*/
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     6
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
package madon
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
import (
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
	"fmt"
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
	"strconv"
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
	"github.com/pkg/errors"
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    14
	"github.com/sendgrid/rest"
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
)
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
// GetList returns a List entity
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
func (mc *Client) GetList(listID int64) (*List, error) {
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    19
	if listID <= 0 {
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    20
		return nil, errors.New("invalid list ID")
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
	}
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
	endPoint := "lists/" + strconv.FormatInt(listID, 10)
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
	var list List
238
1c0042e76902 Do not use a global API version
Mikael Berthe <mikael@lilotux.net>
parents: 231
diff changeset
    24
	if err := mc.apiCall("v1/"+endPoint, rest.Get, nil, nil, nil, &list); err != nil {
209
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
		return nil, err
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
	}
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
	return &list, nil
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
}
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
// GetLists returns a list of List entities
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    31
// If accountID is > 0, this will return the lists containing this account.
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    32
// If lopt.All is true, several requests will be made until the API server
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    33
// has nothing to return.
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
func (mc *Client) GetLists(accountID int64, lopt *LimitParams) ([]List, error) {
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    35
	endPoint := "lists"
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    36
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
	if accountID > 0 {
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
		endPoint = "accounts/" + strconv.FormatInt(accountID, 10) + "/lists"
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
	}
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
	var lists []List
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    42
	var links apiLinks
238
1c0042e76902 Do not use a global API version
Mikael Berthe <mikael@lilotux.net>
parents: 231
diff changeset
    43
	if err := mc.apiCall("v1/"+endPoint, rest.Get, nil, lopt, &links, &lists); err != nil {
209
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    44
		return nil, err
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    45
	}
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    46
	if lopt != nil { // Fetch more pages to reach our limit
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    47
		var listSlice []List
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    48
		for (lopt.All || lopt.Limit > len(lists)) && links.next != nil {
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    49
			newlopt := links.next
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    50
			links = apiLinks{}
238
1c0042e76902 Do not use a global API version
Mikael Berthe <mikael@lilotux.net>
parents: 231
diff changeset
    51
			if err := mc.apiCall("v1/"+endPoint, rest.Get, nil, newlopt, &links, &listSlice); err != nil {
209
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    52
				return nil, err
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    53
			}
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    54
			lists = append(lists, listSlice...)
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    55
			listSlice = listSlice[:0] // Clear struct
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    56
		}
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    57
	}
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    58
	return lists, nil
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    59
}
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    60
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    61
// CreateList creates a List
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    62
func (mc *Client) CreateList(title string) (*List, error) {
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    63
	params := apiCallParams{"title": title}
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    64
	method := rest.Post
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    65
	return mc.setSingleList(method, 0, params)
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    66
}
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    67
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    68
// UpdateList updates an existing List
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    69
func (mc *Client) UpdateList(listID int64, title string) (*List, error) {
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    70
	if listID <= 0 {
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    71
		return nil, errors.New("invalid list ID")
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    72
	}
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    73
	params := apiCallParams{"title": title}
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    74
	method := rest.Put
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    75
	return mc.setSingleList(method, listID, params)
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    76
}
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    77
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    78
// DeleteList deletes a list
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    79
func (mc *Client) DeleteList(listID int64) error {
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    80
	if listID <= 0 {
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    81
		return errors.New("invalid list ID")
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    82
	}
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    83
	method := rest.Delete
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    84
	_, err := mc.setSingleList(method, listID, nil)
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    85
	return err
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    86
}
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    87
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    88
// GetListAccounts returns the accounts belonging to a given list
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    89
func (mc *Client) GetListAccounts(listID int64, lopt *LimitParams) ([]Account, error) {
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    90
	endPoint := "lists/" + strconv.FormatInt(listID, 10) + "/accounts"
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    91
	return mc.getMultipleAccounts(endPoint, nil, lopt)
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    92
}
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    93
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    94
// AddListAccounts adds the accounts to a given list
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    95
func (mc *Client) AddListAccounts(listID int64, accountIDs []int64) error {
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    96
	endPoint := "lists/" + strconv.FormatInt(listID, 10) + "/accounts"
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    97
	method := rest.Post
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    98
	params := make(apiCallParams)
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    99
	for i, id := range accountIDs {
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   100
		if id < 1 {
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   101
			return ErrInvalidID
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   102
		}
243
7386c6a454a8 Change the way parameter lists are handled internally
Mikael Berthe <mikael@lilotux.net>
parents: 238
diff changeset
   103
		qID := fmt.Sprintf("[%d]account_ids", i)
209
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   104
		params[qID] = strconv.FormatInt(id, 10)
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   105
	}
238
1c0042e76902 Do not use a global API version
Mikael Berthe <mikael@lilotux.net>
parents: 231
diff changeset
   106
	return mc.apiCall("v1/"+endPoint, method, params, nil, nil, nil)
209
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   107
}
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   108
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   109
// RemoveListAccounts removes the accounts from the given list
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   110
func (mc *Client) RemoveListAccounts(listID int64, accountIDs []int64) error {
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   111
	endPoint := "lists/" + strconv.FormatInt(listID, 10) + "/accounts"
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   112
	method := rest.Delete
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   113
	params := make(apiCallParams)
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   114
	for i, id := range accountIDs {
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   115
		if id < 1 {
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   116
			return ErrInvalidID
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   117
		}
243
7386c6a454a8 Change the way parameter lists are handled internally
Mikael Berthe <mikael@lilotux.net>
parents: 238
diff changeset
   118
		qID := fmt.Sprintf("[%d]account_ids", i)
209
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   119
		params[qID] = strconv.FormatInt(id, 10)
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   120
	}
238
1c0042e76902 Do not use a global API version
Mikael Berthe <mikael@lilotux.net>
parents: 231
diff changeset
   121
	return mc.apiCall("v1/"+endPoint, method, params, nil, nil, nil)
209
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   122
}
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   123
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   124
func (mc *Client) setSingleList(method rest.Method, listID int64, params apiCallParams) (*List, error) {
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   125
	var endPoint string
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   126
	if listID > 0 {
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   127
		endPoint = "lists/" + strconv.FormatInt(listID, 10)
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   128
	} else {
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   129
		endPoint = "lists"
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   130
	}
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   131
	var list List
238
1c0042e76902 Do not use a global API version
Mikael Berthe <mikael@lilotux.net>
parents: 231
diff changeset
   132
	if err := mc.apiCall("v1/"+endPoint, method, params, nil, nil, &list); err != nil {
209
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   133
		return nil, err
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   134
	}
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   135
	return &list, nil
b9d12bab993e Add Lists support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   136
}