-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
99 lines (81 loc) · 2.17 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
package porkbungo
import (
"fmt"
"github.com/go-resty/resty/v2"
)
const (
// Hostname to force the use of IPv4
IPv4ApiHost = "api-ipv4.porkbun.com"
// Porkbun API hostname
APIHost = "porkbun.com"
// Porkbun API base path
BasePath = "/api/json"
// Porkbun API version
APIVersion = "v3"
// connect to API with http(s)
APIProto = "https"
)
type Keys struct {
// API Key usually starts with 'pk1_'
APIKey string `json:"apikey"`
// Secret key, usually starts with 'sk1_'
SecretAPIKey string `json:"secretapikey"`
}
type APIError struct {
// Status indicating the command was not successful
Status string `json:"status"`
// Error message
Message string `json:"message"`
}
// Client is a wrapper around the Resty client
type Client struct {
resty *resty.Client
keys *Keys
useIPv4 bool
}
// NewClient factory to create a new Client struct
func NewClient() (client Client) {
client.resty = resty.New()
client.useIPv4 = false
u := fmt.Sprintf("%s://%s%s/%s",
APIProto,
APIHost,
BasePath,
APIVersion)
client.resty.SetBaseURL(u).SetError(&APIError{})
return
}
// NewClient factory to create a new client with the
// the specified API keys
func NewClientWithAPIKeys(keys *Keys) (client Client) {
client = NewClient()
client.keys = keys
return
}
// SetAPIKeys sets the API keys for to be used
// with all the requests from this client. The API keys
// can be overridden for individual requests using the
// requests options
func (c *Client) SetAPIKeys(keys *Keys) {
c.keys = keys
}
// Force client to use IPv4
func (c *Client) UseIPv4(value bool) {
c.useIPv4 = value
c.updateHostUrl()
}
func (c *Client) updateHostUrl() {
useIPv4 := c.useIPv4
var apiHost string;
if useIPv4 {
apiHost = IPv4ApiHost
} else {
apiHost = APIHost
}
u := fmt.Sprintf("%s://%s%s/%s",
APIProto,
apiHost,
BasePath,
APIVersion)
c.resty.SetBaseURL(u)
}