-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.go
52 lines (40 loc) · 1.08 KB
/
post.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
package dsupdate
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
)
// Update DS records.
func (c *Client) Update(ctx context.Context, records []DsRecord) ([]byte, error) {
return c.do(ctx, c.form(records))
}
// Delete DS records.
func (c *Client) Delete(ctx context.Context) ([]byte, error) {
return c.do(ctx, c.formDelete())
}
func (c *Client) do(ctx context.Context, form url.Values) ([]byte, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.BaseURL.String(), strings.NewReader(form.Encode()))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := c.httpClient().Do(req)
if (err != nil) || (resp == nil) {
return nil, fmt.Errorf("failed to contact API: %w", err)
}
defer func() { _ = resp.Body.Close() }()
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode == http.StatusOK {
return body, nil
}
s, ok := subStatus(resp.Header)
if ok {
return body, s
}
//nolint:goerr113
return body, errors.New(resp.Status)
}