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

Add a struct and function to extract route forward payload #269

Merged
merged 2 commits into from
Nov 3, 2021
Merged
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
65 changes: 65 additions & 0 deletions routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package mailgun

import (
"context"
"encoding/json"
"net/url"
"strconv"
"time"
)

// A Route structure contains information on a configured or to-be-configured route.
Expand All @@ -28,6 +31,68 @@ type Route struct {
Id string `json:"id,omitempty"`
}

// ForwardedMessage represents the payload the server will get on match
// You can use ExtractForwardRoute() to extract PostForm into the struct, or you can use only the struct and parse the form manually
type ForwardedMessage struct {
BodyPlain string // body-plain
From string // from
MessageHeaders map[string]string // message-headers
Recipient string // recipient
Sender string // sender
Signature string // signature
StrippedHTML string // stripped-html
StrippedText string // stripped-text
Subject string // subject
Timestamp time.Time // timestamp
Token string // token
}

// ExtractForwardedMessage extracts the forward route payload values from a parsed PostForm
// Example usage:
// func Handler(w http.ResponseWriter, r *http.Request) {
// err := r.ParseForm()
// if err != nil {
// log.Fatal(err)
// }
// forwardRoute := mailgun.ExtractForwardedMessage(r.PostForm)
// fmt.Printf("Forwarded message: %#v", forwardRoute)
//}
func ExtractForwardedMessage(formValues url.Values) ForwardedMessage {
forwardedMessage := ForwardedMessage{}
forwardedMessage.BodyPlain = formValues.Get("body-plain")
forwardedMessage.From = formValues.Get("from")
forwardedMessage.Recipient = formValues.Get("recipient")
forwardedMessage.Sender = formValues.Get("sender")
forwardedMessage.Signature = formValues.Get("signature")
forwardedMessage.StrippedHTML = formValues.Get("stripped-html")
forwardedMessage.StrippedText = formValues.Get("stripped-text")
forwardedMessage.Subject = formValues.Get("subject")
forwardedMessage.Token = formValues.Get("token")

timestampStr := formValues.Get("timestamp")
timeInt, err := strconv.Atoi(timestampStr)
if err != nil {
timeInt = 0
}
forwardedMessage.Timestamp = time.Unix(int64(timeInt), 0)

headersStr := formValues.Get("message-headers")
headersParsed := make([][]string, 0)
messageHeaders := make(map[string]string)
err = json.Unmarshal([]byte(headersStr), &headersParsed)
if err == nil {
for _, header := range headersParsed {
if len(header) < 2 {
continue
}
messageHeaders[header[0]] = header[1]
}
}
forwardedMessage.MessageHeaders = messageHeaders

return forwardedMessage
}

type routesListResponse struct {
// is -1 if Next() or First() have not been called
TotalCount int `json:"total_count"`
Expand Down