@@ -29,6 +29,7 @@ type RequestOptions struct {
2929 AuthType string
3030 Username string
3131 Verbose bool
32+ Trace bool
3233}
3334
3435// MultipartOptions contains options specific to multipart requests
@@ -44,7 +45,7 @@ type MultipartOptions struct {
4445// Client is an interface for API clients
4546type Client interface {
4647 GetAuthHeader (method , url string , authType string , username string ) (string , error )
47- BuildRequest (method , endpoint string , headers [] string , body io.Reader , contentType string , authType string , username string ) (* http.Request , error )
48+ BuildRequest (requestOptions RequestOptions , body io.Reader , contentType string ) (* http.Request , error )
4849 SendRequest (options RequestOptions ) (json.RawMessage , error )
4950 StreamRequest (options RequestOptions ) error
5051 SendMultipartRequest (options MultipartOptions ) (json.RawMessage , error )
@@ -114,19 +115,19 @@ func (c *ApiClient) GetAuthHeader(method, url string, authType string, username
114115}
115116
116117// BuildRequest builds an HTTP request
117- func (c * ApiClient ) BuildRequest (method , endpoint string , headers [] string , body io.Reader , contentType string , authType string , username string ) (* http.Request , error ) {
118- httpMethod := strings .ToUpper (method )
118+ func (c * ApiClient ) BuildRequest (requestOptions RequestOptions , body io.Reader , contentType string ) (* http.Request , error ) {
119+ httpMethod := strings .ToUpper (requestOptions . Method )
119120
120- url := endpoint
121- if ! strings .HasPrefix (strings .ToLower (endpoint ), "http" ) {
121+ url := requestOptions . Endpoint
122+ if ! strings .HasPrefix (strings .ToLower (requestOptions . Endpoint ), "http" ) {
122123 url = c .url
123124 if ! strings .HasSuffix (url , "/" ) {
124125 url += "/"
125126 }
126- if strings .HasPrefix (endpoint , "/" ) {
127- url += endpoint [1 :]
127+ if strings .HasPrefix (requestOptions . Endpoint , "/" ) {
128+ url += requestOptions . Endpoint [1 :]
128129 } else {
129- url += endpoint
130+ url += requestOptions . Endpoint
130131 }
131132 }
132133
@@ -135,7 +136,7 @@ func (c *ApiClient) BuildRequest(method, endpoint string, headers []string, body
135136 return nil , xurlErrors .NewHTTPError (err )
136137 }
137138
138- for _ , header := range headers {
139+ for _ , header := range requestOptions . Headers {
139140 parts := strings .SplitN (header , ":" , 2 )
140141 if len (parts ) == 2 {
141142 req .Header .Add (strings .TrimSpace (parts [0 ]), strings .TrimSpace (parts [1 ]))
@@ -147,14 +148,18 @@ func (c *ApiClient) BuildRequest(method, endpoint string, headers []string, body
147148 }
148149
149150 if req .Header .Get ("Authorization" ) == "" {
150- authHeader , err := c .GetAuthHeader (httpMethod , url , authType , username )
151+ authHeader , err := c .GetAuthHeader (httpMethod , url , requestOptions . AuthType , requestOptions . Username )
151152 if err == nil {
152153 req .Header .Add ("Authorization" , authHeader )
153154 }
154155 }
155156
156157 req .Header .Add ("User-Agent" , "xurl/" + version .Version )
157158
159+ if requestOptions .Trace {
160+ req .Header .Add ("X-B3-Flags" , "1" )
161+ }
162+
158163 return req , nil
159164}
160165
@@ -221,7 +226,7 @@ func (c *ApiClient) SendRequest(options RequestOptions) (json.RawMessage, error)
221226 }
222227 }
223228
224- req , err := c .BuildRequest (options . Method , options . Endpoint , options . Headers , body , contentType , options . AuthType , options . Username )
229+ req , err := c .BuildRequest (options , body , contentType )
225230
226231 if err != nil {
227232 return nil , xurlErrors .NewHTTPError (err )
@@ -280,7 +285,7 @@ func (c *ApiClient) SendMultipartRequest(options MultipartOptions) (json.RawMess
280285 return nil , xurlErrors .NewIOError (fmt .Errorf ("error closing multipart writer: %v" , err ))
281286 }
282287
283- req , err := c .BuildRequest (options .Method , options . Endpoint , options . Headers , body , writer .FormDataContentType (), options . AuthType , options . Username )
288+ req , err := c .BuildRequest (options .RequestOptions , body , writer .FormDataContentType ())
284289 if err != nil {
285290 return nil , xurlErrors .NewHTTPError (err )
286291 }
@@ -312,7 +317,7 @@ func (c *ApiClient) StreamRequest(options RequestOptions) error {
312317 }
313318 }
314319
315- req , err := c .BuildRequest (options . Method , options . Endpoint , options . Headers , body , contentType , options . AuthType , options . Username )
320+ req , err := c .BuildRequest (options , body , contentType )
316321 if err != nil {
317322 return xurlErrors .NewHTTPError (err )
318323 }
0 commit comments