lists.go
changeset 238 1c0042e76902
parent 231 741291bb4772
child 243 7386c6a454a8
equal deleted inserted replaced
237:16c27106d83c 238:1c0042e76902
    19 	if listID <= 0 {
    19 	if listID <= 0 {
    20 		return nil, errors.New("invalid list ID")
    20 		return nil, errors.New("invalid list ID")
    21 	}
    21 	}
    22 	endPoint := "lists/" + strconv.FormatInt(listID, 10)
    22 	endPoint := "lists/" + strconv.FormatInt(listID, 10)
    23 	var list List
    23 	var list List
    24 	if err := mc.apiCall(endPoint, rest.Get, nil, nil, nil, &list); err != nil {
    24 	if err := mc.apiCall("v1/"+endPoint, rest.Get, nil, nil, nil, &list); err != nil {
    25 		return nil, err
    25 		return nil, err
    26 	}
    26 	}
    27 	return &list, nil
    27 	return &list, nil
    28 }
    28 }
    29 
    29 
    38 		endPoint = "accounts/" + strconv.FormatInt(accountID, 10) + "/lists"
    38 		endPoint = "accounts/" + strconv.FormatInt(accountID, 10) + "/lists"
    39 	}
    39 	}
    40 
    40 
    41 	var lists []List
    41 	var lists []List
    42 	var links apiLinks
    42 	var links apiLinks
    43 	if err := mc.apiCall(endPoint, rest.Get, nil, lopt, &links, &lists); err != nil {
    43 	if err := mc.apiCall("v1/"+endPoint, rest.Get, nil, lopt, &links, &lists); err != nil {
    44 		return nil, err
    44 		return nil, err
    45 	}
    45 	}
    46 	if lopt != nil { // Fetch more pages to reach our limit
    46 	if lopt != nil { // Fetch more pages to reach our limit
    47 		var listSlice []List
    47 		var listSlice []List
    48 		for (lopt.All || lopt.Limit > len(lists)) && links.next != nil {
    48 		for (lopt.All || lopt.Limit > len(lists)) && links.next != nil {
    49 			newlopt := links.next
    49 			newlopt := links.next
    50 			links = apiLinks{}
    50 			links = apiLinks{}
    51 			if err := mc.apiCall(endPoint, rest.Get, nil, newlopt, &links, &listSlice); err != nil {
    51 			if err := mc.apiCall("v1/"+endPoint, rest.Get, nil, newlopt, &links, &listSlice); err != nil {
    52 				return nil, err
    52 				return nil, err
    53 			}
    53 			}
    54 			lists = append(lists, listSlice...)
    54 			lists = append(lists, listSlice...)
    55 			listSlice = listSlice[:0] // Clear struct
    55 			listSlice = listSlice[:0] // Clear struct
    56 		}
    56 		}
   101 			return ErrInvalidID
   101 			return ErrInvalidID
   102 		}
   102 		}
   103 		qID := fmt.Sprintf("account_ids[%d]", i)
   103 		qID := fmt.Sprintf("account_ids[%d]", i)
   104 		params[qID] = strconv.FormatInt(id, 10)
   104 		params[qID] = strconv.FormatInt(id, 10)
   105 	}
   105 	}
   106 	return mc.apiCall(endPoint, method, params, nil, nil, nil)
   106 	return mc.apiCall("v1/"+endPoint, method, params, nil, nil, nil)
   107 }
   107 }
   108 
   108 
   109 // RemoveListAccounts removes the accounts from the given list
   109 // RemoveListAccounts removes the accounts from the given list
   110 func (mc *Client) RemoveListAccounts(listID int64, accountIDs []int64) error {
   110 func (mc *Client) RemoveListAccounts(listID int64, accountIDs []int64) error {
   111 	endPoint := "lists/" + strconv.FormatInt(listID, 10) + "/accounts"
   111 	endPoint := "lists/" + strconv.FormatInt(listID, 10) + "/accounts"
   116 			return ErrInvalidID
   116 			return ErrInvalidID
   117 		}
   117 		}
   118 		qID := fmt.Sprintf("account_ids[%d]", i)
   118 		qID := fmt.Sprintf("account_ids[%d]", i)
   119 		params[qID] = strconv.FormatInt(id, 10)
   119 		params[qID] = strconv.FormatInt(id, 10)
   120 	}
   120 	}
   121 	return mc.apiCall(endPoint, method, params, nil, nil, nil)
   121 	return mc.apiCall("v1/"+endPoint, method, params, nil, nil, nil)
   122 }
   122 }
   123 
   123 
   124 func (mc *Client) setSingleList(method rest.Method, listID int64, params apiCallParams) (*List, error) {
   124 func (mc *Client) setSingleList(method rest.Method, listID int64, params apiCallParams) (*List, error) {
   125 	var endPoint string
   125 	var endPoint string
   126 	if listID > 0 {
   126 	if listID > 0 {
   127 		endPoint = "lists/" + strconv.FormatInt(listID, 10)
   127 		endPoint = "lists/" + strconv.FormatInt(listID, 10)
   128 	} else {
   128 	} else {
   129 		endPoint = "lists"
   129 		endPoint = "lists"
   130 	}
   130 	}
   131 	var list List
   131 	var list List
   132 	if err := mc.apiCall(endPoint, method, params, nil, nil, &list); err != nil {
   132 	if err := mc.apiCall("v1/"+endPoint, method, params, nil, nil, &list); err != nil {
   133 		return nil, err
   133 		return nil, err
   134 	}
   134 	}
   135 	return &list, nil
   135 	return &list, nil
   136 }
   136 }