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

225 lines
5.5 KiB
Go
Raw Normal View History

2021-07-09 20:50:19 +00:00
package internal
import (
"bytes"
2023-05-05 07:49:38 +00:00
"context"
2021-07-09 20:50:19 +00:00
"encoding/json"
"fmt"
2021-08-25 09:44:11 +00:00
"io"
2021-07-09 20:50:19 +00:00
"net/http"
"net/url"
"strconv"
"time"
2023-05-05 07:49:38 +00:00
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
"golang.org/x/oauth2"
2021-07-09 20:50:19 +00:00
)
const defaultBaseURL = "https://api.ns1.hosttech.eu/api"
// Client a Hosttech client.
type Client struct {
baseURL *url.URL
2023-05-05 07:49:38 +00:00
httpClient *http.Client
2021-07-09 20:50:19 +00:00
}
// NewClient creates a new Client.
2023-05-05 07:49:38 +00:00
func NewClient(hc *http.Client) *Client {
2021-07-09 20:50:19 +00:00
baseURL, _ := url.Parse(defaultBaseURL)
2023-05-05 07:49:38 +00:00
if hc == nil {
hc = &http.Client{Timeout: 10 * time.Second}
2021-07-09 20:50:19 +00:00
}
2023-05-05 07:49:38 +00:00
return &Client{baseURL: baseURL, httpClient: hc}
2021-07-09 20:50:19 +00:00
}
// GetZones Get a list of all zones.
// https://api.ns1.hosttech.eu/api/documentation/#/Zones/get_api_user_v1_zones
2023-05-05 07:49:38 +00:00
func (c Client) GetZones(ctx context.Context, query string, limit, offset int) ([]Zone, error) {
2023-02-09 16:19:58 +00:00
endpoint := c.baseURL.JoinPath("user", "v1", "zones")
2021-07-09 20:50:19 +00:00
values := endpoint.Query()
values.Set("query", query)
if limit > 0 {
values.Set("limit", strconv.Itoa(limit))
}
if offset > 0 {
values.Set("offset", strconv.Itoa(offset))
}
endpoint.RawQuery = values.Encode()
2023-05-05 07:49:38 +00:00
req, err := newJSONRequest(ctx, http.MethodGet, endpoint, nil)
2021-07-09 20:50:19 +00:00
if err != nil {
return nil, fmt.Errorf("create request: %w", err)
}
2023-05-05 07:49:38 +00:00
result := apiResponse[[]Zone]{}
err = c.do(req, &result)
2021-07-09 20:50:19 +00:00
if err != nil {
return nil, err
}
2023-05-05 07:49:38 +00:00
return result.Data, nil
2021-07-09 20:50:19 +00:00
}
// GetZone Get a single zone.
// https://api.ns1.hosttech.eu/api/documentation/#/Zones/get_api_user_v1_zones__zoneId_
2023-05-05 07:49:38 +00:00
func (c Client) GetZone(ctx context.Context, zoneID string) (*Zone, error) {
2023-02-09 16:19:58 +00:00
endpoint := c.baseURL.JoinPath("user", "v1", "zones", zoneID)
2021-07-09 20:50:19 +00:00
2023-05-05 07:49:38 +00:00
req, err := newJSONRequest(ctx, http.MethodGet, endpoint, nil)
2021-07-09 20:50:19 +00:00
if err != nil {
return nil, fmt.Errorf("create request: %w", err)
}
2023-05-05 07:49:38 +00:00
result := apiResponse[*Zone]{}
err = c.do(req, &result)
2021-07-09 20:50:19 +00:00
if err != nil {
return nil, err
}
2023-05-05 07:49:38 +00:00
return result.Data, nil
2021-07-09 20:50:19 +00:00
}
// GetRecords Returns a list of all records for the given zone.
// https://api.ns1.hosttech.eu/api/documentation/#/Records/get_api_user_v1_zones__zoneId__records
2023-05-05 07:49:38 +00:00
func (c Client) GetRecords(ctx context.Context, zoneID, recordType string) ([]Record, error) {
2023-02-09 16:19:58 +00:00
endpoint := c.baseURL.JoinPath("user", "v1", "zones", zoneID, "records")
2021-07-09 20:50:19 +00:00
values := endpoint.Query()
if recordType != "" {
values.Set("type", recordType)
}
endpoint.RawQuery = values.Encode()
2023-05-05 07:49:38 +00:00
req, err := newJSONRequest(ctx, http.MethodGet, endpoint, nil)
2021-07-09 20:50:19 +00:00
if err != nil {
return nil, fmt.Errorf("create request: %w", err)
}
2023-05-05 07:49:38 +00:00
result := apiResponse[[]Record]{}
err = c.do(req, &result)
2021-07-09 20:50:19 +00:00
if err != nil {
return nil, err
}
2023-05-05 07:49:38 +00:00
return result.Data, nil
2021-07-09 20:50:19 +00:00
}
// AddRecord Adds a new record to the zone and returns the newly created record.
// https://api.ns1.hosttech.eu/api/documentation/#/Records/post_api_user_v1_zones__zoneId__records
2023-05-05 07:49:38 +00:00
func (c Client) AddRecord(ctx context.Context, zoneID string, record Record) (*Record, error) {
2023-02-09 16:19:58 +00:00
endpoint := c.baseURL.JoinPath("user", "v1", "zones", zoneID, "records")
2021-07-09 20:50:19 +00:00
2023-05-05 07:49:38 +00:00
req, err := newJSONRequest(ctx, http.MethodPost, endpoint, record)
2021-07-09 20:50:19 +00:00
if err != nil {
return nil, fmt.Errorf("create request: %w", err)
}
2023-05-05 07:49:38 +00:00
result := apiResponse[*Record]{}
err = c.do(req, &result)
2021-07-09 20:50:19 +00:00
if err != nil {
return nil, err
}
2023-05-05 07:49:38 +00:00
return result.Data, nil
2021-07-09 20:50:19 +00:00
}
// DeleteRecord Deletes a single record for the given id.
// https://api.ns1.hosttech.eu/api/documentation/#/Records/delete_api_user_v1_zones__zoneId__records__recordId_
2023-05-05 07:49:38 +00:00
func (c Client) DeleteRecord(ctx context.Context, zoneID, recordID string) error {
2023-02-09 16:19:58 +00:00
endpoint := c.baseURL.JoinPath("user", "v1", "zones", zoneID, "records", recordID)
2021-07-09 20:50:19 +00:00
2023-05-05 07:49:38 +00:00
req, err := newJSONRequest(ctx, http.MethodDelete, endpoint, nil)
2021-07-09 20:50:19 +00:00
if err != nil {
return fmt.Errorf("create request: %w", err)
}
2023-05-05 07:49:38 +00:00
return c.do(req, nil)
2021-07-09 20:50:19 +00:00
}
2023-05-05 07:49:38 +00:00
func (c Client) do(req *http.Request, result any) error {
resp, errD := c.httpClient.Do(req)
2021-07-09 20:50:19 +00:00
if errD != nil {
2023-05-05 07:49:38 +00:00
return errutils.NewHTTPDoError(req, errD)
2021-07-09 20:50:19 +00:00
}
2023-05-05 07:49:38 +00:00
2021-07-09 20:50:19 +00:00
defer func() { _ = resp.Body.Close() }()
switch resp.StatusCode {
case http.StatusOK, http.StatusCreated:
2023-05-05 07:49:38 +00:00
raw, err := io.ReadAll(resp.Body)
2021-07-09 20:50:19 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return errutils.NewReadResponseError(req, resp.StatusCode, err)
2021-07-09 20:50:19 +00:00
}
2023-05-05 07:49:38 +00:00
err = json.Unmarshal(raw, result)
2021-07-09 20:50:19 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return errutils.NewUnmarshalError(req, resp.StatusCode, raw, err)
2021-07-09 20:50:19 +00:00
}
2023-05-05 07:49:38 +00:00
return nil
2021-07-09 20:50:19 +00:00
case http.StatusNoContent:
2023-05-05 07:49:38 +00:00
return nil
2021-07-09 20:50:19 +00:00
default:
2023-05-05 07:49:38 +00:00
return parseError(req, resp)
}
}
func newJSONRequest(ctx context.Context, method string, endpoint *url.URL, payload any) (*http.Request, error) {
buf := new(bytes.Buffer)
2021-07-09 20:50:19 +00:00
2023-05-05 07:49:38 +00:00
if payload != nil {
err := json.NewEncoder(buf).Encode(payload)
2021-07-09 20:50:19 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return nil, fmt.Errorf("failed to create request JSON body: %w", err)
2021-07-09 20:50:19 +00:00
}
2023-05-05 07:49:38 +00:00
}
req, err := http.NewRequestWithContext(ctx, method, endpoint.String(), buf)
if err != nil {
return nil, fmt.Errorf("unable to create request: %w", err)
}
req.Header.Set("Accept", "application/json")
if payload != nil {
req.Header.Set("Content-Type", "application/json")
}
2021-07-09 20:50:19 +00:00
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)
errAPI := &APIError{StatusCode: resp.StatusCode}
err := json.Unmarshal(raw, errAPI)
if err != nil {
return errutils.NewUnexpectedStatusCodeError(req, resp.StatusCode, raw)
2021-07-09 20:50:19 +00:00
}
2023-05-05 07:49:38 +00:00
return errAPI
}
func OAuthStaticAccessToken(client *http.Client, accessToken string) *http.Client {
if client == nil {
client = &http.Client{Timeout: 5 * time.Second}
}
client.Transport = &oauth2.Transport{
Source: oauth2.StaticTokenSource(&oauth2.Token{AccessToken: accessToken}),
Base: client.Transport,
}
return client
2021-07-09 20:50:19 +00:00
}