lego/providers/dns/yandex/internal/client.go

161 lines
3.1 KiB
Go
Raw Permalink Normal View History

2020-04-27 17:58:18 +00:00
package internal
import (
2023-05-05 07:49:38 +00:00
"bytes"
"context"
2020-04-27 17:58:18 +00:00
"encoding/json"
"errors"
"fmt"
2023-05-05 07:49:38 +00:00
"io"
2020-04-27 17:58:18 +00:00
"net/http"
2023-05-05 07:49:38 +00:00
"net/url"
"time"
2020-04-27 17:58:18 +00:00
2023-05-05 07:49:38 +00:00
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
2020-04-27 17:58:18 +00:00
"github.com/google/go-querystring/query"
)
const defaultBaseURL = "https://pddimp.yandex.ru/api2/admin/dns"
const successCode = "ok"
const pddTokenHeader = "PddToken"
type Client struct {
2023-05-05 07:49:38 +00:00
pddToken string
baseURL *url.URL
2020-04-27 17:58:18 +00:00
HTTPClient *http.Client
}
func NewClient(pddToken string) (*Client, error) {
if pddToken == "" {
return nil, errors.New("PDD token is required")
}
2023-05-05 07:49:38 +00:00
baseURL, _ := url.Parse(defaultBaseURL)
2020-04-27 17:58:18 +00:00
return &Client{
pddToken: pddToken,
2023-05-05 07:49:38 +00:00
baseURL: baseURL,
HTTPClient: &http.Client{Timeout: 10 * time.Second},
2020-04-27 17:58:18 +00:00
}, nil
}
2023-05-05 07:49:38 +00:00
func (c *Client) AddRecord(ctx context.Context, payload Record) (*Record, error) {
endpoint := c.baseURL.JoinPath("add")
req, err := newRequest(ctx, http.MethodPost, endpoint, payload)
2020-04-27 17:58:18 +00:00
if err != nil {
return nil, err
}
r := AddResponse{}
2023-05-05 07:49:38 +00:00
err = c.do(req, &r)
2020-04-27 17:58:18 +00:00
if err != nil {
return nil, err
}
return r.Record, nil
}
2023-05-05 07:49:38 +00:00
func (c *Client) RemoveRecord(ctx context.Context, payload Record) (int, error) {
endpoint := c.baseURL.JoinPath("del")
req, err := newRequest(ctx, http.MethodPost, endpoint, payload)
2020-04-27 17:58:18 +00:00
if err != nil {
return 0, err
}
r := RemoveResponse{}
2023-05-05 07:49:38 +00:00
err = c.do(req, &r)
2020-04-27 17:58:18 +00:00
if err != nil {
return 0, err
}
return r.RecordID, nil
}
2023-05-05 07:49:38 +00:00
func (c *Client) GetRecords(ctx context.Context, domain string) ([]Record, error) {
endpoint := c.baseURL.JoinPath("list")
payload := struct {
2020-04-27 17:58:18 +00:00
Domain string `url:"domain"`
2023-05-05 07:49:38 +00:00
}{Domain: domain}
req, err := newRequest(ctx, http.MethodGet, endpoint, payload)
2020-04-27 17:58:18 +00:00
if err != nil {
return nil, err
}
r := ListResponse{}
2023-05-05 07:49:38 +00:00
err = c.do(req, &r)
2020-04-27 17:58:18 +00:00
if err != nil {
return nil, err
}
return r.Records, nil
}
2023-05-05 07:49:38 +00:00
func (c *Client) do(req *http.Request, result Response) error {
req.Header.Set(pddTokenHeader, c.pddToken)
resp, err := c.HTTPClient.Do(req)
2020-04-27 17:58:18 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return errutils.NewHTTPDoError(req, err)
2020-04-27 17:58:18 +00:00
}
2023-05-05 07:49:38 +00:00
defer func() { _ = resp.Body.Close() }()
raw, err := io.ReadAll(resp.Body)
2020-04-27 17:58:18 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return errutils.NewReadResponseError(req, resp.StatusCode, err)
2020-04-27 17:58:18 +00:00
}
2023-05-05 07:49:38 +00:00
err = json.Unmarshal(raw, result)
if err != nil {
return errutils.NewUnmarshalError(req, resp.StatusCode, raw, err)
}
2020-04-27 17:58:18 +00:00
2023-05-05 07:49:38 +00:00
if result.GetSuccess() != successCode {
return fmt.Errorf("error during operation: %s %s", result.GetSuccess(), result.GetError())
}
return nil
2020-04-27 17:58:18 +00:00
}
2023-05-05 07:49:38 +00:00
func newRequest(ctx context.Context, method string, endpoint *url.URL, payload any) (*http.Request, error) {
buf := new(bytes.Buffer)
2020-04-27 17:58:18 +00:00
2023-05-05 07:49:38 +00:00
if payload != nil {
switch method {
case http.MethodPost:
values, err := query.Values(payload)
if err != nil {
return nil, err
}
buf.WriteString(values.Encode())
case http.MethodGet:
values, err := query.Values(payload)
if err != nil {
return nil, err
}
2020-04-27 17:58:18 +00:00
2023-05-05 07:49:38 +00:00
endpoint.RawQuery = values.Encode()
}
}
req, err := http.NewRequestWithContext(ctx, method, endpoint.String(), buf)
2020-04-27 17:58:18 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return nil, fmt.Errorf("unable to create request: %w", err)
2020-04-27 17:58:18 +00:00
}
2023-05-05 07:49:38 +00:00
if method == http.MethodPost {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
2020-04-27 17:58:18 +00:00
2023-05-05 07:49:38 +00:00
return req, nil
2020-04-27 17:58:18 +00:00
}