forked from billputer/go-namecheap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
namecheap.go
167 lines (143 loc) · 4.81 KB
/
namecheap.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Package namecheap implements a client for the Namecheap API.
//
// In order to use this package you will need a Namecheap account and your API Token.
package namecheap
import (
"encoding/xml"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
)
const defaultBaseURL = "https://api.namecheap.com/xml.response"
// Client represents a client used to make calls to the Namecheap API.
type Client struct {
ApiUser string
ApiToken string
UserName string
HttpClient *http.Client
// Base URL for API requests.
// Defaults to the public Namecheap API,
// but can be set to a different endpoint (e.g. the sandbox).
// BaseURL should always be specified with a trailing slash.
BaseURL string
*Registrant
}
type ApiRequest struct {
method string
command string
params url.Values
}
type ApiResponse struct {
Status string `xml:"Status,attr"`
Command string `xml:"RequestedCommand"`
TLDList []TLDListResult `xml:"CommandResponse>Tlds>Tld"`
Domains []DomainGetListResult `xml:"CommandResponse>DomainGetListResult>Domain"`
DomainInfo *DomainInfo `xml:"CommandResponse>DomainGetInfoResult"`
DomainDNSHosts *DomainDNSGetHostsResult `xml:"CommandResponse>DomainDNSGetHostsResult"`
DomainDNSSetHosts *DomainDNSSetHostsResult `xml:"CommandResponse>DomainDNSSetHostsResult"`
DomainCreate *DomainCreateResult `xml:"CommandResponse>DomainCreateResult"`
DomainRenew *DomainRenewResult `xml:"CommandResponse>DomainRenewResult"`
DomainsCheck []DomainCheckResult `xml:"CommandResponse>DomainCheckResult"`
DomainNSInfo *DomainNSInfoResult `xml:"CommandResponse>DomainNSInfoResult"`
DomainDNSSetCustom *DomainDNSSetCustomResult `xml:"CommandResponse>DomainDNSSetCustomResult"`
UsersGetPricing []UsersGetPricingResult `xml:"CommandResponse>UserGetPricingResult>ProductType"`
WhoisguardList []WhoisguardGetListResult `xml:"CommandResponse>WhoisguardGetListResult>Whoisguard"`
WhoisguardEnable whoisguardEnableResult `xml:"CommandResponse>WhoisguardEnableResult"`
WhoisguardDisable whoisguardDisableResult `xml:"CommandResponse>WhoisguardDisableResult"`
WhoisguardRenew *WhoisguardRenewResult `xml:"CommandResponse>WhoisguardRenewResult"`
Errors ApiErrors `xml:"Errors>Error"`
}
// ApiError is the format of the error returned in the api responses.
type ApiError struct {
Number int `xml:"Number,attr"`
Message string `xml:",innerxml"`
}
func (err *ApiError) Error() string {
return err.Message
}
// ApiErrors holds multiple ApiError's but implements the error interface
type ApiErrors []ApiError
func (errs ApiErrors) Error() string {
errMsg := ""
for _, apiError := range errs {
errMsg += fmt.Sprintf("Error %d: %s\n", apiError.Number, apiError.Message)
}
return errMsg
}
func NewClient(apiUser, apiToken, userName string) *Client {
return &Client{
ApiUser: apiUser,
ApiToken: apiToken,
UserName: userName,
HttpClient: http.DefaultClient,
BaseURL: defaultBaseURL,
}
}
// NewRegistrant associates a new registrant with the
func (client *Client) NewRegistrant(
firstName, lastName,
addr1, addr2,
city, state, postalCode, country,
phone, email string,
) {
client.Registrant = newRegistrant(
firstName, lastName,
addr1, addr2,
city, state, postalCode, country,
phone, email,
)
}
func (client *Client) do(request *ApiRequest) (*ApiResponse, error) {
if request.method == "" {
return nil, errors.New("request method cannot be blank")
}
body, _, err := client.sendRequest(request)
if err != nil {
return nil, err
}
resp := new(ApiResponse)
if err = xml.Unmarshal(body, resp); err != nil {
return nil, err
}
if resp.Status == "ERROR" {
return nil, resp.Errors
}
return resp, nil
}
func (client *Client) makeRequest(request *ApiRequest) (*http.Request, error) {
p := request.params
p.Set("ApiUser", client.ApiUser)
p.Set("ApiKey", client.ApiToken)
p.Set("UserName", client.UserName)
// This param is required by the API, but not actually used.
p.Set("ClientIp", "127.0.0.1")
p.Set("Command", request.command)
b := p.Encode()
req, err := http.NewRequest(request.method, client.BaseURL, strings.NewReader(b))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Content-Length", strconv.Itoa(len(b)))
return req, nil
}
func (client *Client) sendRequest(request *ApiRequest) ([]byte, int, error) {
req, err := client.makeRequest(request)
if err != nil {
return nil, 0, err
}
resp, err := client.HttpClient.Do(req)
if err != nil {
return nil, 0, err
}
defer resp.Body.Close()
buf, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, 0, err
}
return buf, resp.StatusCode, nil
}