Skip to content
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
3 changes: 3 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@



49 changes: 49 additions & 0 deletions tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package teamwork

import (
"encoding/json"
"fmt"
)

type ModTag struct {
Content string `json:"content"`
}

func (conn *Connection) AddTag(taskId int, tag string) error {
method := "PUT"
url := fmt.Sprintf("%stasks/%d/tags.json", conn.Account.Url, taskId)
fmt.Printf("adding tag %s to URL: %s\n", tag, url)
m := &ModTag{Content: tag}
mp := make(map[string]*ModTag)
mp["tags"] = m
data_s, _ := json.Marshal(mp)
//fmt.Printf("JSON:\n%s\n", data_s)
data := []byte(data_s)
_, _, err := postrequest(conn.ApiToken, method, url, data)
if err != nil {
return fmt.Errorf("Cannot set tag %s on %d", tag, taskId)
}
return nil
}

type omg struct {
Tags map[string]string `json:"tags"`
RemoveProvidedTags string `json:"removeProvidedTags"`
}

func (conn *Connection) RemoveTag(taskId int, tag string) error {
method := "PUT"
url := fmt.Sprintf("%stasks/%d/tags.json", conn.Account.Url, taskId)
fmt.Printf("removing tag %s to URL: %s\n", tag, url)
o := &omg{Tags: make(map[string]string)}
o.Tags["content"] = tag
o.RemoveProvidedTags = "true"
data_s, _ := json.Marshal(o)
//fmt.Printf("JSON:\n%s\n", data_s)
data := []byte(data_s)
_, _, err := postrequest(conn.ApiToken, method, url, data)
if err != nil {
return fmt.Errorf("Cannot set tag %s on %d", tag, taskId)
}
return nil
}
46 changes: 29 additions & 17 deletions teamwork.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package teamwork

import (
"bytes"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -74,23 +75,7 @@ func Connect(ApiToken string) (*Connection, error) {

// request is the base level function for calling the TeamWork API.
func request(token, method, url string) (io.ReadCloser, http.Header, error) {
client := &http.Client{}
req, err := http.NewRequest(method, url, nil) // TODO: Add payload to support POST
if err != nil {
log.Printf("NewRequest: ", err)
return nil, nil, err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth(token, "notused")

resp, err := client.Do(req)
if err != nil {
log.Printf("Do: ", err)
return nil, nil, err
}

return resp.Body, resp.Header, nil
return postrequest(token, method, url, nil)
}

// build_params takes a struct and builds query params based
Expand Down Expand Up @@ -203,3 +188,30 @@ func get_headers(headers http.Header, obj interface{}) {
}
}
}

// request is the base level function for calling the TeamWork API.
func postrequest(token, method, url string, data []byte) (io.ReadCloser, http.Header, error) {
var err error
var req *http.Request
client := &http.Client{}
if data != nil {
req, err = http.NewRequest(method, url, bytes.NewBuffer(data))
} else {
req, err = http.NewRequest(method, url, nil)
}
if err != nil {
log.Printf("NewRequest: ", err)
return nil, nil, err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth(token, "notused")

resp, err := client.Do(req)
if err != nil {
log.Printf("Do: ", err)
return nil, nil, err
}

return resp.Body, resp.Header, nil
}