vendor/github.com/McKael/madon/v3/lists.go
changeset 270 df7e9dff1b66
parent 268 4dd196a4ee7c
equal deleted inserted replaced
269:c50e88700432 270:df7e9dff1b66
     6 
     6 
     7 package madon
     7 package madon
     8 
     8 
     9 import (
     9 import (
    10 	"fmt"
    10 	"fmt"
    11 	"strconv"
       
    12 
    11 
    13 	"github.com/pkg/errors"
    12 	"github.com/pkg/errors"
    14 	"github.com/sendgrid/rest"
    13 	"github.com/sendgrid/rest"
    15 )
    14 )
    16 
    15 
    17 // GetList returns a List entity
    16 // GetList returns a List entity
    18 func (mc *Client) GetList(listID int64) (*List, error) {
    17 func (mc *Client) GetList(listID ActivityID) (*List, error) {
    19 	if listID <= 0 {
    18 	if listID == "" {
    20 		return nil, errors.New("invalid list ID")
    19 		return nil, errors.New("invalid list ID")
    21 	}
    20 	}
    22 	endPoint := "lists/" + strconv.FormatInt(listID, 10)
    21 	endPoint := "lists/" + listID
    23 	var list List
    22 	var list List
    24 	if err := mc.apiCall("v1/"+endPoint, rest.Get, nil, nil, nil, &list); err != nil {
    23 	if err := mc.apiCall("v1/"+endPoint, rest.Get, nil, nil, nil, &list); err != nil {
    25 		return nil, err
    24 		return nil, err
    26 	}
    25 	}
    27 	return &list, nil
    26 	return &list, nil
    29 
    28 
    30 // GetLists returns a list of List entities
    29 // GetLists returns a list of List entities
    31 // If accountID is > 0, this will return the lists containing this account.
    30 // If accountID is > 0, this will return the lists containing this account.
    32 // If lopt.All is true, several requests will be made until the API server
    31 // If lopt.All is true, several requests will be made until the API server
    33 // has nothing to return.
    32 // has nothing to return.
    34 func (mc *Client) GetLists(accountID int64, lopt *LimitParams) ([]List, error) {
    33 func (mc *Client) GetLists(accountID ActivityID, lopt *LimitParams) ([]List, error) {
    35 	endPoint := "lists"
    34 	endPoint := "lists"
    36 
    35 
    37 	if accountID > 0 {
    36 	if accountID != "" {
    38 		endPoint = "accounts/" + strconv.FormatInt(accountID, 10) + "/lists"
    37 		endPoint = "accounts/" + accountID + "/lists"
    39 	}
    38 	}
    40 
    39 
    41 	var lists []List
    40 	var lists []List
    42 	var links apiLinks
    41 	var links apiLinks
    43 	if err := mc.apiCall("v1/"+endPoint, rest.Get, nil, lopt, &links, &lists); err != nil {
    42 	if err := mc.apiCall("v1/"+endPoint, rest.Get, nil, lopt, &links, &lists); err != nil {
    60 
    59 
    61 // CreateList creates a List
    60 // CreateList creates a List
    62 func (mc *Client) CreateList(title string) (*List, error) {
    61 func (mc *Client) CreateList(title string) (*List, error) {
    63 	params := apiCallParams{"title": title}
    62 	params := apiCallParams{"title": title}
    64 	method := rest.Post
    63 	method := rest.Post
    65 	return mc.setSingleList(method, 0, params)
    64 	return mc.setSingleList(method, "", params)
    66 }
    65 }
    67 
    66 
    68 // UpdateList updates an existing List
    67 // UpdateList updates an existing List
    69 func (mc *Client) UpdateList(listID int64, title string) (*List, error) {
    68 func (mc *Client) UpdateList(listID ActivityID, title string) (*List, error) {
    70 	if listID <= 0 {
    69 	if listID == "" {
    71 		return nil, errors.New("invalid list ID")
    70 		return nil, errors.New("invalid list ID")
    72 	}
    71 	}
    73 	params := apiCallParams{"title": title}
    72 	params := apiCallParams{"title": title}
    74 	method := rest.Put
    73 	method := rest.Put
    75 	return mc.setSingleList(method, listID, params)
    74 	return mc.setSingleList(method, listID, params)
    76 }
    75 }
    77 
    76 
    78 // DeleteList deletes a list
    77 // DeleteList deletes a list
    79 func (mc *Client) DeleteList(listID int64) error {
    78 func (mc *Client) DeleteList(listID ActivityID) error {
    80 	if listID <= 0 {
    79 	if listID == "" {
    81 		return errors.New("invalid list ID")
    80 		return errors.New("invalid list ID")
    82 	}
    81 	}
    83 	method := rest.Delete
    82 	method := rest.Delete
    84 	_, err := mc.setSingleList(method, listID, nil)
    83 	_, err := mc.setSingleList(method, listID, nil)
    85 	return err
    84 	return err
    86 }
    85 }
    87 
    86 
    88 // GetListAccounts returns the accounts belonging to a given list
    87 // GetListAccounts returns the accounts belonging to a given list
    89 func (mc *Client) GetListAccounts(listID int64, lopt *LimitParams) ([]Account, error) {
    88 func (mc *Client) GetListAccounts(listID ActivityID, lopt *LimitParams) ([]Account, error) {
    90 	endPoint := "lists/" + strconv.FormatInt(listID, 10) + "/accounts"
    89 	endPoint := "lists/" + listID + "/accounts"
    91 	return mc.getMultipleAccounts(endPoint, nil, lopt)
    90 	return mc.getMultipleAccounts(endPoint, nil, lopt)
    92 }
    91 }
    93 
    92 
    94 // AddListAccounts adds the accounts to a given list
    93 // AddListAccounts adds the accounts to a given list
    95 func (mc *Client) AddListAccounts(listID int64, accountIDs []int64) error {
    94 func (mc *Client) AddListAccounts(listID ActivityID, accountIDs []ActivityID) error {
    96 	endPoint := "lists/" + strconv.FormatInt(listID, 10) + "/accounts"
    95 	endPoint := "lists/" + listID + "/accounts"
    97 	method := rest.Post
    96 	method := rest.Post
    98 	params := make(apiCallParams)
    97 	params := make(apiCallParams)
    99 	for i, id := range accountIDs {
    98 	for i, id := range accountIDs {
   100 		if id < 1 {
    99 		if id == "" {
   101 			return ErrInvalidID
   100 			return ErrInvalidID
   102 		}
   101 		}
   103 		qID := fmt.Sprintf("[%d]account_ids", i)
   102 		qID := fmt.Sprintf("[%d]account_ids", i)
   104 		params[qID] = strconv.FormatInt(id, 10)
   103 		params[qID] = id
   105 	}
   104 	}
   106 	return mc.apiCall("v1/"+endPoint, method, params, nil, nil, nil)
   105 	return mc.apiCall("v1/"+endPoint, method, params, nil, nil, nil)
   107 }
   106 }
   108 
   107 
   109 // RemoveListAccounts removes the accounts from the given list
   108 // RemoveListAccounts removes the accounts from the given list
   110 func (mc *Client) RemoveListAccounts(listID int64, accountIDs []int64) error {
   109 func (mc *Client) RemoveListAccounts(listID ActivityID, accountIDs []ActivityID) error {
   111 	endPoint := "lists/" + strconv.FormatInt(listID, 10) + "/accounts"
   110 	endPoint := "lists/" + listID + "/accounts"
   112 	method := rest.Delete
   111 	method := rest.Delete
   113 	params := make(apiCallParams)
   112 	params := make(apiCallParams)
   114 	for i, id := range accountIDs {
   113 	for i, id := range accountIDs {
   115 		if id < 1 {
   114 		if id == "" {
   116 			return ErrInvalidID
   115 			return ErrInvalidID
   117 		}
   116 		}
   118 		qID := fmt.Sprintf("[%d]account_ids", i)
   117 		qID := fmt.Sprintf("[%d]account_ids", i)
   119 		params[qID] = strconv.FormatInt(id, 10)
   118 		params[qID] = id
   120 	}
   119 	}
   121 	return mc.apiCall("v1/"+endPoint, method, params, nil, nil, nil)
   120 	return mc.apiCall("v1/"+endPoint, method, params, nil, nil, nil)
   122 }
   121 }
   123 
   122 
   124 func (mc *Client) setSingleList(method rest.Method, listID int64, params apiCallParams) (*List, error) {
   123 func (mc *Client) setSingleList(method rest.Method, listID ActivityID, params apiCallParams) (*List, error) {
   125 	var endPoint string
   124 	var endPoint string
   126 	if listID > 0 {
   125 	if listID != "" {
   127 		endPoint = "lists/" + strconv.FormatInt(listID, 10)
   126 		endPoint = "lists/" + listID
   128 	} else {
   127 	} else {
   129 		endPoint = "lists"
   128 		endPoint = "lists"
   130 	}
   129 	}
   131 	var list List
   130 	var list List
   132 	if err := mc.apiCall("v1/"+endPoint, method, params, nil, nil, &list); err != nil {
   131 	if err := mc.apiCall("v1/"+endPoint, method, params, nil, nil, &list); err != nil {