-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclient.go
157 lines (127 loc) · 3.61 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package lunar
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
)
// ApolloAPI is the interface of apollo api
type ApolloAPI interface {
GetCachedItems(namespace string) (Items, error)
GetNamespace(namespace string, releaseKey string) (*Namespace, error)
GetNotifications(ns Notifications) (Notifications, error)
}
// ApolloClient is the implementation of apollo client.
//
// https://github.com/ctripcorp/apollo/wiki/%E5%85%B6%E5%AE%83%E8%AF%AD%E8%A8%80%E5%AE%A2%E6%88%B7%E7%AB%AF%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8D%97
type ApolloClient struct {
Options // inherited options
AppID string
Client *http.Client
ClientIP string
}
// make sure ApolloClient implements ApolloAPI
var _ ApolloAPI = new(ApolloClient)
// NewApolloClient creates a apollo client
func NewApolloClient(appID string, opts ...Option) *ApolloClient {
c := &ApolloClient{
AppID: appID,
Options: NewOptions(opts...),
ClientIP: GetLocalIP(),
}
c.Client = &http.Client{
Timeout: c.ClientTimeout,
}
return c
}
func (c *ApolloClient) get(pathWithQuery string, result interface{}) error {
url := c.Server + pathWithQuery
c.Logger.Printf("%s", url)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return err
}
if len(c.AccessKeySecret) > 0 {
headers := buildHeaders(pathWithQuery, c.AppID, c.AccessKeySecret)
for k, v := range headers {
req.Header.Add(k, v)
}
}
resp, err := c.Client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode == http.StatusOK {
err = json.Unmarshal(body, result)
}
c.Logger.Printf("[%d] %s", resp.StatusCode, body)
return err
}
// GetCachedItems gets cached configs from apollo
func (c *ApolloClient) GetCachedItems(namespace string) (Items, error) {
url := fmt.Sprintf("/configfiles/json/%s/%s/%s?ip=%s",
url.QueryEscape(c.AppID),
url.QueryEscape(c.Cluster),
url.QueryEscape(namespace),
c.ClientIP,
)
var res Items
err := c.get(url, &res)
return res, err
}
// Namespace is apollo namespace data
type Namespace struct {
AppID string `json:"appId"`
Cluster string `json:"cluster"`
Name string `json:"namespaceName"`
Items Items `json:"configurations"`
ReleaseKey string `json:"releaseKey"`
}
// GetNamespace gets realtime namespace data from apollo
func (c *ApolloClient) GetNamespace(namespace string, releaseKey string) (*Namespace, error) {
if namespace == "" {
namespace = defaultNamespace
}
url := fmt.Sprintf("/configs/%s/%s/%s?releaseKey=%s&ip=%s",
url.QueryEscape(c.AppID),
url.QueryEscape(c.Cluster),
url.QueryEscape(namespace),
url.QueryEscape(releaseKey),
c.ClientIP,
)
var res Namespace
err := c.get(url, &res)
return &res, err
}
// Notifications is a set of notifications
type Notifications []Notification
// String converts Notifications to json string
func (ns Notifications) String() string {
bytes, _ := json.Marshal(ns)
return string(bytes)
}
// Notification is the definition of notification
type Notification struct {
Namespace string `json:"namespaceName"`
NotificationID int `json:"notificationId"`
}
// GetNotifications gets notifications from apollo
func (c *ApolloClient) GetNotifications(ns Notifications) (Notifications, error) {
if len(ns) == 0 {
ns = append(ns, Notification{Namespace: defaultNamespace, NotificationID: defaultNotificationID})
}
url := fmt.Sprintf("/notifications/v2?appId=%s&cluster=%s¬ifications=%s",
url.QueryEscape(c.AppID),
url.QueryEscape(c.Cluster),
url.QueryEscape(ns.String()),
)
var res Notifications
err := c.get(url, &res)
return res, err
}