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

168 lines
4.1 KiB
Go
Raw Permalink Normal View History

package internal
import (
"bytes"
2023-05-05 07:49:38 +00:00
"context"
"encoding/json"
"errors"
"fmt"
2023-05-05 07:49:38 +00:00
"io"
"net/http"
"net/url"
2023-02-09 16:19:58 +00:00
"strings"
"time"
2023-05-05 07:49:38 +00:00
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
)
const defaultBaseURL = "https://api.simply.com/1/"
// Client is a Simply.com API client.
type Client struct {
accountName string
apiKey string
2023-05-05 07:49:38 +00:00
baseURL *url.URL
HTTPClient *http.Client
}
// NewClient creates a new Client.
func NewClient(accountName string, apiKey string) (*Client, error) {
if accountName == "" {
return nil, errors.New("credentials missing: accountName")
}
if apiKey == "" {
return nil, errors.New("credentials missing: apiKey")
}
baseURL, err := url.Parse(defaultBaseURL)
if err != nil {
return nil, err
}
return &Client{
accountName: accountName,
apiKey: apiKey,
2023-05-05 07:49:38 +00:00
baseURL: baseURL,
HTTPClient: &http.Client{Timeout: 5 * time.Second},
}, nil
}
// GetRecords lists all the records in the zone.
2023-05-05 07:49:38 +00:00
func (c *Client) GetRecords(ctx context.Context, zoneName string) ([]Record, error) {
endpoint := c.createEndpoint(zoneName, "/")
req, err := newJSONRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
2023-05-05 07:49:38 +00:00
return nil, fmt.Errorf("failed to create request: %w", err)
}
2023-05-05 07:49:38 +00:00
result := &apiResponse[[]Record, json.RawMessage]{}
err = c.do(req, result)
if err != nil {
2023-05-05 07:49:38 +00:00
return nil, err
}
2023-05-05 07:49:38 +00:00
return result.Records, nil
}
// AddRecord adds a record.
2023-05-05 07:49:38 +00:00
func (c *Client) AddRecord(ctx context.Context, zoneName string, record Record) (int64, error) {
endpoint := c.createEndpoint(zoneName, "/")
2023-05-05 07:49:38 +00:00
req, err := newJSONRequest(ctx, http.MethodPost, endpoint, record)
if err != nil {
2023-05-05 07:49:38 +00:00
return 0, fmt.Errorf("failed to create request: %w", err)
}
2023-05-05 07:49:38 +00:00
result := &apiResponse[json.RawMessage, recordHeader]{}
err = c.do(req, result)
if err != nil {
2023-05-05 07:49:38 +00:00
return 0, err
}
2023-05-05 07:49:38 +00:00
return result.Record.ID, nil
}
// EditRecord updates a record.
2023-05-05 07:49:38 +00:00
func (c *Client) EditRecord(ctx context.Context, zoneName string, id int64, record Record) error {
endpoint := c.createEndpoint(zoneName, fmt.Sprintf("%d", id))
req, err := newJSONRequest(ctx, http.MethodPut, endpoint, record)
if err != nil {
2023-05-05 07:49:38 +00:00
return fmt.Errorf("failed to create request: %w", err)
}
2023-05-05 07:49:38 +00:00
return c.do(req, &apiResponse[json.RawMessage, json.RawMessage]{})
}
// DeleteRecord deletes a record.
2023-05-05 07:49:38 +00:00
func (c *Client) DeleteRecord(ctx context.Context, zoneName string, id int64) error {
endpoint := c.createEndpoint(zoneName, fmt.Sprintf("%d", id))
2023-05-05 07:49:38 +00:00
req, err := newJSONRequest(ctx, http.MethodDelete, endpoint, nil)
if err != nil {
2023-05-05 07:49:38 +00:00
return fmt.Errorf("failed to create request: %w", err)
}
2023-05-05 07:49:38 +00:00
return c.do(req, &apiResponse[json.RawMessage, json.RawMessage]{})
}
func (c *Client) createEndpoint(zoneName string, uri string) *url.URL {
return c.baseURL.JoinPath(c.accountName, c.apiKey, "my", "products", zoneName, "dns", "records", strings.TrimSuffix(uri, "/"))
}
2023-05-05 07:49:38 +00:00
func (c *Client) do(req *http.Request, result Response) error {
resp, err := c.HTTPClient.Do(req)
if err != nil {
2023-05-05 07:49:38 +00:00
return errutils.NewHTTPDoError(req, err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode >= http.StatusInternalServerError {
2023-05-05 07:49:38 +00:00
return errutils.NewUnexpectedResponseStatusCodeError(req, resp)
}
raw, err := io.ReadAll(resp.Body)
if err != nil {
return errutils.NewReadResponseError(req, resp.StatusCode, err)
}
err = json.Unmarshal(raw, result)
if err != nil {
return errutils.NewUnmarshalError(req, resp.StatusCode, raw, err)
}
2023-05-05 07:49:38 +00:00
if result.GetStatus() != http.StatusOK {
return fmt.Errorf("unexpected error: %s", result.GetMessage())
}
return nil
}
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)
}
}
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")
if payload != nil {
req.Header.Set("Content-Type", "application/json")
}
2023-05-05 07:49:38 +00:00
return req, nil
}