@@ -40,14 +40,14 @@ func NewClient(region api.Region, key string, client Doer, logger log.FieldLogge
40
40
}
41
41
42
42
// GetInto processes a GET request and saves the response body into the given target.
43
- func (c * Client ) GetInto (endpoint string , target interface {}) error {
43
+ func (c * Client ) GetInto (endpoint string , target interface {}, reqOptions ... RequestOption ) error {
44
44
logger := c .Logger ().WithFields (
45
45
log.Fields {
46
46
"method" : "GetInto" ,
47
47
"endpoint" : endpoint ,
48
48
},
49
49
)
50
- response , err := c .Get (endpoint )
50
+ response , err := c .Get (endpoint , reqOptions ... )
51
51
if err != nil {
52
52
logger .Debug (err )
53
53
return err
@@ -60,14 +60,14 @@ func (c *Client) GetInto(endpoint string, target interface{}) error {
60
60
}
61
61
62
62
// PostInto processes a POST request and saves the response body into the given target.
63
- func (c * Client ) PostInto (endpoint string , body , target interface {}) error {
63
+ func (c * Client ) PostInto (endpoint string , body , target interface {}, reqOptions ... RequestOption ) error {
64
64
logger := c .Logger ().WithFields (
65
65
log.Fields {
66
66
"method" : "PostInto" ,
67
67
"endpoint" : endpoint ,
68
68
},
69
69
)
70
- response , err := c .Post (endpoint , body )
70
+ response , err := c .Post (endpoint , body , reqOptions ... )
71
71
if err != nil {
72
72
logger .Debug (err )
73
73
return err
@@ -80,7 +80,7 @@ func (c *Client) PostInto(endpoint string, body, target interface{}) error {
80
80
}
81
81
82
82
// Put processes a PUT request.
83
- func (c * Client ) Put (endpoint string , body interface {}) error {
83
+ func (c * Client ) Put (endpoint string , body interface {}, reqOptions ... RequestOption ) error {
84
84
logger := c .Logger ().WithFields (
85
85
log.Fields {
86
86
"method" : "Put" ,
@@ -92,17 +92,17 @@ func (c *Client) Put(endpoint string, body interface{}) error {
92
92
logger .Debug (err )
93
93
return err
94
94
}
95
- _ , err := c .DoRequest ("PUT" , endpoint , buf )
95
+ _ , err := c .DoRequest ("PUT" , endpoint , buf , reqOptions )
96
96
return err
97
97
}
98
98
99
99
// Get processes a GET request.
100
- func (c * Client ) Get (endpoint string ) (* http.Response , error ) {
101
- return c .DoRequest ("GET" , endpoint , nil )
100
+ func (c * Client ) Get (endpoint string , reqOptions ... RequestOption ) (* http.Response , error ) {
101
+ return c .DoRequest ("GET" , endpoint , nil , reqOptions )
102
102
}
103
103
104
104
// Post processes a POST request.
105
- func (c * Client ) Post (endpoint string , body interface {}) (* http.Response , error ) {
105
+ func (c * Client ) Post (endpoint string , body interface {}, reqOptions ... RequestOption ) (* http.Response , error ) {
106
106
logger := c .Logger ().WithFields (
107
107
log.Fields {
108
108
"method" : "Post" ,
@@ -114,19 +114,19 @@ func (c *Client) Post(endpoint string, body interface{}) (*http.Response, error)
114
114
logger .Debug (err )
115
115
return nil , err
116
116
}
117
- return c .DoRequest ("POST" , endpoint , buf )
117
+ return c .DoRequest ("POST" , endpoint , buf , reqOptions )
118
118
}
119
119
120
120
// DoRequest processes a http.Request and returns the response.
121
121
// Rate-Limiting and retrying is handled via the corresponding response headers.
122
- func (c * Client ) DoRequest (method , endpoint string , body io.Reader ) (* http.Response , error ) {
122
+ func (c * Client ) DoRequest (method , endpoint string , body io.Reader , reqOptions [] RequestOption ) (* http.Response , error ) {
123
123
logger := c .Logger ().WithFields (
124
124
log.Fields {
125
125
"method" : "DoRequest" ,
126
126
"endpoint" : endpoint ,
127
127
},
128
128
)
129
- request , err := c .NewRequest (method , endpoint , body )
129
+ request , err := c .NewRequest (method , endpoint , body , reqOptions ... )
130
130
if err != nil {
131
131
logger .Debug (err )
132
132
return nil , err
@@ -154,7 +154,7 @@ func (c *Client) DoRequest(method, endpoint string, body io.Reader) (*http.Respo
154
154
}
155
155
logger .Infof ("rate limited, waiting %d seconds" , seconds )
156
156
time .Sleep (time .Duration (seconds ) * time .Second )
157
- return c .DoRequest (method , endpoint , body )
157
+ return c .DoRequest (method , endpoint , body , reqOptions )
158
158
}
159
159
if response .StatusCode < 200 || response .StatusCode > 299 {
160
160
logger .Debugf ("error response: %v" , response .Status )
@@ -171,7 +171,7 @@ func (c *Client) DoRequest(method, endpoint string, body io.Reader) (*http.Respo
171
171
}
172
172
173
173
// NewRequest returns a new http.Request with necessary headers et.
174
- func (c * Client ) NewRequest (method , endpoint string , body io.Reader ) (* http.Request , error ) {
174
+ func (c * Client ) NewRequest (method , endpoint string , body io.Reader , reqOptions ... RequestOption ) (* http.Request , error ) {
175
175
logger := c .Logger ().WithFields (
176
176
log.Fields {
177
177
"method" : "NewRequest" ,
@@ -185,6 +185,9 @@ func (c *Client) NewRequest(method, endpoint string, body io.Reader) (*http.Requ
185
185
}
186
186
request .Header .Add (apiTokenHeaderKey , c .APIKey )
187
187
request .Header .Add ("Accept" , "application/json" )
188
+ for _ , opt := range reqOptions {
189
+ opt (request )
190
+ }
188
191
return request , nil
189
192
}
190
193
0 commit comments