Skip to content

Commit

Permalink
Change api client to pass through warnings
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Jackson <jacksontj.89@gmail.com>
  • Loading branch information
jacksontj committed Jun 13, 2019
1 parent 110ee50 commit c126de3
Show file tree
Hide file tree
Showing 4 changed files with 241 additions and 256 deletions.
60 changes: 11 additions & 49 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,45 +25,7 @@ import (
"time"
)

func NewErrorAPI(err error, warnings []string) Error {
if apiErr, ok := err.(Error); ok {
return apiErr
}

if err == nil && warnings == nil {
return nil
}
return &ErrorAPI{err, warnings}
}

type ErrorAPI struct {
err error
warnings []string
}

func (w *ErrorAPI) Err() error {
return w.err
}

func (w *ErrorAPI) Error() string {
if w.err != nil {
return w.err.Error()
}
return "Warnings: " + strings.Join(w.warnings, " , ")
}

func (w *ErrorAPI) Warnings() []string {
return w.warnings
}

// Error encapsulates an error + warning
type Error interface {
error
// Err returns the underlying error.
Err() error
// Warnings returns a list of warnings.
Warnings() []string
}
type Warnings []string

// DefaultRoundTripper is used if no RoundTripper is set in Config.
var DefaultRoundTripper http.RoundTripper = &http.Transport{
Expand Down Expand Up @@ -95,30 +57,30 @@ func (cfg *Config) roundTripper() http.RoundTripper {
// Client is the interface for an API client.
type Client interface {
URL(ep string, args map[string]string) *url.URL
Do(context.Context, *http.Request) (*http.Response, []byte, Error)
Do(context.Context, *http.Request) (*http.Response, []byte, Warnings, error)
}

// DoGetFallback will attempt to do the request as-is, and on a 405 it will fallback to a GET request.
func DoGetFallback(c Client, ctx context.Context, u *url.URL, args url.Values) (*http.Response, []byte, Error) {
func DoGetFallback(c Client, ctx context.Context, u *url.URL, args url.Values) (*http.Response, []byte, Warnings, error) {
req, err := http.NewRequest(http.MethodPost, u.String(), strings.NewReader(args.Encode()))
if err != nil {
return nil, nil, NewErrorAPI(err, nil)
return nil, nil, nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

resp, body, err := c.Do(ctx, req)
resp, body, warnings, err := c.Do(ctx, req)
if resp != nil && resp.StatusCode == http.StatusMethodNotAllowed {
u.RawQuery = args.Encode()
req, err = http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return nil, nil, NewErrorAPI(err, nil)
return nil, nil, warnings, err
}

} else {
if err != nil {
return resp, body, NewErrorAPI(err, nil)
return resp, body, warnings, err
}
return resp, body, nil
return resp, body, warnings, nil
}
return c.Do(ctx, req)
}
Expand Down Expand Up @@ -158,7 +120,7 @@ func (c *httpClient) URL(ep string, args map[string]string) *url.URL {
return &u
}

func (c *httpClient) Do(ctx context.Context, req *http.Request) (*http.Response, []byte, Error) {
func (c *httpClient) Do(ctx context.Context, req *http.Request) (*http.Response, []byte, Warnings, error) {
if ctx != nil {
req = req.WithContext(ctx)
}
Expand All @@ -170,7 +132,7 @@ func (c *httpClient) Do(ctx context.Context, req *http.Request) (*http.Response,
}()

if err != nil {
return nil, nil, NewErrorAPI(err, nil)
return nil, nil, nil, err
}

var body []byte
Expand All @@ -190,5 +152,5 @@ func (c *httpClient) Do(ctx context.Context, req *http.Request) (*http.Response,
case <-done:
}

return resp, body, NewErrorAPI(err, nil)
return resp, body, nil, err
}
4 changes: 2 additions & 2 deletions api/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func TestDoGetFallback(t *testing.T) {
client := &httpClient{client: *(server.Client())}

// Do a post, and ensure that the post succeeds.
_, b, err := DoGetFallback(client, context.TODO(), u, v)
_, b, _, err := DoGetFallback(client, context.TODO(), u, v)
if err != nil {
t.Fatalf("Error doing local request: %v", err)
}
Expand All @@ -169,7 +169,7 @@ func TestDoGetFallback(t *testing.T) {

// Do a fallbcak to a get.
u.Path = "/blockPost"
_, b, err = DoGetFallback(client, context.TODO(), u, v)
_, b, _, err = DoGetFallback(client, context.TODO(), u, v)
if err != nil {
t.Fatalf("Error doing local request: %v", err)
}
Expand Down
Loading

0 comments on commit c126de3

Please sign in to comment.