-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
137 lines (117 loc) · 3.09 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package glair
import (
"net/http"
"strings"
)
const (
defaultUrl = "https://api.vision.glair.ai"
defaultVersion = "v1"
)
var (
defaultClient = http.DefaultClient
defaultLogger = &LeveledLogger{
Level: LevelNone,
}
)
// HTTPClient is an interface that users can implement
// to customize HTTP calls behavior when interacting
// with GLAIR Vision API
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
// Config provides configuration for a client instance,
// including credentials and API configuration such as
// base URL and API version.
type Config struct {
// Username represents username to be used for basic authentication
// with GLAIR Vision API
Username string
// Password represents password to be used for basic authentication
// with GLAIR Vision API
Password string
// API key represents API key to be used for authentication
// with GLAIR Vision API
ApiKey string
// BaseUrl represents base URL path for GLAIR Vision API
// endpoints. Defaults to "https://api.vision.glair.ai"
BaseUrl string
// ApiVersion represents GLAIR Vision API version to
// be called. Defaults to "v1"
ApiVersion string
// Client represents the HTTP client that is used
// to call the HTTP endpoints of GLAIR Vision API.
// Defaults to the default HTTP client of Go
Client HTTPClient
// Logger represents the logger to be used by
// GLAIR Vision Go SDK to log necessary informations.
// Defaults to no log
Logger Logger
}
// NewConfig creates a new configuration object with default values for
// base URL and API Version.
func NewConfig(username string, password string, apiKey string) *Config {
return &Config{
Username: username,
Password: password,
ApiKey: apiKey,
BaseUrl: defaultUrl,
ApiVersion: defaultVersion,
Client: defaultClient,
Logger: defaultLogger,
}
}
// GetEndpointURL creates service URL with base URL and API version
func (c *Config) GetEndpointURL(
service string,
endpoint string,
) string {
parts := []string{c.BaseUrl, service, c.ApiVersion, endpoint}
return strings.Join(parts, "/")
}
// WithCredentials sets user credentials for the configuration object
func (c *Config) WithCredentials(
username string,
password string,
apiKey string,
) *Config {
if c == nil {
return nil
}
c.Username = username
c.Password = password
c.ApiKey = apiKey
return c
}
// WithClient sets HTTP client for the configuration object
// that will be used for calling GLAIR Vision API endpoints
func (c *Config) WithClient(client HTTPClient) *Config {
if c == nil {
return nil
}
c.Client = client
return c
}
// WithBaseURL sets base API URL for the configuration object
func (c *Config) WithBaseURL(url string) *Config {
if c == nil {
return nil
}
c.BaseUrl = url
return c
}
// WithVersion sets API version for the configuration object
func (c *Config) WithVersion(version string) *Config {
if c == nil {
return nil
}
c.ApiVersion = version
return c
}
// WithLogger sets the logger to be used by client instances
func (c *Config) WithLogger(logger Logger) *Config {
if c == nil {
return nil
}
c.Logger = logger
return c
}