2022-06-06 22:22:34 +02:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-05-05 09:49:38 +02:00
|
|
|
"context"
|
2022-06-06 22:22:34 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
2023-05-05 09:49:38 +02:00
|
|
|
|
|
|
|
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
|
2022-06-06 22:22:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const defaultBaseURL = "https://api.variomedia.de"
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
const authorizationHeader = "Authorization"
|
|
|
|
|
|
|
|
// Client the API client for Variomedia.
|
2022-06-06 22:22:34 +02:00
|
|
|
type Client struct {
|
2023-05-05 09:49:38 +02:00
|
|
|
apiToken string
|
|
|
|
|
2022-06-06 22:22:34 +02:00
|
|
|
baseURL *url.URL
|
|
|
|
HTTPClient *http.Client
|
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
// NewClient creates a new Client.
|
2022-06-06 22:22:34 +02:00
|
|
|
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 09:49:38 +02: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 18:19:58 +02:00
|
|
|
endpoint := c.baseURL.JoinPath("dns-records")
|
2022-06-06 22:22:34 +02:00
|
|
|
|
|
|
|
data := CreateDNSRecordRequest{Data: Data{
|
|
|
|
Type: "dns-record",
|
|
|
|
Attributes: record,
|
|
|
|
}}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
req, err := newJSONRequest(ctx, http.MethodPost, endpoint, data)
|
2022-06-06 22:22:34 +02:00
|
|
|
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 09:49:38 +02: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 18:19:58 +02:00
|
|
|
endpoint := c.baseURL.JoinPath("dns-records", id)
|
2022-06-06 22:22:34 +02:00
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
req, err := newJSONRequest(ctx, http.MethodDelete, endpoint, nil)
|
2022-06-06 22:22:34 +02:00
|
|
|
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 09:49:38 +02: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 18:19:58 +02:00
|
|
|
endpoint := c.baseURL.JoinPath("queue-jobs", id)
|
2022-06-06 22:22:34 +02:00
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
req, err := newJSONRequest(ctx, http.MethodGet, endpoint, nil)
|
2022-06-06 22:22:34 +02:00
|
|
|
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 09:49:38 +02:00
|
|
|
func (c Client) do(req *http.Request, data any) error {
|
|
|
|
req.Header.Set(authorizationHeader, "token "+c.apiToken)
|
2022-06-06 22:22:34 +02:00
|
|
|
|
|
|
|
resp, err := c.HTTPClient.Do(req)
|
|
|
|
if err != nil {
|
2023-05-05 09:49:38 +02:00
|
|
|
return errutils.NewHTTPDoError(req, err)
|
2022-06-06 22:22:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
|
|
|
|
if resp.StatusCode/100 != 2 {
|
2023-05-05 09:49:38 +02: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
|
|
|
|
}
|
2022-06-06 22:22:34 +02:00
|
|
|
|
2023-05-05 09:49:38 +02: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)
|
2022-06-06 22:22:34 +02:00
|
|
|
if err != nil {
|
2023-05-05 09:49:38 +02:00
|
|
|
return nil, fmt.Errorf("failed to create request JSON body: %w", err)
|
2022-06-06 22:22:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
req, err := http.NewRequestWithContext(ctx, method, endpoint.String(), buf)
|
2022-06-06 22:22:34 +02:00
|
|
|
if err != nil {
|
2023-05-05 09:49:38 +02: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")
|
2022-06-06 22:22:34 +02:00
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02: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)
|
2022-06-06 22:22:34 +02:00
|
|
|
if err != nil {
|
2023-05-05 09:49:38 +02:00
|
|
|
return errutils.NewUnexpectedStatusCodeError(req, resp.StatusCode, raw)
|
2022-06-06 22:22:34 +02:00
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
return errAPI
|
2022-06-06 22:22:34 +02:00
|
|
|
}
|