-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
122 lines (111 loc) · 2.74 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
package idly
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
)
func NewClient(serviceName, uri string) *Client {
return &Client{
uri: uri,
service: serviceName,
}
}
type Client struct {
uri string
service string
}
type Request struct {
client *Client
login Login
}
// List successful logins of a user of a service, records
func (c *Client) List(uid string) ([]Login, error) {
res, err := http.Get(fmt.Sprintf("%s/login/%s/%s", c.uri, url.PathEscape(c.service), url.PathEscape(uid)))
if err != nil {
return nil, err
}
defer res.Body.Close()
var logins []Login
err = json.NewDecoder(res.Body).Decode(&logins)
return logins, err
}
func (c *Client) Request(email string, ipAddress string) *Request {
return &Request{
client: c,
login: Login{
Service: c.service,
UID: email,
At: time.Now(),
Email: email,
IPAddress: ipAddress,
HttpHeaders: map[string][]string{},
}}
}
func Headers(h http.Header) func(request *Request) {
return func(request *Request) {
for k, v := range h {
request.login.HttpHeaders[k] = v
}
}
}
func UserAgent(ua string) func(request *Request) {
return func(request *Request) {
request.login.HttpHeaders.Set("User-Agent", ua)
}
}
func UserId(uid string) func(request *Request) {
return func(request *Request) {
request.login.UID = uid
}
}
func (r *Request) With(ops ...func(request *Request)) *Request {
for _, o := range ops {
o(r)
}
return r
}
func (r *Request) WithHeaders(h http.Header) *Request {
r.With(Headers(h))
return r
}
func (r *Request) WithUserId(uid string) *Request {
r.With(UserId(uid))
return r
}
func (r *Request) WithUserAgent(ua string) *Request {
r.With(UserAgent(ua))
return r
}
// Success reports a successgful login too idly. This is done async.
//
// This is used in ordern to figure out if a Warning mail shall be sent or not
func (r *Request) Success() {
data, err := json.Marshal(r.login)
if err != nil {
fmt.Println("[Idly Client] could not marshal login struct")
}
go func() {
_, err = http.Post(fmt.Sprintf("%s/login", r.client.uri), "application/json", bytes.NewBuffer(data))
if err != nil {
fmt.Println("[Idly Client] could not make login request; err:", err)
}
}()
}
// Fail reports a failed login attempt too idly. This is done async.
//
// This is used by idly to collect metrics which can be monitored through prometheus
func (r *Request) Fail() {
data, err := json.Marshal(r.login)
if err != nil {
fmt.Println("[Idly Client] could not marshal login struct")
}
go func() {
_, err = http.Post(fmt.Sprintf("%s/login-fail", r.client.uri), "application/json", bytes.NewBuffer(data))
if err != nil {
fmt.Println("[Idly Client] could not make login-fail request; err:", err)
}
}()
}