Skip to content

Commit

Permalink
Added GetDomainTracking()
Browse files Browse the repository at this point in the history
  • Loading branch information
thrawn01 committed Jan 4, 2019
1 parent e57a28e commit 9d46f33
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 29 deletions.
34 changes: 30 additions & 4 deletions domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ type DNSRecord struct {
Value string
}

type SingleDomainResponse struct {
type domainResponse struct {
Domain Domain `json:"domain"`
ReceivingDNSRecords []DNSRecord `json:"receiving_dns_records"`
SendingDNSRecords []DNSRecord `json:"sending_dns_records"`
Connection *DomainConnection `json:"connection,omitempty"`
Tracking *DomainTracking `json:"tracking,omitempty"`
}

type DomainConnectionResponse struct {
type domainConnectionResponse struct {
Connection DomainConnection `json:"connection"`
}

Expand All @@ -64,6 +65,22 @@ type DomainConnection struct {
SkipVerification bool `json:"skip_verification"`
}

type DomainTracking struct {
Click TrackingStatus `json:"click"`
Open TrackingStatus `json:"open"`
Unsubscribe TrackingStatus `json:"unsubscribe"`
}

type TrackingStatus struct {
Active bool `json:"active"`
HTMLFooter string `json:"html_footer"`
TextFooter string `json:"text_footer"`
}

type domainTrackingResponse struct {
Tracking DomainTracking `json:"tracking"`
}

// GetCreatedAt returns the time the domain was created as a normal Go time.Time type.
func (d Domain) GetCreatedAt() (t time.Time, err error) {
t, err = parseMailgunTime(d.CreatedAt)
Expand Down Expand Up @@ -100,7 +117,7 @@ func (mg *MailgunImpl) GetSingleDomain(domain string) (Domain, []DNSRecord, []DN
r := newHTTPRequest(generatePublicApiUrl(mg, domainsEndpoint) + "/" + domain)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
var envelope SingleDomainResponse
var envelope domainResponse
err := getResponseFromJSON(r, &envelope)
return envelope.Domain, envelope.ReceivingDNSRecords, envelope.SendingDNSRecords, err
}
Expand Down Expand Up @@ -129,7 +146,7 @@ func (mg *MailgunImpl) GetDomainConnection(domain string) (DomainConnection, err
r := newHTTPRequest(generatePublicApiUrl(mg, domainsEndpoint) + "/" + domain + "/connection")
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
var resp DomainConnectionResponse
var resp domainConnectionResponse
err := getResponseFromJSON(r, &resp)
return resp.Connection, err
}
Expand All @@ -155,6 +172,15 @@ func (mg *MailgunImpl) DeleteDomain(name string) error {
return err
}

func (mg *MailgunImpl) GetDomainTracking(domain string) (DomainTracking, error) {
r := newHTTPRequest(generatePublicApiUrl(mg, domainsEndpoint) + "/" + domain + "/tracking")
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
var resp domainTrackingResponse
err := getResponseFromJSON(r, &resp)
return resp.Tracking, err
}

func boolToString(b bool) string {
if b {
return "true"
Expand Down
25 changes: 22 additions & 3 deletions domains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
"github.com/mailgun/mailgun-go"
)

const (
sampleDomain = "samples.mailgun.org"
)

func TestGetDomains(t *testing.T) {
mg, err := mailgun.NewMailgunFromEnv()
mg.SetAPIBase(server.URL())
Expand Down Expand Up @@ -74,18 +78,33 @@ func TestDomainConnection(t *testing.T) {
mg.SetAPIBase(server.URL())
ensure.Nil(t, err)

info, err := mg.GetDomainConnection("samples.mailgun.org")
info, err := mg.GetDomainConnection(sampleDomain)
ensure.Nil(t, err)

ensure.DeepEqual(t, info.RequireTLS, true)
ensure.DeepEqual(t, info.SkipVerification, true)

info.RequireTLS = false
err = mg.UpdateDomainConnection("samples.mailgun.org", info)
err = mg.UpdateDomainConnection(sampleDomain, info)
ensure.Nil(t, err)

info, err = mg.GetDomainConnection("samples.mailgun.org")
info, err = mg.GetDomainConnection(sampleDomain)
ensure.Nil(t, err)
ensure.DeepEqual(t, info.RequireTLS, false)
ensure.DeepEqual(t, info.SkipVerification, true)
}

func TestDomainTracking(t *testing.T) {
mg, err := mailgun.NewMailgunFromEnv()
mg.SetAPIBase(server.URL())
ensure.Nil(t, err)

info, err := mg.GetDomainTracking(sampleDomain)
ensure.Nil(t, err)

ensure.DeepEqual(t, info.Unsubscribe.Active, false)
ensure.DeepEqual(t, len(info.Unsubscribe.HTMLFooter) != 0, true)
ensure.DeepEqual(t, len(info.Unsubscribe.TextFooter) != 0, true)
ensure.DeepEqual(t, info.Click.Active, true)
ensure.DeepEqual(t, info.Open.Active, true)
}
8 changes: 4 additions & 4 deletions ips.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package mailgun

type IPAddressList struct {
type ipAddressListResponse struct {
TotalCount int `json:"total_count"`
Items []string `json:"items"`
}
Expand All @@ -11,7 +11,7 @@ type IPAddress struct {
Dedicated bool `json:"dedicated"`
}

type OK struct {
type okResp struct {
Message string `json:"message"`
}

Expand All @@ -24,7 +24,7 @@ func (mg *MailgunImpl) ListIPS(dedicated bool) ([]IPAddress, error) {
}
r.setBasicAuth(basicAuthUser, mg.APIKey())

var resp IPAddressList
var resp ipAddressListResponse
if err := getResponseFromJSON(r, &resp); err != nil {
return nil, err
}
Expand All @@ -51,7 +51,7 @@ func (mg *MailgunImpl) ListDomainIPS() ([]IPAddress, error) {
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())

var resp IPAddressList
var resp ipAddressListResponse
if err := getResponseFromJSON(r, &resp); err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions mailgun.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ type Mailgun interface {
GetExportLink(id string) (string, error)
UpdateDomainConnection(domain string, settings DomainConnection) error
GetDomainConnection(domain string) (DomainConnection, error)
GetDomainTracking(domain string) (DomainTracking, error)
}

// MailgunImpl bundles data needed by a large number of methods in order to interact with the Mailgun API.
Expand Down
2 changes: 1 addition & 1 deletion mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type MockServer struct {
srv *httptest.Server

domainIPS []string
domainList []SingleDomainResponse
domainList []domainResponse
exportList []Export
}

Expand Down
45 changes: 35 additions & 10 deletions mock_domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func (ms *MockServer) addDomainRoutes(r chi.Router) {

ms.domainList = append(ms.domainList, SingleDomainResponse{
ms.domainList = append(ms.domainList, domainResponse{
Domain: Domain{
CreatedAt: "Wed, 10 Jul 2013 19:26:52 GMT",
Name: "samples.mailgun.org",
Expand All @@ -24,6 +24,15 @@ func (ms *MockServer) addDomainRoutes(r chi.Router) {
RequireTLS: true,
SkipVerification: true,
},
Tracking: &DomainTracking{
Click: TrackingStatus{Active: true},
Open: TrackingStatus{Active: true},
Unsubscribe: TrackingStatus{
Active: false,
HTMLFooter: "\n<br>\n<p><a href=\"%unsubscribe_url%\">unsubscribe</a></p>\n",
TextFooter: "\n\nTo unsubscribe click: <%unsubscribe_url%>\n\n",
},
},
ReceivingDNSRecords: []DNSRecord{
{
Priority: "10",
Expand Down Expand Up @@ -71,6 +80,8 @@ func (ms *MockServer) addDomainRoutes(r chi.Router) {
//r.Delete("/domains/{domain}/credentials/{login}", ms.deleteCredentials)
r.Get("/domains/{domain}/connection", ms.getConnection)
r.Put("/domains/{domain}/connection", ms.updateConnection)
r.Get("/domains/{domain}/tracking", ms.getTracking)
//r.Put("/domains/{domain}/tracking/{type}", ms.updateTracking)
}

func (ms *MockServer) listDomains(w http.ResponseWriter, _ *http.Request) {
Expand All @@ -94,12 +105,12 @@ func (ms *MockServer) getDomain(w http.ResponseWriter, r *http.Request) {
}
}
w.WriteHeader(http.StatusNotFound)
toJSON(w, OK{Message: "domain not found"})
toJSON(w, okResp{Message: "domain not found"})
}

func (ms *MockServer) createDomain(w http.ResponseWriter, r *http.Request) {
now := time.Now()
ms.domainList = append(ms.domainList, SingleDomainResponse{
ms.domainList = append(ms.domainList, domainResponse{
Domain: Domain{
CreatedAt: formatMailgunTime(&now),
Name: r.FormValue("name"),
Expand All @@ -110,7 +121,7 @@ func (ms *MockServer) createDomain(w http.ResponseWriter, r *http.Request) {
State: "active",
},
})
toJSON(w, OK{Message: "Domain has been created"})
toJSON(w, okResp{Message: "Domain has been created"})
}

func (ms *MockServer) deleteDomain(w http.ResponseWriter, r *http.Request) {
Expand All @@ -123,27 +134,27 @@ func (ms *MockServer) deleteDomain(w http.ResponseWriter, r *http.Request) {
}

if len(result) != len(ms.domainList) {
toJSON(w, OK{Message: "success"})
toJSON(w, okResp{Message: "success"})
ms.domainList = result
return
}

w.WriteHeader(http.StatusNotFound)
toJSON(w, OK{Message: "domain not found"})
toJSON(w, okResp{Message: "domain not found"})
}

func (ms *MockServer) getConnection(w http.ResponseWriter, r *http.Request) {
for _, d := range ms.domainList {
if d.Domain.Name == chi.URLParam(r, "domain") {
resp := DomainConnectionResponse{
resp := domainConnectionResponse{
Connection: *d.Connection,
}
toJSON(w, resp)
return
}
}
w.WriteHeader(http.StatusNotFound)
toJSON(w, OK{Message: "domain not found"})
toJSON(w, okResp{Message: "domain not found"})
}

func (ms *MockServer) updateConnection(w http.ResponseWriter, r *http.Request) {
Expand All @@ -153,12 +164,26 @@ func (ms *MockServer) updateConnection(w http.ResponseWriter, r *http.Request) {
RequireTLS: stringToBool(r.FormValue("require_tls")),
SkipVerification: stringToBool(r.FormValue("skip_verification")),
}
toJSON(w, OK{Message: "Domain connection settings have been updated, may take 10 minutes to fully propagate"})
toJSON(w, okResp{Message: "Domain connection settings have been updated, may take 10 minutes to fully propagate"})
return
}
}
w.WriteHeader(http.StatusNotFound)
toJSON(w, okResp{Message: "domain not found"})
}

func (ms *MockServer) getTracking(w http.ResponseWriter, r *http.Request) {
for _, d := range ms.domainList {
if d.Domain.Name == chi.URLParam(r, "domain") {
resp := domainTrackingResponse{
Tracking: *d.Tracking,
}
toJSON(w, resp)
return
}
}
w.WriteHeader(http.StatusNotFound)
toJSON(w, OK{Message: "domain not found"})
toJSON(w, okResp{Message: "domain not found"})
}

func stringToBool(b string) bool {
Expand Down
4 changes: 2 additions & 2 deletions mock_exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (ms *MockServer) postExports(w http.ResponseWriter, r *http.Request) {
}

ms.exportList = append(ms.exportList, e)
toJSON(w, OK{Message: "success"})
toJSON(w, okResp{Message: "success"})
}

func (ms *MockServer) listExports(w http.ResponseWriter, _ *http.Request) {
Expand All @@ -39,7 +39,7 @@ func (ms *MockServer) getExport(w http.ResponseWriter, r *http.Request) {
}
}
w.WriteHeader(http.StatusNotFound)
toJSON(w, OK{Message: "export not found"})
toJSON(w, okResp{Message: "export not found"})
}

func (ms *MockServer) getExportLink(w http.ResponseWriter, r *http.Request) {
Expand Down
10 changes: 5 additions & 5 deletions mock_ips.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (ms *MockServer) addIPRoutes(r chi.Router) {
}

func (ms *MockServer) listIPS(w http.ResponseWriter, _ *http.Request) {
toJSON(w, IPAddressList{
toJSON(w, ipAddressListResponse{
TotalCount: 2,
Items: []string{"172.0.0.1", "192.168.1.1"},
})
Expand All @@ -33,15 +33,15 @@ func (ms *MockServer) getIPAddress(w http.ResponseWriter, r *http.Request) {
}

func (ms *MockServer) listDomainIPS(w http.ResponseWriter, _ *http.Request) {
toJSON(w, IPAddressList{
toJSON(w, ipAddressListResponse{
TotalCount: 2,
Items: ms.domainIPS,
})
}

func (ms *MockServer) postDomainIPS(w http.ResponseWriter, r *http.Request) {
ms.domainIPS = append(ms.domainIPS, r.FormValue("ip"))
toJSON(w, OK{Message: "success"})
toJSON(w, okResp{Message: "success"})
}

func (ms *MockServer) deleteDomainIPS(w http.ResponseWriter, r *http.Request) {
Expand All @@ -54,12 +54,12 @@ func (ms *MockServer) deleteDomainIPS(w http.ResponseWriter, r *http.Request) {
}

if len(result) != len(ms.domainIPS) {
toJSON(w, OK{Message: "success"})
toJSON(w, okResp{Message: "success"})
ms.domainIPS = result
return
}

// Not the actual error returned by the mailgun API
w.WriteHeader(http.StatusNotFound)
toJSON(w, OK{Message: "ip not found"})
toJSON(w, okResp{Message: "ip not found"})
}

0 comments on commit 9d46f33

Please sign in to comment.