diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..b28b04f --- /dev/null +++ b/README.txt @@ -0,0 +1,3 @@ + + + diff --git a/tags.go b/tags.go new file mode 100644 index 0000000..04ddac0 --- /dev/null +++ b/tags.go @@ -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 +} diff --git a/teamwork.go b/teamwork.go index 49e790e..eb3e770 100644 --- a/teamwork.go +++ b/teamwork.go @@ -1,6 +1,7 @@ package teamwork import ( + "bytes" "encoding/json" "fmt" "io" @@ -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 @@ -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 +}