1
0
mirror of https://github.com/go-acme/lego.git synced 2024-12-23 09:15:11 +02:00
lego/providers/dns/constellix/internal/client.go

111 lines
2.4 KiB
Go
Raw Normal View History

package internal
import (
"encoding/json"
"fmt"
2021-08-25 11:44:11 +02:00
"io"
"net/http"
"net/url"
2023-05-05 09:49:38 +02:00
"time"
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
)
const (
defaultBaseURL = "https://api.dns.constellix.com"
defaultVersion = "v1"
)
// Client the Constellix client.
type Client struct {
BaseURL string
HTTPClient *http.Client
common service // Reuse a single struct instead of allocating one for each service on the heap.
// Services used for communicating with the API
Domains *DomainService
TxtRecords *TxtRecordService
}
// NewClient Creates a Constellix client.
func NewClient(httpClient *http.Client) *Client {
if httpClient == nil {
2023-05-05 09:49:38 +02:00
httpClient = &http.Client{Timeout: 5 * time.Second}
}
client := &Client{
BaseURL: defaultBaseURL,
HTTPClient: httpClient,
}
client.common.client = client
client.Domains = (*DomainService)(&client.common)
client.TxtRecords = (*TxtRecordService)(&client.common)
return client
}
type service struct {
client *Client
}
// do sends an API request and returns the API response.
2023-05-05 09:49:38 +02:00
func (c *Client) do(req *http.Request, result any) error {
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
resp, err := c.HTTPClient.Do(req)
if err != nil {
2023-05-05 09:49:38 +02:00
return errutils.NewHTTPDoError(req, err)
}
2023-05-05 09:49:38 +02:00
defer func() { _ = resp.Body.Close() }()
err = checkResponse(resp)
if err != nil {
return err
}
2021-08-25 11:44:11 +02:00
raw, err := io.ReadAll(resp.Body)
if err != nil {
2023-05-05 09:49:38 +02:00
return errutils.NewReadResponseError(req, resp.StatusCode, err)
}
2023-05-05 09:49:38 +02:00
if err = json.Unmarshal(raw, result); err != nil {
return errutils.NewUnmarshalError(req, resp.StatusCode, raw, err)
}
return nil
}
func (c *Client) createEndpoint(fragment ...string) (string, error) {
2023-02-09 18:19:58 +02:00
return url.JoinPath(c.BaseURL, fragment...)
}
func checkResponse(resp *http.Response) error {
if resp.StatusCode == http.StatusOK {
return nil
}
2023-05-05 09:49:38 +02:00
raw, err := io.ReadAll(resp.Body)
if err == nil && raw != nil {
errAPI := &APIError{StatusCode: resp.StatusCode}
2020-04-18 20:39:10 +02:00
2023-05-05 09:49:38 +02:00
if json.Unmarshal(raw, errAPI) != nil {
return fmt.Errorf("API error: status code: %d: %v", resp.StatusCode, string(raw))
}
2020-04-18 20:39:10 +02:00
switch resp.StatusCode {
case http.StatusNotFound:
2023-05-05 09:49:38 +02:00
return &NotFound{APIError: errAPI}
2020-04-18 20:39:10 +02:00
case http.StatusBadRequest:
2023-05-05 09:49:38 +02:00
return &BadRequest{APIError: errAPI}
2020-04-18 20:39:10 +02:00
default:
2023-05-05 09:49:38 +02:00
return errAPI
2020-04-18 20:39:10 +02:00
}
}
return fmt.Errorf("API error, status code: %d", resp.StatusCode)
}