...

Source file src/github.com/xanzy/go-gitlab/client_options.go

Documentation: github.com/xanzy/go-gitlab

     1  //
     2  // Copyright 2021, Sander van Harmelen
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  //
    16  
    17  package gitlab
    18  
    19  import (
    20  	"net/http"
    21  	"time"
    22  
    23  	retryablehttp "github.com/hashicorp/go-retryablehttp"
    24  )
    25  
    26  // ClientOptionFunc can be used to customize a new GitLab API client.
    27  type ClientOptionFunc func(*Client) error
    28  
    29  // WithBaseURL sets the base URL for API requests to a custom endpoint.
    30  func WithBaseURL(urlStr string) ClientOptionFunc {
    31  	return func(c *Client) error {
    32  		return c.setBaseURL(urlStr)
    33  	}
    34  }
    35  
    36  // WithCustomBackoff can be used to configure a custom backoff policy.
    37  func WithCustomBackoff(backoff retryablehttp.Backoff) ClientOptionFunc {
    38  	return func(c *Client) error {
    39  		c.client.Backoff = backoff
    40  		return nil
    41  	}
    42  }
    43  
    44  // WithCustomLeveledLogger can be used to configure a custom retryablehttp
    45  // leveled logger.
    46  func WithCustomLeveledLogger(leveledLogger retryablehttp.LeveledLogger) ClientOptionFunc {
    47  	return func(c *Client) error {
    48  		c.client.Logger = leveledLogger
    49  		return nil
    50  	}
    51  }
    52  
    53  // WithCustomLimiter injects a custom rate limiter to the client.
    54  func WithCustomLimiter(limiter RateLimiter) ClientOptionFunc {
    55  	return func(c *Client) error {
    56  		c.configureLimiterOnce.Do(func() {})
    57  		c.limiter = limiter
    58  		return nil
    59  	}
    60  }
    61  
    62  // WithCustomLogger can be used to configure a custom retryablehttp logger.
    63  func WithCustomLogger(logger retryablehttp.Logger) ClientOptionFunc {
    64  	return func(c *Client) error {
    65  		c.client.Logger = logger
    66  		return nil
    67  	}
    68  }
    69  
    70  // WithCustomRetry can be used to configure a custom retry policy.
    71  func WithCustomRetry(checkRetry retryablehttp.CheckRetry) ClientOptionFunc {
    72  	return func(c *Client) error {
    73  		c.client.CheckRetry = checkRetry
    74  		return nil
    75  	}
    76  }
    77  
    78  // WithCustomRetryMax can be used to configure a custom maximum number of retries.
    79  func WithCustomRetryMax(retryMax int) ClientOptionFunc {
    80  	return func(c *Client) error {
    81  		c.client.RetryMax = retryMax
    82  		return nil
    83  	}
    84  }
    85  
    86  // WithCustomRetryWaitMinMax can be used to configure a custom minimum and
    87  // maximum time to wait between retries.
    88  func WithCustomRetryWaitMinMax(waitMin, waitMax time.Duration) ClientOptionFunc {
    89  	return func(c *Client) error {
    90  		c.client.RetryWaitMin = waitMin
    91  		c.client.RetryWaitMax = waitMax
    92  		return nil
    93  	}
    94  }
    95  
    96  // WithErrorHandler can be used to configure a custom error handler.
    97  func WithErrorHandler(handler retryablehttp.ErrorHandler) ClientOptionFunc {
    98  	return func(c *Client) error {
    99  		c.client.ErrorHandler = handler
   100  		return nil
   101  	}
   102  }
   103  
   104  // WithHTTPClient can be used to configure a custom HTTP client.
   105  func WithHTTPClient(httpClient *http.Client) ClientOptionFunc {
   106  	return func(c *Client) error {
   107  		c.client.HTTPClient = httpClient
   108  		return nil
   109  	}
   110  }
   111  
   112  // WithRequestLogHook can be used to configure a custom request log hook.
   113  func WithRequestLogHook(hook retryablehttp.RequestLogHook) ClientOptionFunc {
   114  	return func(c *Client) error {
   115  		c.client.RequestLogHook = hook
   116  		return nil
   117  	}
   118  }
   119  
   120  // WithResponseLogHook can be used to configure a custom response log hook.
   121  func WithResponseLogHook(hook retryablehttp.ResponseLogHook) ClientOptionFunc {
   122  	return func(c *Client) error {
   123  		c.client.ResponseLogHook = hook
   124  		return nil
   125  	}
   126  }
   127  
   128  // WithoutRetries disables the default retry logic.
   129  func WithoutRetries() ClientOptionFunc {
   130  	return func(c *Client) error {
   131  		c.disableRetries = true
   132  		return nil
   133  	}
   134  }
   135  
   136  // WithRequestOptions can be used to configure default request options applied to every request.
   137  func WithRequestOptions(options ...RequestOptionFunc) ClientOptionFunc {
   138  	return func(c *Client) error {
   139  		c.defaultRequestOptions = append(c.defaultRequestOptions, options...)
   140  		return nil
   141  	}
   142  }
   143  

View as plain text