Skip to content

Commit 8281587

Browse files
Merge pull request #6 from socketlabs/feature/metadata-tags
Feature/metadata tags
2 parents 10e3a91 + 836cf7d commit 8281587

15 files changed

+564
-72
lines changed

README.md

+33-32
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[![SocketLabs](https://static.socketlabs.com/logos/logo-dark-326x64.png)](https://www.socketlabs.com/developers)
1+
[![SocketLabs](https://static.socketlabs.com/logos/logo-dark-326x64.png)](https://www.socketlabs.com/developers)
22
# [![Twitter Follow](https://img.shields.io/twitter/follow/socketlabs.svg?style=social&label=Follow)](https://twitter.com/socketlabs) [![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](./LICENSE) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/socketlabs/socketlabs-csharp/blob/master/CONTRIBUTING.md)
33
<!--
44
[![GitHub contributors](https://img.shields.io/github/contributors/socketlabs/socketlabs-python.svg)](https://github.com/socketlabs/socketlabs-python/graphs/contributors)
@@ -28,12 +28,12 @@ pip install socketlabs-injectionapi
2828
```
2929
### From a local archive using pip
3030

31-
You can just download the package and install from a local archive file.
31+
You can just download the package and install from a local archive file.
3232

33-
> [socketlabs_injectionapi-1.2.1.tar.gz](https://github.com/socketlabs/socketlabs-python/releases/download/1.2python setup.py sdist bdist_wheel.1/socketlabs_injectionapi-1.2.1.tar.gz)
33+
> [socketlabs_injectionapi-1.4.0.tar.gz](https://github.com/socketlabs/socketlabs-python/releases/download/1.4.0/socketlabs_injectionapi-1.4.0.tar.gz)
3434
3535
```
36-
pip install <path>/socketlabs_injectionapi-1.2.1.tar.gz
36+
pip install <path>/socketlabs_injectionapi-1.4.0.tar.gz
3737
```
3838

3939
### From git using pip
@@ -45,21 +45,21 @@ For more information please see the [Installing Packages](https://packaging.pyth
4545
<a name="getting-started" id="getting-started"></a>
4646
# Getting Started
4747
## Obtaining your API Key and SocketLabs ServerId number
48-
In order to get started, you'll need to enable the Injection API feature in the [SocketLabs Control Panel](https://cp.socketlabs.com).
49-
Once logged in, navigate to your SocketLabs server's dashboard (if you only have one server on your account you'll be taken here immediately after logging in).
50-
Make note of your 4 or 5 digit ServerId number, as you'll need this along with
51-
your API key in order to use the Injection API.
48+
In order to get started, you'll need to enable the Injection API feature in the [SocketLabs Control Panel](https://cp.socketlabs.com).
49+
Once logged in, navigate to your SocketLabs server's dashboard (if you only have one server on your account you'll be taken here immediately after logging in).
50+
Make note of your 4 or 5 digit ServerId number, as you'll need this along with
51+
your API key in order to use the Injection API.
5252

53-
To enable the Injection API, click on the "For Developers" dropdown on the top-level navigation, then choose the "Configure HTTP Injection API" option.
53+
To enable the Injection API, click on the "For Developers" dropdown on the top-level navigation, then choose the "Configure HTTP Injection API" option.
5454
Once here, you can enable the feature by choosing the "Enabled" option in the
55-
dropdown. Enabling the feature will also generate your API key, which you'll
56-
need (along with your ServerId) to start using the API. Be sure to click the
55+
dropdown. Enabling the feature will also generate your API key, which you'll
56+
need (along with your ServerId) to start using the API. Be sure to click the
5757
"Update" button to save your changes once you are finished.
5858

5959

6060
## Basic Message
61-
A basic message is an email message like you'd send from a personal email client such as Outlook.
62-
A basic message can have many recipients, including multiple To addresses, CC addresses, and even BCC addresses.
61+
A basic message is an email message like you'd send from a personal email client such as Outlook.
62+
A basic message can have many recipients, including multiple To addresses, CC addresses, and even BCC addresses.
6363
You can also send a file attachment in a basic message.
6464

6565
```python
@@ -78,7 +78,7 @@ message.plain_text_body = "This is the Plain Text Body of my message.";
7878

7979
message.from_email_address = EmailAddress("[email protected]")
8080

81-
# A basic message supports up to 50 recipients
81+
# A basic message supports up to 50 recipients
8282
# and supports several different ways to add recipients
8383

8484
# Add a To address by passing the email address
@@ -98,9 +98,9 @@ response = client.send(message)
9898
```
9999

100100
## Bulk Message
101-
A bulk message usually contains a single recipient per message
102-
and is generally used to send the same content to many recipients,
103-
optionally customizing the message via the use of MergeData.
101+
A bulk message usually contains a single recipient per message
102+
and is generally used to send the same content to many recipients,
103+
optionally customizing the message via the use of MergeData.
104104
For more information about using Merge data, please see the [Injection API documentation](https://www.socketlabs.com/docs/inject/).
105105
```python
106106
from socketlabs.injectionapi import SocketLabsClient
@@ -131,28 +131,28 @@ response = client.send(message)
131131

132132
<a name="managing-api-keys" id="managing-api-keys"></a>
133133
## Managing API Keys
134-
For ease of demonstration, many of our examples include the ServerId (SOCKETLABS_SERVER_ID) and API key
135-
(SOCKETLABS_INJECTION_API_KEY) directly in our code sample. Generally it is not considered a good practice to store
136-
sensitive information like this directly in your code. Depending on your project type, we recommend either storing your
137-
credentials using Environment Variables. For more information please see:
134+
For ease of demonstration, many of our examples include the ServerId (SOCKETLABS_SERVER_ID) and API key
135+
(SOCKETLABS_INJECTION_API_KEY) directly in our code sample. Generally it is not considered a good practice to store
136+
sensitive information like this directly in your code. Depending on your project type, we recommend either storing your
137+
credentials using Environment Variables. For more information please see:
138138
[Using Environment Variables](https://docs.microsoft.com/en-us/dotnet/api/system.environment.getenvironmentvariable)
139139

140140

141141
<a name="examples-and-use-cases" id="examples-and-use-cases"></a>
142142
# Examples and Use Cases
143-
In order to demonstrate the many possible use cases for the SDK, we've provided
144-
an assortment of code examples. These examples demonstrate many different
145-
features available to the Injection API and SDK, including using templates
146-
created in the [SocketLabs Email Designer](https://www.socketlabs.com/blog/introducing-new-email-designer/), custom email headers, sending
147-
attachments, sending content that is stored in an HTML file, advanced bulk
143+
In order to demonstrate the many possible use cases for the SDK, we've provided
144+
an assortment of code examples. These examples demonstrate many different
145+
features available to the Injection API and SDK, including using templates
146+
created in the [SocketLabs Email Designer](https://www.socketlabs.com/blog/introducing-new-email-designer/), custom email headers, sending
147+
attachments, sending content that is stored in an HTML file, advanced bulk
148148
merging, and even pulling recipients from a datasource.
149149

150150
### [Basic send from SocketLabs Template](https://github.com/socketlabs/socketlabs-python/blob/master/python-examples/basic/basic_send_with_api_template.py)
151-
This example demonstrates the sending of a piece of content that was created in the
151+
This example demonstrates the sending of a piece of content that was created in the
152152
SocketLabs Email Designer. This is also known as the [API Templates](https://www.socketlabs.com/blog/introducing-api-templates/) feature.
153153

154154
### [Basic send from HTML file](https://github.com/socketlabs/socketlabs-python/blob/master/python-examples/basic/basic_send_from_html_file.py)
155-
This example demonstrates how to read in your HTML content from an HTML file
155+
This example demonstrates how to read in your HTML content from an HTML file
156156
rather than passing in a string directly.
157157

158158
### [Basic send with file attachment](https://github.com/socketlabs/socketlabs-python/blob/master/python-examples/basic/basic_send_with_attachment.py)
@@ -196,19 +196,19 @@ This example demonstrates the results of attempting to do a send with invalid re
196196
This example demonstrates how to send a bulk message to multiple recipients.
197197

198198
### [Bulk send with merge data](https://github.com/socketlabs/socketlabs-python/blob/master/python-examples/bulk/bulk_send_from_data_source_with_merge.py)
199-
This example demonstrates how to send a bulk message to multiple recipients with
199+
This example demonstrates how to send a bulk message to multiple recipients with
200200
unique merge data per recipient.
201201

202202
### [Bulk send with complex merge including attachments](https://github.com/socketlabs/socketlabs-python/blob/master/python-examples/bulk/bulk_send_complex.py)
203-
This example demonstrates many features of the `BulkMessage()`, including
203+
This example demonstrates many features of the `BulkMessage()`, including
204204
adding multiple recipients, merge data, and adding an attachment.
205205

206206
### [Bulk send with recipients pulled from a datasource](https://github.com/socketlabs/socketlabs-python/blob/master/python-examples/bulk/bulk_send_with_ascii_charset_merge_data.py)
207-
This example uses a mock repository class to demonstrate how you would pull
207+
This example uses a mock repository class to demonstrate how you would pull
208208
your recipients from a database and create a bulk mailing with merge data.
209209

210210
### [Bulk send with Ascii charset and special characters](https://github.com/socketlabs/socketlabs-python/blob/master/python-examples/bulk/bulk_send_with_ascii_charset_merge_data.py)
211-
This example demonstrates how to send a bulk message with a specified character
211+
This example demonstrates how to send a bulk message with a specified character
212212
set and special characters.
213213

214214
### [Bulk send with Amp ](https://github.com/socketlabs/socketlabs-python/blob/main/python-examples/bulk/bulk_send_with_amp_body.py)
@@ -217,6 +217,7 @@ For more information about AMP please see [AMP Project](https://amp.dev/document
217217

218218
<a name="version"></a>
219219
# Version
220+
* 1.4.0 - Adding Metadata and Tags
220221
* 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)
221222
* 1.1.1 - Adding request timeout value on the client for Http requests
222223
* 1.1.0 - Adds Amp Html Support

python-examples/basic/basic_send_complex.py

+32-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from socketlabs.injectionapi import SocketLabsClient
55
from socketlabs.injectionapi.message.__imports__ import \
6-
Attachment, BasicMessage, CustomHeader, EmailAddress
6+
Attachment, BasicMessage, CustomHeader, EmailAddress, Metadata
77

88

99
# build the message
@@ -132,6 +132,37 @@
132132
# Add CustomHeader using the add_custom_header function
133133
message.add_custom_header("testMessageHeader", "I am a message header")
134134

135+
# Adding Metadata
136+
# ==========================
137+
# Add Metadata using a list
138+
139+
metadata = [
140+
Metadata("example-type", "basic-send-complex-example"),
141+
Metadata("message-contains", "attachments, headers")
142+
]
143+
message.metadata = metadata
144+
145+
# Add Metadata directly to the list
146+
message.metadata.append(Metadata("message-has-attachments", "true"))
147+
148+
# Add Metadata using the add_metadata function
149+
message.add_metadata("testMessageHeader", "I am metadata")
150+
151+
# Adding Tags
152+
# ==========================
153+
# Add Metadata using a list
154+
155+
tags = [
156+
"example-type:basic-send-complex-example"
157+
]
158+
message.tags = tags
159+
160+
# Add Metadata directly to the list
161+
message.tags.append("message-has-attachments:true")
162+
163+
# Add Metadata using the add_metadata function
164+
message.add_tag("I am a test message")
165+
message.add_tag("python-Example")
135166

136167
# get credentials from environment variables
137168
server_id = int(os.environ.get('SOCKETLABS_SERVER_ID'))

python-examples/bulk/bulk_send_complex.py

+32-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from socketlabs.injectionapi import SocketLabsClient
55
from socketlabs.injectionapi.message.__imports__ import \
6-
Attachment, BulkMessage, BulkRecipient, CustomHeader, EmailAddress
6+
Attachment, BulkMessage, BulkRecipient, CustomHeader, EmailAddress, Metadata
77

88

99
# build the message
@@ -156,7 +156,6 @@
156156

157157
message.add_attachment(attachment4)
158158

159-
160159
# Adding Custom Headers
161160
# ==========================
162161
# Add CustomHeader using a list
@@ -172,6 +171,37 @@
172171
# Add CustomHeader using the add_custom_header function
173172
message.add_custom_header("testMessageHeader", "I am a message header")
174173

174+
# Adding Metadata
175+
# ==========================
176+
# Add Metadata using a list
177+
178+
metadata = [
179+
Metadata("example-type", "basic-send-complex-example"),
180+
Metadata("message-contains", "attachments, headers")
181+
]
182+
message.metadata = metadata
183+
184+
# Add Metadata directly to the list
185+
message.metadata.append(Metadata("message-has-attachments", "true"))
186+
187+
# Add Metadata using the add_metadata function
188+
message.add_metadata("testMessageHeader", "I am metadata")
189+
190+
# Adding Tags
191+
# ==========================
192+
# Add Metadata using a list
193+
194+
tags = [
195+
"example-type:basic-send-complex-example"
196+
]
197+
message.tags = tags
198+
199+
# Add Metadata directly to the list
200+
message.tags.append("message-has-attachments:true")
201+
202+
# Add Metadata using the add_metadata function
203+
message.add_tag("I am a test message")
204+
message.add_tag("python-Example")
175205

176206
# get credentials from environment variables
177207
server_id = int(os.environ.get('SOCKETLABS_SERVER_ID'))

setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
'Programming Language :: Python :: 3.6',
2424
'Programming Language :: Python :: 3.7',
2525
'Programming Language :: Python :: 3.8',
26+
'Programming Language :: Python :: 3.9',
2627
"License :: OSI Approved :: MIT License",
2728
"Operating System :: OS Independent",
2829
],

socketlabs/injectionapi/core/injectionrequestfactory.py

+20
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,6 +166,9 @@ 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

socketlabs/injectionapi/core/sendvalidator.py

+21
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ def validate_base_message(message: MessageBase):
3939
if not has_valid_custom_headers(message.custom_headers):
4040
return SendResult.MessageValidationInvalidCustomHeaders
4141

42+
if message.metadata is not None and len(message.metadata) > 0:
43+
if not has_valid_metadata(message.metadata):
44+
return SendResult.MessageValidationInvalidMetadata
45+
4246
return SendResult.Success
4347

4448

@@ -294,6 +298,23 @@ def has_valid_custom_headers(custom_headers: list):
294298
return True
295299

296300

301+
def has_valid_metadata(metadata: list):
302+
"""
303+
Check if the list of metadata is valid
304+
:param metadata: list of Metadata to validate
305+
:type metadata: list
306+
:return the result
307+
:rtype bool
308+
"""
309+
if metadata is None:
310+
return True
311+
for item in metadata:
312+
valid = item.isvalid()
313+
if valid is False:
314+
return False
315+
return True
316+
317+
297318
def validate_basic_message(message: BasicMessage):
298319
"""
299320
Validate a basic email message before sending to the Injection API.

0 commit comments

Comments
 (0)