generated from moul/golang-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from moul/dev/moul/v1
feat: v1
- Loading branch information
Showing
18 changed files
with
413 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
HTTP/1.1 302 Found | ||
Connection: keep-alive | ||
Datacenter-Time-End: 1660117185.120 | ||
Date: Wed, 10 Aug 2022 07:39:45 GMT | ||
Location: /-/login?u=%2Fusers%2Fme | ||
Server: nginx | ||
Strict-Transport-Security: max-age=31536000; includeSubDomains | ||
X-Asana-Pageload-App: pageload-prod | ||
X-Asana-Pageload-Pod: pageload-5b6b5cfcf-bdjcw | ||
X-Loadbalancer: prod-lb021.ec2 | ||
X-Robots-Tag: none | ||
Content-Length: 0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,19 @@ | ||
GOPKG ?= moul.io/asanaman | ||
DOCKER_IMAGE ?= moul/asanaman | ||
GOBINS ?= . | ||
GOBINS ?= ./cmd/asanaman | ||
NPM_PACKAGES ?= . | ||
|
||
include rules.mk | ||
|
||
generate: install | ||
run: fmt | ||
@# requires $ASANAMAN_TOKEN | ||
go run ./cmd/asanaman --debug me | ||
|
||
generate: | ||
GO111MODULE=off go get github.com/campoy/embedmd | ||
mkdir -p .tmp | ||
echo 'foo@bar:~$$ asanaman' > .tmp/usage.txt | ||
asanaman 2>&1 >> .tmp/usage.txt | ||
(ASANAMAN_TOKEN=foo ASANAMAN_DOMAIN=foo go run ./cmd/asanaman 2>&1 || true) >> .tmp/usage.txt | ||
embedmd -w README.md | ||
rm -rf .tmp | ||
.PHONY: generate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
package asana | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
|
||
"github.com/gregjones/httpcache" | ||
"github.com/gregjones/httpcache/diskcache" | ||
"go.uber.org/multierr" | ||
"go.uber.org/zap" | ||
"moul.io/hcfilters" | ||
) | ||
|
||
const ( | ||
defaultUserAgent = "asanaman/1.0" | ||
defaultBaseURL = "https://app.asana.com/api/1.0/" | ||
) | ||
|
||
type Client struct { | ||
// args | ||
token string | ||
domain string | ||
cachePath string | ||
logger *zap.Logger | ||
|
||
// internal | ||
http *http.Client | ||
baseURL *url.URL | ||
userAgent string | ||
} | ||
|
||
func New(token, domain, cachePath string, logger *zap.Logger) (*Client, error) { | ||
client := Client{ | ||
token: token, | ||
domain: domain, | ||
cachePath: cachePath, | ||
userAgent: defaultUserAgent, | ||
logger: logger, | ||
} | ||
client.baseURL, _ = url.Parse(defaultBaseURL) | ||
|
||
client.http = &http.Client{ | ||
Transport: httpcache.NewTransport( | ||
hcfilters.MaxSize( // skip caching results > 10Mb | ||
diskcache.New(cachePath), | ||
10*1024*1024, // nolint:gomnd | ||
), | ||
), | ||
} | ||
|
||
return &client, nil | ||
} | ||
|
||
type ReqOpts struct { | ||
Method string | ||
Path string | ||
Data interface{} | ||
Form url.Values | ||
// opt *Filter | ||
} | ||
|
||
type request struct { | ||
Data interface{} `json:"data,omitempty"` | ||
} | ||
|
||
type response struct { | ||
Data interface{} `json:"data,omitempty"` | ||
Errors []asanaError `json:"errors,omitempty"` | ||
} | ||
|
||
type asanaError struct { | ||
Phrase string `json:"phrase,omitempty"` | ||
Message string `json:"message,omitempty"` | ||
} | ||
|
||
func (e asanaError) Error() string { | ||
return fmt.Sprintf("%v - %v", e.Message, e.Phrase) | ||
} | ||
|
||
func (c *Client) Request(ctx context.Context, opts ReqOpts, ret interface{}) error { | ||
logger := c.logger | ||
defer func() { logger.Debug("req") }() | ||
// compute path | ||
rel, err := url.Parse(opts.Path) | ||
if err != nil { | ||
return fmt.Errorf("invalid path: %w", err) | ||
} | ||
u := c.baseURL.ResolveReference(rel) | ||
logger = logger.With(zap.String("u", u.String())) | ||
|
||
// compute input body | ||
var body io.Reader | ||
switch { | ||
case opts.Data != nil: | ||
b, err := json.Marshal(request{Data: opts.Data}) | ||
if err != nil { | ||
return fmt.Errorf("encode body: %w", err) | ||
} | ||
c.logger.Debug("") | ||
body = bytes.NewReader(b) | ||
case opts.Form != nil: | ||
body = strings.NewReader(opts.Form.Encode()) | ||
} | ||
|
||
// init request | ||
req, err := http.NewRequest(opts.Method, u.String(), body) | ||
if err != nil { | ||
return fmt.Errorf("create request: %w", err) | ||
} | ||
req.Header.Set("User-Agent", c.userAgent) | ||
req.Header.Add("Authorization", "Bearer "+c.token) | ||
|
||
// compute input body | ||
switch { | ||
case opts.Data != nil: | ||
req.Header.Set("Content-Type", "application/json") | ||
case opts.Form != nil: | ||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||
} | ||
|
||
// perform request | ||
resp, err := c.http.Do(req.WithContext(ctx)) | ||
if err != nil { | ||
return fmt.Errorf("HTTP request: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
if resp.StatusCode == http.StatusUnauthorized { | ||
return ErrUnauthorized | ||
} | ||
|
||
// compute response | ||
res := &response{Data: ret} | ||
err = json.NewDecoder(resp.Body).Decode(res) | ||
if err != nil { | ||
return fmt.Errorf("cannot parse response: %w", err) | ||
} | ||
|
||
// error handling | ||
var errs error | ||
for _, err := range res.Errors { | ||
errs = multierr.Append(errs, err) | ||
} | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package asana | ||
|
||
import "context" | ||
|
||
func (c *Client) Me(ctx context.Context) (*User, error) { | ||
var user User | ||
return &user, c.Request(ctx, ReqOpts{Path: "users/me"}, &user) | ||
} | ||
|
||
type ProjectListOpts struct{} | ||
|
||
func (c *Client) ProjectList(ctx context.Context, opts ProjectListOpts) (*Projects, error) { | ||
var projects Projects | ||
return &projects, c.Request(ctx, ReqOpts{Path: "projects"}, &projects) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package asana | ||
|
||
import "errors" | ||
|
||
var ErrUnauthorized = errors.New("asana: unauthorized") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package asana | ||
|
||
type Workspace struct { | ||
GID string `json:"gid,omitempty"` | ||
Name string `json:"name,omitempty"` | ||
ResourceType string `json:"resource_type,omitempty"` | ||
} | ||
|
||
type User struct { | ||
GID string `json:"gid,omitempty"` | ||
Email string `json:"email,omitempty"` | ||
Name string `json:"name,omitempty"` | ||
Photo struct { | ||
Image2121 string `json:"image_21x21,omitempty"` | ||
Image2727 string `json:"image_27x27,omitempty"` | ||
Image3636 string `json:"image_36x36,omitempty"` | ||
Image6060 string `json:"image_60x60,omitempty"` | ||
Image128128 string `json:"image_128x128,omitempty"` | ||
} `json:"photo,omitempty"` | ||
ResourceType string `json:"resource_type,omitempty"` | ||
Workspaces []Workspace `json:"workspaces,omitempty"` | ||
} | ||
|
||
type Project struct { | ||
GID string `json:"gid,omitempty"` | ||
Name string `json:"name,omitempty"` | ||
ResourceType string `json:"resource_type,omitempty"` | ||
} | ||
|
||
type Projects []Project |
Oops, something went wrong.