Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Updated template API to reflect latest changes
  • Loading branch information
thrawn01 committed Feb 7, 2019
1 parent f068dc3 commit d4457c9
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 65 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG
Expand Up @@ -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
Expand Down
29 changes: 14 additions & 15 deletions template.go
Expand Up @@ -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"`
}

Expand Down Expand Up @@ -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")
Expand All @@ -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()
Expand All @@ -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)
Expand Down
19 changes: 9 additions & 10 deletions template_test.go
Expand Up @@ -2,6 +2,7 @@ package mailgun_test

import (
"context"
"strings"
"testing"
"time"

Expand All @@ -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
}
}
Expand All @@ -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"
)
Expand All @@ -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 {
Expand Down
53 changes: 28 additions & 25 deletions template_versions.go
Expand Up @@ -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))
}
Expand All @@ -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())

Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
Expand Down
34 changes: 19 additions & 15 deletions template_versions_test.go
Expand Up @@ -2,6 +2,7 @@ package mailgun_test

import (
"context"
"fmt"
"testing"

"github.com/facebookgo/ensure"
Expand All @@ -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
}
}
Expand All @@ -36,59 +38,61 @@ 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,
Engine: mailgun.TemplateEngineGo,
}

// 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))
}

0 comments on commit d4457c9

Please sign in to comment.