This repository has been archived by the owner on Oct 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdothill.go
131 lines (116 loc) · 3.59 KB
/
dothill.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
/*
* Copyright (c) 2021 Enix, SAS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Authors:
* Paul Laffitte <[email protected]>
* Arthur Chaloin <[email protected]>
* Alexandre Buisine <[email protected]>
*/
package dothill
import (
"crypto/tls"
"errors"
"fmt"
"net/http"
"strings"
"time"
"k8s.io/klog"
)
// Client : Can be used to request the dothill API
type Client struct {
Username string
Password string
Addr string
HTTPClient http.Client
Collector *Collector
sessionKey string
}
// NewClient : Creates a dothill client by setting up its HTTP client
func NewClient() *Client {
return &Client{
HTTPClient: http.Client{
Timeout: time.Duration(15 * time.Second),
Transport: &http.Transport{
// Proxy: http.ProxyURL(proxy),
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
},
Collector: newCollector(),
}
}
// Request : Execute the given request with client's configuration
// Deprecated: Use FormattedRequest instead
func (client *Client) Request(endpoint string) (*Response, *ResponseStatus, error) {
if client.Addr == "" {
err := errors.New("missing server address")
return nil, NewErrorStatus(err.Error()), err
}
return client.request(&Request{Endpoint: endpoint})
}
// FormattedRequest : Format and execute the given request with client's configuration
func (client *Client) FormattedRequest(endpointFormat string, opts ...interface{}) (*Response, *ResponseStatus, error) {
endpoint := fmt.Sprintf(endpointFormat, opts...)
stopTrackAPICall := client.Collector.trackAPICall(endpointFormat)
resp, status, err := client.Request(endpoint)
stopTrackAPICall(err == nil)
return resp, status, err
}
func (client *Client) request(req *Request) (*Response, *ResponseStatus, error) {
isLoginReq := strings.Contains(req.Endpoint, "login")
if !isLoginReq {
if len(client.sessionKey) == 0 {
klog.V(1).Info("no session key stored, authenticating before sending request")
err := client.Login()
if err != nil {
return nil, NewErrorStatus("login failed"), err
}
}
klog.Infof("-> GET %s", req.Endpoint)
} else {
klog.Infof("-> GET /login/<hidden>")
}
raw, code, err := req.execute(client)
if code == 401 && !isLoginReq {
klog.V(1).Info("session key may have expired, trying to re-login")
err = client.Login()
if err != nil {
return nil, NewErrorStatus("re-login failed"), err
}
klog.V(1).Info("re-login succeed, re-trying request")
raw, _, err = req.execute(client)
}
if err != nil {
return nil, NewErrorStatus("request failed"), err
}
res, err := NewResponse(raw)
if err != nil {
if res != nil {
return res, res.GetStatus(), err
}
return nil, NewErrorStatus("corrupted response"), err
}
status := res.GetStatus()
if !isLoginReq {
klog.Infof("<- [%d %s] %s", status.ReturnCode, status.ResponseType, status.Response)
} else {
klog.Infof("<- [%d %s] <hidden>", status.ReturnCode, status.ResponseType)
}
if status.ResponseTypeNumeric != 0 {
return res, status, fmt.Errorf("Dothill API returned non-zero code %d (%s)", status.ReturnCode, status.Response)
}
return res, status, nil
}