forked from sendgrid/sendgrid-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend.py
61 lines (51 loc) · 1.73 KB
/
send.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""A module for sending test SendGrid Inbound Parse messages.
Usage: ./send.py [path to file containing test data]"""
import argparse
import sys
from io import open
try:
from config import Config
except ImportError:
# Python 3+, Travis
from sendgrid.helpers.inbound.config import Config
from python_http_client import Client
class Send(object):
def __init__(self, url):
"""Create a Send object with target `url`."""
self._url = url
def test_payload(self, payload_filepath):
"""Send a test payload.
Load a payload from payload_filepath, apply headers, and POST self.url.
Return the response object.
"""
headers = {
"User-Agent": "SendGrid-Test",
"Content-Type": "multipart/form-data; boundary=xYzZY"
}
client = Client(host=self.url, request_headers=headers)
f = open(payload_filepath, 'r', encoding='utf-8')
data = f.read()
return client.post(request_body=data)
@property
def url(self):
"""URL to send to."""
return self._url
def main():
config = Config()
parser = argparse.ArgumentParser(
description='Test data and optional host.')
parser.add_argument('data',
type=str,
help='path to the sample data')
parser.add_argument('-host',
type=str,
help='name of host to send the sample data to',
default=config.host, required=False)
args = parser.parse_args()
send = Send(args.host)
response = send.test_payload(sys.argv[1])
print(response.status_code)
print(response.headers)
print(response.body)
if __name__ == '__main__':
main()