Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarified how to set EU region #198

Merged
merged 2 commits into from
Jul 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ 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.6.1-rc.1] - 2019-06-26
## [3.6.1-rc.3] - 2019-07-16
### Added
* APIBaseEU and APIBaseUS to help customers change regions
* Documented how to change regions in the README

## [3.6.1-rc.2] - 2019-07-01
### Changes
* Fix the JSON response for `GetMember()`
* Typo in format string in max number of tags error
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,13 @@ The official mailgun documentation includes examples using this library. Go
[here](https://documentation.mailgun.com/en/latest/api_reference.html#api-reference)
and click on the "Go" button at the top of the page.

### EU Region
European customers will need to change the default API Base to access your domains

```go
mg := mailgun.NewMailgun("your-domain.com", "private-api-key")
mg.SetAPIBase(mailgun.APIBaseEU)
```
## Installation

If you are using [golang modules](https://github.com/golang/go/wiki/Modules) make sure you
Expand Down
8 changes: 8 additions & 0 deletions httphelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ import (
"net/url"
"os"
"path"
"regexp"
"strings"
)

var validURL = regexp.MustCompile(`^/v[2-4].*`)

type httpRequest struct {
URL string
Parameters map[string][]string
Expand Down Expand Up @@ -291,6 +294,11 @@ func (r *httpRequest) generateUrlWithParameters() (string, error) {
if err != nil {
return "", err
}

if !validURL.MatchString(url.Path) {
return "", errors.New(`BaseAPI must end with a /v2, /v3 or /v4; setBaseAPI("https://host/v3")`)
}

q := url.Query()
if r.Parameters != nil && len(r.Parameters) > 0 {
for name, values := range r.Parameters {
Expand Down
10 changes: 10 additions & 0 deletions mailgun.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ var Debug = false
const (
// Base Url the library uses to contact mailgun. Use SetAPIBase() to override
APIBase = "https://api.mailgun.net/v3"
APIBaseUS = APIBase
APIBaseEU = "https://api.eu.mailgun.net/v3"
messagesEndpoint = "messages"
mimeMessagesEndpoint = "messages.mime"
bouncesEndpoint = "bounces"
Expand Down Expand Up @@ -299,6 +301,14 @@ func (mg *MailgunImpl) SetClient(c *http.Client) {
}

// SetAPIBase updates the API Base URL for this client.
// // For EU Customers
// mg.SetAPIBase(mailgun.APIBaseEU)
//
// // For US Customers
// mg.SetAPIBase(mailgun.APIBaseUS)
//
// // Set a custom base API
// mg.SetAPIBase("https://localhost/v3")
func (mg *MailgunImpl) SetAPIBase(address string) {
mg.apiBase = address
}
Expand Down
16 changes: 14 additions & 2 deletions mailgun_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package mailgun
package mailgun_test

import (
"context"
"net/http"
"testing"

"github.com/facebookgo/ensure"
"github.com/mailgun/mailgun-go/v3"
)

const domain = "valid-mailgun-domain"
const apiKey = "valid-mailgun-api-key"

func TestMailgun(t *testing.T) {
m := NewMailgun(domain, apiKey)
m := mailgun.NewMailgun(domain, apiKey)

ensure.DeepEqual(t, m.Domain(), domain)
ensure.DeepEqual(t, m.APIKey(), apiKey)
Expand All @@ -21,3 +23,13 @@ func TestMailgun(t *testing.T) {
m.SetClient(client)
ensure.DeepEqual(t, client, m.Client())
}

func TestInvalidBaseAPI(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase("https://localhost")

ctx := context.Background()
_, err := mg.GetDomain(ctx, "unknown.domain")
ensure.NotNil(t, err)
ensure.DeepEqual(t, err.Error(), `BaseAPI must end with a /v2, /v3 or /v4; setBaseAPI("https://host/v3")`)
}
32 changes: 16 additions & 16 deletions messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func TestSendMGOffline(t *testing.T) {
)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
ensure.DeepEqual(t, req.Method, http.MethodPost)
ensure.DeepEqual(t, req.URL.Path, fmt.Sprintf("/%s/messages", exampleDomain))
ensure.DeepEqual(t, req.URL.Path, fmt.Sprintf("/v3/%s/messages", exampleDomain))
ensure.DeepEqual(t, req.FormValue("from"), fromUser)
ensure.DeepEqual(t, req.FormValue("subject"), exampleSubject)
ensure.DeepEqual(t, req.FormValue("text"), exampleText)
Expand All @@ -231,7 +231,7 @@ func TestSendMGOffline(t *testing.T) {
defer srv.Close()

mg := NewMailgun(exampleDomain, exampleAPIKey)
mg.SetAPIBase(srv.URL)
mg.SetAPIBase(srv.URL + "/v3")
ctx := context.Background()

m := mg.NewMessage(fromUser, exampleSubject, exampleText, toUser)
Expand All @@ -253,7 +253,7 @@ func TestSendMGSeparateDomain(t *testing.T) {
)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
ensure.DeepEqual(t, req.Method, http.MethodPost)
ensure.DeepEqual(t, req.URL.Path, fmt.Sprintf("/%s/messages", signingDomain))
ensure.DeepEqual(t, req.URL.Path, fmt.Sprintf("/v3/%s/messages", signingDomain))
ensure.DeepEqual(t, req.FormValue("from"), fromUser)
ensure.DeepEqual(t, req.FormValue("subject"), exampleSubject)
ensure.DeepEqual(t, req.FormValue("text"), exampleText)
Expand All @@ -264,7 +264,7 @@ func TestSendMGSeparateDomain(t *testing.T) {
defer srv.Close()

mg := NewMailgun(exampleDomain, exampleAPIKey)
mg.SetAPIBase(srv.URL)
mg.SetAPIBase(srv.URL + "/v3")

ctx := context.Background()
m := mg.NewMessage(fromUser, exampleSubject, exampleText, toUser)
Expand Down Expand Up @@ -295,7 +295,7 @@ func TestSendMGMessageVariables(t *testing.T) {
)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
ensure.DeepEqual(t, req.Method, http.MethodPost)
ensure.DeepEqual(t, req.URL.Path, fmt.Sprintf("/%s/messages", exampleDomain))
ensure.DeepEqual(t, req.URL.Path, fmt.Sprintf("/v3/%s/messages", exampleDomain))

ensure.DeepEqual(t, req.FormValue("from"), fromUser)
ensure.DeepEqual(t, req.FormValue("subject"), exampleSubject)
Expand All @@ -310,7 +310,7 @@ func TestSendMGMessageVariables(t *testing.T) {
defer srv.Close()

mg := NewMailgun(exampleDomain, exampleAPIKey)
mg.SetAPIBase(srv.URL)
mg.SetAPIBase(srv.URL + "/v3")

m := mg.NewMessage(fromUser, exampleSubject, exampleText, toUser)
m.AddVariable(exampleStrVarKey, exampleStrVarVal)
Expand Down Expand Up @@ -368,7 +368,7 @@ func TestSendEOFError(t *testing.T) {
defer srv.Close()

mg := NewMailgun(exampleDomain, exampleAPIKey)
mg.SetAPIBase(srv.URL)
mg.SetAPIBase(srv.URL + "/v3")

m := mg.NewMessage(fromUser, exampleSubject, exampleText, toUser)
_, _, err := mg.Send(context.Background(), m)
Expand All @@ -386,13 +386,13 @@ func TestHasRecipient(t *testing.T) {

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
ensure.DeepEqual(t, req.Method, http.MethodPost)
ensure.DeepEqual(t, req.URL.Path, fmt.Sprintf("/%s/messages", exampleDomain))
ensure.DeepEqual(t, req.URL.Path, fmt.Sprintf("/v3/%s/messages", exampleDomain))
fmt.Fprint(w, `{"message":"Queued, Thank you", "id":"<[email protected]>"}`)
}))
defer srv.Close()

mg := NewMailgun(exampleDomain, exampleAPIKey)
mg.SetAPIBase(srv.URL)
mg.SetAPIBase(srv.URL + "/v3")

// No recipient
m := mg.NewMessage(fromUser, exampleSubject, exampleText)
Expand Down Expand Up @@ -423,7 +423,7 @@ func TestResendStored(t *testing.T) {
)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
ensure.DeepEqual(t, req.Method, http.MethodPost)
ensure.DeepEqual(t, req.URL.Path, "/some-url")
ensure.DeepEqual(t, req.URL.Path, "/v3/some-url")
ensure.DeepEqual(t, req.FormValue("to"), toUser)

rsp := fmt.Sprintf(`{"message":"%s", "id":"%s"}`, exampleMessage, exampleID)
Expand All @@ -432,13 +432,13 @@ func TestResendStored(t *testing.T) {
defer srv.Close()

mg := NewMailgun(exampleDomain, exampleAPIKey)
mg.SetAPIBase(srv.URL)
mg.SetAPIBase(srv.URL + "/v3")

msg, id, err := mg.ReSend(context.Background(), srv.URL+"/some-url")
msg, id, err := mg.ReSend(context.Background(), srv.URL+"/v3/some-url")
ensure.NotNil(t, err)
ensure.DeepEqual(t, err.Error(), "must provide at least one recipient")

msg, id, err = mg.ReSend(context.Background(), srv.URL+"/some-url", toUser)
msg, id, err = mg.ReSend(context.Background(), srv.URL+"/v3/some-url", toUser)
ensure.Nil(t, err)
ensure.DeepEqual(t, msg, exampleMessage)
ensure.DeepEqual(t, id, exampleID)
Expand All @@ -454,7 +454,7 @@ func TestSendTLSOptions(t *testing.T) {
)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
ensure.DeepEqual(t, req.Method, http.MethodPost)
ensure.DeepEqual(t, req.URL.Path, fmt.Sprintf("/%s/messages", exampleDomain))
ensure.DeepEqual(t, req.URL.Path, fmt.Sprintf("/v3/%s/messages", exampleDomain))
ensure.DeepEqual(t, req.FormValue("from"), fromUser)
ensure.DeepEqual(t, req.FormValue("subject"), exampleSubject)
ensure.DeepEqual(t, req.FormValue("text"), exampleText)
Expand All @@ -467,7 +467,7 @@ func TestSendTLSOptions(t *testing.T) {
defer srv.Close()

mg := NewMailgun(exampleDomain, exampleAPIKey)
mg.SetAPIBase(srv.URL)
mg.SetAPIBase(srv.URL + "/v3")
ctx := context.Background()

m := mg.NewMessage(fromUser, exampleSubject, exampleText, toUser)
Expand Down Expand Up @@ -497,7 +497,7 @@ func TestSendTemplate(t *testing.T) {
defer srv.Close()

mg := NewMailgun(exampleDomain, exampleAPIKey)
mg.SetAPIBase(srv.URL)
mg.SetAPIBase(srv.URL + "/v3")
ctx := context.Background()

m := mg.NewMessage(fromUser, exampleSubject, "", toUser)
Expand Down