Skip to content

Commit 120e7c8

Browse files
adding metadata and tag serialization
1 parent 41344e4 commit 120e7c8

File tree

3 files changed

+161
-1
lines changed

3 files changed

+161
-1
lines changed

socketlabs/injectionapi/core/injectionrequestfactory.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from ..core.serialization.mergedatajson import MergeDataJson
66
from ..core.serialization.mergefieldjson import MergeFieldJson
77
from ..core.serialization.messagejson import MessageJson
8+
from ..core.serialization.metadatajson import MetadataJson
89
from ..message.basicmessage import BasicMessage
910
from ..message.bulkmessage import BulkMessage
1011
from ..message.bulkrecipient import BulkRecipient
@@ -56,6 +57,22 @@ def populate_custom_headers(custom_headers: list):
5657
return custom_header_json
5758

5859

60+
def populate_metadata(metadata: list):
61+
"""
62+
Converts a List of Metadata objects to a List of MetadataJson objects.
63+
:param metadata: list of Metadata to convert
64+
:type metadata: list
65+
:return the converted list of MetadataJson
66+
:rtype list
67+
"""
68+
if metadata is None:
69+
return None
70+
metadata_json = []
71+
for item in metadata:
72+
metadata_json.append(MetadataJson(item.name, item.value))
73+
return metadata_json
74+
75+
5976
def populate_attachments(attachments: list):
6077
"""
6178
Converts a list of Attachment objects to a List of AttachmentJson objects.
@@ -149,12 +166,17 @@ def generate_base_message(message: MessageBase):
149166
message_json.from_email_address = email_address_to_address_json(message.from_email_address)
150167
message_json.custom_headers = populate_custom_headers(message.custom_headers)
151168
message_json.attachments = populate_attachments(message.attachments)
169+
message_json.metadata = populate_metadata(message.custom_headers)
170+
message_json.tags = message.tags
171+
152172
if message.api_template is not None:
153173
message_json.api_template = str(message.api_template)
154174

155175
if message.reply_to_email_address:
156176
message_json.reply_to_email_address = email_address_to_address_json(message.reply_to_email_address)
157177

178+
print (message_json)
179+
158180
return message_json
159181

160182

socketlabs/injectionapi/core/serialization/messagejson.py

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from .attachmentjson import AttachmentJson
33
from .customheaderjson import CustomHeaderJson
44
from .mergedatajson import MergeDataJson
5+
from .metadatajson import MetadataJson
56

67

78
class MessageJson(object):
@@ -27,6 +28,8 @@ def __init__(self):
2728
self._cc_email_address = []
2829
self._bcc_email_address = []
2930
self._merge_data = None
31+
self._metadata = []
32+
self._tags = []
3033

3134
@property
3235
def to_email_address(self):
@@ -165,7 +168,7 @@ def amp_body(self, val: str):
165168
:type val: str
166169
"""
167170
self._amp_body = val
168-
171+
169172
@property
170173
def api_template(self):
171174
"""
@@ -355,6 +358,69 @@ def merge_data(self, val: MergeDataJson):
355358
"""
356359
self._merge_data = val
357360

361+
362+
@property
363+
def metadata(self):
364+
"""
365+
Get the list of MetadataJson.
366+
:return list of MetadataJson
367+
:rtype list
368+
"""
369+
return self._metadata
370+
371+
@metadata.setter
372+
def metadata(self, val: list):
373+
"""
374+
Set the list of MetadataJson.
375+
:param val: list of MetadataJson
376+
:type val: list
377+
"""
378+
self._metadata = []
379+
if val is not None:
380+
for item in val:
381+
if isinstance(item, MetadataJson):
382+
self._metadata.append(item)
383+
384+
def add_metadata(self, name: str, val: str):
385+
"""
386+
Add a MetadataJson to the metadata list
387+
:param name: the name
388+
:type val: str
389+
:param val: the value
390+
:type val: str
391+
"""
392+
self._metadata.append(MetadataJson(name, val))
393+
394+
@property
395+
def tags(self):
396+
"""
397+
Get the list of tags.
398+
:return list of tags
399+
:rtype list
400+
"""
401+
return self._tags
402+
403+
@tags.setter
404+
def tags(self, val: list):
405+
"""
406+
Set the list of tags.
407+
:param val: list of tags
408+
:type val: list
409+
"""
410+
self._tags = []
411+
if val is not None:
412+
for item in val:
413+
if isinstance(item, (dict, list)):
414+
self._tags.append(item)
415+
416+
def add_tags(self, val: str):
417+
"""
418+
Add a string to the tags list
419+
:param val: the value
420+
:type val: str
421+
"""
422+
self._tags.append(val)
423+
358424
def to_json(self):
359425
"""
360426
build json dict for MessageJson
@@ -426,4 +492,13 @@ def to_json(self):
426492
if self.merge_data:
427493
json["mergeData"] = self.merge_data.to_json()
428494

495+
if len(self.metadata) > 0:
496+
e = []
497+
for i in self.metadata:
498+
e.append(i.to_json())
499+
json["meta"] = e
500+
501+
if len(self.tags) > 0:
502+
json["tags"] = self.tags
503+
429504
return json
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
class MetadataJson(object):
2+
"""
3+
Represents a metadata as a name and value pair.
4+
To be serialized into JSON string before sending to the Injection Api.
5+
"""
6+
7+
def __init__(self, name: str = None, val: str = None):
8+
"""
9+
Initializes a new instance of the MetadataJson class
10+
:param name: the name of the metadata
11+
:type name: str
12+
:param val: the value of the metadata
13+
:type val: str
14+
"""
15+
self._name = name
16+
self._value = val
17+
18+
@property
19+
def name(self):
20+
"""
21+
The name of the metadata.
22+
:return the name
23+
:rtype str
24+
"""
25+
return str(self._name)
26+
27+
@name.setter
28+
def name(self, val: str):
29+
"""
30+
Set the name of the metadata.
31+
:param val: the name
32+
:type val: str
33+
"""
34+
self._name = val
35+
36+
@property
37+
def value(self):
38+
"""
39+
The value of the metadata.
40+
:return the value
41+
:rtype str
42+
"""
43+
return str(self._value)
44+
45+
@value.setter
46+
def value(self, val: str):
47+
"""
48+
Set value of the metadata.
49+
:param val: the value
50+
:type val: str
51+
"""
52+
self._value = val
53+
54+
def to_json(self):
55+
"""
56+
build json dict for MetadataJson
57+
:return the json dictionary
58+
:rtype dict
59+
"""
60+
return {
61+
"name": self._name,
62+
"value": self._value
63+
}

0 commit comments

Comments
 (0)