2021-02-19 22:23:48 +02:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-05-05 09:49:38 +02:00
|
|
|
"context"
|
2021-02-19 22:23:48 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2023-05-05 09:49:38 +02:00
|
|
|
"io"
|
2021-02-19 22:23:48 +02:00
|
|
|
"net/http"
|
|
|
|
"time"
|
2023-05-05 09:49:38 +02:00
|
|
|
|
|
|
|
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
|
2021-02-19 22:23:48 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const apiEndpoint = "https://njal.la/api/1/"
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
const authorizationHeader = "Authorization"
|
|
|
|
|
2021-02-19 22:23:48 +02:00
|
|
|
// Client is a Njalla API client.
|
|
|
|
type Client struct {
|
2023-05-05 09:49:38 +02:00
|
|
|
token string
|
|
|
|
|
2021-02-19 22:23:48 +02:00
|
|
|
apiEndpoint string
|
2023-05-05 09:49:38 +02:00
|
|
|
HTTPClient *http.Client
|
2021-02-19 22:23:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient creates a new Client.
|
|
|
|
func NewClient(token string) *Client {
|
|
|
|
return &Client{
|
|
|
|
token: token,
|
2023-05-05 09:49:38 +02:00
|
|
|
apiEndpoint: apiEndpoint,
|
|
|
|
HTTPClient: &http.Client{Timeout: 5 * time.Second},
|
2021-02-19 22:23:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddRecord adds a record.
|
2023-05-05 09:49:38 +02:00
|
|
|
func (c *Client) AddRecord(ctx context.Context, record Record) (*Record, error) {
|
2021-02-19 22:23:48 +02:00
|
|
|
data := APIRequest{
|
|
|
|
Method: "add-record",
|
|
|
|
Params: record,
|
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
req, err := newJSONRequest(ctx, http.MethodPost, c.apiEndpoint, data)
|
2021-02-19 22:23:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
var result APIResponse[*Record]
|
|
|
|
err = c.do(req, &result)
|
2021-02-19 22:23:48 +02:00
|
|
|
if err != nil {
|
2023-05-05 09:49:38 +02:00
|
|
|
return nil, err
|
2021-02-19 22:23:48 +02:00
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
return result.Result, nil
|
2021-02-19 22:23:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveRecord removes a record.
|
2023-05-05 09:49:38 +02:00
|
|
|
func (c *Client) RemoveRecord(ctx context.Context, id string, domain string) error {
|
2021-02-19 22:23:48 +02:00
|
|
|
data := APIRequest{
|
|
|
|
Method: "remove-record",
|
|
|
|
Params: Record{
|
|
|
|
ID: id,
|
|
|
|
Domain: domain,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
req, err := newJSONRequest(ctx, http.MethodPost, c.apiEndpoint, data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.do(req, &APIResponse[json.RawMessage]{})
|
2021-02-19 22:23:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListRecords list the records for one domain.
|
2023-05-05 09:49:38 +02:00
|
|
|
func (c *Client) ListRecords(ctx context.Context, domain string) ([]Record, error) {
|
2021-02-19 22:23:48 +02:00
|
|
|
data := APIRequest{
|
|
|
|
Method: "list-records",
|
|
|
|
Params: Record{
|
|
|
|
Domain: domain,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
req, err := newJSONRequest(ctx, http.MethodPost, c.apiEndpoint, data)
|
2021-02-19 22:23:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
var result APIResponse[Records]
|
|
|
|
err = c.do(req, &result)
|
2021-02-19 22:23:48 +02:00
|
|
|
if err != nil {
|
2023-05-05 09:49:38 +02:00
|
|
|
return nil, err
|
2021-02-19 22:23:48 +02:00
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
return result.Result.Records, nil
|
2021-02-19 22:23:48 +02:00
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
func (c *Client) do(req *http.Request, result Response) error {
|
|
|
|
req.Header.Set(authorizationHeader, "Njalla "+c.token)
|
2021-02-19 22:23:48 +02:00
|
|
|
|
|
|
|
resp, err := c.HTTPClient.Do(req)
|
|
|
|
if err != nil {
|
2023-05-05 09:49:38 +02:00
|
|
|
return errutils.NewHTTPDoError(req, err)
|
2021-02-19 22:23:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2023-05-05 09:49:38 +02:00
|
|
|
return errutils.NewUnexpectedResponseStatusCodeError(req, resp)
|
2021-02-19 22:23:48 +02:00
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
raw, err := io.ReadAll(resp.Body)
|
2021-02-19 22:23:48 +02:00
|
|
|
if err != nil {
|
2023-05-05 09:49:38 +02:00
|
|
|
return errutils.NewReadResponseError(req, resp.StatusCode, err)
|
2021-02-19 22:23:48 +02:00
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
err = json.Unmarshal(raw, result)
|
|
|
|
if err != nil {
|
|
|
|
return errutils.NewUnmarshalError(req, resp.StatusCode, raw, err)
|
2021-02-19 22:23:48 +02:00
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
return result.GetError()
|
2021-02-19 22:23:48 +02:00
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
func newJSONRequest(ctx context.Context, method string, endpoint string, 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)
|
|
|
|
}
|
2021-02-19 22:23:48 +02:00
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
req, err := http.NewRequestWithContext(ctx, method, endpoint, buf)
|
2021-02-19 22:23:48 +02:00
|
|
|
if err != nil {
|
2023-05-05 09:49:38 +02:00
|
|
|
return nil, fmt.Errorf("unable to create request: %w", err)
|
2021-02-19 22:23:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Accept", "application/json")
|
2023-05-05 09:49:38 +02:00
|
|
|
|
|
|
|
if payload != nil {
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
}
|
2021-02-19 22:23:48 +02:00
|
|
|
|
|
|
|
return req, nil
|
|
|
|
}
|