-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient.go
More file actions
94 lines (74 loc) · 2.03 KB
/
client.go
File metadata and controls
94 lines (74 loc) · 2.03 KB
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
// Direct implementation of all required do.de DNS API endpoints
package dode
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
)
const baseURl = "https://www.do.de/api"
type APIResponse struct {
Domain *string `json:"domain"`
Success *bool `json:"success"`
Error *string `json:"error"`
}
func (p *Provider) createACMERecord(ctx context.Context, domain string, value string) error {
baseURL, _ := url.Parse(baseURl)
endpoint := baseURL.JoinPath("letsencrypt")
query := endpoint.Query()
query.Set("token", p.APIToken)
query.Set("domain", domain)
query.Set("value", value)
endpoint.RawQuery = query.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), http.NoBody)
if err != nil {
return err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
var apiResponse APIResponse
err = json.NewDecoder(resp.Body).Decode(&apiResponse)
if err != nil {
return err
}
if apiResponse.Error != nil {
return fmt.Errorf(*apiResponse.Error)
}
if apiResponse.Success == nil || !*apiResponse.Success {
return fmt.Errorf("creating the ACME record was not successfull")
}
return nil
}
func (p *Provider) deleteACMERecord(ctx context.Context, domain string) error {
baseURL, _ := url.Parse(baseURl)
endpoint := baseURL.JoinPath("letsencrypt")
query := endpoint.Query()
query.Set("token", p.APIToken)
query.Set("domain", domain)
query.Set("action", "delete")
endpoint.RawQuery = query.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), http.NoBody)
if err != nil {
return err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
var apiResponse APIResponse
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&apiResponse)
if err != nil {
return err
}
if apiResponse.Error != nil {
return fmt.Errorf("API error: %v", *apiResponse.Error)
}
if apiResponse.Success == nil || !*apiResponse.Success {
return fmt.Errorf("creating the ACME record was not successfull")
}
return nil
}