Repository: mailgun/mailgun-go
Pull request #155 (2019-01-23 19:38:59)

CreateDomain now returns the JSON response
* Changed signature of CreateDomain() Now returns JSON response
* Changed signature of GetDomain() Now returns a single DomainResponse

In response to #154 


Patch:
diff --git a/CHANGELOG b/CHANGELOG
index d347781..f9a4401 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
 The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
 and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
 
+## [3.3.0] - 2019-01-23
+### Changes
+* Changed signature of CreateDomain() Now returns JSON response
+* Changed signature of GetDomain() Now returns a single DomainResponse
+
 ## [3.2.0] - 2019-01-21
 ### Changes
 * Deprecated mg.VerifyWebhookRequest()
diff --git a/domains.go b/domains.go
index b17f9c4..3923028 100644
--- a/domains.go
+++ b/domains.go
@@ -39,7 +39,7 @@ type DNSRecord struct {
 	Value      string
 }
 
-type domainResponse struct {
+type DomainResponse struct {
 	Domain              Domain      `json:"domain"`
 	ReceivingDNSRecords []DNSRecord `json:"receiving_dns_records"`
 	SendingDNSRecords   []DNSRecord `json:"sending_dns_records"`
@@ -228,13 +228,13 @@ func (ri *DomainsIterator) fetch(ctx context.Context, skip, limit int) error {
 }
 
 // Retrieve detailed information about the named domain.
-func (mg *MailgunImpl) GetDomain(ctx context.Context, domain string) (Domain, []DNSRecord, []DNSRecord, error) {
+func (mg *MailgunImpl) GetDomain(ctx context.Context, domain string) (DomainResponse, error) {
 	r := newHTTPRequest(generatePublicApiUrl(mg, domainsEndpoint) + "/" + domain)
 	r.setClient(mg.Client())
 	r.setBasicAuth(basicAuthUser, mg.APIKey())
-	var resp domainResponse
+	var resp DomainResponse
 	err := getResponseFromJSON(ctx, r, &resp)
-	return resp.Domain, resp.ReceivingDNSRecords, resp.SendingDNSRecords, err
+	return resp, err
 }
 
 // Optional parameters when creating a domain
@@ -252,7 +252,7 @@ type CreateDomainOptions struct {
 // The spamAction domain must be one of Delete, Tag, or Disabled.
 // The wildcard parameter instructs Mailgun to treat all subdomains of this domain uniformly if true,
 // and as different domains if false.
-func (mg *MailgunImpl) CreateDomain(ctx context.Context, name string, password string, opts *CreateDomainOptions) error {
+func (mg *MailgunImpl) CreateDomain(ctx context.Context, name string, password string, opts *CreateDomainOptions) (DomainResponse, error) {
 	r := newHTTPRequest(generatePublicApiUrl(mg, domainsEndpoint))
 	r.setClient(mg.Client())
 	r.setBasicAuth(basicAuthUser, mg.APIKey())
@@ -278,8 +278,9 @@ func (mg *MailgunImpl) CreateDomain(ctx context.Context, name string, password s
 			payload.addValue("ips", strings.Join(opts.IPS, ","))
 		}
 	}
-	_, err := makePostRequest(ctx, r, payload)
-	return err
+	var resp DomainResponse
+	err := postResponseFromJSON(ctx, r, payload, &resp)
+	return resp, err
 }
 
 // Returns delivery connection settings for the defined domain
diff --git a/examples/examples.go b/examples/examples.go
index e4deb00..06f1219 100644
--- a/examples/examples.go
+++ b/examples/examples.go
@@ -27,7 +27,7 @@ func CreateComplaint(domain, apiKey string) error {
 	return mg.CreateComplaint(ctx, "bob@example.com")
 }
 
-func AddDomain(domain, apiKey string) error {
+func AddDomain(domain, apiKey string) (mailgun.DomainResponse, error) {
 	mg := mailgun.NewMailgun(domain, apiKey)
 
 	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
@@ -136,7 +136,7 @@ func CreateCredential(domain, apiKey string) error {
 	return mg.CreateCredential(ctx, "alice@example.com", "secret")
 }
 
-func CreateDomain(domain, apiKey string) error {
+func CreateDomain(domain, apiKey string) (mailgun.DomainResponse, error) {
 	mg := mailgun.NewMailgun(domain, apiKey)
 
 	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
@@ -400,7 +400,7 @@ func ListCredentials(domain, apiKey string) ([]mailgun.Credential, error) {
 	return result, nil
 }
 
-func GetDomain(domain, apiKey string) (mailgun.Domain, []mailgun.DNSRecord, []mailgun.DNSRecord, error) {
+func GetDomain(domain, apiKey string) (mailgun.DomainResponse, error) {
 	mg := mailgun.NewMailgun(domain, apiKey)
 
 	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
diff --git a/mailgun.go b/mailgun.go
index 31e8a2e..d746067 100644
--- a/mailgun.go
+++ b/mailgun.go
@@ -125,7 +125,7 @@ type Mailgun interface {
 	SetAPIBase(url string)
 
 	Send(ctx context.Context, m *Message) (string, string, error)
-	ReSend(ctx context.Context, id string, recipients ...string) (string,string, error)
+	ReSend(ctx context.Context, id string, recipients ...string) (string, string, error)
 	NewMessage(from, subject, text string, to ...string) *Message
 	NewMIMEMessage(body io.ReadCloser, to ...string) *Message
 
@@ -140,8 +140,8 @@ type Mailgun interface {
 	ListTags(*ListTagOptions) *TagIterator
 
 	ListDomains(opts *ListOptions) *DomainsIterator
-	GetDomain(ctx context.Context, domain string) (Domain, []DNSRecord, []DNSRecord, error)
-	CreateDomain(ctx context.Context, name string, pass string, opts *CreateDomainOptions) error
+	GetDomain(ctx context.Context, domain string) (DomainResponse, error)
+	CreateDomain(ctx context.Context, name string, pass string, opts *CreateDomainOptions) (DomainResponse, error)
 	DeleteDomain(ctx context.Context, name string) error
 	UpdateDomainConnection(ctx context.Context, domain string, dc DomainConnection) error
 	GetDomainConnection(ctx context.Context, domain string) (DomainConnection, error)


Test patch:
diff --git a/domains_test.go b/domains_test.go
index a61b55f..3353470 100644
--- a/domains_test.go
+++ b/domains_test.go
@@ -41,16 +41,16 @@ func TestGetSingleDomain(t *testing.T) {
 	ensure.True(t, it.Next(ctx, &page))
 	ensure.Nil(t, it.Err())
 
-	dr, rxDnsRecords, txDnsRecords, err := mg.GetDomain(ctx, page[0].Name)
+	dr, err := mg.GetDomain(ctx, page[0].Name)
 	ensure.Nil(t, err)
-	ensure.DeepEqual(t, len(rxDnsRecords) != 0, true)
-	ensure.DeepEqual(t, len(txDnsRecords) != 0, true)
+	ensure.DeepEqual(t, len(dr.ReceivingDNSRecords) != 0, true)
+	ensure.DeepEqual(t, len(dr.SendingDNSRecords) != 0, true)
 
 	t.Logf("TestGetSingleDomain: %#v\n", dr)
-	for _, rxd := range rxDnsRecords {
+	for _, rxd := range dr.ReceivingDNSRecords {
 		t.Logf("TestGetSingleDomains:   %#v\n", rxd)
 	}
-	for _, txd := range txDnsRecords {
+	for _, txd := range dr.SendingDNSRecords {
 		t.Logf("TestGetSingleDomains:   %#v\n", txd)
 	}
 }
@@ -60,7 +60,7 @@ func TestGetSingleDomainNotExist(t *testing.T) {
 	mg.SetAPIBase(server.URL())
 
 	ctx := context.Background()
-	_, _, _, err := mg.GetDomain(ctx, "unknown.domain")
+	_, err := mg.GetDomain(ctx, "unknown.domain")
 	if err == nil {
 		t.Fatal("Did not expect a domain to exist")
 	}
@@ -75,8 +75,10 @@ func TestAddDeleteDomain(t *testing.T) {
 	ctx := context.Background()
 
 	// First, we need to add the domain.
-	ensure.Nil(t, mg.CreateDomain(ctx, "mx.mailgun.test", "supersecret",
-		&mailgun.CreateDomainOptions{SpamAction: mailgun.SpamActionTag}))
+	_, err := mg.CreateDomain(ctx, "mx.mailgun.test", "supersecret",
+		&mailgun.CreateDomainOptions{SpamAction: mailgun.SpamActionTag})
+	ensure.Nil(t, err)
+
 	// Next, we delete it.
 	ensure.Nil(t, mg.DeleteDomain(ctx, "mx.mailgun.test"))
 }