Files
automana/client/client.go

169 lines
3.1 KiB
Go
Raw Normal View History

2021-09-21 03:39:15 +00:00
package client
2021-09-05 20:18:11 +00:00
2021-09-11 03:53:50 +00:00
import "bytes"
2021-09-06 18:19:04 +00:00
import "encoding/json"
2021-09-05 20:18:11 +00:00
import "fmt"
2021-09-11 03:53:50 +00:00
import "io/ioutil"
2021-09-05 20:18:11 +00:00
import "net/http"
2021-09-06 18:19:04 +00:00
import "net/url"
2021-09-05 20:18:11 +00:00
import "os"
2021-09-06 18:19:04 +00:00
import "github.com/firestuff/asana-rules/headers"
2021-09-10 05:19:28 +00:00
2021-09-06 18:19:04 +00:00
type Client struct {
2021-09-10 04:57:54 +00:00
client *http.Client
2021-09-06 18:19:04 +00:00
}
2021-09-12 20:08:13 +00:00
type workspace struct {
2021-09-10 04:57:54 +00:00
GID string `json:"gid"`
Name string `json:"name"`
2021-09-06 18:19:04 +00:00
}
type workspacesResponse struct {
2021-09-12 20:08:13 +00:00
Data []*workspace `json:"data"`
2021-09-06 18:19:04 +00:00
}
func NewClient(token string) *Client {
2021-09-10 04:57:54 +00:00
c := &Client{
client: &http.Client{},
}
2021-09-06 18:19:04 +00:00
2021-09-10 04:57:54 +00:00
hdrs := headers.NewHeaders(c.client)
2021-09-06 18:19:04 +00:00
hdrs.Add("Accept", "application/json")
hdrs.Add("Authorization", fmt.Sprintf("Bearer %s", token))
2021-09-10 04:57:54 +00:00
return c
2021-09-06 18:19:04 +00:00
}
func NewClientFromEnv() *Client {
2021-09-10 04:57:54 +00:00
return NewClient(os.Getenv("ASANA_TOKEN"))
2021-09-05 20:18:11 +00:00
}
2021-09-12 20:08:13 +00:00
func (c *Client) InWorkspace(name string) (*WorkspaceClient, error) {
2021-09-12 21:49:54 +00:00
wrk, err := c.getWorkspaceByName(name)
if err != nil {
return nil, err
}
return &WorkspaceClient{
client: c,
workspace: wrk,
}, nil
2021-09-06 18:50:08 +00:00
}
2021-09-12 20:08:13 +00:00
func (c *Client) getWorkspaces() ([]*workspace, error) {
2021-09-10 04:57:54 +00:00
resp := &workspacesResponse{}
err := c.get("workspaces", nil, resp)
if err != nil {
return nil, err
}
return resp.Data, nil
2021-09-06 18:19:04 +00:00
}
2021-09-12 20:08:13 +00:00
func (c *Client) getWorkspaceByName(name string) (*workspace, error) {
wrks, err := c.getWorkspaces()
2021-09-10 04:57:54 +00:00
if err != nil {
return nil, err
}
2021-09-06 18:19:04 +00:00
2021-09-12 21:49:54 +00:00
for _, wrk := range wrks {
if wrk.Name == name {
return wrk, nil
}
}
2021-09-10 05:19:28 +00:00
2021-09-12 21:49:54 +00:00
return nil, fmt.Errorf("Workspace `%s` not found", name)
2021-09-10 05:19:28 +00:00
}
2021-09-06 18:19:04 +00:00
const baseURL = "https://app.asana.com/api/1.0/"
func (c *Client) get(path string, values *url.Values, out interface{}) error {
2021-09-10 04:57:54 +00:00
if values == nil {
values = &url.Values{}
}
values.Add("limit", "100")
url := fmt.Sprintf("%s%s?%s", baseURL, path, values.Encode())
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
resp, err := c.client.Do(req)
if err != nil {
2021-09-11 03:53:50 +00:00
return err
}
2021-09-13 00:11:30 +00:00
defer resp.Body.Close()
2021-09-11 03:53:50 +00:00
2021-09-12 20:08:13 +00:00
dec := json.NewDecoder(resp.Body)
2021-09-11 03:53:50 +00:00
if resp.StatusCode != 200 {
2021-09-12 21:49:54 +00:00
errorResp := &errorResponse{}
err = dec.Decode(errorResp)
2021-09-11 03:53:50 +00:00
if err != nil {
return err
}
2021-09-12 20:08:13 +00:00
return fmt.Errorf("%s: %s", resp.Status, errorResp.Errors[0].Message)
2021-09-11 03:53:50 +00:00
}
err = dec.Decode(out)
if err != nil {
return err
}
return nil
}
func (c *Client) post(path string, body interface{}, out interface{}) error {
return c.doWithBody("POST", path, body, out)
}
func (c *Client) put(path string, body interface{}, out interface{}) error {
return c.doWithBody("PUT", path, body, out)
}
func (c *Client) doWithBody(method string, path string, body interface{}, out interface{}) error {
2021-09-11 03:53:50 +00:00
url := fmt.Sprintf("%s%s", baseURL, path)
buf := &bytes.Buffer{}
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
err := enc.Encode(body)
2021-09-11 03:53:50 +00:00
if err != nil {
return err
}
req, err := http.NewRequest(method, url, buf)
2021-09-11 03:53:50 +00:00
if err != nil {
return err
}
resp, err := c.client.Do(req)
if err != nil {
2021-09-10 04:57:54 +00:00
return err
}
2021-09-13 00:11:30 +00:00
defer resp.Body.Close()
2021-09-10 04:57:54 +00:00
if resp.StatusCode != 200 {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return fmt.Errorf("%s: %s", resp.Status, string(body))
}
dec := json.NewDecoder(resp.Body)
err = dec.Decode(out)
if err != nil {
return err
}
return nil
}
2021-09-12 20:08:13 +00:00
func (wrk *workspace) String() string {
2021-09-10 04:57:54 +00:00
return fmt.Sprintf("%s (%s)", wrk.GID, wrk.Name)
2021-09-05 20:18:11 +00:00
}