Skip to content

Commit

Permalink
Implement registrar methods for Domain Prices API (#103)
Browse files Browse the repository at this point in the history
This commit introduces a new client method for our Domain Prices API:
- Registrar.GetDomainPrices - Retrieves the prices for a domain.

It also includes if the domain is premium or not in the response.

Co-authored-by: Santiago Traversa <[email protected]>
Co-authored-by: Simone Carletti <[email protected]>
  • Loading branch information
3 people authored Apr 20, 2021
1 parent 27c8ab7 commit 2ae4fe6
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#### master

- NEW: Added Registrar.GetDomainPrices() to retrieve whether a domain is premium and the prices to register, transfer, and renew. (dnsimple/dnsimple-go#103)

Incompatible changes:

- CHANGED: Domain.ExpiresOn has been replaced by Domain.ExpiresAt. (dnsimple/dnsimple-go#98)
Expand Down
39 changes: 35 additions & 4 deletions dnsimple/registrar.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,38 @@ func (s *RegistrarService) GetDomainPremiumPrice(ctx context.Context, accountID
return priceResponse, nil
}

// DomainRegistration represents the result of a domain renewal call.
// DomainPrice represents the result of a domain prices call.
type DomainPrice struct {
Domain string `json:"domain"`
Premium bool `json:"premium"`
RegistrationPrice float64 `json:"registration_price"`
RenewalPrice float64 `json:"renewal_price"`
TransferPrice float64 `json:"transfer_price"`
}

// DomainPriceResponse represents a response from an API method that returns a DomainPrice struct.
type DomainPriceResponse struct {
Response
Data *DomainPrice `json:"data"`
}

// GetDomainPrices get prices for a domain.
//
// See https://developer.dnsimple.com/v2/registrar/#getDomainPrices
func (s *RegistrarService) GetDomainPrices(ctx context.Context, accountID string, domainName string) (*DomainPriceResponse, error) {
path := versioned(fmt.Sprintf("/%v/registrar/domains/%v/prices", accountID, domainName))
pricesResponse := &DomainPriceResponse{}

resp, err := s.client.get(ctx, path, pricesResponse)
if err != nil {
return nil, err
}

pricesResponse.HTTPResponse = resp
return pricesResponse, nil
}

// DomainRegistration represents the result of a domain registration call.
type DomainRegistration struct {
ID int64 `json:"id"`
DomainID int64 `json:"domain_id"`
Expand Down Expand Up @@ -130,7 +161,7 @@ type RegisterDomainInput struct {

// RegisterDomain registers a domain name.
//
// See https://developer.dnsimple.com/v2/registrar/#register
// See https://developer.dnsimple.com/v2/registrar/#registerDomain
func (s *RegistrarService) RegisterDomain(ctx context.Context, accountID string, domainName string, input *RegisterDomainInput) (*DomainRegistrationResponse, error) {
path := versioned(fmt.Sprintf("/%v/registrar/domains/%v/registrations", accountID, domainName))
registrationResponse := &DomainRegistrationResponse{}
Expand All @@ -146,7 +177,7 @@ func (s *RegistrarService) RegisterDomain(ctx context.Context, accountID string,
return registrationResponse, nil
}

// DomainTransfer represents the result of a domain renewal call.
// DomainTransfer represents the result of a domain transfer call.
type DomainTransfer struct {
ID int64 `json:"id"`
DomainID int64 `json:"domain_id"`
Expand Down Expand Up @@ -284,7 +315,7 @@ type RenewDomainInput struct {

// RenewDomain renews a domain name.
//
// See https://developer.dnsimple.com/v2/registrar/#register
// See https://developer.dnsimple.com/v2/registrar/#renewDomain
func (s *RegistrarService) RenewDomain(ctx context.Context, accountID string, domainName string, input *RenewDomainInput) (*DomainRenewalResponse, error) {
path := versioned(fmt.Sprintf("/%v/registrar/domains/%v/renewals", accountID, domainName))
renewalResponse := &DomainRenewalResponse{}
Expand Down
41 changes: 41 additions & 0 deletions dnsimple/registrar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,47 @@ func TestRegistrarService_GetDomainPremiumPrice_WithOptions(t *testing.T) {
}
}

func TestRegistrarService_GetDomainPrices(t *testing.T) {
setupMockServer()
defer teardownMockServer()

mux.HandleFunc("/v2/1010/registrar/domains/bingo.pizza/prices", func(w http.ResponseWriter, r *http.Request) {
httpResponse := httpResponseFixture(t, "/api/getDomainPrices/success.http")

testMethod(t, r, "GET")
testHeaders(t, r)

w.WriteHeader(httpResponse.StatusCode)
_, _ = io.Copy(w, httpResponse.Body)
})

checkResponse, err := client.Registrar.GetDomainPrices(context.Background(), "1010", "bingo.pizza")
if err != nil {
t.Fatalf("Registrar.GetDomainPrices() returned error: %v", err)
}

check := checkResponse.Data
if want, got := "bingo.pizza", check.Domain; want != got {
t.Fatalf("Registrar.GetDomainPrices() returned Domain expected to be `%v`, got `%v`", want, got)
}

if want, got := true, check.Premium; want != got {
t.Fatalf("Registrar.GetDomainPrices() returned Premium expected to be `%v`, got `%v`", want, got)
}

if want, got := float64(20.0), check.RegistrationPrice; want != got {
t.Fatalf("Registrar.GetDomainPrices() returned RegistrationPrice expected to be `%v`, got `%v`", want, got)
}

if want, got := float64(20.0), check.RenewalPrice; want != got {
t.Fatalf("Registrar.GetDomainPrices() returned RenewalPrice expected to be `%v`, got `%v`", want, got)
}

if want, got := float64(20.0), check.TransferPrice; want != got {
t.Fatalf("Registrar.GetDomainPrices() returned TransferPrice expected to be `%v`, got `%v`", want, got)
}
}

func TestRegistrarService_RegisterDomain(t *testing.T) {
setupMockServer()
defer teardownMockServer()
Expand Down

0 comments on commit 2ae4fe6

Please sign in to comment.