-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.go
72 lines (60 loc) · 1.73 KB
/
helpers.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
package gobam
import (
"crypto/tls"
"fmt"
"net/http"
"github.com/fiorix/wsdl2go/soap"
)
// Client logs you in to BAM and keeps your session cookie
func Client(username string, password string, endpoint string, insecure bool) (ProteusAPI, error) {
//var response *http.Response
cli := soap.Client{
URL: "https://" + endpoint + "/Services/API?wsdl",
Namespace: Namespace,
Pre: setBlueCatAuthToken,
Post: getBlueCatAuthToken,
}
if insecure {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
cli.Config = client
}
soapService := NewProteusAPI(&cli)
err := soapService.Login(username, password)
if err != nil {
return nil, fmt.Errorf("Login error: %s", err)
}
return soapService, nil
}
// NewClient creates a client without a login
func NewClient(endpoint string, insecure bool) ProteusAPI {
//var response *http.Response
cli := soap.Client{
URL: "https://" + endpoint + "/Services/API?wsdl",
Namespace: Namespace,
Pre: setBlueCatAuthToken,
Post: getBlueCatAuthToken,
}
if insecure {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
cli.Config = client
}
soapService := NewProteusAPI(&cli)
return soapService
}
func setBlueCatAuthToken(request *http.Request) {
//a session cookie is required for all calls except Login
for i := range sessionCookies {
request.AddCookie(sessionCookies[i])
}
}
func getBlueCatAuthToken(response *http.Response) {
//response.Cookies() is usually empty except when calling Login
//couldn't think of a better way to handle this
sessionCookies = append(sessionCookies, response.Cookies()...)
}