-
Notifications
You must be signed in to change notification settings - Fork 13
/
client.go
171 lines (140 loc) · 4.93 KB
/
client.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package cd
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"github.com/harness/harness-go-sdk/harness"
"github.com/harness/harness-go-sdk/harness/helpers"
"github.com/harness/harness-go-sdk/harness/utils"
"github.com/harness/harness-go-sdk/logging"
retryablehttp "github.com/hashicorp/go-retryablehttp"
log "github.com/sirupsen/logrus"
)
type service struct {
ApiClient *ApiClient
}
type Config struct {
AccountId string
APIKey string
Endpoint string
HTTPClient *retryablehttp.Client
UserAgent string
DefaultHeaders map[string]string
DebugLogging bool
Logger *log.Logger
}
type ApiClient struct {
common service // Reuse a single struct instead of allocating one for each service on the heap.
Configuration *Config
ApplicationClient *ApplicationClient
CloudProviderClient *CloudProviderClient
ConfigAsCodeClient *ConfigAsCodeClient
ConnectorClient *ConnectorClient
DelegateClient *DelegateClient
ExecutionClient *ExecutionClient
Log *log.Logger
SecretClient *SecretClient
ServiceClient *ServiceClient
SSOClient *SSOClient
UserClient *UserClient
TagClient *TagClient
TriggerClient *TriggerClient
ApprovalClient *ApprovalClient
}
func DefaultConfig() *Config {
logger := logging.NewLogger()
if helpers.EnvVars.DebugEnabled.Get() == "true" {
logger.SetLevel(log.DebugLevel)
}
cfg := &Config{
AccountId: helpers.EnvVars.AccountId.Get(),
APIKey: helpers.EnvVars.ApiKey.Get(),
DefaultHeaders: make(map[string]string),
Endpoint: helpers.EnvVars.Endpoint.GetWithDefault(utils.BaseUrl),
HTTPClient: utils.GetDefaultHttpClient(logger),
Logger: logger,
UserAgent: fmt.Sprintf("%s-%s", harness.SDKName, harness.SDKVersion),
}
return cfg
}
func NewClient(cfg *Config) (*ApiClient, error) {
if cfg == nil {
return nil, errors.New("config is is required")
}
if cfg.AccountId == "" {
return nil, cfg.NewInvalidConfigError("AccountId", nil)
}
if cfg.APIKey == "" {
return nil, cfg.NewInvalidConfigError("ApiKey", nil)
}
if cfg.Endpoint == "" {
return nil, cfg.NewInvalidConfigError("Endpoint", nil)
}
if cfg.HTTPClient == nil {
return nil, cfg.NewInvalidConfigError("Endpoint", nil)
}
// Set default headers for all requests
cfg.DefaultHeaders[helpers.HTTPHeaders.UserAgent.String()] = utils.CoalesceStr(cfg.UserAgent, fmt.Sprintf("%s-%s", harness.SDKName, harness.SDKVersion))
cfg.DefaultHeaders[helpers.HTTPHeaders.Accept.String()] = helpers.HTTPHeaders.ApplicationJson.String()
cfg.DefaultHeaders[helpers.HTTPHeaders.ContentType.String()] = helpers.HTTPHeaders.ApplicationJson.String()
cfg.DefaultHeaders[helpers.HTTPHeaders.ApiKey.String()] = cfg.APIKey
c := &ApiClient{}
c.Log = cfg.Logger
c.Configuration = cfg
c.common.ApiClient = c
// API Services
c.ApplicationClient = (*ApplicationClient)(&c.common)
c.ApprovalClient = (*ApprovalClient)(&c.common)
c.CloudProviderClient = (*CloudProviderClient)(&c.common)
c.ConfigAsCodeClient = (*ConfigAsCodeClient)(&c.common)
c.ConnectorClient = (*ConnectorClient)(&c.common)
c.DelegateClient = (*DelegateClient)(&c.common)
c.ExecutionClient = (*ExecutionClient)(&c.common)
c.SecretClient = (*SecretClient)(&c.common)
c.ServiceClient = (*ServiceClient)(&c.common)
c.SSOClient = (*SSOClient)(&c.common)
c.UserClient = (*UserClient)(&c.common)
c.TagClient = (*TagClient)(&c.common)
c.TriggerClient = (*TriggerClient)(&c.common)
return c, nil
}
func (client *ApiClient) NewAuthorizedGetRequest(path string) (*retryablehttp.Request, error) {
return client.NewAuthorizedRequest(path, http.MethodGet, nil)
}
func (client *ApiClient) NewAuthorizedPostRequest(path string, rawBody interface{}) (*retryablehttp.Request, error) {
return client.NewAuthorizedRequest(path, http.MethodPost, rawBody)
}
func (client *ApiClient) NewAuthorizedDeleteRequest(path string) (*retryablehttp.Request, error) {
return client.NewAuthorizedRequest(path, http.MethodDelete, nil)
}
func (c *ApiClient) NewAuthorizedRequest(path string, method string, rawBody interface{}) (*retryablehttp.Request, error) {
url := strings.Join([]string{c.Configuration.Endpoint, path}, "")
req, err := retryablehttp.NewRequest(method, url, rawBody)
if err != nil {
return nil, err
}
for key, value := range c.Configuration.DefaultHeaders {
req.Header.Set(key, value)
}
return req, err
}
func (c *ApiClient) getJson(req *retryablehttp.Request, obj interface{}) error {
res, err := c.Configuration.HTTPClient.Do(req)
if err != nil {
return err
}
if res.StatusCode == http.StatusUnauthorized {
return errors.New("unauthorized")
}
defer res.Body.Close()
// Unmarshal into our response object
if err := json.NewDecoder(res.Body).Decode(obj); err != nil {
return fmt.Errorf("error decoding response: %s", err)
}
if res.StatusCode == http.StatusUnauthorized {
return errors.New("unauthorized")
}
return nil
}