Repository: mailgun/mailgun-go
Pull request #164 (2019-02-07 20:09:19)

Update template api
### Changes
* Updated Template calls to reflect the most recent Template API changes.

### Added
* Added `GetStoredAttachmentForURL()` to the mailgun interface


Patch:
diff --git a/CHANGELOG b/CHANGELOG
index ab4d7ea..1122405 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -4,6 +4,13 @@ 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).
 
+## [Unreleased]
+### Changes
+* Updated Template calls to reflect the most recent Template API changes.
+
+### Added
+* Added `GetStoredAttachmentForURL()`
+
 ## [3.3.0] - 2019-01-28
 ### Changes
 * Changed signature of CreateDomain() Now returns JSON response
diff --git a/mailgun.go b/mailgun.go
index fd0b205..b98eecf 100644
--- a/mailgun.go
+++ b/mailgun.go
@@ -152,6 +152,7 @@ type Mailgun interface {
 	GetStoredMessageRaw(ctx context.Context, id string) (StoredMessageRaw, error)
 	GetStoredMessageForURL(ctx context.Context, url string) (StoredMessage, error)
 	GetStoredMessageRawForURL(ctx context.Context, url string) (StoredMessageRaw, error)
+	GetStoredAttachmentForURL(ctx context.Context, url string) ([]byte, error)
 	DeleteStoredMessage(ctx context.Context, id string) error
 
 	ListCredentials(opts *ListOptions) *CredentialsIterator
diff --git a/template.go b/template.go
index 5a0292d..b6a7da3 100644
--- a/template.go
+++ b/template.go
@@ -16,15 +16,14 @@ const (
 )
 
 type Template struct {
-	Id          string `json:"id"`
-	Name        string `json:"name"`
-	Description string `json:"description"`
-	CreatedAt   RFC2822Time `json:"createdAt"`
-	Version   TemplateVersion `json:"version,omitempty"`
+	Name        string          `json:"name"`
+	Description string          `json:"description"`
+	CreatedAt   RFC2822Time     `json:"createdAt"`
+	Version     TemplateVersion `json:"version,omitempty"`
 }
 
 type templateResp struct {
-	Item    Template `json:"item"`
+	Item    Template `json:"template"`
 	Message string   `json:"message"`
 }
 
@@ -66,9 +65,9 @@ func (mg *MailgunImpl) CreateTemplate(ctx context.Context, template *Template) e
 	return nil
 }
 
-// Get a template given the template id
-func (mg *MailgunImpl) GetTemplate(ctx context.Context, id string) (Template, error) {
-	r := newHTTPRequest(generateApiUrl(mg, templatesEndpoint) + "/" + id)
+// Get a template given the template name
+func (mg *MailgunImpl) GetTemplate(ctx context.Context, name string) (Template, error) {
+	r := newHTTPRequest(generateApiUrl(mg, templatesEndpoint) + "/" + name)
 	r.setClient(mg.Client())
 	r.setBasicAuth(basicAuthUser, mg.APIKey())
 	r.addParameter("active", "yes")
@@ -83,11 +82,11 @@ func (mg *MailgunImpl) GetTemplate(ctx context.Context, id string) (Template, er
 
 // Update the name and description of a template
 func (mg *MailgunImpl) UpdateTemplate(ctx context.Context, template *Template) error {
-	if template.Id == "" {
-		return errors.New("UpdateTemplate() Template.Id cannot be empty")
+	if template.Name == "" {
+		return errors.New("UpdateTemplate() Template.Name cannot be empty")
 	}
 
-	r := newHTTPRequest(generateApiUrl(mg, templatesEndpoint) + "/" + template.Id)
+	r := newHTTPRequest(generateApiUrl(mg, templatesEndpoint) + "/" + template.Name)
 	r.setClient(mg.Client())
 	r.setBasicAuth(basicAuthUser, mg.APIKey())
 	p := newUrlEncodedPayload()
@@ -108,9 +107,9 @@ func (mg *MailgunImpl) UpdateTemplate(ctx context.Context, template *Template) e
 	return nil
 }
 
-// Delete a template given a template id
-func (mg *MailgunImpl) DeleteTemplate(ctx context.Context, id string) error {
-	r := newHTTPRequest(generateApiUrl(mg, templatesEndpoint) + "/" + id)
+// Delete a template given a template name
+func (mg *MailgunImpl) DeleteTemplate(ctx context.Context, name string) error {
+	r := newHTTPRequest(generateApiUrl(mg, templatesEndpoint) + "/" + name)
 	r.setClient(mg.Client())
 	r.setBasicAuth(basicAuthUser, mg.APIKey())
 	_, err := makeDeleteRequest(ctx, r)
diff --git a/template_versions.go b/template_versions.go
index c3ddf38..2ed6eda 100644
--- a/template_versions.go
+++ b/template_versions.go
@@ -6,31 +6,34 @@ import (
 )
 
 type TemplateVersion struct {
-	Id        string         `json:"id"`
+	Tag       string         `json:"tag"`
 	Template  string         `json:"template,omitempty"`
 	Engine    TemplateEngine `json:"engine"`
 	CreatedAt RFC2822Time    `json:"createdAt"`
-	Comment string `json:"comment"`
-	Active  bool   `json:"active"`
+	Comment   string         `json:"comment"`
+	Active    bool           `json:"active"`
 }
 
 type templateVersionListResp struct {
-	Item struct {
+	Template struct {
 		Template
 		Versions []TemplateVersion `json:"versions,omitempty"`
-	} `json:"item"`
+	} `json:"template"`
 	Paging Paging `json:"paging"`
 }
 
 // Add a template version to a template
-func (mg *MailgunImpl) AddTemplateVersion(ctx context.Context, templateId string, version *TemplateVersion) error {
-	r := newHTTPRequest(generateApiUrl(mg, templatesEndpoint) + "/" + templateId + "/versions")
+func (mg *MailgunImpl) AddTemplateVersion(ctx context.Context, templateName string, version *TemplateVersion) error {
+	r := newHTTPRequest(generateApiUrl(mg, templatesEndpoint) + "/" + templateName + "/versions")
 	r.setClient(mg.Client())
 	r.setBasicAuth(basicAuthUser, mg.APIKey())
 
 	payload := newUrlEncodedPayload()
 	payload.addValue("template", version.Template)
 
+	if version.Tag != "" {
+		payload.addValue("tag", string(version.Tag))
+	}
 	if version.Engine != "" {
 		payload.addValue("engine", string(version.Engine))
 	}
@@ -50,8 +53,8 @@ func (mg *MailgunImpl) AddTemplateVersion(ctx context.Context, templateId string
 }
 
 // Get a specific version of a template
-func (mg *MailgunImpl) GetTemplateVersion(ctx context.Context, templateId, versionId string) (TemplateVersion, error) {
-	r := newHTTPRequest(generateApiUrl(mg, templatesEndpoint) + "/" + templateId + "/versions/" + versionId)
+func (mg *MailgunImpl) GetTemplateVersion(ctx context.Context, templateName, tag string) (TemplateVersion, error) {
+	r := newHTTPRequest(generateApiUrl(mg, templatesEndpoint) + "/" + templateName + "/versions/" + tag)
 	r.setClient(mg.Client())
 	r.setBasicAuth(basicAuthUser, mg.APIKey())
 
@@ -64,8 +67,8 @@ func (mg *MailgunImpl) GetTemplateVersion(ctx context.Context, templateId, versi
 }
 
 // Update the comment and mark a version of a template active
-func (mg *MailgunImpl) UpdateTemplateVersion(ctx context.Context, templateId string, version *TemplateVersion) error {
-	r := newHTTPRequest(generateApiUrl(mg, templatesEndpoint) + "/" + templateId + "/versions/" + version.Id)
+func (mg *MailgunImpl) UpdateTemplateVersion(ctx context.Context, templateName string, version *TemplateVersion) error {
+	r := newHTTPRequest(generateApiUrl(mg, templatesEndpoint) + "/" + templateName + "/versions/" + version.Tag)
 	r.setClient(mg.Client())
 	r.setBasicAuth(basicAuthUser, mg.APIKey())
 	p := newUrlEncodedPayload()
@@ -87,8 +90,8 @@ func (mg *MailgunImpl) UpdateTemplateVersion(ctx context.Context, templateId str
 }
 
 // Delete a specific version of a template
-func (mg *MailgunImpl) DeleteTemplateVersion(ctx context.Context, templateId, versionId string) error {
-	r := newHTTPRequest(generateApiUrl(mg, templatesEndpoint) + "/" + templateId + "/versions/" + versionId)
+func (mg *MailgunImpl) DeleteTemplateVersion(ctx context.Context, templateName, tag string) error {
+	r := newHTTPRequest(generateApiUrl(mg, templatesEndpoint) + "/" + templateName + "/versions/" + tag)
 	r.setClient(mg.Client())
 	r.setBasicAuth(basicAuthUser, mg.APIKey())
 	_, err := makeDeleteRequest(ctx, r)
@@ -102,8 +105,8 @@ type TemplateVersionsIterator struct {
 }
 
 // List all the versions of a specific template
-func (mg *MailgunImpl) ListTemplateVersions(templateId string, opts *ListOptions) *TemplateVersionsIterator {
-	r := newHTTPRequest(generateApiUrl(mg, templatesEndpoint) + "/" + templateId + "/versions")
+func (mg *MailgunImpl) ListTemplateVersions(templateName string, opts *ListOptions) *TemplateVersionsIterator {
+	r := newHTTPRequest(generateApiUrl(mg, templatesEndpoint) + "/" + templateName + "/versions")
 	r.setClient(mg.Client())
 	r.setBasicAuth(basicAuthUser, mg.APIKey())
 	if opts != nil {
@@ -135,10 +138,10 @@ func (li *TemplateVersionsIterator) Next(ctx context.Context, items *[]TemplateV
 	if li.err != nil {
 		return false
 	}
-	cpy := make([]TemplateVersion, len(li.Item.Versions))
-	copy(cpy, li.Item.Versions)
+	cpy := make([]TemplateVersion, len(li.Template.Versions))
+	copy(cpy, li.Template.Versions)
 	*items = cpy
-	if len(li.Item.Versions) == 0 {
+	if len(li.Template.Versions) == 0 {
 		return false
 	}
 	return true
@@ -155,8 +158,8 @@ func (li *TemplateVersionsIterator) First(ctx context.Context, items *[]Template
 	if li.err != nil {
 		return false
 	}
-	cpy := make([]TemplateVersion, len(li.Item.Versions))
-	copy(cpy, li.Item.Versions)
+	cpy := make([]TemplateVersion, len(li.Template.Versions))
+	copy(cpy, li.Template.Versions)
 	*items = cpy
 	return true
 }
@@ -173,8 +176,8 @@ func (li *TemplateVersionsIterator) Last(ctx context.Context, items *[]TemplateV
 	if li.err != nil {
 		return false
 	}
-	cpy := make([]TemplateVersion, len(li.Item.Versions))
-	copy(cpy, li.Item.Versions)
+	cpy := make([]TemplateVersion, len(li.Template.Versions))
+	copy(cpy, li.Template.Versions)
 	*items = cpy
 	return true
 }
@@ -193,10 +196,10 @@ func (li *TemplateVersionsIterator) Previous(ctx context.Context, items *[]Templ
 	if li.err != nil {
 		return false
 	}
-	cpy := make([]TemplateVersion, len(li.Item.Versions))
-	copy(cpy, li.Item.Versions)
+	cpy := make([]TemplateVersion, len(li.Template.Versions))
+	copy(cpy, li.Template.Versions)
 	*items = cpy
-	if len(li.Item.Versions) == 0 {
+	if len(li.Template.Versions) == 0 {
 		return false
 	}
 	return true


Test patch:
diff --git a/template_test.go b/template_test.go
index 6e232b3..943f742 100644
--- a/template_test.go
+++ b/template_test.go
@@ -2,6 +2,7 @@ package mailgun_test
 
 import (
 	"context"
+	"strings"
 	"testing"
 	"time"
 
@@ -19,13 +20,13 @@ func TestTemplateCRUD(t *testing.T) {
 	ensure.Nil(t, err)
 	ctx := context.Background()
 
-	findTemplate := func(id string) bool {
+	findTemplate := func(name string) bool {
 		it := mg.ListTemplates(nil)
 
 		var page []mailgun.Template
 		for it.Next(ctx, &page) {
 			for _, template := range page {
-				if template.Id == id {
+				if template.Name == name {
 					return true
 				}
 			}
@@ -35,7 +36,7 @@ func TestTemplateCRUD(t *testing.T) {
 	}
 
 	const (
-		Name        = "Mailgun-Go TestTemplateCRUD"
+		Name        = "Mailgun-Go-TestTemplateCRUD"
 		Description = "Mailgun-Go Test Template Description"
 		UpdatedDesc = "Mailgun-Go Test Updated Description"
 	)
@@ -47,28 +48,26 @@ func TestTemplateCRUD(t *testing.T) {
 
 	// Create a template
 	ensure.Nil(t, mg.CreateTemplate(ctx, &tmpl))
-	ensure.True(t, tmpl.Id != "")
+	ensure.DeepEqual(t, tmpl.Name, strings.ToLower(Name))
 	ensure.DeepEqual(t, tmpl.Description, Description)
-	ensure.DeepEqual(t, tmpl.Name, Name)
 
 	// Wait the template to show up
-	ensure.Nil(t, waitForTemplate(mg, tmpl.Id))
+	ensure.Nil(t, waitForTemplate(mg, tmpl.Name))
 
 	// Ensure the template is in the list
-	ensure.True(t, findTemplate(tmpl.Id))
+	ensure.True(t, findTemplate(tmpl.Name))
 
 	// Update the description
 	tmpl.Description = UpdatedDesc
 	ensure.Nil(t, mg.UpdateTemplate(ctx, &tmpl))
 
 	// Ensure update took
-	updated, err := mg.GetTemplate(ctx, tmpl.Id)
+	updated, err := mg.GetTemplate(ctx, tmpl.Name)
 
-	ensure.DeepEqual(t, updated.Id, tmpl.Id)
 	ensure.DeepEqual(t, updated.Description, UpdatedDesc)
 
 	// Delete the template
-	ensure.Nil(t, mg.DeleteTemplate(ctx, tmpl.Id))
+	ensure.Nil(t, mg.DeleteTemplate(ctx, tmpl.Name))
 }
 
 func waitForTemplate(mg mailgun.Mailgun, id string) error {
diff --git a/template_versions_test.go b/template_versions_test.go
index 07fddfd..8649195 100644
--- a/template_versions_test.go
+++ b/template_versions_test.go
@@ -2,6 +2,7 @@ package mailgun_test
 
 import (
 	"context"
+	"fmt"
 	"testing"
 
 	"github.com/facebookgo/ensure"
@@ -17,13 +18,14 @@ func TestTemplateVersionsCRUD(t *testing.T) {
 	ensure.Nil(t, err)
 	ctx := context.Background()
 
-	findVersion := func(templateId, versionId string) bool {
-		it := mg.ListTemplateVersions(templateId, nil)
+	findVersion := func(templateName, tag string) bool {
+		fmt.Printf("List\n")
+		it := mg.ListTemplateVersions(templateName, nil)
 
 		var page []mailgun.TemplateVersion
 		for it.Next(ctx, &page) {
 			for _, v := range page {
-				if v.Id == versionId {
+				if v.Tag == tag {
 					return true
 				}
 			}
@@ -36,16 +38,18 @@ func TestTemplateVersionsCRUD(t *testing.T) {
 		Comment        = "Mailgun-Go TestTemplateVersionsCRUD"
 		UpdatedComment = "Mailgun-Go Test Version Updated"
 		Template       = "{{.Name}}"
+		Tag            = "v1"
 	)
 
 	tmpl := mailgun.Template{
-		Name: "Mailgun-go TestTemplateVersionsCRUD",
+		Name: "Mailgun-go-TestTemplateVersionsCRUD",
 	}
 
 	// Create a template
 	ensure.Nil(t, mg.CreateTemplate(ctx, &tmpl))
 
 	version := mailgun.TemplateVersion{
+		Tag:      Tag,
 		Comment:  Comment,
 		Template: Template,
 		Active:   true,
@@ -53,42 +57,42 @@ func TestTemplateVersionsCRUD(t *testing.T) {
 	}
 
 	// Add a version version
-	ensure.Nil(t, mg.AddTemplateVersion(ctx, tmpl.Id, &version))
-	ensure.True(t, version.Id != "")
+	ensure.Nil(t, mg.AddTemplateVersion(ctx, tmpl.Name, &version))
+	ensure.DeepEqual(t, version.Tag, Tag)
 	ensure.DeepEqual(t, version.Comment, Comment)
 	ensure.DeepEqual(t, version.Engine, mailgun.TemplateEngineGo)
 
 	// Ensure the version is in the list
-	ensure.True(t, findVersion(tmpl.Id, version.Id))
+	ensure.True(t, findVersion(tmpl.Name, version.Tag))
 
 	// Update the Comment
 	version.Comment = UpdatedComment
-	ensure.Nil(t, mg.UpdateTemplateVersion(ctx, tmpl.Id, &version))
+	ensure.Nil(t, mg.UpdateTemplateVersion(ctx, tmpl.Name, &version))
 
 	// Ensure update took
-	updated, err := mg.GetTemplateVersion(ctx, tmpl.Id, version.Id)
+	updated, err := mg.GetTemplateVersion(ctx, tmpl.Name, version.Tag)
 
-	ensure.DeepEqual(t, version.Id, updated.Id)
 	ensure.DeepEqual(t, updated.Comment, UpdatedComment)
 
 	// Add a new active Version
 	version2 := mailgun.TemplateVersion{
+		Tag:      "v2",
 		Comment:  Comment,
 		Template: Template,
 		Active:   true,
 		Engine:   mailgun.TemplateEngineGo,
 	}
-	ensure.Nil(t, mg.AddTemplateVersion(ctx, tmpl.Id, &version2))
+	ensure.Nil(t, mg.AddTemplateVersion(ctx, tmpl.Name, &version2))
 
 	// Ensure the version is in the list
-	ensure.True(t, findVersion(tmpl.Id, version2.Id))
+	ensure.True(t, findVersion(tmpl.Name, version2.Tag))
 
 	// Delete the first version
-	ensure.Nil(t, mg.DeleteTemplateVersion(ctx, tmpl.Id, version.Id))
+	ensure.Nil(t, mg.DeleteTemplateVersion(ctx, tmpl.Name, version.Tag))
 
 	// Ensure version was deleted
-	ensure.False(t, findVersion(tmpl.Id, version.Id))
+	ensure.False(t, findVersion(tmpl.Name, version.Tag))
 
 	// Delete the template
-	ensure.Nil(t, mg.DeleteTemplate(ctx, tmpl.Id))
+	ensure.Nil(t, mg.DeleteTemplate(ctx, tmpl.Name))
 }