-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
94 lines (79 loc) · 2.01 KB
/
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
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
// Copyright 2017 EasyStack, Inc.
package keystone
import (
"github.com/Treetreewu/golang-keystoneclient/openapi"
"net/http"
"net/url"
"path"
"github.com/imroc/req"
"github.com/pkg/errors"
)
const (
TokenHeader = "X-Auth-Token"
)
type Client struct {
*req.Req
Token string
BaseURL *url.URL
// Managers
Auth *AuthManager
// OpenAPI
*openapi.APIClient
}
func NewClientByToken(config *openapi.Configuration, token string) (*Client, error) {
client, err := NewClient(config)
if err != nil {
return nil, err
}
client.SetToken(token)
return client, nil
}
func NewDefaultClientByToken(token string) (*Client, error) {
return NewClientByToken(openapi.NewConfiguration(), token)
}
func NewDefaultClient() (*Client, error) {
return NewClient(openapi.NewConfiguration())
}
func NewClient(config *openapi.Configuration) (*Client, error) {
rawURL, _ := config.Servers.URL(0, nil)
config.DefaultHeader["Enhanced"] = "enabled"
client := &Client{
Req: req.New(),
APIClient: openapi.NewAPIClient(config),
}
client.Req.SetClient(&http.Client{})
u, err := url.Parse(rawURL)
if err != nil {
return nil, errors.New("Invalid URL:")
}
u.Path = path.Join(u.Path, "v3")
client.BaseURL = u
client.Auth = NewAuthManager(client)
return client, nil
}
func (c *Client) SetToken(token string) {
c.Token = token
c.APIClient.GetConfig().AddDefaultHeader(TokenHeader, token)
}
func (c *Client) Do(method, url string, vs ...interface{}) (resp *req.Resp, err error) {
vs = append(
vs,
// Mapping OpenAPI client configurations.
req.Header{"User-Agent": c.APIClient.GetConfig().UserAgent},
c.APIClient.GetConfig().DefaultHeader,
// Add Keystone token header.
req.Header{TokenHeader: c.Token},
)
resp, err = c.Req.Do(method, url, vs...)
if err == nil && resp.Response().StatusCode >= 400 {
err = errors.New("Request failed")
}
return resp, errors.WithStack(err)
}
func (c *Client) AuthRequired() error {
if c.Token == "" {
return errors.New("Not authorized.")
}
_, err := c.Auth.TokenDetail()
return err
}