Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Timeout to Client #25

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
)

const (
domainsDNSGetHosts = "namecheap.domains.dns.getHosts"
domainsDNSSetHosts = "namecheap.domains.dns.setHosts"
domainsDNSSetCustom = "namecheap.domains.dns.setCustom"
domainsDNSGetHosts = "namecheap.domains.dns.getHosts"
domainsDNSSetHosts = "namecheap.domains.dns.setHosts"
domainsDNSSetCustom = "namecheap.domains.dns.setCustom"
domainsDNSSetDefault = "namecheap.domains.dns.setDefault"
)

type DomainDNSGetHostsResult struct {
Expand Down Expand Up @@ -99,3 +100,24 @@ func (client *Client) DomainDNSSetCustom(sld, tld, nameservers string) (*DomainD
}
return resp.DomainDNSSetCustom, nil
}

type DomainDNSSetDefaultResult struct {
Domain string `xml:"Domain,attr"`
Update bool `xml:"Updated,attr"`
}

func (client *Client) DomainDNSSetDefault(sld, tld string) (*DomainDNSSetDefaultResult, error) {
requestInfo := &ApiRequest{
command: domainsDNSSetDefault,
method: "POST",
params: url.Values{},
}
requestInfo.params.Set("SLD", sld)
requestInfo.params.Set("TLD", tld)

resp, err := client.do(requestInfo)
if err != nil {
return nil, err
}
return resp.DomainDNSSetDefault, nil
}
42 changes: 42 additions & 0 deletions dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,45 @@ func TestDomainsDNSSetCustom(t *testing.T) {
t.Errorf("DomainsDNSSetCustom returned %+v, want %+v", result, want)
}
}

func TestDomainsDNSSetDefault(t *testing.T) {
setup()
defer teardown()

respXML := `
<?xml version="1.0" encoding="UTF-8"?>
<ApiResponse xmlns="http://api.namecheap.com/xml.response" Status="OK">
<Errors />
<RequestedCommand>namecheap.domains.dns.setDefault</RequestedCommand>
<CommandResponse Type="namecheap.domains.dns.setDefault">
<DomainDNSSetDefaultResult Domain="domain.com" Updated="true" />
</CommandResponse>
<Server>SERVER-NAME</Server>
<GMTTimeDifference>+5</GMTTimeDifference>
<ExecutionTime>32.76</ExecutionTime>
</ApiResponse>`

mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
correctParams := fillDefaultParams(url.Values{})
correctParams.Set("Command", "namecheap.domains.dns.setDefault")
correctParams.Set("SLD", "domain")
correctParams.Set("TLD", "com")
testBody(t, r, correctParams)
testMethod(t, r, "POST")
fmt.Fprint(w, respXML)
})

result, err := client.DomainDNSSetDefault("domain", "com")
if err != nil {
t.Errorf("DomainDNSSetDefault returned error: %v", err)
}

want := &DomainDNSSetDefaultResult{
Domain: "domain.com",
Update: true,
}

if !reflect.DeepEqual(result, want) {
t.Errorf("DomainsDNSSetDefault returned %+v, want %+v", result, want)
}
}
76 changes: 67 additions & 9 deletions domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
)

const (
domainsGetList = "namecheap.domains.getList"
domainsGetInfo = "namecheap.domains.getInfo"
domainsCheck = "namecheap.domains.check"
domainsCreate = "namecheap.domains.create"
domainsTLDList = "namecheap.domains.getTldList"
domainsRenew = "namecheap.domains.renew"
domainsGetList = "namecheap.domains.getList"
domainsGetInfo = "namecheap.domains.getInfo"
domainsCheck = "namecheap.domains.check"
domainsCreate = "namecheap.domains.create"
domainsTLDList = "namecheap.domains.getTldList"
domainsRenew = "namecheap.domains.renew"
domainsReactivate = "namecheap.domains.reactivate"
domainsSetContacts = "namecheap.domains.setContacts"
)

// DomainGetListResult represents the data returned by 'domains.getList'
Expand Down Expand Up @@ -50,9 +52,10 @@ type DNSDetails struct {
}

type Whoisguard struct {
Enabled bool `xml:"Enabled,attr"`
ID int64 `xml:"ID"`
ExpiredDate string `xml:"ExpiredDate"`
EnabledString string `xml:"Enabled,attr"`
Enabled bool
ID int64 `xml:"ID"`
ExpiredDate string `xml:"ExpiredDate"`
}

type DomainCheckResult struct {
Expand Down Expand Up @@ -91,6 +94,19 @@ type DomainRenewResult struct {
ExpireDate string `xml:"DomainDetails>ExpiredDate"`
}

type DomainReactivateResult struct {
Name string `xml:"Domain,attr"`
Reactivated bool `xml:"IsSuccess,attr"`
ChargedAmount float64 `xml:"ChargedAmount,attr"`
OrderID int `xml:"OrderID,attr"`
TransactionID int `xml:"TransactionID,attr"`
}

type DomainSetContactsResult struct {
Name string `xml:"Domain,attr"`
ContactsChanged bool `xml:"IsSuccess,attr"`
}

type DomainCreateOption struct {
AddFreeWhoisguard bool
WGEnabled bool
Expand Down Expand Up @@ -126,6 +142,11 @@ func (client *Client) DomainGetInfo(domainName string) (*DomainInfo, error) {
return nil, err
}

switch strings.ToLower(resp.DomainInfo.Whoisguard.EnabledString) {
case "1", "true":
resp.DomainInfo.Whoisguard.Enabled = true
}

return resp.DomainInfo, nil
}

Expand Down Expand Up @@ -212,3 +233,40 @@ func (client *Client) DomainRenew(domainName string, years int) (*DomainRenewRes

return resp.DomainRenew, nil
}

func (client *Client) DomainReactivate(domainName string, years int) (*DomainReactivateResult, error) {
requestInfo := &ApiRequest{
command: domainsReactivate,
method: "POST",
params: url.Values{},
}
requestInfo.params.Set("DomainName", domainName)
requestInfo.params.Set("Years", strconv.Itoa(years))

resp, err := client.do(requestInfo)
if err != nil {
return nil, err
}

return resp.DomainReactivate, nil
}

func (client *Client) DomainSetContacts(domainName string, registrant *Registrant) (*DomainSetContactsResult, error) {
requestInfo := &ApiRequest{
command: domainsSetContacts,
method: "POST",
params: url.Values{},
}
requestInfo.params.Set("DomainName", domainName)

if err := registrant.addValues(requestInfo.params); err != nil {
return nil, err
}

resp, err := client.do(requestInfo)
if err != nil {
return nil, err
}

return resp.DomainSetContacts, nil
}
192 changes: 189 additions & 3 deletions domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,94 @@ func TestDomainGetInfo(t *testing.T) {
},
},
Whoisguard: Whoisguard{
Enabled: true,
ID: 53536,
ExpiredDate: "11/04/2015",
Enabled: true,
EnabledString: "True",
ID: 53536,
ExpiredDate: "11/04/2015",
},
}

if !reflect.DeepEqual(domain, want) {
t.Errorf("DomainGetInfo returned %+v, want %+v", domain, want)
}
}

func TestDomainGetInfoNotAlloted(t *testing.T) {
setup()
defer teardown()

respXML := `<?xml version="1.0" encoding="utf-8"?>
<ApiResponse Status="OK" xmlns="http://api.namecheap.com/xml.response">
<Errors />
<Warnings />
<RequestedCommand>namecheap.domains.getinfo</RequestedCommand>
<CommandResponse Type="namecheap.domains.getInfo">
<DomainGetInfoResult Status="Ok" ID="37103805" DomainName="example.com" OwnerName="anUser" IsOwner="true" IsPremium="false">
<DomainDetails>
<CreatedDate>07/25/2019</CreatedDate>
<ExpiredDate>07/25/2020</ExpiredDate>
<NumYears>0</NumYears>
</DomainDetails>
<LockDetails />
<Whoisguard Enabled="NotAlloted">
<ID>0</ID>
</Whoisguard>
<PremiumDnsSubscription>
<UseAutoRenew>false</UseAutoRenew>
<SubscriptionId>-1</SubscriptionId>
<CreatedDate>0001-01-01T00:00:00</CreatedDate>
<ExpirationDate>0001-01-01T00:00:00</ExpirationDate>
<IsActive>false</IsActive>
</PremiumDnsSubscription>
<DnsDetails ProviderType="FREE" IsUsingOurDNS="true" HostCount="2" EmailType="FWD" DynamicDNSStatus="false" IsFailover="false">
<Nameserver>dns1.registrar-servers.com</Nameserver>
<Nameserver>dns2.registrar-servers.com</Nameserver>
</DnsDetails>
<Modificationrights All="true" />
</DomainGetInfoResult>
</CommandResponse>
<Server>PHX01APIEXT01</Server>
<GMTTimeDifference>--4:00</GMTTimeDifference>
<ExecutionTime>0.018</ExecutionTime>
</ApiResponse>`

mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
correctParams := fillDefaultParams(url.Values{})
correctParams.Set("Command", "namecheap.domains.getInfo")
correctParams.Set("DomainName", "example.com")
testBody(t, r, correctParams)
testMethod(t, r, "POST")
fmt.Fprint(w, respXML)
})

domain, err := client.DomainGetInfo("example.com")

if err != nil {
t.Errorf("DomainGetInfo returned error: %v", err)
}

// DomainInfo we expect, given the respXML above
want := &DomainInfo{
ID: 37103805,
Name: "example.com",
Owner: "anUser",
Created: "07/25/2019",
Expires: "07/25/2020",
IsExpired: false,
IsLocked: false,
AutoRenew: false,
DNSDetails: DNSDetails{
ProviderType: "FREE",
IsUsingOurDNS: true,
Nameservers: []string{
"dns1.registrar-servers.com",
"dns2.registrar-servers.com",
},
},
Whoisguard: Whoisguard{
Enabled: false,
EnabledString: "NotAlloted",
ID: 0,
},
}

Expand Down Expand Up @@ -321,3 +406,104 @@ func TestDomainsRenew(t *testing.T) {
t.Errorf("DomainRenew returned %+v, want %+v", result, want)
}
}

func TestDomainsReactivate(t *testing.T) {
setup()
defer teardown()

respXML := `<?xml version="1.0" encoding="UTF-8"?>
<ApiResponse xmlns="http://api.namecheap.com/xml.response" Status="OK">
<Errors />
<Warnings />
<RequestedCommand>namecheap.domains.reactivate</RequestedCommand>
<CommandResponse Type="namecheap.domains.reactivate">
<DomainReactivateResult Domain="domain1.com" IsSuccess="true" ChargedAmount="650.0000" OrderID="23569" TransactionID="25080" />
</CommandResponse>
<Server>SERVER-NAME</Server>
<GMTTimeDifference>+5:00</GMTTimeDifference>
<ExecutionTime>12.915</ExecutionTime>
</ApiResponse>`

mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
correctParams := fillDefaultParams(url.Values{})
correctParams.Set("Command", "namecheap.domains.reactivate")
correctParams.Set("DomainName", "domain1.com")
correctParams.Set("Years", "1")
testBody(t, r, correctParams)
testMethod(t, r, "POST")
fmt.Fprint(w, respXML)
})

result, err := client.DomainReactivate("domain1.com", 1)
if err != nil {
t.Errorf("DomainReactivate returned error: %v", err)
}

// DomainReactivateResult we expect, given the respXML above
want := &DomainReactivateResult{
Name: "domain1.com",
Reactivated: true,
ChargedAmount: 650,
TransactionID: 25080,
OrderID: 23569,
}
if !reflect.DeepEqual(result, want) {
t.Errorf("DomainsReactivate returned %+v, want %+v", result, want)
}
}

func TestDomainsSetContacts(t *testing.T) {
setup()
defer teardown()

respXML := `<?xml version="1.0" encoding="UTF-8"?>
<ApiResponse xmlns="http://api.namecheap.com/xml.response" Status="OK">
<Errors />
<RequestedCommand>namecheap.domains.setContacts</RequestedCommand>
<CommandResponse Type="namecheap.domains.setContacts">
<DomainSetContactResult Domain="domain1.com" IsSuccess="true" />
</CommandResponse>
<Server>SERVER-NAME</Server>
<GMTTimeDifference>+5</GMTTimeDifference>
<ExecutionTime>0.078</ExecutionTime>
</ApiResponse>`

mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
correctParams := fillDefaultParams(url.Values{})
fillInfo := func(prefix string) {
correctParams.Set(prefix+"FirstName", "John")
correctParams.Set(prefix+"LastName", "Smith")
correctParams.Set(prefix+"Address1", "8939 S.cross Blvd")
correctParams.Set(prefix+"StateProvince", "CA")
correctParams.Set(prefix+"PostalCode", "90045")
correctParams.Set(prefix+"Country", "US")
correctParams.Set(prefix+"Phone", "+1.6613102107")
correctParams.Set(prefix+"EmailAddress", "[email protected]")
correctParams.Set(prefix+"City", "CA")
}
correctParams.Set("Command", "namecheap.domains.setContacts")
correctParams.Set("DomainName", "domain1.com")
fillInfo("AuxBilling")
fillInfo("Tech")
fillInfo("Admin")
fillInfo("Registrant")
testBody(t, r, correctParams)
testMethod(t, r, "POST")
fmt.Fprint(w, respXML)
})

registrant := NewRegistrant("John", "Smith", "8939 S.cross Blvd", "", "CA", "CA", "90045", "US", "+1.6613102107", "[email protected]")
result, err := client.DomainSetContacts("domain1.com", registrant)
if err != nil {
t.Errorf("DomainSetContacts returned error: %v", err)
}

// DomainSetContactsResult we expect, given the respXML above
want := &DomainSetContactsResult{
Name: "domain1.com",
ContactsChanged: true,
}
if !reflect.DeepEqual(result, want) {
t.Errorf("DomainSetContacts returned %+v, want %+v", result, want)
}
}
Loading