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

DE-1383 Move the API version out of the base URL #377

Merged
merged 12 commits into from
Jan 3, 2025
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func main() {
// Create an instance of the Mailgun Client
mg := mailgun.NewMailgun(privateAPIKey)

//When you have an EU-domain, you must specify the endpoint:
//mg.SetAPIBase("https://api.eu.mailgun.net/v3")
// When you have an EU domain, you must specify the endpoint:
// err := mg.SetAPIBase(mailgun.APIBaseEU)

sender := "[email protected]"
subject := "Fancy subject!"
Expand Down
7 changes: 1 addition & 6 deletions analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package mailgun

import (
"context"
"strings"

"github.com/mailgun/errors"
)
Expand Down Expand Up @@ -35,15 +34,11 @@ type MetricsPagination struct {
//
// https://documentation.mailgun.com/docs/mailgun/api-reference/openapi-final/tag/Metrics/
func (mg *MailgunImpl) ListMetrics(opts MetricsOptions) (*MetricsIterator, error) {
if !strings.HasSuffix(mg.APIBase(), "/v1") {
return nil, errors.New("only v1 API is supported")
}

if opts.Pagination.Limit == 0 {
opts.Pagination.Limit = 10
}

req := newHTTPRequest(generateApiUrl(mg, metricsEndpoint))
req := newHTTPRequest(generateApiUrl(mg, 1, metricsEndpoint))
req.setClient(mg.HTTPClient())
req.setBasicAuth(basicAuthUser, mg.APIKey())

Expand Down
3 changes: 2 additions & 1 deletion analytics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (

func TestListMetrics(t *testing.T) {
mg := mailgun.NewMailgun(testKey)
mg.SetAPIBase(server.URL1())
err := mg.SetAPIBase(server.URL())
require.NoError(t, err)

start, _ := mailgun.NewRFC2822Time("Tue, 24 Sep 2024 00:00:00 +0000")
end, _ := mailgun.NewRFC2822Time("Tue, 24 Oct 2024 00:00:00 +0000")
Expand Down
3 changes: 2 additions & 1 deletion attachments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ func createAttachment(t *testing.T) string {

func TestMultipleAttachments(t *testing.T) {
mg := mailgun.NewMailgun(testKey)
mg.SetAPIBase(server.URL3())
err := mg.SetAPIBase(server.URL())
require.NoError(t, err)

var ctx = context.Background()

Expand Down
12 changes: 6 additions & 6 deletions bounces.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type bouncesListResponse struct {
// and the slice of bounces specified, if successful.
// Note that the length of the slice may be smaller than the total number of bounces.
func (mg *MailgunImpl) ListBounces(domain string, opts *ListOptions) *BouncesIterator {
r := newHTTPRequest(generateApiUrlWithDomain(mg, bouncesEndpoint, domain))
r := newHTTPRequest(generateApiV3UrlWithDomain(mg, bouncesEndpoint, domain))
r.setClient(mg.HTTPClient())
r.setBasicAuth(basicAuthUser, mg.APIKey())
if opts != nil {
Expand Down Expand Up @@ -147,7 +147,7 @@ func (ci *BouncesIterator) fetch(ctx context.Context, url string) error {

// GetBounce retrieves a single bounce record, if any exist, for the given recipient address.
func (mg *MailgunImpl) GetBounce(ctx context.Context, domain, address string) (Bounce, error) {
r := newHTTPRequest(generateApiUrlWithDomain(mg, bouncesEndpoint, domain) + "/" + address)
r := newHTTPRequest(generateApiV3UrlWithDomain(mg, bouncesEndpoint, domain) + "/" + address)
r.setClient(mg.HTTPClient())
r.setBasicAuth(basicAuthUser, mg.APIKey())

Expand All @@ -173,7 +173,7 @@ func (mg *MailgunImpl) GetBounce(ctx context.Context, domain, address string) (B
// Note that both code and error exist as strings, even though
// code will report as a number.
func (mg *MailgunImpl) AddBounce(ctx context.Context, domain, address, code, bounceError string) error {
r := newHTTPRequest(generateApiUrlWithDomain(mg, bouncesEndpoint, domain))
r := newHTTPRequest(generateApiV3UrlWithDomain(mg, bouncesEndpoint, domain))
r.setClient(mg.HTTPClient())
r.setBasicAuth(basicAuthUser, mg.APIKey())

Expand All @@ -191,7 +191,7 @@ func (mg *MailgunImpl) AddBounce(ctx context.Context, domain, address, code, bou

// Add Bounces adds a list of bounces to the bounce list
func (mg *MailgunImpl) AddBounces(ctx context.Context, domain string, bounces []Bounce) error {
r := newHTTPRequest(generateApiUrlWithDomain(mg, bouncesEndpoint, domain))
r := newHTTPRequest(generateApiV3UrlWithDomain(mg, bouncesEndpoint, domain))
r.setClient(mg.HTTPClient())
r.setBasicAuth(basicAuthUser, mg.APIKey())

Expand All @@ -203,7 +203,7 @@ func (mg *MailgunImpl) AddBounces(ctx context.Context, domain string, bounces []

// DeleteBounce removes all bounces associted with the provided e-mail address.
func (mg *MailgunImpl) DeleteBounce(ctx context.Context, domain, address string) error {
r := newHTTPRequest(generateApiUrlWithDomain(mg, bouncesEndpoint, domain) + "/" + address)
r := newHTTPRequest(generateApiV3UrlWithDomain(mg, bouncesEndpoint, domain) + "/" + address)
r.setClient(mg.HTTPClient())
r.setBasicAuth(basicAuthUser, mg.APIKey())
_, err := makeDeleteRequest(ctx, r)
Expand All @@ -212,7 +212,7 @@ func (mg *MailgunImpl) DeleteBounce(ctx context.Context, domain, address string)

// DeleteBounceList removes all bounces in the bounce list
func (mg *MailgunImpl) DeleteBounceList(ctx context.Context, domain string) error {
r := newHTTPRequest(generateApiUrlWithDomain(mg, bouncesEndpoint, domain))
r := newHTTPRequest(generateApiV3UrlWithDomain(mg, bouncesEndpoint, domain))
r.setClient(mg.HTTPClient())
r.setBasicAuth(basicAuthUser, mg.APIKey())
_, err := makeDeleteRequest(ctx, r)
Expand Down
17 changes: 11 additions & 6 deletions bounces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import (

func TestGetBounces(t *testing.T) {
mg := mailgun.NewMailgun(testKey)
mg.SetAPIBase(server.URL3())
err := mg.SetAPIBase(server.URL())
require.NoError(t, err)

ctx := context.Background()
it := mg.ListBounces(testDomain, nil)
Expand All @@ -31,12 +32,13 @@ func TestGetBounces(t *testing.T) {

func TestGetSingleBounce(t *testing.T) {
mg := mailgun.NewMailgun(testKey)
mg.SetAPIBase(server.URL3())
err := mg.SetAPIBase(server.URL())
require.NoError(t, err)

ctx := context.Background()
exampleEmail := fmt.Sprintf("%s@%s", strings.ToLower(randomString(64, "")),
os.Getenv("MG_DOMAIN"))
_, err := mg.GetBounce(ctx, testDomain, exampleEmail)
_, err = mg.GetBounce(ctx, testDomain, exampleEmail)
require.NotNil(t, err)

var ure *mailgun.UnexpectedResponseError
Expand All @@ -46,7 +48,9 @@ func TestGetSingleBounce(t *testing.T) {

func TestAddDelBounces(t *testing.T) {
mg := mailgun.NewMailgun(testKey)
mg.SetAPIBase(server.URL3())
err := mg.SetAPIBase(server.URL())
require.NoError(t, err)

ctx := context.Background()

findBounce := func(address string) bool {
Expand All @@ -71,7 +75,7 @@ func TestAddDelBounces(t *testing.T) {
exampleEmail := fmt.Sprintf("%s@%s", strings.ToLower(randomString(8, "bounce")), domain)

// Add the bounce for our address.
err := mg.AddBounce(ctx, testDomain, exampleEmail, "550", "TestAddDelBounces-generated error")
err = mg.AddBounce(ctx, testDomain, exampleEmail, "550", "TestAddDelBounces-generated error")
require.NoError(t, err)

// Give API some time to refresh cache
Expand Down Expand Up @@ -104,7 +108,8 @@ func TestAddDelBounces(t *testing.T) {

func TestAddDelBounceList(t *testing.T) {
mg := mailgun.NewMailgun(testKey)
mg.SetAPIBase(server.URL3())
err := mg.SetAPIBase(server.URL())
require.NoError(t, err)

ctx := context.Background()

Expand Down
6 changes: 4 additions & 2 deletions credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (

func TestGetCredentials(t *testing.T) {
mg := mailgun.NewMailgun(testKey)
mg.SetAPIBase(server.URL3())
err := mg.SetAPIBase(server.URL())
require.NoError(t, err)

ctx := context.Background()
it := mg.ListCredentials(testDomain, nil)
Expand All @@ -29,7 +30,8 @@ func TestGetCredentials(t *testing.T) {

func TestCreateDeleteCredentials(t *testing.T) {
mg := mailgun.NewMailgun(testKey)
mg.SetAPIBase(server.URL3())
err := mg.SetAPIBase(server.URL())
require.NoError(t, err)

randomPassword := randomString(16, "pw")
randomID := strings.ToLower(randomString(16, "usr"))
Expand Down
32 changes: 16 additions & 16 deletions domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (mg *MailgunImpl) ListDomains(opts *ListOptions) *DomainsIterator {
}
return &DomainsIterator{
mg: mg,
url: generateApiUrl(mg, domainsEndpoint),
url: generateApiUrl(mg, 3, domainsEndpoint),
domainsListResponse: domainsListResponse{TotalCount: -1},
limit: limit,
}
Expand Down Expand Up @@ -233,7 +233,7 @@ func (ri *DomainsIterator) fetch(ctx context.Context, skip, limit int) error {

// GetDomain retrieves detailed information about the named domain.
func (mg *MailgunImpl) GetDomain(ctx context.Context, domain string) (DomainResponse, error) {
r := newHTTPRequest(generateApiUrl(mg, domainsEndpoint) + "/" + domain)
r := newHTTPRequest(generateApiUrl(mg, 3, domainsEndpoint) + "/" + domain)
r.setClient(mg.HTTPClient())
r.setBasicAuth(basicAuthUser, mg.APIKey())
var resp DomainResponse
Expand All @@ -242,7 +242,7 @@ func (mg *MailgunImpl) GetDomain(ctx context.Context, domain string) (DomainResp
}

func (mg *MailgunImpl) VerifyDomain(ctx context.Context, domain string) (string, error) {
r := newHTTPRequest(generateApiUrl(mg, domainsEndpoint) + "/" + domain + "/verify")
r := newHTTPRequest(generateApiUrl(mg, 3, domainsEndpoint) + "/" + domain + "/verify")
r.setClient(mg.HTTPClient())
r.setBasicAuth(basicAuthUser, mg.APIKey())

Expand All @@ -254,7 +254,7 @@ func (mg *MailgunImpl) VerifyDomain(ctx context.Context, domain string) (string,

// VerifyAndReturnDomain verifies & retrieves detailed information about the named domain.
func (mg *MailgunImpl) VerifyAndReturnDomain(ctx context.Context, domain string) (DomainResponse, error) {
r := newHTTPRequest(generateApiUrl(mg, domainsEndpoint) + "/" + domain + "/verify")
r := newHTTPRequest(generateApiUrl(mg, 3, domainsEndpoint) + "/" + domain + "/verify")
r.setClient(mg.HTTPClient())
r.setBasicAuth(basicAuthUser, mg.APIKey())

Expand Down Expand Up @@ -282,7 +282,7 @@ type CreateDomainOptions struct {
// 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, opts *CreateDomainOptions) (DomainResponse, error) {
r := newHTTPRequest(generateApiUrl(mg, domainsEndpoint))
r := newHTTPRequest(generateApiUrl(mg, 3, domainsEndpoint))
r.setClient(mg.HTTPClient())
r.setBasicAuth(basicAuthUser, mg.APIKey())

Expand Down Expand Up @@ -319,17 +319,17 @@ func (mg *MailgunImpl) CreateDomain(ctx context.Context, name string, opts *Crea

// GetDomainConnection returns delivery connection settings for the defined domain
func (mg *MailgunImpl) GetDomainConnection(ctx context.Context, domain string) (DomainConnection, error) {
r := newHTTPRequest(generateApiUrl(mg, domainsEndpoint) + "/" + domain + "/connection")
r := newHTTPRequest(generateApiUrl(mg, 3, domainsEndpoint) + "/" + domain + "/connection")
r.setClient(mg.HTTPClient())
r.setBasicAuth(basicAuthUser, mg.APIKey())
var resp domainConnectionResponse
err := getResponseFromJSON(ctx, r, &resp)
return resp.Connection, err
}

// Updates the specified delivery connection settings for the defined domain
// UpdateDomainConnection updates the specified delivery connection settings for the defined domain
func (mg *MailgunImpl) UpdateDomainConnection(ctx context.Context, domain string, settings DomainConnection) error {
r := newHTTPRequest(generateApiUrl(mg, domainsEndpoint) + "/" + domain + "/connection")
r := newHTTPRequest(generateApiUrl(mg, 3, domainsEndpoint) + "/" + domain + "/connection")
r.setClient(mg.HTTPClient())
r.setBasicAuth(basicAuthUser, mg.APIKey())

Expand All @@ -342,7 +342,7 @@ func (mg *MailgunImpl) UpdateDomainConnection(ctx context.Context, domain string

// DeleteDomain instructs Mailgun to dispose of the named domain name
func (mg *MailgunImpl) DeleteDomain(ctx context.Context, name string) error {
r := newHTTPRequest(generateApiUrl(mg, domainsEndpoint) + "/" + name)
r := newHTTPRequest(generateApiUrl(mg, 3, domainsEndpoint) + "/" + name)
r.setClient(mg.HTTPClient())
r.setBasicAuth(basicAuthUser, mg.APIKey())
_, err := makeDeleteRequest(ctx, r)
Expand All @@ -351,7 +351,7 @@ func (mg *MailgunImpl) DeleteDomain(ctx context.Context, name string) error {

// GetDomainTracking returns tracking settings for a domain
func (mg *MailgunImpl) GetDomainTracking(ctx context.Context, domain string) (DomainTracking, error) {
r := newHTTPRequest(generateApiUrl(mg, domainsEndpoint) + "/" + domain + "/tracking")
r := newHTTPRequest(generateApiUrl(mg, 3, domainsEndpoint) + "/" + domain + "/tracking")
r.setClient(mg.HTTPClient())
r.setBasicAuth(basicAuthUser, mg.APIKey())
var resp domainTrackingResponse
Expand All @@ -360,7 +360,7 @@ func (mg *MailgunImpl) GetDomainTracking(ctx context.Context, domain string) (Do
}

func (mg *MailgunImpl) UpdateClickTracking(ctx context.Context, domain, active string) error {
r := newHTTPRequest(generateApiUrl(mg, domainsEndpoint) + "/" + domain + "/tracking/click")
r := newHTTPRequest(generateApiUrl(mg, 3, domainsEndpoint) + "/" + domain + "/tracking/click")
r.setClient(mg.HTTPClient())
r.setBasicAuth(basicAuthUser, mg.APIKey())

Expand All @@ -371,7 +371,7 @@ func (mg *MailgunImpl) UpdateClickTracking(ctx context.Context, domain, active s
}

func (mg *MailgunImpl) UpdateUnsubscribeTracking(ctx context.Context, domain, active, htmlFooter, textFooter string) error {
r := newHTTPRequest(generateApiUrl(mg, domainsEndpoint) + "/" + domain + "/tracking/unsubscribe")
r := newHTTPRequest(generateApiUrl(mg, 3, domainsEndpoint) + "/" + domain + "/tracking/unsubscribe")
r.setClient(mg.HTTPClient())
r.setBasicAuth(basicAuthUser, mg.APIKey())

Expand All @@ -384,7 +384,7 @@ func (mg *MailgunImpl) UpdateUnsubscribeTracking(ctx context.Context, domain, ac
}

func (mg *MailgunImpl) UpdateOpenTracking(ctx context.Context, domain, active string) error {
r := newHTTPRequest(generateApiUrl(mg, domainsEndpoint) + "/" + domain + "/tracking/open")
r := newHTTPRequest(generateApiUrl(mg, 3, domainsEndpoint) + "/" + domain + "/tracking/open")
r.setClient(mg.HTTPClient())
r.setBasicAuth(basicAuthUser, mg.APIKey())

Expand All @@ -396,7 +396,7 @@ func (mg *MailgunImpl) UpdateOpenTracking(ctx context.Context, domain, active st

// Update the DKIM selector for a domain
func (mg *MailgunImpl) UpdateDomainDkimSelector(ctx context.Context, domain, dkimSelector string) error {
r := newHTTPRequest(generateApiUrl(mg, domainsEndpoint) + "/" + domain + "/dkim_selector")
r := newHTTPRequest(generateApiUrl(mg, 3, domainsEndpoint) + "/" + domain + "/dkim_selector")
r.setClient(mg.HTTPClient())
r.setBasicAuth(basicAuthUser, mg.APIKey())

Expand All @@ -408,7 +408,7 @@ func (mg *MailgunImpl) UpdateDomainDkimSelector(ctx context.Context, domain, dki

// Update the CNAME used for tracking opens and clicks
func (mg *MailgunImpl) UpdateDomainTrackingWebPrefix(ctx context.Context, domain, webPrefix string) error {
r := newHTTPRequest(generateApiUrl(mg, domainsEndpoint) + "/" + domain + "/web_prefix")
r := newHTTPRequest(generateApiUrl(mg, 3, domainsEndpoint) + "/" + domain + "/web_prefix")
r.setClient(mg.HTTPClient())
r.setBasicAuth(basicAuthUser, mg.APIKey())

Expand All @@ -426,7 +426,7 @@ type UpdateDomainOptions struct {
// UpdateDomain updates a domain's attributes.
// Currently only the web_scheme update is supported, spam_action and wildcard are to be added.
func (mg *MailgunImpl) UpdateDomain(ctx context.Context, name string, opts *UpdateDomainOptions) error {
r := newHTTPRequest(generateApiUrl(mg, domainsEndpoint) + "/" + name)
r := newHTTPRequest(generateApiUrl(mg, 3, domainsEndpoint) + "/" + name)
r.setClient(mg.HTTPClient())
r.setBasicAuth(basicAuthUser, mg.APIKey())

Expand Down
Loading
Loading