diff --git a/CHANGELOG.md b/CHANGELOG.md index cb72c2c..1ac676c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/dnsimple/registrar.go b/dnsimple/registrar.go index a3a68d6..16f1a62 100644 --- a/dnsimple/registrar.go +++ b/dnsimple/registrar.go @@ -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"` @@ -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{} @@ -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"` @@ -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{} diff --git a/dnsimple/registrar_test.go b/dnsimple/registrar_test.go index a53bac3..e9f5188 100644 --- a/dnsimple/registrar_test.go +++ b/dnsimple/registrar_test.go @@ -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()