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

178 lines
3.6 KiB
Go
Raw Normal View History

package internal
import (
"bytes"
2023-05-05 07:49:38 +00:00
"context"
"encoding/json"
"errors"
"fmt"
2021-08-25 09:44:11 +00:00
"io"
"net/http"
2023-05-05 07:49:38 +00:00
"net/url"
"time"
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
)
const baseAPIURL = "https://api.servercow.de/dns/v1/domains"
// Client the Servercow client.
type Client struct {
username string
password string
2023-05-05 07:49:38 +00:00
baseURL *url.URL
HTTPClient *http.Client
}
// NewClient Creates a Servercow client.
func NewClient(username, password string) *Client {
2023-05-05 07:49:38 +00:00
baseURL, _ := url.Parse(baseAPIURL)
return &Client{
username: username,
password: password,
2023-05-05 07:49:38 +00:00
baseURL: baseURL,
HTTPClient: &http.Client{Timeout: 5 * time.Second},
}
}
// GetRecords from API.
2023-05-05 07:49:38 +00:00
func (c *Client) GetRecords(ctx context.Context, domain string) ([]Record, error) {
endpoint := c.baseURL.JoinPath(domain)
2023-05-05 07:49:38 +00:00
req, err := newJSONRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return nil, err
}
var records []Record
2023-05-05 07:49:38 +00:00
err = c.do(req, &records)
if err != nil {
return nil, err
}
return records, nil
}
// CreateUpdateRecord creates or updates a record.
2023-05-05 07:49:38 +00:00
func (c *Client) CreateUpdateRecord(ctx context.Context, domain string, data Record) (*Message, error) {
endpoint := c.baseURL.JoinPath(domain)
req, err := newJSONRequest(ctx, http.MethodPost, endpoint, data)
if err != nil {
return nil, err
}
2023-05-05 07:49:38 +00:00
var msg Message
err = c.do(req, &msg)
if err != nil {
return nil, err
}
2023-05-05 07:49:38 +00:00
if msg.ErrorMsg != "" {
return nil, msg
}
2023-05-05 07:49:38 +00:00
return &msg, nil
}
// DeleteRecord deletes a record.
func (c *Client) DeleteRecord(ctx context.Context, domain string, data Record) (*Message, error) {
endpoint := c.baseURL.JoinPath(domain)
req, err := newJSONRequest(ctx, http.MethodDelete, endpoint, data)
if err != nil {
2023-05-05 07:49:38 +00:00
return nil, err
}
var msg Message
2023-05-05 07:49:38 +00:00
err = c.do(req, &msg)
if err != nil {
return nil, err
}
if msg.ErrorMsg != "" {
return nil, msg
}
return &msg, nil
}
2023-05-05 07:49:38 +00:00
func (c *Client) do(req *http.Request, result any) error {
req.Header.Set("X-Auth-Username", c.username)
req.Header.Set("X-Auth-Password", c.password)
resp, err := c.HTTPClient.Do(req)
if err != nil {
2023-05-05 07:49:38 +00:00
return errutils.NewHTTPDoError(req, err)
}
2023-05-05 07:49:38 +00:00
defer func() { _ = resp.Body.Close() }()
// Note the API always return 200 even if the authentication failed.
if resp.StatusCode/100 != 2 {
2023-05-05 07:49:38 +00:00
return errutils.NewUnexpectedResponseStatusCodeError(req, resp)
}
2023-05-05 07:49:38 +00:00
if result == nil {
return nil
}
2023-05-05 07:49:38 +00:00
raw, err := io.ReadAll(resp.Body)
if err != nil {
2023-05-05 07:49:38 +00:00
return errutils.NewReadResponseError(req, resp.StatusCode, err)
}
2023-05-05 07:49:38 +00:00
err = unmarshal(raw, result)
if err != nil {
return err
}
2023-05-05 07:49:38 +00:00
return nil
}
2023-05-05 07:49:38 +00:00
func newJSONRequest(ctx context.Context, method string, endpoint *url.URL, payload any) (*http.Request, error) {
buf := new(bytes.Buffer)
if payload != nil {
err := json.NewEncoder(buf).Encode(payload)
if err != nil {
return nil, fmt.Errorf("failed to create request JSON body: %w", err)
}
}
2023-05-05 07:49:38 +00:00
req, err := http.NewRequestWithContext(ctx, method, endpoint.String(), buf)
if err != nil {
2023-05-05 07:49:38 +00:00
return nil, fmt.Errorf("unable to create request: %w", err)
}
2023-05-05 07:49:38 +00:00
req.Header.Set("Accept", "application/json")
2023-07-21 14:56:01 +00:00
// Content-Type should be added even if there is no request body.
req.Header.Set("Content-Type", "application/json")
return req, nil
}
2023-05-05 07:49:38 +00:00
func unmarshal(raw []byte, v any) error {
err := json.Unmarshal(raw, v)
if err == nil {
return nil
}
2023-05-05 07:49:38 +00:00
var utErr *json.UnmarshalTypeError
if !errors.As(err, &utErr) {
return fmt.Errorf("unmarshaling %T error: %w: %s", v, err, string(raw))
}
2023-05-05 07:49:38 +00:00
var apiErr Message
errU := json.Unmarshal(raw, &apiErr)
if errU != nil {
return fmt.Errorf("unmarshaling %T error: %w: %s", v, err, string(raw))
}
2023-05-05 07:49:38 +00:00
return apiErr
}