MCPcopy
hub / github.com/digitalocean/doctl / GetGodoClient

Method GetGodoClient

doit.go:238–303  ·  view source on GitHub ↗

GetGodoClient returns a GodoClient.

(trace, allowRetries bool, accessToken string)

Source from the content-addressed store, hash-verified

236
237// GetGodoClient returns a GodoClient.
238func (c *LiveConfig) GetGodoClient(trace, allowRetries bool, accessToken string) (*godo.Client, error) {
239 if accessToken == "" {
240 return nil, fmt.Errorf("access token is required. (hint: run 'doctl auth init')")
241 }
242
243 tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: accessToken})
244 oauthClient := oauth2.NewClient(context.Background(), tokenSource)
245
246 args := []godo.ClientOpt{
247 godo.SetUserAgent(userAgent()),
248 }
249
250 logger := log.New(os.Stderr, "doctl: ", log.LstdFlags)
251
252 retryMax := viper.GetInt("http-retry-max")
253 retryWaitMax := viper.GetInt("http-retry-wait-max")
254 retryWaitMin := viper.GetInt("http-retry-wait-min")
255 if retryMax > 0 && allowRetries {
256 retryConfig := godo.RetryConfig{
257 RetryMax: retryMax,
258 }
259
260 if retryWaitMax > 0 {
261 retryConfig.RetryWaitMax = godo.PtrTo(float64(retryWaitMax))
262 }
263
264 if retryWaitMin > 0 {
265 retryConfig.RetryWaitMin = godo.PtrTo(float64(retryWaitMin))
266 }
267
268 if trace {
269 retryConfig.Logger = logger
270 }
271
272 args = append(args, godo.WithRetryAndBackoffs(retryConfig))
273 }
274
275 apiURL := viper.GetString("api-url")
276 if apiURL != "" {
277 args = append(args, godo.SetBaseURL(apiURL))
278 }
279
280 client, err := godo.New(oauthClient, args...)
281 if err != nil {
282 return nil, err
283 }
284
285 if trace {
286 r := newRecorder(client.HTTPClient.Transport)
287
288 go func() {
289 for {
290 select {
291 case msg := <-r.req:
292 logger.Println("->", strconv.Quote(msg))
293 case msg := <-r.resp:
294 logger.Println("<-", strconv.Quote(msg))
295 }

Callers

nothing calls this directly

Calls 5

userAgentFunction · 0.85
newRecorderFunction · 0.85
ErrorfMethod · 0.80
GetIntMethod · 0.65
GetStringMethod · 0.65

Tested by

no test coverage detected