Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add context support, ErrUnauthorized and Doer interface for http.Client #4

Merged
merged 13 commits into from
Oct 15, 2016
15 changes: 13 additions & 2 deletions asana/asana.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"net/http"
"net/url"
"strings"
"time"

"github.com/google/go-querystring/query"
Expand Down Expand Up @@ -124,13 +125,15 @@ type (

Response struct {
Data interface{} `json:"data,omitempty"`
Errors []Error `json:"errors,omitempty"`
Errors Errors `json:"errors,omitempty"`
}

Error struct {
Phrase string `json:"phrase,omitempty"`
Message string `json:"message,omitempty"`
}

Errors []Error
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any restrictions on Errors? Can it contain 0 elements? 1 element?

)

func (f DoerFunc) Do(req *http.Request) (resp *http.Response, err error) {
Expand All @@ -141,6 +144,14 @@ func (e Error) Error() string {
return fmt.Sprintf("%v - %v", e.Message, e.Phrase)
}

func (e Errors) Error() string {
var sErrs []string
for _, err := range e {
sErrs = append(sErrs, err.Error())
}
return strings.Join(sErrs, ", ")
}

//NewClient created new asana client with doer.
//If doer is nil then http.DefaultClient used intead.
func NewClient(doer Doer) *Client {
Expand Down Expand Up @@ -272,7 +283,7 @@ func (c *Client) request(method string, path string, data interface{}, opt *Filt
res := &Response{Data: v}
err = json.NewDecoder(resp.Body).Decode(res)
if len(res.Errors) > 0 {
return res.Errors[0]
return res.Errors
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to this, at least 1 element. I think it's worth documenting that for the Errors type, so users will know to always expect either at least one error there.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct, add godoc for Errors type

}
return err
}
Expand Down