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
17package gitlab
18
19import (
20	"net/http"
21
22	retryablehttp "github.com/hashicorp/go-retryablehttp"
23)
24
25// ClientOptionFunc can be used to customize a new GitLab API client.
26type ClientOptionFunc func(*Client) error
27
28// WithBaseURL sets the base URL for API requests to a custom endpoint.
29func WithBaseURL(urlStr string) ClientOptionFunc {
30	return func(c *Client) error {
31		return c.setBaseURL(urlStr)
32	}
33}
34
35// WithCustomBackoff can be used to configure a custom backoff policy.
36func WithCustomBackoff(backoff retryablehttp.Backoff) ClientOptionFunc {
37	return func(c *Client) error {
38		c.client.Backoff = backoff
39		return nil
40	}
41}
42
43// WithCustomLeveledLogger can be used to configure a custom retryablehttp
44// leveled logger.
45func WithCustomLeveledLogger(leveledLogger retryablehttp.LeveledLogger) ClientOptionFunc {
46	return func(c *Client) error {
47		c.client.Logger = leveledLogger
48		return nil
49	}
50}
51
52// WithCustomLimiter injects a custom rate limiter to the client.
53func WithCustomLimiter(limiter RateLimiter) ClientOptionFunc {
54	return func(c *Client) error {
55		c.configureLimiterOnce.Do(func() {
56			c.limiter = limiter
57		})
58		return nil
59	}
60}
61
62// WithCustomLogger can be used to configure a custom retryablehttp logger.
63func 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.
71func WithCustomRetry(checkRetry retryablehttp.CheckRetry) ClientOptionFunc {
72	return func(c *Client) error {
73		c.client.CheckRetry = checkRetry
74		return nil
75	}
76}
77
78// WithHTTPClient can be used to configure a custom HTTP client.
79func WithHTTPClient(httpClient *http.Client) ClientOptionFunc {
80	return func(c *Client) error {
81		c.client.HTTPClient = httpClient
82		return nil
83	}
84}
85
86// WithRequestLogHook can be used to configure a custom request log hook.
87func WithRequestLogHook(hook retryablehttp.RequestLogHook) ClientOptionFunc {
88	return func(c *Client) error {
89		c.client.RequestLogHook = hook
90		return nil
91	}
92}
93
94// WithResponseLogHook can be used to configure a custom response log hook.
95func WithResponseLogHook(hook retryablehttp.ResponseLogHook) ClientOptionFunc {
96	return func(c *Client) error {
97		c.client.ResponseLogHook = hook
98		return nil
99	}
100}
101
102// WithoutRetries disables the default retry logic.
103func WithoutRetries() ClientOptionFunc {
104	return func(c *Client) error {
105		c.disableRetries = true
106		return nil
107	}
108}
109