Skip to content

Commit 7a171b7

Browse files
committed
First commit
Signed-off-by: Vishal Rana <[email protected]>
0 parents  commit 7a171b7

File tree

6 files changed

+118
-0
lines changed

6 files changed

+118
-0
lines changed

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
bin
2+
lib
3+
include
4+
pip-selfcheck.json
5+
.Python
6+
.DS_Store
7+
__pycache__

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 LabStack
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

labstack/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from .client import Client
2+
from .email import EmailMessage, EmailError

labstack/client.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import requests
2+
from .email import _Email
3+
4+
class Client():
5+
def __init__(self, api_key):
6+
self.interceptor = _Interceptor(api_key)
7+
self.app_id = ''
8+
self.app_name = ''
9+
10+
def email(self):
11+
return _Email(self.interceptor)
12+
13+
class _Interceptor(requests.auth.AuthBase):
14+
def __init__(self, api_key):
15+
self.api_key = api_key
16+
17+
def __call__(self, r):
18+
r.headers['Authorization'] = 'Bearer ' + self.api_key
19+
r.headers['Content-Type'] = 'application/json; charset=utf-8'
20+
return r

labstack/common.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
API_URL = 'https://api.labstack.com'

labstack/email.py

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import os
2+
import base64
3+
import requests
4+
import json
5+
from .common import API_URL
6+
7+
class _Email():
8+
def __init__(self, interceptor):
9+
self.path = '/email'
10+
self.interceptor = interceptor
11+
12+
def send(self, message):
13+
message._add_files()
14+
r = requests.post(API_URL + self.path, auth=self.interceptor, data=message.toJSON())
15+
if not 200 <= r.status_code < 300:
16+
response = r.json()
17+
raise EmailError(response['code'], r['message'])
18+
19+
class EmailMessage():
20+
def __init__(self, to, sender, subject):
21+
self._inlines = []
22+
self._attachments = []
23+
self.to = to
24+
self.sender = sender
25+
self.subject = subject
26+
self.body = ''
27+
self.inlines = []
28+
self.attachments = []
29+
self.status = ''
30+
31+
def _add_files(self):
32+
for path in self.inlines:
33+
self._inlines.append(_email_file_from_path(path))
34+
for path in self.attachments:
35+
self._attachments.append(_email_file_from_path(path))
36+
37+
def add_inline(self, path):
38+
self.inlines.append(path)
39+
40+
def add_attachment(self, path):
41+
self.attachments.append(path)
42+
43+
def toJSON(self):
44+
return json.dumps({
45+
'to': self.to,
46+
'from': self.sender,
47+
'subject': self.subject,
48+
'body': self.body,
49+
'inlines': self._inlines,
50+
'attachments': self._attachments
51+
})
52+
53+
def _email_file_from_path(path):
54+
with open(path, 'rb') as file:
55+
return {
56+
'name': os.path.basename(path),
57+
'content': base64.b64encode(file.read()).decode('utf-8')
58+
}
59+
60+
class EmailError(Exception):
61+
def __init__(self, code, message):
62+
self.code = code
63+
self.message = message
64+
65+
def __str__(self):
66+
return 'email error, code={0}, message={1}'.format(self.code, self.message)
67+

0 commit comments

Comments
 (0)