-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
42 lines (37 loc) · 854 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
package nap
import (
"net/http"
"strings"
)
type Client struct {
Client *http.Client
AuthInfo Authentication
}
func NewClient() *Client {
return &Client{
Client: http.DefaultClient,
}
}
func (c *Client) SetAuth(auth Authentication) {
c.AuthInfo = auth
}
func (c *Client) ProcessRequest(baseURL string, res *RestResource, params map[string]string) error {
endpoint := strings.TrimLeft(res.RenderEndpoint(params), "/")
trimmedBaseURL := strings.TrimRight(baseURL, "/")
url := trimmedBaseURL + "/" + endpoint
req, err := http.NewRequest(res.Method, url, nil)
if err != nil {
return err
}
if c.AuthInfo != nil {
req.Header.Add("Authorization", c.AuthInfo.AuthorizationHeader())
}
resp, err := c.Client.Do(req)
if err != nil {
return err
}
if err := res.Router.CallFunc(resp, nil); err != nil {
return err
}
return nil
}