-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
171 lines (132 loc) · 3.79 KB
/
Copy pathclient.go
File metadata and controls
171 lines (132 loc) · 3.79 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package liara
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"time"
)
// https://openapi.liara.ir/?urls.primaryName=DNS
const (
apiBaseURL string = "https://dns-service.iran.liara.ir/api/v1/"
)
// Client is a struct, that knows how to interact with the Liara's api thorugh
// its methods.
type client struct {
apiToken string
httpClient *http.Client
}
// newClient creates a new Liara API client.
func newClient(token string) *client {
return &client{
apiToken: token,
httpClient: &http.Client{
Timeout: time.Second * 20,
},
}
}
// Gets all the dns records in a specific zone.
func (c client) APIGetRecords(ctx context.Context, zone string) ([]APIRecord, error) {
endpoint, err := url.JoinPath(apiBaseURL, "zones", zone, "dns-records")
if err != nil {
return nil, err
}
resp, err := c.do(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var result GetRecordsResponse
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, err
}
return result.Data, nil
}
// Inserts a single dns record through Liara api, and returns it.
func (c client) APIPostRecord(ctx context.Context, zone string, record APIRecord) (APIRecord, error) {
endpoint, err := url.JoinPath(apiBaseURL, "zones", zone, "dns-records")
if err != nil {
return APIRecord{}, err
}
body, err := json.Marshal(record)
if err != nil {
return APIRecord{}, err
}
payload := bytes.NewReader(body)
resp, err := c.do(ctx, http.MethodPost, endpoint, payload)
if err != nil {
return APIRecord{}, err
}
defer resp.Body.Close()
var result PostRecordResponse
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return APIRecord{}, err
}
return result.Data, nil
}
// APIUpdateRecord updates a single DNS record through the Liara API
// and returns the updated record.
// It needs the entire entity to be replaces with another.
func (c client) APIUpdateRecord(ctx context.Context, zone string, id string, record APIRecord) (APIRecord, error) {
endpoint, err := url.JoinPath(apiBaseURL, "zones", zone, "dns-records", id)
if err != nil {
return APIRecord{}, err
}
body, err := json.Marshal(record)
if err != nil {
return APIRecord{}, err
}
payload := bytes.NewReader(body)
resp, err := c.do(ctx, http.MethodPut, endpoint, payload)
if err != nil {
return APIRecord{}, err
}
defer resp.Body.Close()
var result PostRecordResponse
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return APIRecord{}, err
}
return result.Data, nil
}
// APIDeleteRecord updates a single DNS record through the Liara API
// and returns the deleted record.
func (c client) APIDeleteRecord(ctx context.Context, zone string, id string) error {
endpoint, err := url.JoinPath(apiBaseURL, "zones", zone, "dns-records", id)
if err != nil {
return err
}
_, err = c.do(ctx, http.MethodDelete, endpoint, nil)
if err != nil {
return err
}
return nil
}
// A helper function to handle the repetitive portions of the request process to
// Liara.
func (c client) do(ctx context.Context, method, endpoint string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequestWithContext(ctx, method, endpoint, body)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+c.apiToken)
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode < 200 || resp.StatusCode >= 400 {
var apiErr APIError
if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil {
return nil, fmt.Errorf("api request failed with status %s", resp.Status)
}
return nil, fmt.Errorf(
"api request failed: %s",
apiErr.Error(),
)
}
return resp, nil
}