Skip to content

Commit

Permalink
Merge pull request #78 from TJM/make_args_optional
Browse files Browse the repository at this point in the history
Fix #74 - make extra arguments optional
  • Loading branch information
adlio committed Nov 16, 2020
2 parents b5a5d26 + cc656c6 commit 4e5d614
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 100 deletions.
9 changes: 6 additions & 3 deletions action.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,24 @@ type ActionDataCard struct {
}

// GetActions make a GET call for a board's actions
func (b *Board) GetActions(args Arguments) (actions ActionCollection, err error) {
func (b *Board) GetActions(extraArgs ...Arguments) (actions ActionCollection, err error) {
args := flattenArguments(extraArgs)
path := fmt.Sprintf("boards/%s/actions", b.ID)
err = b.client.Get(path, args, &actions)
return
}

// GetActions makes a GET call for a list's actions
func (l *List) GetActions(args Arguments) (actions ActionCollection, err error) {
func (l *List) GetActions(extraArgs ...Arguments) (actions ActionCollection, err error) {
args := flattenArguments(extraArgs)
path := fmt.Sprintf("lists/%s/actions", l.ID)
err = l.client.Get(path, args, &actions)
return
}

// GetActions makes a GET for a card's actions
func (c *Card) GetActions(args Arguments) (actions ActionCollection, err error) {
func (c *Card) GetActions(extraArgs ...Arguments) (actions ActionCollection, err error) {
args := flattenArguments(extraArgs)
path := fmt.Sprintf("cards/%s/actions", c.ID)
err = c.client.Get(path, args, &actions)
return
Expand Down
18 changes: 18 additions & 0 deletions arguments.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,21 @@ func (args Arguments) ToURLValues() url.Values {
}
return v
}

// flattenArguments will return a Arguments by merging a slice of Arguments,
// where each successive slice can override fields in the previous.
func flattenArguments(extraArgs []Arguments) (args Arguments) {
args = make(Arguments)
args.flatten(extraArgs)
return
}

// flatten will merge a slice of Arguments into the current one
// where each successive slice can override fields in the previous.
func (args Arguments) flatten(extraArgs []Arguments) {
for _, extraArg := range extraArgs {
for key, val := range extraArg {
args[key] = val
}
}
}
44 changes: 19 additions & 25 deletions board.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (b *Board) CreatedAt() time.Time {
// Attributes currently known to be unsupported: idBoardSource, keepFromSource.
//
// API Docs: https://developers.trello.com/reference/#boardsid
func (c *Client) CreateBoard(board *Board, extraArgs Arguments) error {
func (c *Client) CreateBoard(board *Board, extraArgs ...Arguments) error {
path := "boards"
args := Arguments{
"desc": board.Desc,
Expand Down Expand Up @@ -120,14 +120,7 @@ func (c *Client) CreateBoard(board *Board, extraArgs Arguments) error {
args["prefs_cardAging"] = board.Prefs.CardAging
}

// Expects "true" or "false"
if defaultLists, ok := extraArgs["defaultLists"]; ok {
args["defaultLists"] = defaultLists
}
// Expects one of "all", "calendar", "cardAging", "recap", or "voting".
if powerUps, ok := extraArgs["powerUps"]; ok {
args["powerUps"] = powerUps
}
args.flatten(extraArgs)

err := c.Post(path, args, &board)
if err == nil {
Expand All @@ -138,14 +131,16 @@ func (c *Client) CreateBoard(board *Board, extraArgs Arguments) error {

// Update PUTs the supported board attributes remote and updates
// the struct from the returned values.
func (b *Board) Update(extraArgs Arguments) error {
return b.client.PutBoard(b, extraArgs)
func (b *Board) Update(extraArgs ...Arguments) error {
args := flattenArguments(extraArgs)
return b.client.PutBoard(b, args)
}

// Delete makes a DELETE call for the receiver Board.
func (b *Board) Delete(extraArgs Arguments) error {
func (b *Board) Delete(extraArgs ...Arguments) error {
args := flattenArguments(extraArgs)
path := fmt.Sprintf("boards/%s", b.ID)
return b.client.Delete(path, Arguments{}, b)
return b.client.Delete(path, args, b)
}

// AddedMembersResponse represents a response after adding a new member.
Expand All @@ -157,26 +152,21 @@ type AddedMembersResponse struct {

// AddMember adds a new member to the board.
// https://developers.trello.com/reference#boardsidlabelnamesmembers
func (b *Board) AddMember(member *Member, extraArgs Arguments) (response *AddedMembersResponse, err error) {
func (b *Board) AddMember(member *Member, extraArgs ...Arguments) (response *AddedMembersResponse, err error) {
args := Arguments{
"email": member.Email,
}

// "normal" is the default type, so we can omit it.
if memberType, ok := extraArgs["type"]; ok {
switch memberType {
case "admin", "observer":
args["type"] = memberType
}
}
args.flatten(extraArgs)

path := fmt.Sprintf("boards/%s/members", b.ID)
err = b.client.Put(path, args, &response)
return
}

// GetBoard retrieves a Trello board by its ID.
func (c *Client) GetBoard(boardID string, args Arguments) (board *Board, err error) {
func (c *Client) GetBoard(boardID string, extraArgs ...Arguments) (board *Board, err error) {
args := flattenArguments(extraArgs)
path := fmt.Sprintf("boards/%s", boardID)
err = c.Get(path, args, &board)
if board != nil {
Expand All @@ -186,7 +176,8 @@ func (c *Client) GetBoard(boardID string, args Arguments) (board *Board, err err
}

// GetMyBoards returns a slice of all boards associated with the credentials set on the client.
func (c *Client) GetMyBoards(args Arguments) (boards []*Board, err error) {
func (c *Client) GetMyBoards(extraArgs ...Arguments) (boards []*Board, err error) {
args := flattenArguments(extraArgs)
path := "members/me/boards"
err = c.Get(path, args, &boards)
for i := range boards {
Expand All @@ -196,7 +187,8 @@ func (c *Client) GetMyBoards(args Arguments) (boards []*Board, err error) {
}

// GetBoards returns a slice of all public boards of the receiver Member.
func (m *Member) GetBoards(args Arguments) (boards []*Board, err error) {
func (m *Member) GetBoards(extraArgs ...Arguments) (boards []*Board, err error) {
args := flattenArguments(extraArgs)
path := fmt.Sprintf("members/%s/boards", m.ID)
err = m.client.Get(path, args, &boards)
for i := range boards {
Expand All @@ -212,7 +204,7 @@ func (m *Member) GetBoards(args Arguments) (boards []*Board, err error) {
// PutBoard PUTs a board remote. Extra arguments are currently unsupported.
//
// API Docs: https://developers.trello.com/reference#idnext
func (c *Client) PutBoard(board *Board, extraArgs Arguments) error {
func (c *Client) PutBoard(board *Board, extraArgs ...Arguments) error {
path := fmt.Sprintf("boards/%s", board.ID)
args := Arguments{
"desc": board.Desc,
Expand Down Expand Up @@ -241,6 +233,8 @@ func (c *Client) PutBoard(board *Board, extraArgs Arguments) error {
args["prefs/cardAging"] = board.Prefs.CardAging
}

args.flatten(extraArgs)

err := c.Put(path, args, &board)
if err == nil {
board.client = c
Expand Down
69 changes: 41 additions & 28 deletions card.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ func (c *Card) CustomFields(boardCustomFields []*CustomField) map[string]interfa
}

// MoveToList moves a card to a list given by listID.
func (c *Card) MoveToList(listID string, args Arguments) error {
func (c *Card) MoveToList(listID string, extraArgs ...Arguments) error {
args := flattenArguments(extraArgs)
path := fmt.Sprintf("cards/%s", c.ID)
args["idList"] = listID
return c.client.Put(path, args, &c)
Expand Down Expand Up @@ -196,7 +197,8 @@ func (c *Card) MoveToBottomOfList() error {
}

// Update UPDATEs the card's attributes.
func (c *Card) Update(args Arguments) error {
func (c *Card) Update(extraArgs ...Arguments) error {
args := flattenArguments(extraArgs)
path := fmt.Sprintf("cards/%s", c.ID)
return c.client.Put(path, args, c)
}
Expand All @@ -218,7 +220,7 @@ func (c *Card) Delete() error {
}

// CreateCard takes a Card and Arguments and POSTs the card.
func (c *Client) CreateCard(card *Card, extraArgs Arguments) error {
func (c *Client) CreateCard(card *Card, extraArgs ...Arguments) error {
path := "cards"
args := Arguments{
"name": card.Name,
Expand All @@ -231,10 +233,8 @@ func (c *Client) CreateCard(card *Card, extraArgs Arguments) error {
if card.Due != nil {
args["due"] = card.Due.Format(time.RFC3339)
}
// Allow overriding the creation position with 'top' or 'botttom'
if pos, ok := extraArgs["pos"]; ok {
args["pos"] = pos
}

args.flatten(extraArgs)
err := c.Post(path, args, &card)
if err == nil {
card.client = c
Expand All @@ -243,7 +243,7 @@ func (c *Client) CreateCard(card *Card, extraArgs Arguments) error {
}

// AddCard takes a Card and Arguments and adds the card to the receiver list.
func (l *List) AddCard(card *Card, extraArgs Arguments) error {
func (l *List) AddCard(card *Card, extraArgs ...Arguments) error {
path := fmt.Sprintf("lists/%s/cards", l.ID)
args := Arguments{
"name": card.Name,
Expand All @@ -254,10 +254,9 @@ func (l *List) AddCard(card *Card, extraArgs Arguments) error {
if card.Due != nil {
args["due"] = card.Due.Format(time.RFC3339)
}
// Allow overwriting the creation position with 'top' or 'bottom'
if pos, ok := extraArgs["pos"]; ok {
args["pos"] = pos
}

args.flatten(extraArgs)

err := l.client.Post(path, args, &card)
if err == nil {
card.client = l.client
Expand All @@ -273,11 +272,14 @@ func (l *List) AddCard(card *Card, extraArgs Arguments) error {
// Arguments["keepFromSource"] = "all"
// Arguments["keepFromSource"] = "none"
// Arguments["keepFromSource"] = "attachments,checklists,comments"
func (c *Card) CopyToList(listID string, args Arguments) (*Card, error) {
func (c *Card) CopyToList(listID string, extraArgs ...Arguments) (*Card, error) {
args := Arguments{
"idList": listID,
"idCardSource": c.ID,
}
path := "cards"
args["idList"] = listID
args["idCardSource"] = c.ID
newCard := Card{}
args.flatten(extraArgs)
err := c.client.Post(path, args, &newCard)
if err == nil {
newCard.client = c.client
Expand All @@ -288,9 +290,12 @@ func (c *Card) CopyToList(listID string, args Arguments) (*Card, error) {
}

// AddComment takes a comment string and Arguments and adds the comment to the card.
func (c *Card) AddComment(comment string, args Arguments) (*Action, error) {
func (c *Card) AddComment(comment string, extraArgs ...Arguments) (*Action, error) {
args := Arguments{
"text": comment,
}
args.flatten(extraArgs)
path := fmt.Sprintf("cards/%s/actions/comments", c.ID)
args["text"] = comment
action := Action{}
err := c.client.Post(path, args, &action)
if err != nil {
Expand All @@ -300,12 +305,13 @@ func (c *Card) AddComment(comment string, args Arguments) (*Action, error) {
}

// AddURLAttachment takes an Attachment and adds it to the card.
func (c *Card) AddURLAttachment(attachment *Attachment) error {
func (c *Card) AddURLAttachment(attachment *Attachment, extraArgs ...Arguments) error {
path := fmt.Sprintf("cards/%s/attachments", c.ID)
args := Arguments{
"url": attachment.URL,
"name": attachment.Name,
}
args.flatten(extraArgs)
err := c.client.Post(path, args, &attachment)
if err != nil {
err = errors.Wrapf(err, "Error adding attachment to card %s", c.ID)
Expand All @@ -318,8 +324,8 @@ func (c *Card) AddURLAttachment(attachment *Attachment) error {
// from a copy of another Card. Returns an error only when a low-level failure occurred.
// If this Card has no parent, a nil card and nil error are returned. In other words, the
// non-existence of a parent is not treated as an error.
func (c *Card) GetParentCard(args Arguments) (*Card, error) {

func (c *Card) GetParentCard(extraArgs ...Arguments) (*Card, error) {
args := flattenArguments(extraArgs)
// Hopefully the card came pre-loaded with Actions including the card creation
action := c.Actions.FirstCardCreateAction()

Expand All @@ -343,8 +349,8 @@ func (c *Card) GetParentCard(args Arguments) (*Card, error) {
}

// GetAncestorCards takes Arguments, GETs the card's ancestors and returns them as a slice.
func (c *Card) GetAncestorCards(args Arguments) (ancestors []*Card, err error) {

func (c *Card) GetAncestorCards(extraArgs ...Arguments) (ancestors []*Card, err error) {
args := flattenArguments(extraArgs)
// Get the first parent
parent, err := c.GetParentCard(args)
if IsNotFound(err) || IsPermissionDenied(err) {
Expand All @@ -367,7 +373,8 @@ func (c *Card) GetAncestorCards(args Arguments) (ancestors []*Card, err error) {
}

// GetOriginatingCard takes Arguments, GETs ancestors and returns most recent ancestor card of the Card.
func (c *Card) GetOriginatingCard(args Arguments) (*Card, error) {
func (c *Card) GetOriginatingCard(extraArgs ...Arguments) (*Card, error) {
args := flattenArguments(extraArgs)
ancestors, err := c.GetAncestorCards(args)
if err != nil {
return c, err
Expand Down Expand Up @@ -427,8 +434,11 @@ func (c *Card) CreatorMemberID() (string, error) {

// ContainsCopyOfCard accepts a card id and Arguments and returns true
// if the receiver Board contains a Card with the id.
func (b *Board) ContainsCopyOfCard(cardID string, args Arguments) (bool, error) {
args["filter"] = "copyCard"
func (b *Board) ContainsCopyOfCard(cardID string, extraArgs ...Arguments) (bool, error) {
args := Arguments{
"filter": "copyCard",
}
args.flatten(extraArgs)
actions, err := b.GetActions(args)
if err != nil {
err := errors.Wrapf(err, "GetCards() failed inside ContainsCopyOf() for board '%s' and card '%s'.", b.ID, cardID)
Expand All @@ -445,7 +455,8 @@ func (b *Board) ContainsCopyOfCard(cardID string, args Arguments) (bool, error)
// GetCard receives a card id and Arguments and returns the card if found
// with the credentials given for the receiver Client. Returns an error
// otherwise.
func (c *Client) GetCard(cardID string, args Arguments) (card *Card, err error) {
func (c *Client) GetCard(cardID string, extraArgs ...Arguments) (card *Card, err error) {
args := flattenArguments(extraArgs)
path := fmt.Sprintf("cards/%s", cardID)
err = c.Get(path, args, &card)
if card != nil {
Expand All @@ -455,7 +466,8 @@ func (c *Client) GetCard(cardID string, args Arguments) (card *Card, err error)
}

// GetCards takes Arguments and retrieves all Cards on a Board as slice or returns error.
func (b *Board) GetCards(args Arguments) (cards []*Card, err error) {
func (b *Board) GetCards(extraArgs ...Arguments) (cards []*Card, err error) {
args := flattenArguments(extraArgs)
path := fmt.Sprintf("boards/%s/cards", b.ID)

err = b.client.Get(path, args, &cards)
Expand Down Expand Up @@ -484,7 +496,8 @@ func (b *Board) GetCards(args Arguments) (cards []*Card, err error) {
}

// GetCards retrieves all Cards in a List or an error if something goes wrong.
func (l *List) GetCards(args Arguments) (cards []*Card, err error) {
func (l *List) GetCards(extraArgs ...Arguments) (cards []*Card, err error) {
args := flattenArguments(extraArgs)
path := fmt.Sprintf("lists/%s/cards", l.ID)
err = l.client.Get(path, args, &cards)
for i := range cards {
Expand Down
Loading

0 comments on commit 4e5d614

Please sign in to comment.