Skip to content

Commit

Permalink
Added support for Templates API
Browse files Browse the repository at this point in the history
  • Loading branch information
thrawn01 committed Jan 12, 2019
1 parent f4ff6d0 commit c62a48b
Show file tree
Hide file tree
Showing 12 changed files with 722 additions and 27 deletions.
1 change: 1 addition & 0 deletions cmd/mailgun/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func main() {
parser.AddCommand("tag", Tag)
parser.AddCommand("events", ListEvents)
parser.AddCommand("mailing-lists", MailingLists)
parser.AddCommand("templates", Templates)

// Parser and set global options
opts := parser.ParseOrExit(nil)
Expand Down
5 changes: 0 additions & 5 deletions cmd/mailgun/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ func ListTag(parser *args.ArgParser, data interface{}) (int, error) {
parser.SetDesc(desc)
parser.AddOption("--prefix").Alias("-p").Help("list only tags with the given prefix")
parser.AddOption("--limit").Alias("-l").IsInt().Help("Limit the result set")
parser.AddOption("--tag").Alias("-t").Help("The tag that marks piviot point for the --page parameter")
parser.AddOption("--page").Alias("-pg").
Help("The page direction based off the tag parameter; valid choices are (first, last, next, prev)")

opts := parser.ParseSimple(nil)
if opts == nil {
Expand All @@ -71,8 +68,6 @@ func ListTag(parser *args.ArgParser, data interface{}) (int, error) {
it := mg.ListTags(&mailgun.ListTagOptions{
Limit: limit,
Prefix: opts.String("prefix"),
Page: opts.String("page"),
Tag: opts.String("tag"),
})

var ctx = context.Background()
Expand Down
66 changes: 66 additions & 0 deletions cmd/mailgun/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main

import (
"context"
"time"

"github.com/davecgh/go-spew/spew"
"github.com/mailgun/mailgun-go/v3"
"github.com/thrawn01/args"
)

func Templates(parser *args.ArgParser, data interface{}) (int, error) {
mg := data.(mailgun.Mailgun)

desc := args.Dedent(`Manage templates via the mailgun HTTP API
Examples:
list all available templates
$ mailgun templates list`)

parser.SetDesc(desc)

// Commands
parser.AddCommand("list", ListTemplates)

// Run the command chosen by our user
return parser.ParseAndRun(nil, mg)
}

func ListTemplates(parser *args.ArgParser, data interface{}) (int, error) {
mg := data.(mailgun.Mailgun)

desc := args.Dedent(`list templates via the mailgun HTTP API
Examples:
list all available tags
$ mailgun templates list`)

parser.SetDesc(desc)
parser.AddOption("--limit").Alias("-l").IsInt().Help("Limit the page size")

opts := parser.ParseSimple(nil)
if opts == nil {
return 1, nil
}

limit := opts.Int("limit")

// Create the event iterator
it := mg.ListTemplates(&mailgun.ListOptions{
Limit: limit,
})

var page []mailgun.Template
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
for it.Next(ctx, &page) {
for _, event := range page {
spew.Printf("%+v\n", event)
}
}
cancel()
if it.Err() != nil {
return 1, it.Err()
}
return 0, nil
}
13 changes: 13 additions & 0 deletions mailgun.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ const (
webhooksEndpoint = "webhooks"
listsEndpoint = "lists"
basicAuthUser = "api"
templatesEndpoint = "templates"
)

// Mailgun defines the supported subset of the Mailgun API.
Expand Down Expand Up @@ -229,6 +230,18 @@ type Mailgun interface {
CreateExport(ctx context.Context, url string) error

GetTagLimits(ctx context.Context, domain string) (TagLimits, error)

CreateTemplate(ctx context.Context, template *Template) error
GetTemplate(ctx context.Context, id string) (Template, error)
UpdateTemplate(ctx context.Context, template *Template) error
DeleteTemplate(ctx context.Context, id string) error
ListTemplates(opts *ListOptions) *TemplatesIterator

AddTemplateVersion(ctx context.Context, templateId string, version *TemplateVersion) error
GetTemplateVersion(ctx context.Context, templateId, versionId string) (TemplateVersion, error)
UpdateTemplateVersion(ctx context.Context, templateId string, version *TemplateVersion) error
DeleteTemplateVersion(ctx context.Context, templateId, versionId string) error
ListTemplateVersions(templateId string, opts *ListOptions) *TemplateVersionsIterator
}

// MailgunImpl bundles data needed by a large number of methods in order to interact with the Mailgun API.
Expand Down
10 changes: 5 additions & 5 deletions mock_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (ms *MockServer) addRoutes(r chi.Router) {

for i := 0; i < 10; i++ {
ms.routeList = append(ms.routeList, Route{
ID: randomString(10, "ID-"),
Id: randomString(10, "ID-"),
Priority: 0,
Description: fmt.Sprintf("Sample Route %d", i),
Actions: []string{
Expand Down Expand Up @@ -65,7 +65,7 @@ func (ms *MockServer) listRoutes(w http.ResponseWriter, r *http.Request) {

func (ms *MockServer) getRoute(w http.ResponseWriter, r *http.Request) {
for _, item := range ms.routeList {
if item.ID == chi.URLParam(r, "id") {
if item.Id == chi.URLParam(r, "id") {
toJSON(w, routeResponse{Route: item})
return
}
Expand All @@ -83,7 +83,7 @@ func (ms *MockServer) createRoute(w http.ResponseWriter, r *http.Request) {

ms.routeList = append(ms.routeList, Route{
CreatedAt: RFC2822Time(time.Now().UTC()),
ID: randomString(10, "ID-"),
Id: randomString(10, "ID-"),
Priority: stringToInt(r.FormValue("priority")),
Description: r.FormValue("description"),
Expression: r.FormValue("expression"),
Expand All @@ -97,7 +97,7 @@ func (ms *MockServer) createRoute(w http.ResponseWriter, r *http.Request) {

func (ms *MockServer) updateRoute(w http.ResponseWriter, r *http.Request) {
for i, item := range ms.routeList {
if item.ID == chi.URLParam(r, "id") {
if item.Id == chi.URLParam(r, "id") {

if r.FormValue("action") != "" {
ms.routeList[i].Actions = r.Form["action"]
Expand All @@ -122,7 +122,7 @@ func (ms *MockServer) updateRoute(w http.ResponseWriter, r *http.Request) {
func (ms *MockServer) deleteRoute(w http.ResponseWriter, r *http.Request) {
result := ms.routeList[:0]
for _, item := range ms.routeList {
if item.ID == chi.URLParam(r, "id") {
if item.Id == chi.URLParam(r, "id") {
continue
}
result = append(result, item)
Expand Down
2 changes: 1 addition & 1 deletion routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Route struct {
// The CreatedAt field provides a time-stamp for when the route came into existence.
CreatedAt RFC2822Time `json:"created_at,omitempty"`
// ID field provides a unique identifier for this route.
ID string `json:"id,omitempty"`
Id string `json:"id,omitempty"`
}

type routesListResponse struct {
Expand Down
12 changes: 6 additions & 6 deletions routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@ func TestRouteCRUD(t *testing.T) {
},
})
ensure.Nil(t, err)
ensure.True(t, newRoute.ID != "")
ensure.True(t, newRoute.Id != "")

defer func() {
ensure.Nil(t, mg.DeleteRoute(ctx, newRoute.ID))
_, err = mg.GetRoute(ctx, newRoute.ID)
ensure.Nil(t, mg.DeleteRoute(ctx, newRoute.Id))
_, err = mg.GetRoute(ctx, newRoute.Id)
ensure.NotNil(t, err)
}()

newCount := countRoutes()
ensure.False(t, newCount <= routeCount)

theRoute, err := mg.GetRoute(ctx, newRoute.ID)
theRoute, err := mg.GetRoute(ctx, newRoute.Id)
ensure.Nil(t, err)
ensure.DeepEqual(t, newRoute, theRoute)

changedRoute, err := mg.UpdateRoute(ctx, newRoute.ID, mailgun.Route{
changedRoute, err := mg.UpdateRoute(ctx, newRoute.Id, mailgun.Route{
Priority: 2,
})
ensure.Nil(t, err)
Expand Down Expand Up @@ -90,7 +90,7 @@ func TestRoutesIterator(t *testing.T) {

ensure.True(t, it.Previous(ctx, &previousPage))
ensure.True(t, len(previousPage) != 0)
ensure.DeepEqual(t, previousPage[0].ID, firstPage[0].ID)
ensure.DeepEqual(t, previousPage[0].Id, firstPage[0].Id)

// First()
ensure.True(t, it.First(ctx, &firstPage))
Expand Down
10 changes: 0 additions & 10 deletions tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ type ListTagOptions struct {
Limit int
// Return only the tags starting with the given prefix
Prefix string
// The page direction based off the 'tag' parameter; valid choices are (first, last, next, prev)
Page string
// The tag that marks piviot point for the 'page' parameter
Tag string
}

// DeleteTag removes all counters for a particular tag, including the tag itself.
Expand Down Expand Up @@ -68,12 +64,6 @@ func (mg *MailgunImpl) ListTags(opts *ListTagOptions) *TagIterator {
if opts.Prefix != "" {
req.addParameter("prefix", opts.Prefix)
}
if opts.Page != "" {
req.addParameter("page", opts.Page)
}
if opts.Tag != "" {
req.addParameter("tag", opts.Tag)
}
}

url, err := req.generateUrlWithParameters()
Expand Down
Loading

0 comments on commit c62a48b

Please sign in to comment.