-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
46 lines (40 loc) · 873 Bytes
/
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
package gofortiadc
import (
"fmt"
"io"
"net/http"
"net/url"
)
// Client represents a FortiADC API client instance
type Client struct {
Client *http.Client
Address string
Username string
Password string
Token string
VDom string
}
// NewRequest create an http.Request with authorization header set
func (c *Client) NewRequest(method string, path string, body io.Reader) (*http.Request, error) {
uri, err := c.getUrl(path)
if err != nil {
return nil, err
}
req, err := http.NewRequest(method, uri, body)
if err == nil {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.Token))
}
return req, err
}
func (c *Client) getUrl(path string) (string, error) {
u, err := url.Parse(path)
if err != nil {
return "", err
}
q := u.Query()
if c.VDom != "" {
q.Set("vdom", c.VDom)
}
u.RawQuery = q.Encode()
return u.String(), nil
}