Skip to content

Commit

Permalink
feat(Client): Add BasicAuth settings
Browse files Browse the repository at this point in the history
Allow a user to attach basic auth headers to all requests generated by
Client.
  • Loading branch information
AdamSLevy committed Mar 28, 2019
1 parent cd73a7b commit 2554e1e
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import (
type Client struct {
http.Client
DebugRequest bool

BasicAuth bool
User string
Password string
}

// Request uses c to make a JSON-RPC 2.0 Request to url with the given method
Expand All @@ -27,6 +31,9 @@ type Client struct {
//
// Request uses a pseudorandom uint32 for the Request.ID.
//
// If c.BasicAuth is true then SetBasicAuth will be called on the http.Request
// using c.User and c.Password.
//
// If c.DebugRequest is true then the Request and Response will be printed to
// stdout.
func (c *Client) Request(url, method string, params, result interface{}) error {
Expand All @@ -49,6 +56,9 @@ func (c *Client) Request(url, method string, params, result interface{}) error {
return err
}
req.Header.Add("Content-Type", "application/json")
if c.BasicAuth {
req.SetBasicAuth(c.User, c.Password)
}
res, err := c.Do(req)
if err != nil {
return err
Expand Down

0 comments on commit 2554e1e

Please sign in to comment.