From cf5fdbb592d8fa3da6d486089455db39d835e1cc Mon Sep 17 00:00:00 2001 From: "kayos@tcp.direct" Date: Fri, 27 Aug 2021 21:37:04 -0700 Subject: [PATCH] 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. --- domains.go | 33 +++++++++++++++++ domains_test.go | 98 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+) diff --git a/domains.go b/domains.go index ba63e37..e1d3421 100644 --- a/domains.go +++ b/domains.go @@ -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 +} diff --git a/domains_test.go b/domains_test.go index b3ba41a..41dfea7 100644 --- a/domains_test.go +++ b/domains_test.go @@ -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) +}