Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix add/removeUser and added html_description to GET /team #12

Open
wants to merge 3 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
73 changes: 71 additions & 2 deletions v1/asana.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,14 @@ func (c *Client) httpClient() *http.Client {
}

type User struct {
UID UserID `json:"user"`
UID UserID `json:"id"`
GID string `json:"gid"`
Name string `json:"name"`
Email string `json:"email"`
ResourceType string `json:"resource_type"`
}

type UserID string
type UserID int64

var _ json.Marshaler = (*UserID)(nil)

Expand All @@ -104,6 +108,71 @@ func (uid UserID) String() string {
return str
}

type userSlurp struct {
User User `json:"data"`
}

func (c *Client) GetUser(id UserID) (*User, error) {
fullURL := fmt.Sprintf("%s/users/%d", baseURL, id)
req, err := http.NewRequest("GET", fullURL, nil)
if err != nil {
return nil, err
}
slurp, _, err := c.doAuthReqThenSlurpBody(req)
if err != nil {
return nil, err
}
u := new(userSlurp)
if err := json.Unmarshal(slurp, u); err != nil {
return nil, err
}
return &u.User, nil
}

func (c *Client) ListAllUsersInOrganization(teamID string) (pagesChan chan *UsersPage, cancelChan chan<- bool, err error) {
if teamID == "" {
return nil, nil, errEmptyTeamID
}

cancelChan = make(chan bool, 1)
pagesChan = make(chan *UsersPage)

go func() {
defer close(pagesChan)

path := fmt.Sprintf("/users?opt_fields=id,email&workspace=%s", teamID)
for {
fullURL := fmt.Sprintf("%s%s", baseURL, path)
req, err := http.NewRequest("GET", fullURL, nil)
if err != nil {
pagesChan <- &UsersPage{Err: err}
return
}
slurp, _, err := c.doAuthReqThenSlurpBody(req)
if err != nil {
pagesChan <- &UsersPage{Err: err}
return
}

pager := new(usersPager)
if err := json.Unmarshal(slurp, pager); err != nil {
pager.Err = err
}
usersPage := pager.UsersPage
pagesChan <- &usersPage

if np := pager.NextPage; np != nil && np.Path == "" {
path = np.Path
} else {
// End of this pagination
break
}
}
}()

return pagesChan, cancelChan, nil
}

var errUnimplemented = errors.New("unimplemented")

func (c *Client) personalAccessTokenAuthValue() string {
Expand Down
62 changes: 54 additions & 8 deletions v1/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ func (l *Layout) UnmarshalJSON(b []byte) error {
}

type ProjectRequest struct {
ProjectID string `json:"id"`
ProjectID string `json:"id"`
ProjectGID string `json:"project_gid"`

Name string `json:"name,omitempty"`
Notes string `json:"notes,omitempty"`
Expand All @@ -69,15 +70,18 @@ type ProjectRequest struct {

Workspace string `json:"workspace,omitempty"`

PublicToOrganization bool `json:"public,omitempty"`
PublicToOrganization bool `json:"public,omitempty"`
Members []string `json:"members,omitempty"`
}

type Project struct {
ID int64 `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Notes string `json:"notes,omitempty"`
Color string `json:"color,omitempty"`
Archived bool `json:"archived,omitempty"`
ID int64 `json:"id,omitempty"`
GID string `json:"gid,omitempty"`
Team *NamedAndIDdEntity `json:"team,omitempty"`
Name string `json:"name,omitempty"`
Notes string `json:"notes,omitempty"`
Color string `json:"color,omitempty"`
Archived bool `json:"archived,omitempty"`

Owner *NamedAndIDdEntity `json:"owner,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
Expand Down Expand Up @@ -159,6 +163,48 @@ func (c *Client) UpdateProject(preq *ProjectRequest) (*Project, error) {

}

func (c *Client) AddUsersToProject(preq *ProjectRequest) error {
if err := preq.Validate(); err != nil {
return err
}

qs, err := otils.ToURLValues(preq)
if err != nil {
return err
}

queryStr := qs.Encode()
fullURL := fmt.Sprintf("%s/projects/%s/addMembers", baseURL, preq.ProjectGID)
req, err := http.NewRequest("POST", fullURL, strings.NewReader(queryStr))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
_, _, err = c.doAuthReqThenSlurpBody(req)
return err
}

func (c *Client) RemoveUsersFromProject(preq *ProjectRequest) error {
if err := preq.Validate(); err != nil {
return err
}

qs, err := otils.ToURLValues(preq)
if err != nil {
return err
}

queryStr := qs.Encode()
fullURL := fmt.Sprintf("%s/projects/%s/removeMembers", baseURL, preq.ProjectGID)
req, err := http.NewRequest("POST", fullURL, strings.NewReader(queryStr))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
_, _, err = c.doAuthReqThenSlurpBody(req)
return err
}

func (c *Client) CreateProject(preq *ProjectRequest) (*Project, error) {
if err := preq.Validate(); err != nil {
return nil, err
Expand Down Expand Up @@ -250,7 +296,7 @@ func (c *Client) QueryForProjects(pq *ProjectQuery) (pagesChan chan *ProjectsPag
go func() {
defer close(pagesChan)

path := fmt.Sprintf("/projects?%s", qs.Encode())
path := fmt.Sprintf("/projects?opt_fields=id,gid,name,notes,team,members&%s", qs.Encode())
for {
fullURL := fmt.Sprintf("%s%s", baseURL, path)
req, _ := http.NewRequest("GET", fullURL, nil)
Expand Down
15 changes: 8 additions & 7 deletions v1/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"strings"

Expand All @@ -29,7 +28,11 @@ type teamCRUDData struct {
UserID UserID `json:"user"`
}

type Team NamedAndIDdEntity
type Team struct {
Name string `json:"name"`
ID int64 `json:"id"`
HtmlDescription string `json:"html_description"`
}

type TeamRequest struct {
// TeamID is a globally unique identifier for the team.
Expand Down Expand Up @@ -71,7 +74,7 @@ func (c *Client) AddUserToTeam(treq *TeamRequest) (*Team, error) {
return nil, err
}

fullURL := fmt.Sprintf("%s/teams/%s/addUser", baseURL, treq.TeamID)
fullURL := fmt.Sprintf("%s/teams/%s/addUser?user=%s", baseURL, treq.TeamID, treq.UserID)
req, err := http.NewRequest("POST", fullURL, strings.NewReader(qs.Encode()))
if err != nil {
return nil, err
Expand Down Expand Up @@ -100,7 +103,7 @@ func (c *Client) RemoveUserFromTeam(treq *TeamRequest) error {
return err
}

fullURL := fmt.Sprintf("%s/teams/%s/removeUser", baseURL, treq.TeamID)
fullURL := fmt.Sprintf("%s/teams/%s/removeUser?user=%s", baseURL, treq.TeamID, treq.UserID)
req, err := http.NewRequest("POST", fullURL, strings.NewReader(qs.Encode()))
if err != nil {
return err
Expand Down Expand Up @@ -150,7 +153,7 @@ func (c *Client) ListAllTeamsInOrganization(organizationID string) (pagesChan ch
return nil, nil, errEmptyOrganizationID
}

startingPath := fmt.Sprintf("/organizations/%s/teams", organizationID)
startingPath := fmt.Sprintf("/organizations/%s/teams?opt_fields=html_description,name,id", organizationID)
return c.pageForTeams(startingPath)
}

Expand Down Expand Up @@ -182,7 +185,6 @@ func (c *Client) pageForTeams(path string) (pagesChan chan *TeamPage, cancelChan

for {
fullURL := fmt.Sprintf("%s%s", baseURL, path)
log.Printf("fullURL: %q\n", fullURL)
req, _ := http.NewRequest("GET", fullURL, nil)
slurp, _, err := c.doAuthReqThenSlurpBody(req)
if err != nil {
Expand Down Expand Up @@ -250,7 +252,6 @@ func (c *Client) ListAllUsersInTeam(teamID string) (pagesChan chan *UsersPage, c
if err := json.Unmarshal(slurp, pager); err != nil {
pager.Err = err
}

usersPage := pager.UsersPage
pagesChan <- &usersPage

Expand Down