Skip to content

Commit ba00d1d

Browse files
Merge pull request #6 from socketlabs/usr/mike.boshuyzen/update-meta-data-and-tags
Updated go code templates to add meta data and tags.
2 parents 0263472 + af2cd5e commit ba00d1d

File tree

12 files changed

+167
-1
lines changed

12 files changed

+167
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ debug
1313

1414
# Output of the go coverage tool, specifically when used with LiteIDE
1515
*.out
16+
17+
.vs/*

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ For more information about AMP please see [AMP Project](https://amp.dev/document
192192

193193
<a name="version"></a>
194194
# Version
195+
* 1.4.0 - Adding Metadata and Tags
195196
* 1.2.1 - Adding optional retry logic for Http requests. If configured, the request will retry when certain 500 errors occur (500, 502, 503, 504)
196197
* 1.1.1 - Adding request timeout value on the client for Http requests
197198
* 1.1.0 - Adds Amp Html Support

examples/basic/BasicComplex/BasicSendComplex.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ func main() {
5555
//Configure custom message headers
5656
basic.CustomHeaders = append(basic.CustomHeaders, message.NewCustomHeader("MyMessageHeader", "I am a message header"))
5757

58+
//Configure message meta data
59+
basic.Metadata = append(basic.Metadata, message.NewMetadata("MyMetaDatra", "I am meta data"))
60+
61+
//Add message tags
62+
basic.Tags = append(basic.Tags, "I am a Tag")
63+
basic.Tags = append(basic.Tags, "go-Example")
64+
5865
//Send the message
5966
sendResponse, _ := client.SendBasic(&basic)
6067

examples/bulk/BulkComplex/BulkComplex.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ func main() {
4545

4646
bulk.AddCustomHeader("testMessageHeader", "I am a message header")
4747

48+
bulk.AddMetadata("testMetadata", "I am meta data")
49+
50+
//Add message tags
51+
bulk.Tags = append(bulk.Tags, "I am a Tag")
52+
bulk.Tags = append(bulk.Tags, "go-Example")
53+
4854
var htmlBody bytes.Buffer
4955
htmlBody.WriteString("<html>")
5056
htmlBody.WriteString(" <head><title>Complex</title></head>")

injectionapi/core/injectionrequestfactory.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ func (factory InjectionRequestFactory) GenerateBulkRequest(message *message.Bulk
7878
MessageID: message.MessageId,
7979
CharSet: message.CharSet,
8080
APITemplate: message.ApiTemplate,
81+
Tags: message.Tags,
8182
}
8283

8384
//set attachments
@@ -86,6 +87,9 @@ func (factory InjectionRequestFactory) GenerateBulkRequest(message *message.Bulk
8687
//set custom headers
8788
messageJson.CustomHeaders = getCustomHeaders(&message.CustomHeaders)
8889

90+
//set meta data
91+
messageJson.Metadata = getMetadata(&message.Metadata)
92+
8993
//set from address
9094
messageJson.From = serialization.AddressJson{
9195
EmailAddress: message.From.EmailAddress,
@@ -197,6 +201,25 @@ func getCustomHeaders(customHeaders *[]message.CustomHeader) []serialization.Cus
197201
return results
198202
}
199203

204+
// getMetadata converts meta data to json-serialization ready format
205+
func getMetadata(metadata *[]message.Metadata) []serialization.MetadataJson {
206+
207+
if len(*metadata) == 0 {
208+
return nil
209+
}
210+
211+
var results = []serialization.MetadataJson{}
212+
213+
for _, sourceData := range *metadata {
214+
results = append(results, serialization.MetadataJson{
215+
Key: sourceData.Key,
216+
Value: sourceData.Value,
217+
})
218+
}
219+
220+
return results
221+
}
222+
200223
// getBulkMergeFields extracts merge fields from slice of bulk recipients in a json-serialization ready format
201224
func getBulkMergeFields(bulkRecipients *[]message.BulkRecipient) [][]serialization.MergeFieldJson {
202225

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package serialization
2+
3+
// MetadataJson represents a piece of meta data as a key and value pair.
4+
// To be serialized into JSON string before sending to the Injection Api.
5+
type MetadataJson struct {
6+
7+
// Gets or sets the meta data key
8+
Key string `json:"Key"`
9+
10+
// Gets or sets the meta data value
11+
Value string `json:"Value"`
12+
}

injectionapi/core/serialization/messageJson.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,11 @@ type MessageJson struct {
5151

5252
// List of custom message headers added to the message.
5353
CustomHeaders []CustomHeadersJson `json:"CustomHeaders,omitempty"`
54+
55+
// List of custom meta data added to the message.
56+
Metadata []MetadataJson `json:"Metadata,omitempty"`
57+
58+
// List of tags added to the message.
59+
Tags []string `json:"Tags,omitempty"`
60+
5461
}

injectionapi/message/basicmessage.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ type BasicMessage struct {
5252
//(Optional)
5353
CustomHeaders []CustomHeader
5454

55+
//Optional collection of meta data for the message.
56+
//(Optional)
57+
Metadata []Metadata
58+
59+
//Optional collection of tags for the message.
60+
//(Optional)
61+
Tags []string
62+
5563
//Collection of To Recipients for the message.
5664
To []EmailAddress
5765

@@ -104,3 +112,14 @@ func (basic *BasicMessage) AddCustomHeader(name string, value string) {
104112
customHeader := NewCustomHeader(name, value)
105113
basic.CustomHeaders = append(basic.CustomHeaders, customHeader)
106114
}
115+
116+
// AddMetadata adds meta data to the message
117+
func (basic *BasicMessage) AddMetadata(key string, value string) {
118+
metadata := NewMetadata(key, value)
119+
basic.Metadata = append(basic.Metadata, metadata)
120+
}
121+
122+
// AddTag adds a tag to the message
123+
func (basic *BasicMessage) AddTag(value string) {
124+
basic.Tags = append(basic.Tags, value)
125+
}

injectionapi/message/bulkmessage.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ type BulkMessage struct {
5252
// (Optional)
5353
CustomHeaders []CustomHeader
5454

55+
//Optional collection of meta data for the message.
56+
//(Optional)
57+
Metadata []Metadata
58+
59+
//Optional collection of tags for the message.
60+
//(Optional)
61+
Tags []string
62+
5563
//Collection of To Recipients for the message.
5664
To []BulkRecipient
5765

@@ -80,6 +88,17 @@ func (bulk *BulkMessage) AddCustomHeader(name string, value string) {
8088
bulk.CustomHeaders = append(bulk.CustomHeaders, customHeader)
8189
}
8290

91+
// AddCustomHeader adds meta data to the message
92+
func (bulk *BulkMessage) AddMetadata(key string, value string) {
93+
metadata := NewMetadata(key, value)
94+
bulk.Metadata = append(bulk.Metadata, metadata)
95+
}
96+
97+
// AddCustomHeader adds a tag to the message
98+
func (bulk *BulkMessage) AddTag(value string) {
99+
bulk.Tags = append(bulk.Tags, value)
100+
}
101+
83102
// AddGlobalMergeData adds global merge data
84103
func (bulk *BulkMessage) AddGlobalMergeData(key string, value string) {
85104
if bulk.Global == nil || len(bulk.Global) == 0 {

injectionapi/message/metadata.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package message
2+
3+
// CustomHeader info
4+
type Metadata struct {
5+
Key string
6+
Value string
7+
}
8+
9+
// NewMetadata Factory Constructor
10+
func NewMetadata(key string, value string) Metadata {
11+
var c Metadata
12+
c.Key = key
13+
c.Value = value
14+
return c
15+
}
16+
17+
// IsValid Determines if the Metadata is valid
18+
func (c Metadata) IsValid() bool {
19+
20+
if len(c.Key) == 0 {
21+
return false
22+
}
23+
24+
if len(c.Value) == 0 {
25+
return false
26+
}
27+
28+
return true
29+
}

0 commit comments

Comments
 (0)