-
Notifications
You must be signed in to change notification settings - Fork 0
/
baclient.go
50 lines (40 loc) · 995 Bytes
/
baclient.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
package odata
import (
"bytes"
"crypto/tls"
"net/http"
)
// this implements the odata.Client interface
// but uses HTTP Basic Auth for authentication
type BaClient struct {
Username string
Password string
insecure bool
client *http.Client
}
func NewBaClient(uname, pass string, acceptBadCert bool) *BaClient {
c := &BaClient{Username: uname, Password: pass}
c.insecure = acceptBadCert
tr := &http.Transport{}
if acceptBadCert {
tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
c.client = &http.Client{Transport: tr}
return c
}
func (c BaClient) DoRaw(meth, uri string, body []byte) (*http.Response, error) {
req, err := http.NewRequest(meth, uri, bytes.NewReader(body))
req.SetBasicAuth(c.Username, c.Password)
var res *http.Response
res, err = c.client.Do(req)
if err != nil {
return nil, err
}
return res, nil
}
func (c BaClient) Insecure() bool {
return c.insecure
}
func (c BaClient) Token() string {
return c.Username + ":" + c.Password
}