Lots of structure
This commit is contained in:
143
asana/client.go
143
asana/client.go
@@ -1,44 +1,143 @@
|
|||||||
package asana
|
package asana
|
||||||
|
|
||||||
|
import "encoding/json"
|
||||||
|
import "io/ioutil"
|
||||||
import "fmt"
|
import "fmt"
|
||||||
import "net/http"
|
import "net/http"
|
||||||
|
import "net/url"
|
||||||
import "os"
|
import "os"
|
||||||
|
|
||||||
type withHeader struct {
|
import "github.com/firestuff/asana-rules/headers"
|
||||||
Header http.Header
|
|
||||||
rt http.RoundTripper
|
type Client struct {
|
||||||
|
client *http.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func WithHeader(rt http.RoundTripper) withHeader {
|
type Project struct {
|
||||||
if rt == nil {
|
GID string `json:"gid"`
|
||||||
rt = http.DefaultTransport
|
Name string `json:"name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
return withHeader{
|
type Task struct {
|
||||||
Header: make(http.Header),
|
GID string `json:"gid"`
|
||||||
rt: rt,
|
Name string `json:"name"`
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h withHeader) RoundTrip(req *http.Request) (*http.Response, error) {
|
type User struct {
|
||||||
for k, v := range h.Header {
|
GID string `json:"gid"`
|
||||||
req.Header[k] = v
|
Name string `json:"name"`
|
||||||
|
Email string `json:"email"`
|
||||||
}
|
}
|
||||||
|
|
||||||
return h.rt.RoundTrip(req)
|
type Workspace struct {
|
||||||
|
GID string `json:"gid"`
|
||||||
|
Name string `json:"name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func Fetch() {
|
type projectsResponse struct {
|
||||||
c := &http.Client{}
|
Data []*Project `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
rt := WithHeader(c.Transport)
|
type userResponse struct {
|
||||||
rt.Header.Set("Authorization", fmt.Sprintf("Bearer %s", os.Getenv("ASANA_TOKEN")))
|
Data *User `json:"data"`
|
||||||
c.Transport = rt
|
}
|
||||||
|
|
||||||
resp, err := c.Get("https://app.asana.com/api/1.0/users/me")
|
type workspacesResponse struct {
|
||||||
|
Data []*Workspace `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewClient(token string) *Client {
|
||||||
|
c := &Client{
|
||||||
|
client: &http.Client{},
|
||||||
|
}
|
||||||
|
|
||||||
|
hdrs := headers.NewHeaders(c.client)
|
||||||
|
hdrs.Add("Accept", "application/json")
|
||||||
|
hdrs.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
||||||
|
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewClientFromEnv() *Client {
|
||||||
|
return NewClient(os.Getenv("ASANA_TOKEN"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) Me() (*User, error) {
|
||||||
|
resp := &userResponse{}
|
||||||
|
err := c.get("users/me", nil, resp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return nil, err
|
||||||
|
}
|
||||||
|
return resp.Data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("%#v\n", resp)
|
func (c *Client) Projects(workspaceGID string) ([]*Project, error) {
|
||||||
|
resp := &projectsResponse{}
|
||||||
|
path := fmt.Sprintf("workspaces/%s/projects", workspaceGID)
|
||||||
|
err := c.get(path, nil, resp)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return resp.Data, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) Workspaces() ([]*Workspace, error) {
|
||||||
|
resp := &workspacesResponse{}
|
||||||
|
err := c.get("workspaces", nil, resp)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return resp.Data, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns one workspace if there is only one
|
||||||
|
func (c *Client) Workspace() (*Workspace, error) {
|
||||||
|
workspaces, err := c.Workspaces()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(workspaces) != 1 {
|
||||||
|
return nil, fmt.Errorf("%d workspaces found", len(workspaces))
|
||||||
|
}
|
||||||
|
|
||||||
|
return workspaces[0], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
const baseURL = "https://app.asana.com/api/1.0/"
|
||||||
|
|
||||||
|
func (c *Client) get(path string, values *url.Values, out interface{}) error {
|
||||||
|
if values == nil {
|
||||||
|
values = &url.Values{}
|
||||||
|
}
|
||||||
|
values.Add("limit", "100")
|
||||||
|
|
||||||
|
url := fmt.Sprintf("%s%s?%s", baseURL, path, values.Encode())
|
||||||
|
fmt.Printf("%s\n", url)
|
||||||
|
|
||||||
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := c.client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
37
headers/headers.go
Normal file
37
headers/headers.go
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
package headers
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
|
type Headers struct {
|
||||||
|
header http.Header
|
||||||
|
rt http.RoundTripper
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHeaders(c *http.Client) Headers {
|
||||||
|
if c.Transport == nil {
|
||||||
|
c.Transport = http.DefaultTransport
|
||||||
|
}
|
||||||
|
|
||||||
|
ret := Headers{
|
||||||
|
header: http.Header{},
|
||||||
|
rt: c.Transport,
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Transport = ret
|
||||||
|
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Headers) Add(key, value string) {
|
||||||
|
h.header.Add(key, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h Headers) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||||
|
for key, vals := range h.header {
|
||||||
|
for _, val := range vals {
|
||||||
|
req.Header.Add(key, val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return h.rt.RoundTrip(req)
|
||||||
|
}
|
||||||
25
main.go
25
main.go
@@ -1,7 +1,30 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
import "github.com/firestuff/asana-rules/asana"
|
import "github.com/firestuff/asana-rules/asana"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
asana.Fetch()
|
a := asana.NewClientFromEnv()
|
||||||
|
|
||||||
|
/*
|
||||||
|
me, err := a.Me()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
wrk, err := a.Workspace()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
prjs, err := a.Projects(wrk.GID)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, prj := range prjs {
|
||||||
|
fmt.Printf("%#v\n", prj)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user