-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
142 lines (116 loc) · 2.58 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package porkbun
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"os"
)
const (
PORKBUN_API_KEY = "PORKBUN_API_KEY"
PORKBUN_SECRET_KEY = "PORKBUN_SECRET_KEY"
)
type MissingAccessKeyError struct {
Key string
}
func (e MissingAccessKeyError) Error() string {
var keyType string
if e.Key == PORKBUN_API_KEY {
keyType = "api"
}
if e.Key == PORKBUN_SECRET_KEY {
keyType = "secret"
}
return fmt.Sprintf("missing porkbun %q key. try setting %q to the environment", keyType, e.Key)
}
type HttpClient interface {
Do(*http.Request) (*http.Response, error)
}
type Option func(*Client) error
type Client struct {
apiKey string
secretKey string
baseUrl string
client HttpClient
}
// NewClient creates a new porkbun client.
// By default, it
func NewClient(options ...Option) (*Client, error) {
c := &Client{
apiKey: os.Getenv(PORKBUN_API_KEY),
secretKey: os.Getenv(PORKBUN_SECRET_KEY),
baseUrl: "https://porkbun.com",
client: &http.Client{},
}
for _, option := range options {
err := option(c)
if err != nil {
return nil, err
}
}
return c, nil
}
func WithApiKey(key string) Option {
return func(c *Client) error {
c.apiKey = key
return nil
}
}
func WithSecretKey(key string) Option {
return func(c *Client) error {
c.secretKey = key
return nil
}
}
func WithBaseUrl(url string) Option {
return func(c *Client) error {
c.baseUrl = url
return nil
}
}
func WithHttpClient(httpClient HttpClient) Option {
return func(c *Client) error {
c.client = httpClient
return nil
}
}
func (c *Client) withAuthentication(body []byte) ([]byte, error) {
if c.apiKey == "" {
return nil, MissingAccessKeyError{Key: PORKBUN_API_KEY}
}
if c.secretKey == "" {
return nil, MissingAccessKeyError{Key: PORKBUN_SECRET_KEY}
}
var orig map[string]interface{}
if len(body) > 0 {
if err := json.Unmarshal(body, &orig); err != nil {
return nil, fmt.Errorf("could not unmarshal body, %w", err)
}
}
newMap := map[string]interface{}{
"apikey": c.apiKey,
"secretapikey": c.secretKey,
}
// Add original body
for k, v := range orig {
newMap[k] = v
}
// Marshal new body
newBody, err := json.Marshal(newMap)
if err != nil {
return nil, fmt.Errorf("err marshaling json, %w", err)
}
return newBody, nil
}
func (c *Client) do(ctx context.Context, endpoint string, body []byte) (*http.Response, error) {
req, err := http.NewRequest(
http.MethodPost,
fmt.Sprintf("%s/%s", c.baseUrl, endpoint),
bytes.NewReader(body),
)
if err != nil {
return nil, fmt.Errorf("err creating new request, %w", err)
}
return c.client.Do(req)
}