Skip to content

Commit 238b127

Browse files
committed
Email: separated inline and attachment
Signed-off-by: Vishal Rana <vr@labstack.com>
1 parent 7b6e12d commit 238b127

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

labstack/email.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,14 @@
44
import json
55
from .common import API_URL
66

7-
def _email_file_from_path(path):
8-
with open(path, 'rb') as file:
9-
return {
10-
'name': os.path.basename(path),
11-
'content': base64.b64encode(file.read()).decode('utf-8')
12-
}
13-
147
class _Email():
158
def __init__(self, interceptor):
169
self.path = '/email'
1710
self.interceptor = interceptor
1811

1912
def send(self, message):
20-
message._add_files()
13+
message._add_inlines()
14+
message._add_attachments()
2115
r = requests.post(API_URL + self.path, auth=self.interceptor, data=message.to_json())
2216
data = r.json()
2317
if not 200 <= r.status_code < 300:
@@ -36,6 +30,22 @@ def __init__(self, to, sender, subject):
3630
self.attachments = []
3731
self.status = ''
3832

33+
def _add_inlines(self):
34+
for inline in self.inlines:
35+
with open(inline, 'rb') as file:
36+
self._inlines.append({
37+
'name': os.path.basename(inline),
38+
'content': base64.b64encode(file.read()).decode('utf-8')
39+
})
40+
41+
def _add_attachments(self):
42+
for attachment in self.attachments:
43+
with open(attachment, 'rb') as file:
44+
self._attachments.append({
45+
'name': os.path.basename(attachment),
46+
'content': base64.b64encode(file.read()).decode('utf-8')
47+
})
48+
3949
def _add_files(self):
4050
for path in self.inlines:
4151
self._inlines.append(_email_file_from_path(path))

labstack/log.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@ async def _schedule(self):
3333
async def _dispatch(self):
3434
if len(self.entries) == 0:
3535
return
36-
37-
r = requests.post(API_URL + self.path, auth=self.interceptor, data=json.dumps(self.entries))
38-
if not 200 <= r.status_code < 300:
39-
data = r.json()
40-
raise LogError(data['code'], data['message'])
41-
self.entries.clear()
36+
try:
37+
r = requests.post(API_URL + self.path, auth=self.interceptor, data=json.dumps(self.entries))
38+
if not 200 <= r.status_code < 300:
39+
data = r.json()
40+
raise LogError(data['code'], data['message'])
41+
finally:
42+
self.entries.clear()
4243

4344
def debug(self, format, *argv):
4445
self._log(Level.DEBUG, format, *argv)

labstack/store.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ def __init__(self):
7777
def from_json(self, response):
7878
qr = StoreQueryResponse()
7979
qr.total = response['total']
80-
qr.entries = response['entries']
80+
for entry in response['entries']:
81+
qr.entries.append(StoreEntry.from_json(entry))
8182
return qr
8283

8384
class StoreError(Exception):

0 commit comments

Comments
 (0)