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

163 lines
3.6 KiB
Go
Raw Normal View History

package internal
import (
"bytes"
2023-05-05 07:49:38 +00:00
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"time"
2023-05-05 07:49:38 +00:00
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
)
const defaultBaseURL = "https://api.variomedia.de"
2023-05-05 07:49:38 +00:00
const authorizationHeader = "Authorization"
// Client the API client for Variomedia.
type Client struct {
2023-05-05 07:49:38 +00:00
apiToken string
baseURL *url.URL
HTTPClient *http.Client
}
2023-05-05 07:49:38 +00:00
// NewClient creates a new Client.
func NewClient(apiToken string) *Client {
baseURL, _ := url.Parse(defaultBaseURL)
return &Client{
apiToken: apiToken,
baseURL: baseURL,
HTTPClient: &http.Client{Timeout: 10 * time.Second},
}
}
2023-05-05 07:49:38 +00:00
// CreateDNSRecord creates a new DNS entry.
// https://api.variomedia.de/docs/dns-records.html#erstellen
func (c Client) CreateDNSRecord(ctx context.Context, record DNSRecord) (*CreateDNSRecordResponse, error) {
2023-02-09 16:19:58 +00:00
endpoint := c.baseURL.JoinPath("dns-records")
data := CreateDNSRecordRequest{Data: Data{
Type: "dns-record",
Attributes: record,
}}
2023-05-05 07:49:38 +00:00
req, err := newJSONRequest(ctx, http.MethodPost, endpoint, data)
if err != nil {
return nil, err
}
var result CreateDNSRecordResponse
err = c.do(req, &result)
if err != nil {
return nil, err
}
return &result, nil
}
2023-05-05 07:49:38 +00:00
// DeleteDNSRecord deletes a DNS record.
// https://api.variomedia.de/docs/dns-records.html#l%C3%B6schen
func (c Client) DeleteDNSRecord(ctx context.Context, id string) (*DeleteRecordResponse, error) {
2023-02-09 16:19:58 +00:00
endpoint := c.baseURL.JoinPath("dns-records", id)
2023-05-05 07:49:38 +00:00
req, err := newJSONRequest(ctx, http.MethodDelete, endpoint, nil)
if err != nil {
return nil, err
}
var result DeleteRecordResponse
err = c.do(req, &result)
if err != nil {
return nil, err
}
return &result, nil
}
2023-05-05 07:49:38 +00:00
// GetJob returns a single job based on its ID.
// https://api.variomedia.de/docs/job-queue.html
func (c Client) GetJob(ctx context.Context, id string) (*GetJobResponse, error) {
2023-02-09 16:19:58 +00:00
endpoint := c.baseURL.JoinPath("queue-jobs", id)
2023-05-05 07:49:38 +00:00
req, err := newJSONRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return nil, err
}
var result GetJobResponse
err = c.do(req, &result)
if err != nil {
return nil, err
}
return &result, nil
}
2023-05-05 07:49:38 +00:00
func (c Client) do(req *http.Request, data any) error {
req.Header.Set(authorizationHeader, "token "+c.apiToken)
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/100 != 2 {
2023-05-05 07:49:38 +00:00
return parseError(req, resp)
}
raw, err := io.ReadAll(resp.Body)
if err != nil {
return errutils.NewReadResponseError(req, resp.StatusCode, err)
}
err = json.Unmarshal(raw, data)
if err != nil {
return errutils.NewUnmarshalError(req, resp.StatusCode, raw, err)
}
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 {
2023-05-05 07:49:38 +00:00
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)
}
req.Header.Set("Accept", "application/vnd.variomedia.v1+json")
if payload != nil {
req.Header.Set("Content-Type", "application/vnd.api+json")
}
2023-05-05 07:49:38 +00:00
return req, nil
}
func parseError(req *http.Request, resp *http.Response) error {
raw, _ := io.ReadAll(resp.Body)
var errAPI APIError
err := json.Unmarshal(raw, &errAPI)
if err != nil {
2023-05-05 07:49:38 +00:00
return errutils.NewUnexpectedStatusCodeError(req, resp.StatusCode, raw)
}
2023-05-05 07:49:38 +00:00
return errAPI
}