config.Config.getAuthData   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
package config
2
3
import (
4
	"os"
5
	"time"
6
7
	"github.com/evalphobia/minfraud-api-go/client"
8
)
9
10
const (
11
	defaultEnvAccountID  = "MINFRAUD_ACCOUNT_ID"
12
	defaultEnvLicenseKey = "MINFRAUD_LICENSE_KEY"
13
)
14
15
var (
16
	envAccountID  string
17
	envLicenseKey string
18
)
19
20
func init() {
21
	envAccountID = os.Getenv(defaultEnvAccountID)
22
	envLicenseKey = os.Getenv(defaultEnvLicenseKey)
23
}
24
25
// Config contains parameters for minFraud.
26
type Config struct {
27
	AccountID  string
28
	LicenseKey string
29
30
	Debug   bool
31
	Timeout time.Duration
32
}
33
34
// Client generates client.Client from Config data.
35
func (c Config) Client() (*client.Client, error) {
36
	cli := client.New()
37
	cli.SetOption(client.Option{
38
		Debug:   c.Debug,
39
		Timeout: c.Timeout,
40
	})
41
	cli.SetAuthData(c.getAuthData())
42
	return cli, nil
43
}
44
45
// getAuthData returns MaxMind's AccountID and LicenseKey from Config data or Environment variables.
46
func (c Config) getAuthData() (accountID, licenseKey string) {
47
	accountID = c.AccountID
48
	if accountID == "" {
49
		accountID = envAccountID
50
	}
51
52
	licenseKey = c.LicenseKey
53
	if licenseKey == "" {
54
		licenseKey = envLicenseKey
55
	}
56
	return
57
}
58