-
Notifications
You must be signed in to change notification settings - Fork 0
/
userli.go
110 lines (87 loc) · 2.21 KB
/
userli.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"time"
)
type UserliService interface {
GetAliases(email string) ([]string, error)
GetDomain(domain string) (bool, error)
GetMailbox(email string) (bool, error)
GetSenders(email string) ([]string, error)
}
type Userli struct {
token string
baseURL string
Client *http.Client
}
func NewUserli(token, baseURL string) *Userli {
client := &http.Client{
Timeout: time.Second * 10,
}
return &Userli{token: token, baseURL: baseURL, Client: client}
}
func (u *Userli) GetAliases(email string) ([]string, error) {
resp, err := u.call(fmt.Sprintf("%s/api/postfix/alias/%s", u.baseURL, email))
if err != nil {
return []string{}, err
}
var aliases []string
err = json.NewDecoder(resp.Body).Decode(&aliases)
if err != nil {
return []string{}, err
}
return aliases, nil
}
func (u *Userli) GetDomain(domain string) (bool, error) {
resp, err := u.call(fmt.Sprintf("%s/api/postfix/domain/%s", u.baseURL, domain))
if err != nil {
return false, err
}
var result bool
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
return false, err
}
return result, nil
}
func (u *Userli) GetMailbox(email string) (bool, error) {
resp, err := u.call(fmt.Sprintf("%s/api/postfix/mailbox/%s", u.baseURL, email))
if err != nil {
return false, err
}
var result bool
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
return false, err
}
return result, nil
}
func (u *Userli) GetSenders(email string) ([]string, error) {
resp, err := u.call(fmt.Sprintf("%s/api/postfix/senders/%s", u.baseURL, email))
if err != nil {
return []string{}, err
}
var senders []string
err = json.NewDecoder(resp.Body).Decode(&senders)
if err != nil {
return []string{}, err
}
return senders, nil
}
func (u *Userli) call(url string) (*http.Response, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", u.token))
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", "userli-postfix-adapter")
resp, err := u.Client.Do(req)
if err != nil {
return nil, err
}
return resp, nil
}