Skip to content

Commit

Permalink
feat: disable env values with option
Browse files Browse the repository at this point in the history
Signed-off-by: Eray Ates <[email protected]>
  • Loading branch information
rytsh committed Dec 4, 2023
1 parent cb57b48 commit 367b3c5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,4 @@ if err != nil {
| `KLIENT_BASE_URL` | Base url of client same with `API_GATEWAY_ADDRESS` but more priority. |
| `KLIENT_INSECURE_SKIP_VERIFY` | Skip tls verify. Ex `KLIENT_INSECURE_SKIP_VERIFY=true` |
| `KLIENT_TIMEOUT` | Timeout for http client. Ex: `KLIENT_TIMEOUT=30s` |
| `KLIENT_RETRY_DISABLE` | Disable retry. Ex: `KLIENT_RETRY_DISABLE=true` |
19 changes: 15 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var (
EnvKlientBaseURLGlobal = "API_GATEWAY_ADDRESS"
EnvKlientInsecureSkipVerify = "KLIENT_INSECURE_SKIP_VERIFY"
EnvKlientTimeout = "KLIENT_TIMEOUT"
EnvKlientRetryDisable = "KLIENT_RETRY_DISABLE"
)

type Client struct {
Expand Down Expand Up @@ -72,11 +73,15 @@ func New(opts ...OptionClientFn) (*Client, error) {
}
}

if DisableEnvValues {
o.DisableEnvValues = true
}

var baseURL *url.URL
if o.BaseURL == "" {

Check failure on line 81 in client.go

View workflow job for this annotation

GitHub Actions / sonarcloud

`if o.BaseURL == ""` has complex nested blocks (complexity: 5) (nestif)

Check failure on line 81 in client.go

View workflow job for this annotation

GitHub Actions / sonarcloud

`if o.BaseURL == ""` has complex nested blocks (complexity: 5) (nestif)
baseURL := DefaultBaseURL

if !DisableEnvValues {
if !o.DisableEnvValues {
if baseURL == "" {
baseURL = os.Getenv(EnvKlientBaseURL)
}
Expand Down Expand Up @@ -113,7 +118,7 @@ func New(opts ...OptionClientFn) (*Client, error) {
}

// skip verify
if !DisableEnvValues {
if !o.DisableEnvValues {
if v, _ := strconv.ParseBool(os.Getenv(EnvKlientInsecureSkipVerify)); v {
o.InsecureSkipVerify = true
}
Expand All @@ -135,7 +140,13 @@ func New(opts ...OptionClientFn) (*Client, error) {
client.Transport.(*http.Transport).TLSClientConfig = tlsClientConfig
}

// disable retry
// disable
if !o.DisableEnvValues {
if v, _ := strconv.ParseBool(os.Getenv(EnvKlientRetryDisable)); v {
o.DisableRetry = true
}
}

if !o.DisableRetry {
// create retry client
retryClient := retryablehttp.Client{
Expand Down Expand Up @@ -178,7 +189,7 @@ func New(opts ...OptionClientFn) (*Client, error) {
}

// set timeout
if !DisableEnvValues {
if !o.DisableEnvValues {
if v, _ := time.ParseDuration(os.Getenv(EnvKlientTimeout)); v > 0 {
o.Timeout = v
}
Expand Down
1 change: 1 addition & 0 deletions example/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func main() {
client, err := klient.New(
klient.WithBaseURL("https://api.punkapi.com/v2/"),
// klient.WithBaseURL("https://expired.badssl.com/"),
// klient.WithDisableEnvValues(true),
// klient.WithLogger(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
// Level: slog.LevelDebug,
// }))),
Expand Down
9 changes: 9 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ type optionClientValue struct {
RetryLog bool
// OptionRetryFns is the retry options for default retry policy.
OptionRetryFns []OptionRetryFn
// DisableEnvValues is the flag to disable all env values check.
DisableEnvValues bool
}

// OptionClientFn is a function that configures the client.
Expand Down Expand Up @@ -201,3 +203,10 @@ func WithRetryOptions(opts ...OptionRetryFn) OptionClientFn {
options.OptionRetryFns = append(options.OptionRetryFns, opts...)
}
}

// WithDisableEnvValues configures the client to disable all env values check.
func WithDisableEnvValues(disableEnvValues bool) OptionClientFn {
return func(options *optionClientValue) {
options.DisableEnvValues = disableEnvValues
}
}

0 comments on commit 367b3c5

Please sign in to comment.