|
6 | 6 | "context" |
7 | 7 | "crypto/tls" |
8 | 8 | "encoding/json" |
| 9 | + "errors" |
9 | 10 | "fmt" |
10 | 11 | "io" |
11 | 12 | "net/http" |
@@ -71,10 +72,10 @@ type request struct { |
71 | 72 |
|
72 | 73 | // ClientOpts encapsulates the options given to NewClient. |
73 | 74 | type ClientOpts struct { |
74 | | - EndpointURL *url.URL |
75 | | - AccessToken string |
76 | | - AdditionalHeaders map[string]string |
77 | | - RequireAccessToken bool |
| 75 | + EndpointURL *url.URL |
| 76 | + AccessToken string |
| 77 | + AdditionalHeaders map[string]string |
| 78 | + RequireAccessTokenInCI bool |
78 | 79 |
|
79 | 80 | // Flags are the standard API client flags provided by NewFlags. If nil, |
80 | 81 | // default values will be used. |
@@ -139,20 +140,20 @@ func NewClient(opts ClientOpts) Client { |
139 | 140 |
|
140 | 141 | return &client{ |
141 | 142 | opts: ClientOpts{ |
142 | | - EndpointURL: opts.EndpointURL, |
143 | | - AccessToken: opts.AccessToken, |
144 | | - AdditionalHeaders: opts.AdditionalHeaders, |
145 | | - RequireAccessToken: opts.RequireAccessToken, |
146 | | - Flags: flags, |
147 | | - Out: opts.Out, |
| 143 | + EndpointURL: opts.EndpointURL, |
| 144 | + AccessToken: opts.AccessToken, |
| 145 | + AdditionalHeaders: opts.AdditionalHeaders, |
| 146 | + RequireAccessTokenInCI: opts.RequireAccessTokenInCI, |
| 147 | + Flags: flags, |
| 148 | + Out: opts.Out, |
148 | 149 | }, |
149 | 150 | httpClient: httpClient, |
150 | 151 | } |
151 | 152 | } |
152 | 153 |
|
153 | | -func (c *client) requireAccessToken() error { |
154 | | - if c.opts.RequireAccessToken && c.opts.AccessToken == "" { |
155 | | - return fmt.Errorf("SRC_ACCESS_TOKEN must be set in CI") |
| 154 | +func (c *client) checkIfCIAccessTokenRequired() error { |
| 155 | + if c.opts.RequireAccessTokenInCI && c.opts.AccessToken == "" { |
| 156 | + return errors.New("SRC_ACCESS_TOKEN must be set when CI=true") |
156 | 157 | } |
157 | 158 |
|
158 | 159 | return nil |
@@ -183,7 +184,7 @@ func (c *client) NewHTTPRequest(ctx context.Context, method, p string, body io.R |
183 | 184 | } |
184 | 185 |
|
185 | 186 | func (c *client) createHTTPRequest(ctx context.Context, method, p string, body io.Reader) (*http.Request, error) { |
186 | | - if err := c.requireAccessToken(); err != nil { |
| 187 | + if err := c.checkIfCIAccessTokenRequired(); err != nil { |
187 | 188 | return nil, err |
188 | 189 | } |
189 | 190 |
|
@@ -216,7 +217,7 @@ func (c *client) createHTTPRequest(ctx context.Context, method, p string, body i |
216 | 217 | } |
217 | 218 |
|
218 | 219 | func (r *request) do(ctx context.Context, result any) (bool, error) { |
219 | | - if err := r.client.requireAccessToken(); err != nil { |
| 220 | + if err := r.client.checkIfCIAccessTokenRequired(); err != nil { |
220 | 221 | return false, err |
221 | 222 | } |
222 | 223 |
|
|
0 commit comments