-
Notifications
You must be signed in to change notification settings - Fork 0
/
domains_availability.go
86 lines (69 loc) · 1.91 KB
/
domains_availability.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
package resellerclub
import (
"encoding/json"
"io/ioutil"
"net/http"
"sort"
)
const (
DomainStatusAvailable = "available"
DomainStatusRegisteredThroughUs = "regthroughus"
DomainStatusRegisteredThroughOthers = "regthroughothers"
DomainStatusUnknown = "unknown"
)
type DomainAvailabilityResponse struct {
Domain string `json:"domain"`
Classkey string `json:"classkey"`
Status string `json:"status"`
}
// CheckAvailability checks domains' availability.
// https://manage.resellerclub.com/kb/answer/764
func (domains *Domains) CheckAvailability(domainNames []string, tlds []string) ([]*DomainAvailabilityResponse, error) {
u := domains.url("/available.json")
q := u.Query()
q["domain-name"] = domainNames
q["tlds"] = tlds
u.RawQuery = q.Encode()
resp, err := http.Get(u.String())
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, somethingWentWrong(string(body))
}
mapResp := map[string]interface{}{}
err = json.Unmarshal(body, &mapResp)
if err != nil {
return nil, somethingWentWrong(string(body))
}
err = checkResponseError(mapResp)
if err != nil {
return nil, err
}
domainsAvailability := []*DomainAvailabilityResponse{}
for domain, data := range mapResp {
d, ok := data.(map[string]interface{})
if !ok {
return nil, somethingWentWrong(mapResp)
}
status, ok := d["status"].(string)
if !ok {
return nil, somethingWentWrong(mapResp)
}
classkey, _ := d["classkey"].(string)
domainsAvailability = append(domainsAvailability, &DomainAvailabilityResponse{
Domain: domain,
Classkey: classkey,
Status: status,
})
}
if len(domainsAvailability) == 0 {
return nil, somethingWentWrong(mapResp)
}
sort.Slice(domainsAvailability, func(i, j int) bool {
return domainsAvailability[i].Domain < domainsAvailability[j].Domain
})
return domainsAvailability, nil
}