Add find-domains endpoint

The `find-domains` endpoint allows you to check the availability and
price of domains for a given query. So if the query was `example`, it'd
show availability and price of domains `example.com`, `example.net`,
etc.
This commit is contained in:
kayos@tcp.direct 2021-08-27 21:37:04 -07:00 committed by Sighery
parent 3b41066138
commit cf5fdbb592
2 changed files with 131 additions and 0 deletions

View file

@ -15,6 +15,13 @@ type Domain struct {
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{}{}
@ -56,3 +63,29 @@ func GetDomain(token string, domain string) (Domain, error) {
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
}

View file

@ -161,3 +161,101 @@ func TestGetDomainError(t *testing.T) {
_, err := GetDomain(token, domain)
assert.Error(t, err)
}
func TestFindDomainsExpected(t *testing.T) {
token := "test-token"
query := "testing"
Client = &mocks.MockClient{}
testData := `{
"result": {
"jsonrpc": "2.0",
"domains": [
{
"name": "testing.com",
"status": "taken",
"price": 45
},
{
"name": "testing.net",
"status": "available",
"price": 30
},
{
"name": "testing.rocks",
"status": "in progress",
"price": 15
},
{
"name": "testing.express",
"status": "failed",
"price": 75
}
]
}
}`
r := ioutil.NopCloser(bytes.NewReader([]byte(testData)))
mocks.GetDoFunc = func(*http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: 200,
Body: r,
}, nil
}
domains, err := FindDomains(token, query)
if err != nil {
t.Error(err)
}
expected := []MarketDomain{
{
Name: "testing.com",
Status: "taken",
Price: 45,
},
{
Name: "testing.net",
Status: "available",
Price: 30,
},
{
Name: "testing.rocks",
Status: "in progress",
Price: 15,
},
{
Name: "testing.express",
Status: "failed",
Price: 75,
},
}
assert.Equal(t, domains, expected)
}
func TestFindDomainsError(t *testing.T) {
token := "test-token"
query := "testing"
Client = &mocks.MockClient{}
testData := `{
"jsonrpc": "2.0",
"error": {
"code": 0,
"message": "Testing error"
}
}`
r := ioutil.NopCloser(bytes.NewReader([]byte(testData)))
mocks.GetDoFunc = func(*http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: 200,
Body: r,
}, nil
}
domains, err := FindDomains(token, query)
assert.Nil(t, domains)
assert.Error(t, err)
}