OLD | NEW |
1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
4 | 4 |
5 package internal | 5 package internal |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "encoding/json" | 9 "encoding/json" |
10 "fmt" | 10 "fmt" |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 func (r *Request) Do(c context.Context) error { | 64 func (r *Request) Do(c context.Context) error { |
65 // Grab a client first. Use same client for all retry attempts. | 65 // Grab a client first. Use same client for all retry attempts. |
66 var client *http.Client | 66 var client *http.Client |
67 if clientFactory != nil { | 67 if clientFactory != nil { |
68 var err error | 68 var err error |
69 if client, err = clientFactory(c, r.Scopes); err != nil { | 69 if client, err = clientFactory(c, r.Scopes); err != nil { |
70 return err | 70 return err |
71 } | 71 } |
72 } else { | 72 } else { |
73 client = http.DefaultClient | 73 client = http.DefaultClient |
| 74 if testTransport := c.Value(&testTransportKey); testTransport !=
nil { |
| 75 client = &http.Client{Transport: testTransport.(http.Rou
ndTripper)} |
| 76 } |
74 } | 77 } |
75 | 78 |
76 // Prepare a blob with the request body. Marshal it once, to avoid | 79 // Prepare a blob with the request body. Marshal it once, to avoid |
77 // remarshaling on retries. | 80 // remarshaling on retries. |
78 isJSON := false | 81 isJSON := false |
79 var bodyBlob []byte | 82 var bodyBlob []byte |
80 if blob, ok := r.Body.([]byte); ok { | 83 if blob, ok := r.Body.([]byte); ok { |
81 bodyBlob = blob | 84 bodyBlob = blob |
82 } else if r.Body != nil { | 85 } else if r.Body != nil { |
83 var err error | 86 var err error |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 return err | 138 return err |
136 } | 139 } |
137 if val != nil { | 140 if val != nil { |
138 if err = json.NewDecoder(resp.Body).Decode(val); err != nil { | 141 if err = json.NewDecoder(resp.Body).Decode(val); err != nil { |
139 logging.Errorf(c, "auth: URL fetch failed, bad JSON - %s
", err) | 142 logging.Errorf(c, "auth: URL fetch failed, bad JSON - %s
", err) |
140 return fmt.Errorf("auth: can't deserialize JSON at %q -
%s", r.URL, err) | 143 return fmt.Errorf("auth: can't deserialize JSON at %q -
%s", r.URL, err) |
141 } | 144 } |
142 } | 145 } |
143 return nil | 146 return nil |
144 } | 147 } |
OLD | NEW |