Skip to content

Commit

Permalink
Merge pull request #288 from thrawn01/thrawn/develop
Browse files Browse the repository at this point in the history
Added CaptureCurlOutput
  • Loading branch information
thrawn01 authored May 27, 2022
2 parents 908899b + bfb8129 commit 6e8672e
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 57 deletions.
7 changes: 6 additions & 1 deletion exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,13 @@ func (mg *MailgunImpl) GetExportLink(ctx context.Context, id string) (string, er
if err != nil {
return "", err
}

if Debug {
fmt.Println(r.curlString(req, nil))
if CaptureCurlOutput {
r.capturedCurlOutput = r.curlString(req, nil)
} else {
fmt.Println(r.curlString(req, nil))
}
}

resp, err := r.Client.Do(req)
Expand Down
25 changes: 17 additions & 8 deletions httphelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ import (
var validURL = regexp.MustCompile(`/v[2-4].*`)

type httpRequest struct {
URL string
Parameters map[string][]string
Headers map[string]string
BasicAuthUser string
BasicAuthPassword string
Client *http.Client
URL string
Parameters map[string][]string
Headers map[string]string
BasicAuthUser string
BasicAuthPassword string
Client *http.Client
capturedCurlOutput string
}

type httpResponse struct {
Expand Down Expand Up @@ -292,7 +293,11 @@ func (r *httpRequest) makeRequest(ctx context.Context, method string, payload pa
}

if Debug {
fmt.Println(r.curlString(req, payload))
if CaptureCurlOutput {
r.capturedCurlOutput = r.curlString(req, payload)
} else {
fmt.Println(r.curlString(req, payload))
}
}

response := httpResponse{}
Expand Down Expand Up @@ -347,7 +352,11 @@ func (r *httpRequest) curlString(req *http.Request, p payload) string {

parts := []string{"curl", "-i", "-X", req.Method, req.URL.String()}
for key, value := range req.Header {
parts = append(parts, fmt.Sprintf("-H \"%s: %s\"", key, value[0]))
if key == "Authorization" {
parts = append(parts, fmt.Sprintf("-H \"%s: %s\"", key, "<redacted>"))
} else {
parts = append(parts, fmt.Sprintf("-H \"%s: %s\"", key, value[0]))
}
}

//parts = append(parts, fmt.Sprintf(" --user '%s:%s'", r.BasicAuthUser, r.BasicAuthPassword))
Expand Down
31 changes: 24 additions & 7 deletions mailgun.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,17 @@ import (
"time"
)

// Set true to write the HTTP requests in curl for to stdout
// Debug set true to write the HTTP requests in curl for to stdout
var Debug = false

// CaptureCurlOutput if true, will capture the curl request of the last Send()
// can be retrieved by calling GetCurlOutput()
var CaptureCurlOutput = false

// RedactCurlAuth will redact the authentication header when CaptureCurlOutput is
// used.
var RedactCurlAuth = false

const (
// Base Url the library uses to contact mailgun. Use SetAPIBase() to override
APIBase = "https://api.mailgun.net/v3"
Expand Down Expand Up @@ -126,6 +134,7 @@ type Mailgun interface {
SetClient(client *http.Client)
SetAPIBase(url string)
AddOverrideHeader(k string, v string)
GetCurlOutput() string

Send(ctx context.Context, m *Message) (string, string, error)
ReSend(ctx context.Context, id string, recipients ...string) (string, string, error)
Expand Down Expand Up @@ -242,12 +251,13 @@ type Mailgun interface {
// MailgunImpl bundles data needed by a large number of methods in order to interact with the Mailgun API.
// Colloquially, we refer to instances of this structure as "clients."
type MailgunImpl struct {
apiBase string
domain string
apiKey string
client *http.Client
baseURL string
overrideHeaders map[string]string
apiBase string
domain string
apiKey string
client *http.Client
baseURL string
overrideHeaders map[string]string
capturedCurlOutput string
}

// NewMailGun creates a new client instance.
Expand Down Expand Up @@ -329,6 +339,13 @@ func (mg *MailgunImpl) AddOverrideHeader(k string, v string) {
mg.overrideHeaders[k] = v
}

// GetCurlOutput will retrieve the output of the last Send() request as a curl command.
// mailgun.CaptureCurlOutput must be set to true
// This is mostly useful for testing the Mailgun API hosted at a different endpoint.
func (mg *MailgunImpl) GetCurlOutput() string {
return mg.capturedCurlOutput
}

// generateApiUrl renders a URL for an API endpoint using the domain and endpoint name.
func generateApiUrl(m Mailgun, endpoint string) string {
return fmt.Sprintf("%s/%s/%s", m.APIBase(), m.Domain(), endpoint)
Expand Down
4 changes: 4 additions & 0 deletions messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,10 @@ func (mg *MailgunImpl) Send(ctx context.Context, message *Message) (mes string,
id = response.Id
}

if r.capturedCurlOutput != "" {
mg.capturedCurlOutput = r.capturedCurlOutput
}

return
}

Expand Down
Loading

0 comments on commit 6e8672e

Please sign in to comment.