-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapiclient.go
71 lines (62 loc) · 1.57 KB
/
apiclient.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
package main
import (
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
)
type apiClient struct {
baseURL string
user, pass string
}
func initApiClient() (apiClient, error) {
a := apiClient{
baseURL: os.Getenv("MAILINABOX_URL"),
user: os.Getenv("MAILINABOX_USER"),
pass: os.Getenv("MAILINABOX_PASSWORD"),
}
return a, a.Validate()
}
func (a apiClient) Validate() error {
if a.baseURL == "" {
return errors.New("MAILINABOX_URL must not be empty. Example: https://box.yourdomain.com/admin/dns/custom/")
}
if a.user == "" {
return errors.New("MAILINABOX_USER must not be empty")
}
if a.pass == "" {
return errors.New("MAILINABOX_PASSWORD must not be empty")
}
return nil
}
func (a apiClient) createTxtRecord(l legoHttpReq) error {
url := fmt.Sprintf("%s%s/txt", a.baseURL, strings.TrimSuffix(l.FQDN, "."))
payload := strings.NewReader(l.Value)
req, err := http.NewRequest(l.Action, url, payload)
if err != nil {
return err
}
req.Header.Add("authorization", a.basicAuth())
req.Header.Add("content-type", "application/x-www-form-urlencoded")
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
if res.StatusCode != 200 {
return fmt.Errorf("request failed. upstream_code=%d, upstream_response=%s", res.StatusCode, string(body))
}
return nil
}
func (a apiClient) basicAuth() string {
s := fmt.Sprintf("%s:%s", a.user, a.pass)
enc := base64.StdEncoding.EncodeToString([]byte(s))
return fmt.Sprintf("Basic %s", enc)
}