-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdomains.go
155 lines (125 loc) · 3.14 KB
/
domains.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
package gonjalla
import (
"encoding/json"
"time"
)
// Domain struct contains data returned by `list-domains` and `get-domains`
type Domain struct {
Name string `json:"name"`
Status string `json:"status"`
Expiry time.Time `json:"expiry"`
Locked *bool `json:"locked,omitempty"`
Mailforwarding *bool `json:"mailforwarding,omitempty"`
MaxNameservers *int `json:"max_nameservers,omitempty"`
}
// Domain availability and price data returned by `find-domains`
type MarketDomain struct {
Name string `json:"name"`
Status string `json:"status"`
Price int `json:"price"`
}
// ListDomains returns a listing of domains with minimal data
func ListDomains(token string) ([]Domain, error) {
params := map[string]interface{}{}
data, err := Request(token, "list-domains", params)
if err != nil {
return nil, err
}
type Response struct {
Domains []Domain `json:"domains"`
}
var response Response
err = json.Unmarshal(data, &response)
if err != nil {
return nil, err
}
return response.Domains, nil
}
// GetDomain returns detailed information for each domain
func GetDomain(token string, domain string) (Domain, error) {
params := map[string]interface{}{
"domain": domain,
}
data, err := Request(token, "get-domain", params)
if err != nil {
return Domain{}, err
}
var domainStruct Domain
err = json.Unmarshal(data, &domainStruct)
if err != nil {
return Domain{}, err
}
return domainStruct, nil
}
// FindDomains returns availability and price information for a query.
// If query was `example`, then it'd show availability and price of
// domains `example.com`, `example.net`, etc.
func FindDomains(token string, query string) ([]MarketDomain, error) {
params := map[string]interface{}{
"query": query,
}
data, err := Request(token, "find-domains", params)
if err != nil {
return nil, err
}
type Response struct {
Domains []MarketDomain `json:"domains"`
}
var response Response
err = json.Unmarshal(data, &response)
if err != nil {
return nil, err
}
return response.Domains, nil
}
// Checks the task status given a task id
func CheckTask(token string, id string) (string, error) {
params := map[string]interface{}{
"id": id,
}
data, err := Request(token, "check-task", params)
if err != nil {
return "", err
}
type Response struct {
id string `json:"id"`
status string `json:"status"`
}
var response Response
err = json.Unmarshal(data, &response)
if err != nil {
return "", err
}
return response.status, nil
}
// Registers a domain given a domain name and desired term length
func RegisterDomain(token string, domain string, years int) error {
params := map[string]interface{}{
"domain": domain,
"years": years,
}
data, err := Request(token, "register-domain", params)
if err != nil {
return err
}
type Response struct {
task string `json:"task"`
}
var response Response
err = json.Unmarshal(data, &response)
if err != nil {
return err
}
var status string
for true {
status, err = CheckTask(token, response.task)
if err != nil {
return err
}
if status == "active" {
break
}
time.Sleep(5 * time.Second)
}
return nil
}