Skip to content

Commit

Permalink
Moved password into create domain options
Browse files Browse the repository at this point in the history
  • Loading branch information
thrawn01 committed Apr 22, 2019
1 parent 785654a commit 850df88
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 11 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +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.3] - 2019-03-28
## [3.4.0] - 2019-04-23
### Added
* `Message.SetTemplate()` to allow sending with the body of a template.
* Added `Message.SetTemplate()` to allow sending with the body of a template.
### Changes
* Changed signature of `CreateDomain()` moved password into `CreateDomainOptions`

## [3.3.2] - 2019-03-28
### Changes
Expand Down
7 changes: 5 additions & 2 deletions domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ func (mg *MailgunImpl) VerifyDomain(ctx context.Context, domain string) (string,

// Optional parameters when creating a domain
type CreateDomainOptions struct {
Password string
SpamAction SpamAction
Wildcard bool
ForceDKIMAuthority bool
Expand All @@ -267,14 +268,13 @@ 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) (DomainResponse, error) {
func (mg *MailgunImpl) CreateDomain(ctx context.Context, name string, opts *CreateDomainOptions) (DomainResponse, error) {
r := newHTTPRequest(generatePublicApiUrl(mg, domainsEndpoint))
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())

payload := newUrlEncodedPayload()
payload.addValue("name", name)
payload.addValue("smtp_password", password)

if opts != nil {
if opts.SpamAction != "" {
Expand All @@ -292,6 +292,9 @@ func (mg *MailgunImpl) CreateDomain(ctx context.Context, name string, password s
if len(opts.IPS) != 0 {
payload.addValue("ips", strings.Join(opts.IPS, ","))
}
if len(opts.Password) != 0 {
payload.addValue("smtp_password", opts.Password)
}
}
var resp DomainResponse
err := postResponseFromJSON(ctx, r, payload, &resp)
Expand Down
4 changes: 2 additions & 2 deletions domains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ func TestAddDeleteDomain(t *testing.T) {
ctx := context.Background()

// First, we need to add the domain.
_, err := mg.CreateDomain(ctx, "mx.mailgun.test", "supersecret",
&mailgun.CreateDomainOptions{SpamAction: mailgun.SpamActionTag})
_, err := mg.CreateDomain(ctx, "mx.mailgun.test",
&mailgun.CreateDomainOptions{SpamAction: mailgun.SpamActionTag, Password: "supersecret"})
ensure.Nil(t, err)

// Next, we delete it.
Expand Down
24 changes: 22 additions & 2 deletions examples/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ func AddDomain(domain, apiKey string) (mailgun.DomainResponse, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()

return mg.CreateDomain(ctx, "example.com", "super_secret", &mailgun.CreateDomainOptions{
return mg.CreateDomain(ctx, "example.com", &mailgun.CreateDomainOptions{
Password: "super_secret",
SpamAction: mailgun.SpamActionTag,
Wildcard: false,
})
Expand Down Expand Up @@ -142,7 +143,8 @@ func CreateDomain(domain, apiKey string) (mailgun.DomainResponse, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()

return mg.CreateDomain(ctx, "example.com", "super_secret", &mailgun.CreateDomainOptions{
return mg.CreateDomain(ctx, "example.com", &mailgun.CreateDomainOptions{
Password: "super_secret",
SpamAction: mailgun.SpamActionTag,
Wildcard: false,
})
Expand Down Expand Up @@ -981,6 +983,24 @@ func GetTemplate(domain, apiKey string) (mailgun.Template, error) {
return mg.GetTemplate(ctx, "my-template")
}

func ListActiveTemplates(domain, apiKey string) ([]mailgun.Template, error) {
mg := mailgun.NewMailgun(domain, apiKey)
it := mg.ListTemplates(&mailgun.ListTemplateOptions{Active: true})

ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()

var page, result []mailgun.Template
for it.Next(ctx, &page) {
result = append(result, page...)
}

if it.Err() != nil {
return nil, it.Err()
}
return result, nil
}

func ListTemplates(domain, apiKey string) ([]mailgun.Template, error) {
mg := mailgun.NewMailgun(domain, apiKey)
it := mg.ListTemplates(nil)
Expand Down
4 changes: 2 additions & 2 deletions mailgun.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ type Mailgun interface {

ListDomains(opts *ListOptions) *DomainsIterator
GetDomain(ctx context.Context, domain string) (DomainResponse, error)
CreateDomain(ctx context.Context, name string, pass string, opts *CreateDomainOptions) (DomainResponse, error)
CreateDomain(ctx context.Context, name string, opts *CreateDomainOptions) (DomainResponse, error)
DeleteDomain(ctx context.Context, name string) error
VerifyDomain(ctx context.Context, name string) (string, error)
UpdateDomainConnection(ctx context.Context, domain string, dc DomainConnection) error
Expand Down Expand Up @@ -219,7 +219,7 @@ type Mailgun interface {
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
ListTemplates(opts *ListTemplateOptions) *TemplatesIterator

AddTemplateVersion(ctx context.Context, templateId string, version *TemplateVersion) error
GetTemplateVersion(ctx context.Context, templateId, versionId string) (TemplateVersion, error)
Expand Down
10 changes: 9 additions & 1 deletion template.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,23 @@ type TemplatesIterator struct {
err error
}

type ListTemplateOptions struct {
Limit int
Active bool
}

// List all available templates
func (mg *MailgunImpl) ListTemplates(opts *ListOptions) *TemplatesIterator {
func (mg *MailgunImpl) ListTemplates(opts *ListTemplateOptions) *TemplatesIterator {
r := newHTTPRequest(generateApiUrl(mg, templatesEndpoint))
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
if opts != nil {
if opts.Limit != 0 {
r.addParameter("limit", strconv.Itoa(opts.Limit))
}
if opts.Active {
r.addParameter("active", "yes")
}
}
url, err := r.generateUrlWithParameters()
return &TemplatesIterator{
Expand Down

0 comments on commit 850df88

Please sign in to comment.