Skip to content
This repository has been archived by the owner on Apr 2, 2022. It is now read-only.

more card actions #19

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 87 additions & 4 deletions card.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package trello
import (
"encoding/json"
"net/url"
"strconv"
)

type Card struct {
Expand Down Expand Up @@ -154,14 +155,14 @@ func (c *Card) Actions() (actions []Action, err error) {
// AddChecklist will add a checklist to the card.
// https://developers.trello.com/advanced-reference/card#post-1-cards-card-id-or-shortlink-checklists
func (c *Card) AddChecklist(name string) (*Checklist, error) {
newList := &Checklist{}

payload := url.Values{}
payload.Set("name", name)
body, err := c.client.Post("/cards/"+c.Id+"/checklists", payload)
if err != nil {
return nil, err
}

newList := &Checklist{}
if err = json.Unmarshal(body, newList); err != nil {
return nil, err
}
Expand All @@ -172,10 +173,92 @@ func (c *Card) AddChecklist(name string) (*Checklist, error) {

// AddComment will add a new comment to the card
// https://developers.trello.com/advanced-reference/card#post-1-cards-card-id-or-shortlink-actions-comments
func (c *Card) AddComment(text string) ([]byte, error) {
func (c *Card) AddComment(text string) (*Action, error) {
payload := url.Values{}
payload.Set("text", text)

body, err := c.client.Post("/cards/"+c.Id+"/actions/comments", payload)
return body, err
if err != nil {
return nil, err
}

newAction := &Action{}
if err = json.Unmarshal(body, newAction); err != nil {
return nil, err
}
newAction.client = c.client
return newAction, nil
}

// Archive will archive the card
// https://developers.trello.com/advanced-reference/card#put-1-cards-card-id-or-shortlink-closed
func (c *Card) Archive() (*Card, error) {
payload := url.Values{}
payload.Set("value", "true")

body, err := c.client.Put("/cards/"+c.Id+"/closed", payload)
if err != nil {
return nil, err
}

newCard := &Card{}
if err = json.Unmarshal(body, newCard); err != nil {
return nil, err
}
newCard.client = c.client
return newCard, nil
}

// SendToBoard will dearchive the card, or send the card to the board back from archive
// https://developers.trello.com/advanced-reference/card#put-1-cards-card-id-or-shortlink-closed
func (c *Card) SendToBoard() (*Card, error) {
payload := url.Values{}
payload.Set("value", "false")

body, err := c.client.Put("/cards/"+c.Id+"/closed", payload)
if err != nil {
return nil, err
}
newCard := &Card{}
if err = json.Unmarshal(body, newCard); err != nil {
return nil, err
}
newCard.client = c.client
return newCard, nil
}

// MoveToList will move the card to another list
// https://developers.trello.com/advanced-reference/card#put-1-cards-card-id-or-shortlink-idlist
func (c *Card) MoveToList(listId string) (*Card, error) {
payload := url.Values{}
payload.Set("value", listId)

body, err := c.client.Put("/cards/"+c.Id+"/idList", payload)
if err != nil {
return nil, err
}
newCard := &Card{}
if err = json.Unmarshal(body, newCard); err != nil {
return nil, err
}
newCard.client = c.client
return newCard, nil
}

// MoveToPos will move card to the specified position
// https://developers.trello.com/advanced-reference/card#put-1-cards-card-id-or-shortlink-pos
func (c *Card) MoveToPos(pos int) (*Card, error) {
payload := url.Values{}
payload.Set("value", strconv.Itoa(pos))

body, err := c.client.Put("/cards/"+c.Id+"/pos", payload)
if err != nil {
return nil, err
}
newCard := &Card{}
if err = json.Unmarshal(body, newCard); err != nil {
return nil, err
}
newCard.client = c.client
return newCard, nil
}
10 changes: 10 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ func (c *Client) Post(resource string, data url.Values) ([]byte, error) {
return c.do(req)
}

func (c *Client) Put(resource string, data url.Values) ([]byte, error) {
req, err := http.NewRequest("PUT", c.endpoint+resource, strings.NewReader(data.Encode()))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any endpoint that'd use different payload content-type, eg. JSON? Or is it always x-www-form-urlencoded?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think all of them are application/x-www-form-urlencoded. At least also the POST endpoints needs urlencoded parameters… (I implemented some more functions but with breaking changes)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Luzifer ok, cool. Pls, keep them coming, I'm willing to release breaking changes in v0.2 shortly.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to you can take a look at the breaking changes here: Luzifer/go-trello@master...Luzifer:dev - switching from []byte responses to objects also will be breaking for everyone relying on getting []byte

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This project is not in stable version anyway, so we can just "tag" the old API and keep rolling :)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I'm definitely interested in the struct type changes you made. Can you submit them as PR, please?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case I'd suggest to merge this PR containing []byte responses and I'll change the responses in my dev branch to objects. Would not increase the scope of this PR and create a clean cut for the next one…

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw: the AddCard() would be also a good addition, I like the validation funcs etc. -- what would you think of

card, err := list.AddCard(trello.Card{
 // ...
 // ...
})

// and/or
card, err := board.AddCard(trello.Card{
 // ...
 // ...
})

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to use an extra struct because not all values of trello.Card are supported in creating a new card. (Also for example types of the position diffs float32 in Card vs. string in AddCardOpts - In that case the API is not very consistent…)


return c.do(req)
}

func (c *Client) Delete(resource string) ([]byte, error) {
req, err := http.NewRequest("DELETE", c.endpoint+resource, nil)
if err != nil {
Expand Down