Skip to content

Commit

Permalink
minor changes to boost Go Report Card score
Browse files Browse the repository at this point in the history
  • Loading branch information
koron committed Jul 23, 2024
1 parent d26c4d4 commit 25f1b62
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
31 changes: 20 additions & 11 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,15 @@ func (c *Client) Do(ctx context.Context, method, pathOrURL string, body, receive
if strings.HasPrefix(pathOrURL, "jsonhttpc.Parse error: ") {
return errors.New(pathOrURL)
}
// prepare the request.
if ctx == nil {
ctx = context.Background()
}
u, err := c.parseURL(pathOrURL)
if err != nil {
return err
}
r, err := c.bodyReader(body)
if err != nil {
return err
}
req, err := http.NewRequestWithContext(ctx, method, u.String(), r)

// prepare the request.
req, err := c.newRawRequest(ctx, method, pathOrURL, body)
if err != nil {
return err
}
c.setupHeader(req, body)

// make messages round trip.
c.logReq(ctx, req)
Expand Down Expand Up @@ -124,6 +116,23 @@ func (c *Client) Do(ctx context.Context, method, pathOrURL string, body, receive
return nil
}

func (c *Client) newRawRequest(ctx context.Context, method, pathOrURL string, body any) (*http.Request, error) {
u, err := c.parseURL(pathOrURL)
if err != nil {
return nil, err
}
r, err := c.bodyReader(body)
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, method, u.String(), r)
if err != nil {
return nil, err
}
c.setupHeader(req, body)
return req, nil
}

func (c *Client) parseURL(pathOrURL string) (*url.URL, error) {
if c.u != nil {
return c.u.Parse(pathOrURL)
Expand Down
2 changes: 1 addition & 1 deletion path.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Params map[string]interface{}
// '\' escapes a rune succeeding.
//
// This returns a string with prefix "jsonhttpc.Parse error: " if detects some
// errors,
// errors.
func Path(s string, p Params) string {
s2, err := path(s, p)
if err != nil {
Expand Down

0 comments on commit 25f1b62

Please sign in to comment.