Skip to content

Commit 3aab19e

Browse files
authored
Additional headers support (#6)
1 parent 6637bd7 commit 3aab19e

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

client.go

+22-5
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,23 @@ func (c *Client) SetHTTPProxyURL(proxyURL *url.URL) {
4545
c.httpClient.Transport.(*http.Transport).Proxy = http.ProxyURL(proxyURL)
4646
}
4747

48+
// RunOptions represents options that can be used to configure a Run
49+
// operation.
50+
type RunOptions struct {
51+
AdditionalHeaders map[string]string
52+
}
53+
4854
// Run executes the given method having the given params setting the response
4955
// value in the given result interface.
50-
func (c *Client) Run(method string, params interface{}, result interface{}) error {
56+
func (c *Client) Run(method string, params interface{}, result interface{}, opts ...RunOptions) error {
5157
request := NewRequest(method, params, RandInt(10000000, 99999999))
5258

5359
jsonRequest, err := json.Marshal(request)
5460
if err != nil {
5561
return err
5662
}
5763

58-
jsonResponse, err := c.sendJSONRequest(jsonRequest)
64+
jsonResponse, err := c.sendJSONRequest(jsonRequest, opts...)
5965
if err != nil {
6066
return err
6167
}
@@ -75,25 +81,29 @@ func (c *Client) Run(method string, params interface{}, result interface{}) erro
7581
return nil
7682
}
7783

84+
// NotifyOptions represents options that can be used to configure a Notify
85+
// operation.
86+
type NotifyOptions = RunOptions
87+
7888
// Notify executes the given method with the given parameters.
7989
// Doesn't expect any result.
80-
func (c *Client) Notify(method string, params interface{}) error {
90+
func (c *Client) Notify(method string, params interface{}, opts ...NotifyOptions) error {
8191
request := NewRequest(method, params, 0)
8292

8393
jsonRequest, err := json.Marshal(request)
8494
if err != nil {
8595
return err
8696
}
8797

88-
_, err = c.sendJSONRequest(jsonRequest)
98+
_, err = c.sendJSONRequest(jsonRequest, opts...)
8999
if err != nil {
90100
return err
91101
}
92102

93103
return nil
94104
}
95105

96-
func (c *Client) sendJSONRequest(jsonRequest []byte) ([]byte, error) {
106+
func (c *Client) sendJSONRequest(jsonRequest []byte, opts ...RunOptions) ([]byte, error) {
97107
var jsonResponse []byte
98108

99109
httpRequest, err := http.NewRequest("POST", c.serverURL, strings.NewReader(string(jsonRequest)))
@@ -102,6 +112,13 @@ func (c *Client) sendJSONRequest(jsonRequest []byte) ([]byte, error) {
102112
httpRequest.Header.Set("Accept", "application/json")
103113
httpRequest.Header.Set("Connection", "close")
104114

115+
// Apply additional headers
116+
for _, o := range opts {
117+
for key, value := range o.AdditionalHeaders {
118+
httpRequest.Header.Set(key, value)
119+
}
120+
}
121+
105122
httpResponse, err := c.httpClient.Do(httpRequest)
106123
if err != nil {
107124
return jsonResponse, err

0 commit comments

Comments
 (0)