Skip to content

Commit 9f89df4

Browse files
committed
Add DomainService/batchCheckAvailability
1 parent 1e0a3de commit 9f89df4

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

README.md

+30
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Small library in Golang that implements:
55
* DomainService/getInfo
66
* DomainService/batchGetInfo
77
* DomainService/setDnsEntries
8+
* DomainService/batchCheckAvailability
89
* DomainService/checkAvailability
910

1011
If you need other methods on the TransIP API be free to fork my
@@ -142,3 +143,32 @@ func checkAvailability(username, privKeyPath, domain string) (string, error) {
142143
return res, nil
143144
}
144145
```
146+
147+
Check availability of a batch of domains
148+
```go
149+
package main
150+
151+
import (
152+
"github.com/mpdroog/transip"
153+
"fmt"
154+
)
155+
156+
func checkAvailability(username, privKeyPath string, domains []string) ([]transip.DomainCheckResult, error) {
157+
creds := transip.Client{
158+
Login: username,
159+
ReadWrite: false,
160+
}
161+
if err := creds.SetPrivateKeyFromPath(privKeyPath); err != nil {
162+
return nil, fmt.Errorf("could not load private key from path %s: %s",
163+
privKeyPath, err)
164+
}
165+
166+
domainService := transip.DomainService{creds}
167+
res, err := domainService.BatchCheckAvailability(domains)
168+
if err != nil {
169+
return nil, err
170+
}
171+
172+
return res, nil
173+
}
174+
```

decode.go

+10
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ type Domain struct {
7171
RenewalDate string `xml:"renewalDate"`
7272
}
7373

74+
type domainCheckResults struct {
75+
Results []DomainCheckResult `xml:"item"`
76+
}
77+
78+
type DomainCheckResult struct {
79+
DomainName string `xml:"domainName"`
80+
Status string `xml:"status"`
81+
Actions []string `xml:"actions>item"`
82+
}
83+
7484
// Convert rawbody to XML and subtract the 'body' from the
7585
// SOAP-envelope into the struct given with out
7686
func decode(rawbody []byte, out interface{}) error {

domainservice.go

+27
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,30 @@ func (c *DomainService) CheckAvailability(name string) (string, error) {
123123
e = decode(rawbody, &availability)
124124
return availability.Item, e
125125
}
126+
127+
func (c *DomainService) BatchCheckAvailability(names []string) ([]DomainCheckResult, error) {
128+
entryTemplate := `<item xsi:type="xsd:string">%s</item>`
129+
params := []kV{}
130+
xml := ``
131+
132+
for idx, v := range names {
133+
xml = xml + fmt.Sprintf(entryTemplate, v)
134+
params = append(params, []kV{
135+
{Key: fmt.Sprintf("0[%d]", idx), Value: v},
136+
}...)
137+
}
138+
139+
rawbody, e := lookup(c.Creds, request{
140+
Service: domainService,
141+
ExtraParams: params,
142+
Method: "batchCheckAvailability",
143+
Body: fmt.Sprintf(`<ns1:batchCheckAvailability soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><domainNames xsi:type="soap:ArrayOfstring" soapenc:arrayType="xsd:string[%d]">%s</domainNames></ns1:batchCheckAvailability>`, len(names), xml),
144+
})
145+
if e != nil {
146+
return nil, e
147+
}
148+
149+
dcResults := &domainCheckResults{}
150+
e = decode(rawbody, &dcResults)
151+
return dcResults.Results, e
152+
}

0 commit comments

Comments
 (0)