-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsyncsession.go
146 lines (115 loc) · 3.34 KB
/
syncsession.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
package v3io
import (
"encoding/base64"
"encoding/xml"
"fmt"
"github.com/nuclio/logger"
"github.com/valyala/fasthttp"
)
type SyncSession struct {
logger logger.Logger
context *SyncContext
authenticatioHeaderKey string
authenticatioHeaderValue string
}
func newSyncSession(parentLogger logger.Logger,
context *SyncContext,
username string,
password string,
label string,
sessionKey string) (*SyncSession, error) {
if sessionKey != "" {
//if sessionKey not empty
return &SyncSession{
logger: parentLogger.GetChild("session"),
context: context,
authenticatioHeaderKey: "X-v3io-session-key",
authenticatioHeaderValue: sessionKey,
}, nil
}
// generate token for basic authentication
usernameAndPassword := fmt.Sprintf("%s:%s", username, password)
encodedUsernameAndPassword := base64.StdEncoding.EncodeToString([]byte(usernameAndPassword))
return &SyncSession{
logger: parentLogger.GetChild("session"),
context: context,
authenticatioHeaderKey: "Authorization",
authenticatioHeaderValue: "Basic " + encodedUsernameAndPassword,
}, nil
}
func (ss *SyncSession) ListAll() (*Response, error) {
output := ListAllOutput{}
return ss.sendRequestAndXMLUnmarshal("GET", fmt.Sprintf("http://%s/", ss.context.clusterURL), nil, nil, &output)
}
func (ss *SyncSession) sendRequestViaContext(request *fasthttp.Request, response *fasthttp.Response) error {
request.Header.Set(ss.authenticatioHeaderKey, ss.authenticatioHeaderValue)
// delegate to context
return ss.context.sendRequest(request, response)
}
func (ss *SyncSession) sendRequest(
method string,
uri string,
headers map[string]string,
body []byte,
releaseResponse bool) (*Response, error) {
var success bool
var statusCode int
request := fasthttp.AcquireRequest()
response := allocateResponse()
// init request
request.SetRequestURI(uri)
request.Header.SetMethod(method)
request.SetBody(body)
if headers != nil {
for headerName, headerValue := range headers {
request.Header.Add(headerName, headerValue)
}
}
// execute the request
err := ss.sendRequestViaContext(request, response.response)
if err != nil {
goto cleanup
}
statusCode = response.response.StatusCode()
// did we get a 2xx response?
success = statusCode >= 200 && statusCode < 300
// make sure we got expected status
if !success {
err = NewErrorWithStatusCode(statusCode, "Failed %s with status %d", method, statusCode)
goto cleanup
}
cleanup:
// we're done with the request - the response must be released by the user
// unless there's an error
fasthttp.ReleaseRequest(request)
if err != nil {
response.Release()
return nil, err
}
// if the user doesn't need the response, release it
if releaseResponse {
response.Release()
return nil, nil
}
return response, nil
}
func (ss *SyncSession) sendRequestAndXMLUnmarshal(
method string,
uri string,
headers map[string]string,
body []byte,
output interface{}) (*Response, error) {
response, err := ss.sendRequest(method, uri, headers, body, false)
if err != nil {
return nil, err
}
// unmarshal the body into the output
err = xml.Unmarshal(response.response.Body(), output)
if err != nil {
response.Release()
return nil, err
}
// set output in response
response.Output = output
return response, nil
}