From 1936e24d0ae79600fe52aace3b8b6780f2397edd Mon Sep 17 00:00:00 2001 From: d grossman Date: Tue, 17 Oct 2017 10:38:54 -0700 Subject: [PATCH 01/52] Get unassigned IPs #392, made helper function --- sendgrid/helpers/endpoints/__init__.py | 0 sendgrid/helpers/endpoints/ip/__init__.py | 0 .../helpers/endpoints/ip/test_unassigned.py | 80 +++++++++++++++++++ sendgrid/helpers/endpoints/ip/unassigned.py | 52 ++++++++++++ 4 files changed, 132 insertions(+) create mode 100644 sendgrid/helpers/endpoints/__init__.py create mode 100644 sendgrid/helpers/endpoints/ip/__init__.py create mode 100644 sendgrid/helpers/endpoints/ip/test_unassigned.py create mode 100644 sendgrid/helpers/endpoints/ip/unassigned.py diff --git a/sendgrid/helpers/endpoints/__init__.py b/sendgrid/helpers/endpoints/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/sendgrid/helpers/endpoints/ip/__init__.py b/sendgrid/helpers/endpoints/ip/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/sendgrid/helpers/endpoints/ip/test_unassigned.py b/sendgrid/helpers/endpoints/ip/test_unassigned.py new file mode 100644 index 000000000..cc2af3b40 --- /dev/null +++ b/sendgrid/helpers/endpoints/ip/test_unassigned.py @@ -0,0 +1,80 @@ +import json +import pytest + +from .unassigned import unassigned + + +def test_unassigned_ip(): + + ret_json = '''[ { + "ip": "167.89.21.3", + "pools": [ + "pool1", + "pool2" + ], + "whitelabeled": false, + "start_date": 1409616000, + "subusers": [ + "tim@sendgrid.net" + ], + "warmup": false, + "assigned_at": 1482883200 + }, + { + "ip": "192.168.1.1", + "pools": [ + "pool1", + "pool2" + ], + "whitelabeled": false, + "start_date": 1409616000, + "subusers": [ + "tim@sendgrid.net" + ], + "warmup": false, + "assigned_at": 1482883200 + }, + { + "ip": "208.115.214.22", + "pools": [], + "whitelabeled": true, + "rdns": "o1.email.burgermail.com", + "start_date": 1409616000, + "subusers": [], + "warmup": false, + "assigned_at": 1482883200 + }, + { + "ip": "208.115.214.23", + "pools": [], + "whitelabeled": true, + "rdns": "o1.email.burgermail.com", + "start_date": 1409616000, + "subusers": [], + "warmup": false, + "assigned_at": 1482883200 + + } ] + ''' + + def get_all_ip(): + ret_val = json.loads(ret_json) + return ret_val + + data = {"208.115.214.23", "208.115.214.22"} + + as_json = True + calculated = unassigned(get_all_ip(), as_json=as_json) + calculated = json.loads(calculated) + + for item in calculated: + assert item["ip"] in data + + as_json = False + calculated = unassigned(get_all_ip(), as_json=as_json) + + for item in calculated: + assert item["ip"] in data + + calculated = unassigned(dict(), as_json=as_json) + assert calculated == [] diff --git a/sendgrid/helpers/endpoints/ip/unassigned.py b/sendgrid/helpers/endpoints/ip/unassigned.py new file mode 100644 index 000000000..075f19857 --- /dev/null +++ b/sendgrid/helpers/endpoints/ip/unassigned.py @@ -0,0 +1,52 @@ +import json + + +def format_ret(return_set, as_json=False): + """ decouple, allow for modifications to return type + returns a list of ip addresses in object or json form """ + ret_list = list() + for item in return_set: + d = {"ip": item} + ret_list.append(d) + + if as_json: + return json.dumps(ret_list) + + return ret_list + + +def unassigned(data, as_json=False): + """ https://sendgrid.com/docs/API_Reference/api_v3.html#ip-addresses + The /ips rest endpoint returns information about the IP addresses + and the usernames assigned to an IP + + unassigned returns a listing of the IP addresses that are allocated + but have 0 usera assigned + + + data (response.body from sg.client.ips.get()) + as_json False -> get list of dicts + True -> get json object + + example: + sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) + + params = {'subuser': 'test_string', 'ip': 'test_string', 'limit': 1, 'exclude_whitelabels': 'true', 'offset': 1} + response = sg.client.ips.get(query_params=params) + if response.status_code == 201: + data = response.body + unused = unassinged(data) """ + + no_subusers = set() + + if not isinstance(data, list): + return format_ret(no_subusers, as_json=as_json) + + for current in data: + num_subusers = len(current["subusers"]) + if num_subusers == 0: + current_ip = current["ip"] + no_subusers.add(current_ip) + + ret_val = format_ret(no_subusers, as_json=as_json) + return ret_val From c8fc9eb87a764ee27b9a0b7a9ea40fd29c3ba1f0 Mon Sep 17 00:00:00 2001 From: d grossman Date: Tue, 17 Oct 2017 10:45:29 -0700 Subject: [PATCH 02/52] break up tests --- .../helpers/endpoints/ip/test_unassigned.py | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/sendgrid/helpers/endpoints/ip/test_unassigned.py b/sendgrid/helpers/endpoints/ip/test_unassigned.py index cc2af3b40..a639f34a9 100644 --- a/sendgrid/helpers/endpoints/ip/test_unassigned.py +++ b/sendgrid/helpers/endpoints/ip/test_unassigned.py @@ -3,15 +3,12 @@ from .unassigned import unassigned - -def test_unassigned_ip(): - - ret_json = '''[ { - "ip": "167.89.21.3", +ret_json = '''[ { + "ip": "167.89.21.3", "pools": [ - "pool1", - "pool2" - ], + "pool1", + "pool2" + ], "whitelabeled": false, "start_date": 1409616000, "subusers": [ @@ -57,9 +54,13 @@ def test_unassigned_ip(): } ] ''' - def get_all_ip(): - ret_val = json.loads(ret_json) - return ret_val +def get_all_ip(): + ret_val = json.loads(ret_json) + return ret_val + + + +def test_unassigned_ip_json(): data = {"208.115.214.23", "208.115.214.22"} @@ -70,11 +71,17 @@ def get_all_ip(): for item in calculated: assert item["ip"] in data +def test_unassigned_ip_obj(): + + data = {"208.115.214.23", "208.115.214.22"} + as_json = False calculated = unassigned(get_all_ip(), as_json=as_json) for item in calculated: assert item["ip"] in data +def test_unassigned_baddata(): + as_json = False calculated = unassigned(dict(), as_json=as_json) assert calculated == [] From df73eb0b066b73fa62963fbb2e925d587d679d18 Mon Sep 17 00:00:00 2001 From: d grossman Date: Tue, 17 Oct 2017 10:53:21 -0700 Subject: [PATCH 03/52] make the python2.6 travis build happy --- sendgrid/helpers/endpoints/ip/test_unassigned.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/sendgrid/helpers/endpoints/ip/test_unassigned.py b/sendgrid/helpers/endpoints/ip/test_unassigned.py index a639f34a9..be5904018 100644 --- a/sendgrid/helpers/endpoints/ip/test_unassigned.py +++ b/sendgrid/helpers/endpoints/ip/test_unassigned.py @@ -59,10 +59,17 @@ def get_all_ip(): return ret_val +def make_data(): + data = set() + data.add("208.115.214.23") + data.add("208.115.214.22") + return data + + def test_unassigned_ip_json(): - data = {"208.115.214.23", "208.115.214.22"} + data = make_data() as_json = True calculated = unassigned(get_all_ip(), as_json=as_json) @@ -73,7 +80,7 @@ def test_unassigned_ip_json(): def test_unassigned_ip_obj(): - data = {"208.115.214.23", "208.115.214.22"} + data = make_data() as_json = False calculated = unassigned(get_all_ip(), as_json=as_json) From 9f0932f4634f5abeb24947d6078e6d49558f5a41 Mon Sep 17 00:00:00 2001 From: Aaron Mak Kang Sheng Date: Sun, 22 Oct 2017 10:11:27 +0800 Subject: [PATCH 04/52] Add global stats helper --- examples/helpers/stats/stats_example.py | 27 +++++++ sendgrid/helpers/stats/README.md | 1 + sendgrid/helpers/stats/__init__.py | 1 + sendgrid/helpers/stats/stats.py | 98 +++++++++++++++++++++++++ 4 files changed, 127 insertions(+) create mode 100644 examples/helpers/stats/stats_example.py create mode 100644 sendgrid/helpers/stats/README.md create mode 100644 sendgrid/helpers/stats/__init__.py create mode 100644 sendgrid/helpers/stats/stats.py diff --git a/examples/helpers/stats/stats_example.py b/examples/helpers/stats/stats_example.py new file mode 100644 index 000000000..3d2846e11 --- /dev/null +++ b/examples/helpers/stats/stats_example.py @@ -0,0 +1,27 @@ +import json +import os +from sendgrid.helpers.stats import * +from sendgrid import * + +# NOTE: you will need move this file to the root directory of this project to execute properly. + + +def build_global_stats(): + global_stats = Stats() + global_stats.start_date = '2017-10-14' + global_stats.end_date = '2017-10-20' + global_stats.aggregated_by = 'day' + return global_stats.get() + + +def get_global_stats(): + # Assumes you set your environment variable: + # https://github.com/sendgrid/sendgrid-python/blob/master/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key + sg = SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) + stats_params = build_global_stats() + response = sg.client.stats.get(query_params=stats_params) + print(response.status_code) + print(response.headers) + print(json.dumps(json.loads(response.body), indent=4, sort_keys=True)) + +get_global_stats() \ No newline at end of file diff --git a/sendgrid/helpers/stats/README.md b/sendgrid/helpers/stats/README.md new file mode 100644 index 000000000..2c062c3be --- /dev/null +++ b/sendgrid/helpers/stats/README.md @@ -0,0 +1 @@ +**This helper allows you to quickly and easily build a Stats object for sending your email stats to a database.** \ No newline at end of file diff --git a/sendgrid/helpers/stats/__init__.py b/sendgrid/helpers/stats/__init__.py new file mode 100644 index 000000000..9ee4dcdd8 --- /dev/null +++ b/sendgrid/helpers/stats/__init__.py @@ -0,0 +1 @@ +from .stats import * # noqa diff --git a/sendgrid/helpers/stats/stats.py b/sendgrid/helpers/stats/stats.py new file mode 100644 index 000000000..398fe7583 --- /dev/null +++ b/sendgrid/helpers/stats/stats.py @@ -0,0 +1,98 @@ +import json +import csv + + +class Stats(object): + def __init__( + self, start_date=None): + self._start_date = None + self._end_date = None + self._aggregated_by = None + self._sort_by_metric = None + self._sort_by_direction = None + self._limit = None + self._offset = None + + # Minimum required for stats + if start_date: + self.start_date = start_date + + def __str__(self): + return str(self.get()) + + def get(self): + """ + :return: response stats dict + """ + stats = {} + if self.start_date is not None: + stats["start_date"] = self.start_date + if self.end_date is not None: + stats["end_date"] = self.end_date + if self.aggregated_by is not None: + stats["aggregated_by"] = self.aggregated_by + if self.sort_by_metric is not None: + stats["sort_by_metric"] = self.sort_by_metric + if self.sort_by_direction is not None: + stats["sort_by_direction"] = self.sort_by_direction + if self.limit is not None: + stats["limit"] = self.limit + if self.offset is not None: + stats["offset"] = self.offset + return stats + + @property + def start_date(self): + return self._start_date + + @start_date.setter + def start_date(self, value): + self._start_date = value + + @property + def end_date(self): + return self._end_date + + @end_date.setter + def end_date(self, value): + self._end_date = value + + @property + def aggregated_by(self): + return self._aggregated_by + + @aggregated_by.setter + def aggregated_by(self, value): + self._aggregated_by = value + + @property + def sort_by_metric(self): + return self._sort_by_metric + + @sort_by_metric.setter + def sort_by_metric(self, value): + self._sort_by_metric = value + + @property + def sort_by_direction(self): + return self._sort_by_direction + + @sort_by_direction.setter + def sort_by_direction(self, value): + self._sort_by_direction = value + + @property + def limit(self): + return self._limit + + @limit.setter + def limit(self, value): + self._limit = value + + @property + def offset(self): + return self._offset + + @offset.setter + def offset(self, value): + self._offset = value From eb65f75e55c8bf5dc0d5ba076ab79f9bf01e4bd0 Mon Sep 17 00:00:00 2001 From: Aaron Mak Kang Sheng Date: Sun, 22 Oct 2017 12:52:30 +0800 Subject: [PATCH 05/52] Add child class CategoryStats --- examples/helpers/stats/stats_example.py | 44 +++++++++++++++-- sendgrid/helpers/stats/stats.py | 64 ++++++++++++++++++++++++- 2 files changed, 103 insertions(+), 5 deletions(-) diff --git a/examples/helpers/stats/stats_example.py b/examples/helpers/stats/stats_example.py index 3d2846e11..8688440d4 100644 --- a/examples/helpers/stats/stats_example.py +++ b/examples/helpers/stats/stats_example.py @@ -5,6 +5,10 @@ # NOTE: you will need move this file to the root directory of this project to execute properly. +# Assumes you set your environment variable: +# https://github.com/sendgrid/sendgrid-python/blob/master/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key +sg = SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) + def build_global_stats(): global_stats = Stats() @@ -14,14 +18,46 @@ def build_global_stats(): return global_stats.get() +def build_category_stats(): + category_stats = CategoryStats() + category_stats.start_date = '2017-10-15' + category_stats.add_category(Category("foo")) + category_stats.add_category(Category("bar")) + return category_stats.get() + + +def build_category_stats_sums(): + category_stats = CategoryStats() + category_stats.start_date = '2017-10-15' + category_stats.limit = 5 + category_stats.offset = 1 + return category_stats.get() + + def get_global_stats(): - # Assumes you set your environment variable: - # https://github.com/sendgrid/sendgrid-python/blob/master/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key - sg = SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) stats_params = build_global_stats() response = sg.client.stats.get(query_params=stats_params) + response_body = json.loads(response.body)[0]['date'] + print(response.status_code) + print(response.headers) + print(json.dumps(response_body, indent=4, sort_keys=True)) + + +def get_category_stats(): + stats_params = build_category_stats() + response = sg.client.categories.stats.get(query_params=stats_params) + print(response.status_code) + print(response.headers) + print(json.dumps(json.loads(response.body), indent=4, sort_keys=True)) + + +def get_category_stats_sums(): + stats_params = build_category_stats_sums() + response = sg.client.categories.stats.sums.get(query_params=stats_params) print(response.status_code) print(response.headers) print(json.dumps(json.loads(response.body), indent=4, sort_keys=True)) -get_global_stats() \ No newline at end of file +get_global_stats() +get_category_stats() +get_category_stats_sums() diff --git a/sendgrid/helpers/stats/stats.py b/sendgrid/helpers/stats/stats.py index 398fe7583..8719a9f7d 100644 --- a/sendgrid/helpers/stats/stats.py +++ b/sendgrid/helpers/stats/stats.py @@ -1,7 +1,6 @@ import json import csv - class Stats(object): def __init__( self, start_date=None): @@ -96,3 +95,66 @@ def offset(self): @offset.setter def offset(self, value): self._offset = value + + +class CategoryStats(Stats): + def __init__(self, start_date=None, categories=None): + self._categories = None + super(CategoryStats, self).__init__() + + # Minimum required for category stats + if start_date and categories: + self.start_date = start_date + self.categories = categories + + def get(self): + """ + :return: response stats dict + """ + stats = {} + if self.start_date is not None: + stats["start_date"] = self.start_date + if self.end_date is not None: + stats["end_date"] = self.end_date + if self.aggregated_by is not None: + stats["aggregated_by"] = self.aggregated_by + if self.sort_by_metric is not None: + stats["sort_by_metric"] = self.sort_by_metric + if self.sort_by_direction is not None: + stats["sort_by_direction"] = self.sort_by_direction + if self.limit is not None: + stats["limit"] = self.limit + if self.offset is not None: + stats["offset"] = self.offset + if self.categories is not None: + stats['categories'] = [category.get() for category in + self.categories] + return stats + + @property + def categories(self): + return self._categories + + def add_category(self, category): + if self._categories is None: + self._categories = [] + self._categories.append(category) + + +class Category(object): + + def __init__(self, name=None): + self._name = None + if name is not None: + self._name = name + + @property + def name(self): + return self._name + + @name.setter + def name(self, value): + self._name = value + + def get(self): + return self.name \ No newline at end of file From a1fab6789e21932cf736d8550a474a95e26fb199 Mon Sep 17 00:00:00 2001 From: Aaron Mak Kang Sheng Date: Sun, 22 Oct 2017 12:53:10 +0800 Subject: [PATCH 06/52] fix typo --- examples/helpers/stats/stats_example.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/helpers/stats/stats_example.py b/examples/helpers/stats/stats_example.py index 8688440d4..3862b2097 100644 --- a/examples/helpers/stats/stats_example.py +++ b/examples/helpers/stats/stats_example.py @@ -37,10 +37,9 @@ def build_category_stats_sums(): def get_global_stats(): stats_params = build_global_stats() response = sg.client.stats.get(query_params=stats_params) - response_body = json.loads(response.body)[0]['date'] print(response.status_code) print(response.headers) - print(json.dumps(response_body, indent=4, sort_keys=True)) + print(json.dumps(json.loads(response.body), indent=4, sort_keys=True)) def get_category_stats(): From 67d0b634712d3b8c60f4a9c2a1e5ecf810016e07 Mon Sep 17 00:00:00 2001 From: Aaron Mak Kang Sheng Date: Sun, 22 Oct 2017 13:04:20 +0800 Subject: [PATCH 07/52] Clean up json and refine CategoryStats Class --- examples/helpers/stats/stats_example.py | 19 ++++++++++++------- sendgrid/helpers/stats/stats.py | 3 ++- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/examples/helpers/stats/stats_example.py b/examples/helpers/stats/stats_example.py index 3862b2097..41f4b85ab 100644 --- a/examples/helpers/stats/stats_example.py +++ b/examples/helpers/stats/stats_example.py @@ -10,6 +10,10 @@ sg = SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) +def pprint_json(json_raw): + print(json.dumps(json.loads(json_raw), indent=4, sort_keys=True)) + + def build_global_stats(): global_stats = Stats() global_stats.start_date = '2017-10-14' @@ -19,10 +23,10 @@ def build_global_stats(): def build_category_stats(): - category_stats = CategoryStats() - category_stats.start_date = '2017-10-15' - category_stats.add_category(Category("foo")) - category_stats.add_category(Category("bar")) + category_stats = CategoryStats('2017-10-15', ['foo', 'bar']) + # category_stats.start_date = '2017-10-15' + # category_stats.add_category(Category("foo")) + # category_stats.add_category(Category("bar")) return category_stats.get() @@ -39,15 +43,16 @@ def get_global_stats(): response = sg.client.stats.get(query_params=stats_params) print(response.status_code) print(response.headers) - print(json.dumps(json.loads(response.body), indent=4, sort_keys=True)) + pprint_json(response.body) def get_category_stats(): stats_params = build_category_stats() + print(stats_params) response = sg.client.categories.stats.get(query_params=stats_params) print(response.status_code) print(response.headers) - print(json.dumps(json.loads(response.body), indent=4, sort_keys=True)) + pprint_json(response.body) def get_category_stats_sums(): @@ -55,7 +60,7 @@ def get_category_stats_sums(): response = sg.client.categories.stats.sums.get(query_params=stats_params) print(response.status_code) print(response.headers) - print(json.dumps(json.loads(response.body), indent=4, sort_keys=True)) + pprint_json(response.body) get_global_stats() get_category_stats() diff --git a/sendgrid/helpers/stats/stats.py b/sendgrid/helpers/stats/stats.py index 8719a9f7d..b5ddebcc1 100644 --- a/sendgrid/helpers/stats/stats.py +++ b/sendgrid/helpers/stats/stats.py @@ -105,7 +105,8 @@ def __init__(self, start_date=None, categories=None): # Minimum required for category stats if start_date and categories: self.start_date = start_date - self.categories = categories + for cat in categories: + self.add_category(Category(cat)) def get(self): """ From f8183a689a0952445dec9b59bb00e8b35ec550ba Mon Sep 17 00:00:00 2001 From: Aaron Mak Kang Sheng Date: Sun, 22 Oct 2017 13:41:53 +0800 Subject: [PATCH 08/52] Add SubuserStats class --- examples/helpers/stats/stats_example.py | 42 +++++++++++++-- sendgrid/helpers/stats/stats.py | 70 +++++++++++++++++++++++-- 2 files changed, 102 insertions(+), 10 deletions(-) diff --git a/examples/helpers/stats/stats_example.py b/examples/helpers/stats/stats_example.py index 41f4b85ab..00a704c47 100644 --- a/examples/helpers/stats/stats_example.py +++ b/examples/helpers/stats/stats_example.py @@ -11,7 +11,7 @@ def pprint_json(json_raw): - print(json.dumps(json.loads(json_raw), indent=4, sort_keys=True)) + print(json.dumps(json.loads(json_raw), indent=2, sort_keys=True)) def build_global_stats(): @@ -38,6 +38,21 @@ def build_category_stats_sums(): return category_stats.get() +def build_subuser_stats(): + subuser_stats = SubuserStats('2017-10-20', ['aaronmakks','foo']) + # subuser_stats.start_date = '2017-10-15' + # subuser_stats.add_subuser(Subuser("foo")) + # subuser_stats.add_subuser(Subuser("bar")) + return subuser_stats.get() + +def build_subuser_stats_sums(): + subuser_stats = SubuserStats() + subuser_stats.start_date = '2017-10-15' + subuser_stats.limit = 5 + subuser_stats.offset = 1 + return subuser_stats.get() + + def get_global_stats(): stats_params = build_global_stats() response = sg.client.stats.get(query_params=stats_params) @@ -48,7 +63,6 @@ def get_global_stats(): def get_category_stats(): stats_params = build_category_stats() - print(stats_params) response = sg.client.categories.stats.get(query_params=stats_params) print(response.status_code) print(response.headers) @@ -62,6 +76,24 @@ def get_category_stats_sums(): print(response.headers) pprint_json(response.body) -get_global_stats() -get_category_stats() -get_category_stats_sums() + +def get_subuser_stats(): + stats_params = build_subuser_stats() + response = sg.client.subusers.stats.get(query_params=stats_params) + print(response.status_code) + print(response.headers) + pprint_json(response.body) + + +def get_subuser_stats_sums(): + stats_params = build_subuser_stats_sums() + response = sg.client.subusers.stats.sums.get(query_params=stats_params) + print(response.status_code) + print(response.headers) + pprint_json(response.body) + +# get_global_stats() +# get_category_stats() +# get_category_stats_sums() +# get_subuser_stats() +# get_subuser_stats_sums() diff --git a/sendgrid/helpers/stats/stats.py b/sendgrid/helpers/stats/stats.py index b5ddebcc1..550599138 100644 --- a/sendgrid/helpers/stats/stats.py +++ b/sendgrid/helpers/stats/stats.py @@ -1,6 +1,3 @@ -import json -import csv - class Stats(object): def __init__( self, start_date=None): @@ -105,8 +102,8 @@ def __init__(self, start_date=None, categories=None): # Minimum required for category stats if start_date and categories: self.start_date = start_date - for cat in categories: - self.add_category(Category(cat)) + for cat_name in categories: + self.add_category(Category(cat_name)) def get(self): """ @@ -142,8 +139,71 @@ def add_category(self, category): self._categories.append(category) +class SubuserStats(Stats): + def __init__(self, start_date=None, subusers=None): + self._subusers = None + super(SubuserStats, self).__init__() + + # Minimum required for subusers stats + if start_date and subusers: + self.start_date = start_date + for subuser_name in subusers: + self.add_subuser(Subuser(subuser_name)) + + def get(self): + """ + :return: response stats dict + """ + stats = {} + if self.start_date is not None: + stats["start_date"] = self.start_date + if self.end_date is not None: + stats["end_date"] = self.end_date + if self.aggregated_by is not None: + stats["aggregated_by"] = self.aggregated_by + if self.sort_by_metric is not None: + stats["sort_by_metric"] = self.sort_by_metric + if self.sort_by_direction is not None: + stats["sort_by_direction"] = self.sort_by_direction + if self.limit is not None: + stats["limit"] = self.limit + if self.offset is not None: + stats["offset"] = self.offset + if self.subusers is not None: + stats['subusers'] = [subuser.get() for subuser in + self.subusers] + return stats + + @property + def subusers(self): + return self._subusers + + def add_subuser(self, subuser): + if self._subusers is None: + self._subusers = [] + self._subusers.append(subuser) + + class Category(object): + def __init__(self, name=None): + self._name = None + if name is not None: + self._name = name + + @property + def name(self): + return self._name + + @name.setter + def name(self, value): + self._name = value + + def get(self): + return self.name + +class Subuser(object): + def __init__(self, name=None): self._name = None if name is not None: From 57439d976c5b3423f3797ee4c39b7006e394ba92 Mon Sep 17 00:00:00 2001 From: Aaron Mak Kang Sheng Date: Sun, 22 Oct 2017 13:54:56 +0800 Subject: [PATCH 09/52] Add to readme --- sendgrid/helpers/stats/README.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/sendgrid/helpers/stats/README.md b/sendgrid/helpers/stats/README.md index 2c062c3be..1fe31558b 100644 --- a/sendgrid/helpers/stats/README.md +++ b/sendgrid/helpers/stats/README.md @@ -1 +1,10 @@ -**This helper allows you to quickly and easily build a Stats object for sending your email stats to a database.** \ No newline at end of file +**This helper allows you to quickly and easily build a Stats object for sending your email stats to a database.** + +# Quick Start + +Run the [example](https://github.com/sendgrid/sendgrid-python/tree/master/examples/helpers/stats) (make sure you have set your environment variable to include your SENDGRID_API_KEY). + +## Usage + +- See the [examples](https://github.com/sendgrid/sendgrid-python/tree/master/examples/helpers/stats) for complete working examples. +- [Documentation](https://sendgrid.com/docs/API_Reference/Web_API_v3/Stats/index.html) \ No newline at end of file From 246efcc9ed1d525847fb73c4da874340358d48ce Mon Sep 17 00:00:00 2001 From: Aaron Mak Kang Sheng Date: Fri, 27 Oct 2017 08:15:56 +0800 Subject: [PATCH 10/52] add tests for stats helper --- examples/helpers/stats/stats_example.py | 10 ++-- sendgrid/helpers/stats/stats.py | 3 +- test/test_stats.py | 79 +++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 test/test_stats.py diff --git a/examples/helpers/stats/stats_example.py b/examples/helpers/stats/stats_example.py index 00a704c47..d48664c3f 100644 --- a/examples/helpers/stats/stats_example.py +++ b/examples/helpers/stats/stats_example.py @@ -92,8 +92,8 @@ def get_subuser_stats_sums(): print(response.headers) pprint_json(response.body) -# get_global_stats() -# get_category_stats() -# get_category_stats_sums() -# get_subuser_stats() -# get_subuser_stats_sums() +get_global_stats() +get_category_stats() +get_category_stats_sums() +get_subuser_stats() +get_subuser_stats_sums() diff --git a/sendgrid/helpers/stats/stats.py b/sendgrid/helpers/stats/stats.py index 550599138..8fe1399a2 100644 --- a/sendgrid/helpers/stats/stats.py +++ b/sendgrid/helpers/stats/stats.py @@ -202,6 +202,7 @@ def name(self, value): def get(self): return self.name + class Subuser(object): def __init__(self, name=None): @@ -218,4 +219,4 @@ def name(self, value): self._name = value def get(self): - return self.name \ No newline at end of file + return self.name diff --git a/test/test_stats.py b/test/test_stats.py new file mode 100644 index 000000000..eafd131db --- /dev/null +++ b/test/test_stats.py @@ -0,0 +1,79 @@ +# -*- coding: utf-8 -*- +import json +from sendgrid.helpers.stats import * + +try: + import unittest2 as unittest +except ImportError: + import unittest + + +class UnitTests(unittest.TestCase): + + def test_basicStats(self): + + """Minimum required for stats""" + global_stats = Stats(start_date='12-09-2017') + + self.assertEqual( + json.dumps( + global_stats.get(), + sort_keys=True), + '{"start_date": "12-09-2017"}' + ) + + self.assertTrue(isinstance(str(global_stats), str)) + + def test_Stats(self): + + all_stats = Stats(start_date='12-09-2017') + all_stats.end_date = '12-10-2017' + all_stats.aggregated_by = 'day' + all_stats._sort_by_direction = 'asc' + all_stats._limit = 100 + all_stats._offset = 2 + + self.assertEqual( + json.dumps( + all_stats.get(), + sort_keys=True), + '{"aggregated_by": "day", "end_date": "12-10-2017", ' + '"limit": 100, "offset": 2, "sort_by_direction": "asc", ' + '"start_date": "12-09-2017"}' + ) + + def test_categoryStats(self): + + category_stats = CategoryStats(start_date='12-09-2017', categories=['foo', 'bar']) + category_stats.end_date = '12-10-2017' + category_stats.aggregated_by = 'day' + category_stats._sort_by_direction = 'asc' + category_stats._limit = 100 + category_stats._offset = 2 + + self.assertEqual( + json.dumps( + category_stats.get(), + sort_keys=True), + '{"aggregated_by": "day", "categories": ["foo", "bar"], ' + '"end_date": "12-10-2017", "limit": 100, "offset": 2, ' + '"sort_by_direction": "asc", "start_date": "12-09-2017"}' + ) + + def test_subuserStats(self): + + subuser_stats = SubuserStats(start_date = '12-09-2017', subusers=['foo', 'bar']) + subuser_stats.end_date = '12-10-2017' + subuser_stats.aggregated_by = 'day' + subuser_stats._sort_by_direction = 'asc' + subuser_stats._limit = 100 + subuser_stats._offset = 2 + + self.assertEqual( + json.dumps( + subuser_stats.get(), + sort_keys=True), + '{"aggregated_by": "day", "end_date": "12-10-2017", ' + '"limit": 100, "offset": 2, "sort_by_direction": "asc", ' + '"start_date": "12-09-2017", "subusers": ["foo", "bar"]}' + ) From 760c5f105a76ebf4eeac32f002e5f4b68bf183e0 Mon Sep 17 00:00:00 2001 From: Aaron Mak Kang Sheng Date: Fri, 27 Oct 2017 08:43:37 +0800 Subject: [PATCH 11/52] Add more tests --- test/test_stats.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/test_stats.py b/test/test_stats.py index eafd131db..7da54b2e2 100644 --- a/test/test_stats.py +++ b/test/test_stats.py @@ -45,6 +45,7 @@ def test_Stats(self): def test_categoryStats(self): category_stats = CategoryStats(start_date='12-09-2017', categories=['foo', 'bar']) + category_stats.add_category(Category('woo')) category_stats.end_date = '12-10-2017' category_stats.aggregated_by = 'day' category_stats._sort_by_direction = 'asc' @@ -55,7 +56,7 @@ def test_categoryStats(self): json.dumps( category_stats.get(), sort_keys=True), - '{"aggregated_by": "day", "categories": ["foo", "bar"], ' + '{"aggregated_by": "day", "categories": ["foo", "bar", "woo"], ' '"end_date": "12-10-2017", "limit": 100, "offset": 2, ' '"sort_by_direction": "asc", "start_date": "12-09-2017"}' ) @@ -63,6 +64,7 @@ def test_categoryStats(self): def test_subuserStats(self): subuser_stats = SubuserStats(start_date = '12-09-2017', subusers=['foo', 'bar']) + subuser_stats.add_subuser(Subuser('blah')) subuser_stats.end_date = '12-10-2017' subuser_stats.aggregated_by = 'day' subuser_stats._sort_by_direction = 'asc' @@ -75,5 +77,5 @@ def test_subuserStats(self): sort_keys=True), '{"aggregated_by": "day", "end_date": "12-10-2017", ' '"limit": 100, "offset": 2, "sort_by_direction": "asc", ' - '"start_date": "12-09-2017", "subusers": ["foo", "bar"]}' + '"start_date": "12-09-2017", "subusers": ["foo", "bar", "blah"]}' ) From b12cb7bd464405598f9b43b1d43d418b38b1228e Mon Sep 17 00:00:00 2001 From: Aaron Mak Kang Sheng Date: Fri, 27 Oct 2017 22:47:18 +0800 Subject: [PATCH 12/52] add more tests --- test/test_stats.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/test/test_stats.py b/test/test_stats.py index 7da54b2e2..c71117397 100644 --- a/test/test_stats.py +++ b/test/test_stats.py @@ -30,6 +30,7 @@ def test_Stats(self): all_stats.end_date = '12-10-2017' all_stats.aggregated_by = 'day' all_stats._sort_by_direction = 'asc' + all_stats.sort_by_metric = 'clicks' all_stats._limit = 100 all_stats._offset = 2 @@ -39,7 +40,7 @@ def test_Stats(self): sort_keys=True), '{"aggregated_by": "day", "end_date": "12-10-2017", ' '"limit": 100, "offset": 2, "sort_by_direction": "asc", ' - '"start_date": "12-09-2017"}' + '"sort_by_metric": "clicks", "start_date": "12-09-2017"}' ) def test_categoryStats(self): @@ -49,6 +50,7 @@ def test_categoryStats(self): category_stats.end_date = '12-10-2017' category_stats.aggregated_by = 'day' category_stats._sort_by_direction = 'asc' + category_stats.sort_by_metric = 'clicks' category_stats._limit = 100 category_stats._offset = 2 @@ -58,7 +60,8 @@ def test_categoryStats(self): sort_keys=True), '{"aggregated_by": "day", "categories": ["foo", "bar", "woo"], ' '"end_date": "12-10-2017", "limit": 100, "offset": 2, ' - '"sort_by_direction": "asc", "start_date": "12-09-2017"}' + '"sort_by_direction": "asc", "sort_by_metric": "clicks", ' + '"start_date": "12-09-2017"}' ) def test_subuserStats(self): @@ -68,6 +71,7 @@ def test_subuserStats(self): subuser_stats.end_date = '12-10-2017' subuser_stats.aggregated_by = 'day' subuser_stats._sort_by_direction = 'asc' + subuser_stats.sort_by_metric = 'clicks' subuser_stats._limit = 100 subuser_stats._offset = 2 @@ -77,5 +81,6 @@ def test_subuserStats(self): sort_keys=True), '{"aggregated_by": "day", "end_date": "12-10-2017", ' '"limit": 100, "offset": 2, "sort_by_direction": "asc", ' - '"start_date": "12-09-2017", "subusers": ["foo", "bar", "blah"]}' + '"sort_by_metric": "clicks", "start_date": "12-09-2017", ' + '"subusers": ["foo", "bar", "blah"]}' ) From 1b9f336880b683e9c571185c8ba40da46d2c83d5 Mon Sep 17 00:00:00 2001 From: navinpai Date: Sun, 29 Oct 2017 00:28:24 +0530 Subject: [PATCH 13/52] Add slack event integration --- USE_CASES.md | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/USE_CASES.md b/USE_CASES.md index 810d345ca..b59981e19 100644 --- a/USE_CASES.md +++ b/USE_CASES.md @@ -7,6 +7,7 @@ This documentation provides examples for specific use cases. Please [open an iss * [How to Setup a Domain Whitelabel](#domain_whitelabel) * [How to View Email Statistics](#email_stats) * [Asynchronous Mail Send](#asynchronous-mail-send) +* [Slack Event API Integration](#slack_event_integration) # Transactional Templates @@ -272,3 +273,50 @@ if __name__ == "__main__": loop.run_until_complete(task) ``` + +# Integrate with Slack Events API + +It's fairly straightforward to integrate Sendgrid with Slack, to allow emails to be triggered by events happening on Slack. + +For this, we make use of the [Official Slack Events API](https://github.com/slackapi/python-slack-events-api), which can be installed using pip. + +To allow our application to get notifications of slack events, we first create a Slack App with Event Subscriptions as described [here](https://github.com/slackapi/python-slack-events-api#--development-workflow) + +Then, we set `SENDGRID_API_KEY` _(which you can create on the Sendgrid dashboard)_ and `SLACK_VERIFICATION_TOKEN` _(which you can get in the App Credentials section of the Slack App)_ as environment variables. + +Once this is done, we can subscribe to [events on Slack](https://api.slack.com/events) and trigger emails when an event occurs. In the example below, we trigger an email to `test@example.com` whenever someone posts a message on Slack that has the word "_help_" in it. + +``` +from slackeventsapi import SlackEventAdapter +from slackclient import SlackClient +import os +import sendgrid +from sendgrid.helpers.mail import * + +sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) + +SLACK_VERIFICATION_TOKEN = os.environ["SLACK_VERIFICATION_TOKEN"] +slack_events_adapter = SlackEventAdapter(SLACK_VERIFICATION_TOKEN, "/slack/events") + +@slack_events_adapter.on("message") +def handle_message(event_data): + message = event_data["event"] + # If the incoming message contains "help", then send an email using SendGrid + if message.get("subtype") is None and "help" in message.get('text').lower(): + message = "Someone needs your help: \n\n %s" % message["text"] + r = send_email(message) + print(r) + + +def send_email(message): + from_email = Email("slack_integration@example.com") + to_email = Email("test@example.com") + subject = "Psst... Someone needs help!" + content = Content("text/plain", message) + mail = Mail(from_email, subject, to_email, content) + response = sg.client.mail.send.post(request_body=mail.get()) + return response.status_code + +# Start the slack event listener server on port 3000 +slack_events_adapter.start(port=3000) +``` From 57c30401fab8c8883fd5bd5a7e6d9de7ff1bbcf8 Mon Sep 17 00:00:00 2001 From: heisendumb Date: Mon, 30 Oct 2017 00:26:08 -0200 Subject: [PATCH 14/52] docker-compose for issue #444 --- docker/Dockerfile | 4 +- docker/Makefile | 28 ++ docker/docker-compose.yml | 24 ++ .../examples/accesssettings/accesssettings.py | 84 ++++ docker/examples/alerts/alerts.py | 63 +++ docker/examples/apikeys/apikeys.py | 85 ++++ docker/examples/asm/asm.py | 174 ++++++++ docker/examples/browsers/browsers.py | 17 + docker/examples/campaigns/campaigns.py | 154 +++++++ docker/examples/categories/categories.py | 37 ++ docker/examples/clients/clients.py | 28 ++ docker/examples/contactdb/contactdb.py | 396 ++++++++++++++++++ docker/examples/devices/devices.py | 17 + docker/examples/geo/geo.py | 17 + docker/examples/helpers/mail/mail_example.py | 219 ++++++++++ docker/examples/ips/ips.py | 155 +++++++ docker/examples/mail/mail.py | 174 ++++++++ .../mailboxproviders/mailboxproviders.py | 17 + docker/examples/mailsettings/mailsettings.py | 220 ++++++++++ .../partnersettings/partnersettings.py | 40 ++ docker/examples/scopes/scopes.py | 16 + docker/examples/senders/senders.py | 99 +++++ docker/examples/stats/stats.py | 17 + docker/examples/subusers/subusers.py | 170 ++++++++ docker/examples/suppression/suppression.py | 202 +++++++++ .../trackingsettings/trackingsettings.py | 111 +++++ docker/examples/user/user.py | 294 +++++++++++++ docker/examples/whitelabel/whitelabel.py | 311 ++++++++++++++ 28 files changed, 3171 insertions(+), 2 deletions(-) create mode 100644 docker/Makefile create mode 100644 docker/docker-compose.yml create mode 100644 docker/examples/accesssettings/accesssettings.py create mode 100644 docker/examples/alerts/alerts.py create mode 100644 docker/examples/apikeys/apikeys.py create mode 100644 docker/examples/asm/asm.py create mode 100644 docker/examples/browsers/browsers.py create mode 100644 docker/examples/campaigns/campaigns.py create mode 100644 docker/examples/categories/categories.py create mode 100644 docker/examples/clients/clients.py create mode 100644 docker/examples/contactdb/contactdb.py create mode 100644 docker/examples/devices/devices.py create mode 100644 docker/examples/geo/geo.py create mode 100644 docker/examples/helpers/mail/mail_example.py create mode 100644 docker/examples/ips/ips.py create mode 100644 docker/examples/mail/mail.py create mode 100644 docker/examples/mailboxproviders/mailboxproviders.py create mode 100644 docker/examples/mailsettings/mailsettings.py create mode 100644 docker/examples/partnersettings/partnersettings.py create mode 100644 docker/examples/scopes/scopes.py create mode 100644 docker/examples/senders/senders.py create mode 100644 docker/examples/stats/stats.py create mode 100644 docker/examples/subusers/subusers.py create mode 100644 docker/examples/suppression/suppression.py create mode 100644 docker/examples/trackingsettings/trackingsettings.py create mode 100644 docker/examples/user/user.py create mode 100644 docker/examples/whitelabel/whitelabel.py diff --git a/docker/Dockerfile b/docker/Dockerfile index bc4ce8e79..393d91bfe 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -30,8 +30,8 @@ RUN python2.7 get-pip.py && \ # set up default sendgrid env WORKDIR /root/sources -RUN git clone https://github.com/sendgrid/sendgrid-python.git && \ - git clone https://github.com/sendgrid/python-http-client.git +RUN git clone https://github.com/sendgrid/sendgrid-python.git --branch && \ + git clone https://github.com/sendgrid/python-http-client.git --branch WORKDIR /root RUN ln -s /root/sources/sendgrid-python/sendgrid && \ ln -s /root/sources/python-http-client/python_http_client diff --git a/docker/Makefile b/docker/Makefile new file mode 100644 index 000000000..6ec47459d --- /dev/null +++ b/docker/Makefile @@ -0,0 +1,28 @@ +# import deploy config +deployfile=deploy.env +ifdef dpl +deployfile=$(dpl) +endif +include $(deployfile) +export $(shell sed 's/=.*//' $(deployfile)) + +stop: + docker-compose stop + +rm: stop + docker-compose stop -fvs + +clean: + docker rmi %(docker images -aq) + +clean_untagged: + docker rmi $(docker images --quiet --filter "dangling=true") 2>/dev/null + +build: + docker-compose up -d + +build-build: + docker-compose up --build -d + +up: rm clean build-build + echo "Sendgrid dev environment is alive :D" diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml new file mode 100644 index 000000000..d35772fd2 --- /dev/null +++ b/docker/docker-compose.yml @@ -0,0 +1,24 @@ +version: "3.3" + +services: + sendgrid: + image: sendgrid/sendgrid-python:${TAG} + restart: unless-stopped + container_name: sendgrid-prod + volumes: + - ${PATH_TO_SENDGRID-PYTHON_PROD}:/mnt/sendgrid-python + - ${PATH_TO_HTTP-CLIENT_PROD}:/mnt/python-http-client + env_file: .env + + sendgrid-dev: + build: + context: . + args: + - SENDGRID-PYTHON_VERSION: {SENDGRID-PYTHON_VERSION} + - HTTP-CLIENT_VERSION: {HTTP-CLIENT_VERSION} + restart: unless-stopped + container_name: sendgrid-dev + env_file: .env + volumes: + - ${PATH_TO_SENDGRID-PYTHON}:/mnt/sendgrid-python + - ${PATH_TO_HTTP-CLIENT}:/mnt/python-http-client diff --git a/docker/examples/accesssettings/accesssettings.py b/docker/examples/accesssettings/accesssettings.py new file mode 100644 index 000000000..aac0e4a54 --- /dev/null +++ b/docker/examples/accesssettings/accesssettings.py @@ -0,0 +1,84 @@ +import sendgrid +import json +import os + + +sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) + +################################################## +# Retrieve all recent access attempts # +# GET /access_settings/activity # + +params = {'limit': 1} +response = sg.client.access_settings.activity.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Add one or more IPs to the whitelist # +# POST /access_settings/whitelist # + +data = { + "ips": [ + { + "ip": "192.168.1.1" + }, + { + "ip": "192.*.*.*" + }, + { + "ip": "192.168.1.3/32" + } + ] +} +response = sg.client.access_settings.whitelist.post(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve a list of currently whitelisted IPs # +# GET /access_settings/whitelist # + +response = sg.client.access_settings.whitelist.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Remove one or more IPs from the whitelist # +# DELETE /access_settings/whitelist # + +data = { + "ids": [ + 1, + 2, + 3 + ] +} +response = sg.client.access_settings.whitelist.delete(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve a specific whitelisted IP # +# GET /access_settings/whitelist/{rule_id} # + +rule_id = "test_url_param" +response = sg.client.access_settings.whitelist._(rule_id).get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Remove a specific IP from the whitelist # +# DELETE /access_settings/whitelist/{rule_id} # + +rule_id = "test_url_param" +response = sg.client.access_settings.whitelist._(rule_id).delete() +print(response.status_code) +print(response.body) +print(response.headers) + diff --git a/docker/examples/alerts/alerts.py b/docker/examples/alerts/alerts.py new file mode 100644 index 000000000..e30d48748 --- /dev/null +++ b/docker/examples/alerts/alerts.py @@ -0,0 +1,63 @@ +import sendgrid +import json +import os + + +sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) + +################################################## +# Create a new Alert # +# POST /alerts # + +data = { + "email_to": "example@example.com", + "frequency": "daily", + "type": "stats_notification" +} +response = sg.client.alerts.post(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve all alerts # +# GET /alerts # + +response = sg.client.alerts.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Update an alert # +# PATCH /alerts/{alert_id} # + +data = { + "email_to": "example@example.com" +} +alert_id = "test_url_param" +response = sg.client.alerts._(alert_id).patch(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve a specific alert # +# GET /alerts/{alert_id} # + +alert_id = "test_url_param" +response = sg.client.alerts._(alert_id).get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Delete an alert # +# DELETE /alerts/{alert_id} # + +alert_id = "test_url_param" +response = sg.client.alerts._(alert_id).delete() +print(response.status_code) +print(response.body) +print(response.headers) + diff --git a/docker/examples/apikeys/apikeys.py b/docker/examples/apikeys/apikeys.py new file mode 100644 index 000000000..42c3afa10 --- /dev/null +++ b/docker/examples/apikeys/apikeys.py @@ -0,0 +1,85 @@ +import sendgrid +import json +import os + + +sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) + +################################################## +# Create API keys # +# POST /api_keys # + +data = { + "name": "My API Key", + "sample": "data", + "scopes": [ + "mail.send", + "alerts.create", + "alerts.read" + ] +} +response = sg.client.api_keys.post(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve all API Keys belonging to the authenticated user # +# GET /api_keys # + +params = {'limit': 1} +response = sg.client.api_keys.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Update the name & scopes of an API Key # +# PUT /api_keys/{api_key_id} # + +data = { + "name": "A New Hope", + "scopes": [ + "user.profile.read", + "user.profile.update" + ] +} +api_key_id = "test_url_param" +response = sg.client.api_keys._(api_key_id).put(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Update API keys # +# PATCH /api_keys/{api_key_id} # + +data = { + "name": "A New Hope" +} +api_key_id = "test_url_param" +response = sg.client.api_keys._(api_key_id).patch(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve an existing API Key # +# GET /api_keys/{api_key_id} # + +api_key_id = "test_url_param" +response = sg.client.api_keys._(api_key_id).get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Delete API keys # +# DELETE /api_keys/{api_key_id} # + +api_key_id = "test_url_param" +response = sg.client.api_keys._(api_key_id).delete() +print(response.status_code) +print(response.body) +print(response.headers) + diff --git a/docker/examples/asm/asm.py b/docker/examples/asm/asm.py new file mode 100644 index 000000000..43130cf06 --- /dev/null +++ b/docker/examples/asm/asm.py @@ -0,0 +1,174 @@ +import sendgrid +import json +import os + + +sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) + +################################################## +# Create a new suppression group # +# POST /asm/groups # + +data = { + "description": "Suggestions for products our users might like.", + "is_default": True, + "name": "Product Suggestions" +} +response = sg.client.asm.groups.post(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve information about multiple suppression groups # +# GET /asm/groups # + +params = {'id': 1} +response = sg.client.asm.groups.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Update a suppression group. # +# PATCH /asm/groups/{group_id} # + +data = { + "description": "Suggestions for items our users might like.", + "id": 103, + "name": "Item Suggestions" +} +group_id = "test_url_param" +response = sg.client.asm.groups._(group_id).patch(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Get information on a single suppression group. # +# GET /asm/groups/{group_id} # + +group_id = "test_url_param" +response = sg.client.asm.groups._(group_id).get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Delete a suppression group. # +# DELETE /asm/groups/{group_id} # + +group_id = "test_url_param" +response = sg.client.asm.groups._(group_id).delete() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Add suppressions to a suppression group # +# POST /asm/groups/{group_id}/suppressions # + +data = { + "recipient_emails": [ + "test1@example.com", + "test2@example.com" + ] +} +group_id = "test_url_param" +response = sg.client.asm.groups._(group_id).suppressions.post(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve all suppressions for a suppression group # +# GET /asm/groups/{group_id}/suppressions # + +group_id = "test_url_param" +response = sg.client.asm.groups._(group_id).suppressions.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Search for suppressions within a group # +# POST /asm/groups/{group_id}/suppressions/search # + +data = { + "recipient_emails": [ + "exists1@example.com", + "exists2@example.com", + "doesnotexists@example.com" + ] +} +group_id = "test_url_param" +response = sg.client.asm.groups._(group_id).suppressions.search.post(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Delete a suppression from a suppression group # +# DELETE /asm/groups/{group_id}/suppressions/{email} # + +group_id = "test_url_param" +email = "test_url_param" +response = sg.client.asm.groups._(group_id).suppressions._(email).delete() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve all suppressions # +# GET /asm/suppressions # + +response = sg.client.asm.suppressions.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Add recipient addresses to the global suppression group. # +# POST /asm/suppressions/global # + +data = { + "recipient_emails": [ + "test1@example.com", + "test2@example.com" + ] +} +response = sg.client.asm.suppressions._("global").post(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve a Global Suppression # +# GET /asm/suppressions/global/{email} # + +email = "test_url_param" +response = sg.client.asm.suppressions._("global")._(email).get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Delete a Global Suppression # +# DELETE /asm/suppressions/global/{email} # + +email = "test_url_param" +response = sg.client.asm.suppressions._("global")._(email).delete() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve all suppression groups for an email address # +# GET /asm/suppressions/{email} # + +email = "test_url_param" +response = sg.client.asm.suppressions._(email).get() +print(response.status_code) +print(response.body) +print(response.headers) + diff --git a/docker/examples/browsers/browsers.py b/docker/examples/browsers/browsers.py new file mode 100644 index 000000000..c123c12e5 --- /dev/null +++ b/docker/examples/browsers/browsers.py @@ -0,0 +1,17 @@ +import sendgrid +import json +import os + + +sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) + +################################################## +# Retrieve email statistics by browser. # +# GET /browsers/stats # + +params = {'end_date': '2016-04-01', 'aggregated_by': 'day', 'browsers': 'test_string', 'limit': 'test_string', 'offset': 'test_string', 'start_date': '2016-01-01'} +response = sg.client.browsers.stats.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + diff --git a/docker/examples/campaigns/campaigns.py b/docker/examples/campaigns/campaigns.py new file mode 100644 index 000000000..c77fc878b --- /dev/null +++ b/docker/examples/campaigns/campaigns.py @@ -0,0 +1,154 @@ +import sendgrid +import json +import os + + +sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) + +################################################## +# Create a Campaign # +# POST /campaigns # + +data = { + "categories": [ + "spring line" + ], + "custom_unsubscribe_url": "", + "html_content": "

Check out our spring line!

", + "ip_pool": "marketing", + "list_ids": [ + 110, + 124 + ], + "plain_content": "Check out our spring line!", + "segment_ids": [ + 110 + ], + "sender_id": 124451, + "subject": "New Products for Spring!", + "suppression_group_id": 42, + "title": "March Newsletter" +} +response = sg.client.campaigns.post(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve all Campaigns # +# GET /campaigns # + +params = {'limit': 1, 'offset': 1} +response = sg.client.campaigns.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Update a Campaign # +# PATCH /campaigns/{campaign_id} # + +data = { + "categories": [ + "summer line" + ], + "html_content": "

Check out our summer line!

", + "plain_content": "Check out our summer line!", + "subject": "New Products for Summer!", + "title": "May Newsletter" +} +campaign_id = "test_url_param" +response = sg.client.campaigns._(campaign_id).patch(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve a single campaign # +# GET /campaigns/{campaign_id} # + +campaign_id = "test_url_param" +response = sg.client.campaigns._(campaign_id).get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Delete a Campaign # +# DELETE /campaigns/{campaign_id} # + +campaign_id = "test_url_param" +response = sg.client.campaigns._(campaign_id).delete() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Update a Scheduled Campaign # +# PATCH /campaigns/{campaign_id}/schedules # + +data = { + "send_at": 1489451436 +} +campaign_id = "test_url_param" +response = sg.client.campaigns._(campaign_id).schedules.patch(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Schedule a Campaign # +# POST /campaigns/{campaign_id}/schedules # + +data = { + "send_at": 1489771528 +} +campaign_id = "test_url_param" +response = sg.client.campaigns._(campaign_id).schedules.post(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# View Scheduled Time of a Campaign # +# GET /campaigns/{campaign_id}/schedules # + +campaign_id = "test_url_param" +response = sg.client.campaigns._(campaign_id).schedules.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Unschedule a Scheduled Campaign # +# DELETE /campaigns/{campaign_id}/schedules # + +campaign_id = "test_url_param" +response = sg.client.campaigns._(campaign_id).schedules.delete() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Send a Campaign # +# POST /campaigns/{campaign_id}/schedules/now # + +campaign_id = "test_url_param" +response = sg.client.campaigns._(campaign_id).schedules.now.post() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Send a Test Campaign # +# POST /campaigns/{campaign_id}/schedules/test # + +data = { + "to": "your.email@example.com" +} +campaign_id = "test_url_param" +response = sg.client.campaigns._(campaign_id).schedules.test.post(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + diff --git a/docker/examples/categories/categories.py b/docker/examples/categories/categories.py new file mode 100644 index 000000000..7984f0fe0 --- /dev/null +++ b/docker/examples/categories/categories.py @@ -0,0 +1,37 @@ +import sendgrid +import json +import os + + +sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) + +################################################## +# Retrieve all categories # +# GET /categories # + +params = {'category': 'test_string', 'limit': 1, 'offset': 1} +response = sg.client.categories.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve Email Statistics for Categories # +# GET /categories/stats # + +params = {'end_date': '2016-04-01', 'aggregated_by': 'day', 'limit': 1, 'offset': 1, 'start_date': '2016-01-01', 'categories': 'test_string'} +response = sg.client.categories.stats.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve sums of email stats for each category [Needs: Stats object defined, has category ID?] # +# GET /categories/stats/sums # + +params = {'end_date': '2016-04-01', 'aggregated_by': 'day', 'limit': 1, 'sort_by_metric': 'test_string', 'offset': 1, 'start_date': '2016-01-01', 'sort_by_direction': 'asc'} +response = sg.client.categories.stats.sums.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + diff --git a/docker/examples/clients/clients.py b/docker/examples/clients/clients.py new file mode 100644 index 000000000..7831ef78f --- /dev/null +++ b/docker/examples/clients/clients.py @@ -0,0 +1,28 @@ +import sendgrid +import json +import os + + +sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) + +################################################## +# Retrieve email statistics by client type. # +# GET /clients/stats # + +params = {'aggregated_by': 'day', 'start_date': '2016-01-01', 'end_date': '2016-04-01'} +response = sg.client.clients.stats.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve stats by a specific client type. # +# GET /clients/{client_type}/stats # + +params = {'aggregated_by': 'day', 'start_date': '2016-01-01', 'end_date': '2016-04-01'} +client_type = "test_url_param" +response = sg.client.clients._(client_type).stats.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + diff --git a/docker/examples/contactdb/contactdb.py b/docker/examples/contactdb/contactdb.py new file mode 100644 index 000000000..c234d7724 --- /dev/null +++ b/docker/examples/contactdb/contactdb.py @@ -0,0 +1,396 @@ +import sendgrid +import json +import os + + +sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) + +################################################## +# Create a Custom Field # +# POST /contactdb/custom_fields # + +data = { + "name": "pet", + "type": "text" +} +response = sg.client.contactdb.custom_fields.post(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve all custom fields # +# GET /contactdb/custom_fields # + +response = sg.client.contactdb.custom_fields.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve a Custom Field # +# GET /contactdb/custom_fields/{custom_field_id} # + +custom_field_id = "test_url_param" +response = sg.client.contactdb.custom_fields._(custom_field_id).get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Delete a Custom Field # +# DELETE /contactdb/custom_fields/{custom_field_id} # + +custom_field_id = "test_url_param" +response = sg.client.contactdb.custom_fields._(custom_field_id).delete() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Create a List # +# POST /contactdb/lists # + +data = { + "name": "your list name" +} +response = sg.client.contactdb.lists.post(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve all lists # +# GET /contactdb/lists # + +response = sg.client.contactdb.lists.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Delete Multiple lists # +# DELETE /contactdb/lists # + +data = [ + 1, + 2, + 3, + 4 +] +response = sg.client.contactdb.lists.delete(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Update a List # +# PATCH /contactdb/lists/{list_id} # + +data = { + "name": "newlistname" +} +params = {'list_id': 1} +list_id = "test_url_param" +response = sg.client.contactdb.lists._(list_id).patch(request_body=data, query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve a single list # +# GET /contactdb/lists/{list_id} # + +params = {'list_id': 1} +list_id = "test_url_param" +response = sg.client.contactdb.lists._(list_id).get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Delete a List # +# DELETE /contactdb/lists/{list_id} # + +params = {'delete_contacts': 'true'} +list_id = "test_url_param" +response = sg.client.contactdb.lists._(list_id).delete(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Add Multiple Recipients to a List # +# POST /contactdb/lists/{list_id}/recipients # + +data = [ + "recipient_id1", + "recipient_id2" +] +list_id = "test_url_param" +response = sg.client.contactdb.lists._(list_id).recipients.post(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve all recipients on a List # +# GET /contactdb/lists/{list_id}/recipients # + +params = {'page': 1, 'page_size': 1} +list_id = "test_url_param" +response = sg.client.contactdb.lists._(list_id).recipients.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Add a Single Recipient to a List # +# POST /contactdb/lists/{list_id}/recipients/{recipient_id} # + +list_id = "test_url_param" +recipient_id = "test_url_param" +response = sg.client.contactdb.lists._(list_id).recipients._(recipient_id).post() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Delete a Single Recipient from a Single List # +# DELETE /contactdb/lists/{list_id}/recipients/{recipient_id} # + +params = {'recipient_id': 1, 'list_id': 1} +list_id = "test_url_param" +recipient_id = "test_url_param" +response = sg.client.contactdb.lists._(list_id).recipients._(recipient_id).delete(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Update Recipient # +# PATCH /contactdb/recipients # + +data = [ + { + "email": "jones@example.com", + "first_name": "Guy", + "last_name": "Jones" + } +] +response = sg.client.contactdb.recipients.patch(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Add recipients # +# POST /contactdb/recipients # + +data = [ + { + "age": 25, + "email": "example@example.com", + "first_name": "", + "last_name": "User" + }, + { + "age": 25, + "email": "example2@example.com", + "first_name": "Example", + "last_name": "User" + } +] +response = sg.client.contactdb.recipients.post(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve recipients # +# GET /contactdb/recipients # + +params = {'page': 1, 'page_size': 1} +response = sg.client.contactdb.recipients.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Delete Recipient # +# DELETE /contactdb/recipients # + +data = [ + "recipient_id1", + "recipient_id2" +] +response = sg.client.contactdb.recipients.delete(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve the count of billable recipients # +# GET /contactdb/recipients/billable_count # + +response = sg.client.contactdb.recipients.billable_count.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve a Count of Recipients # +# GET /contactdb/recipients/count # + +response = sg.client.contactdb.recipients.count.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve recipients matching search criteria # +# GET /contactdb/recipients/search # + +params = {'{field_name}': 'test_string'} +response = sg.client.contactdb.recipients.search.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve a single recipient # +# GET /contactdb/recipients/{recipient_id} # + +recipient_id = "test_url_param" +response = sg.client.contactdb.recipients._(recipient_id).get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Delete a Recipient # +# DELETE /contactdb/recipients/{recipient_id} # + +recipient_id = "test_url_param" +response = sg.client.contactdb.recipients._(recipient_id).delete() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve the lists that a recipient is on # +# GET /contactdb/recipients/{recipient_id}/lists # + +recipient_id = "test_url_param" +response = sg.client.contactdb.recipients._(recipient_id).lists.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve reserved fields # +# GET /contactdb/reserved_fields # + +response = sg.client.contactdb.reserved_fields.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Create a Segment # +# POST /contactdb/segments # + +data = { + "conditions": [ + { + "and_or": "", + "field": "last_name", + "operator": "eq", + "value": "Miller" + }, + { + "and_or": "and", + "field": "last_clicked", + "operator": "gt", + "value": "01/02/2015" + }, + { + "and_or": "or", + "field": "clicks.campaign_identifier", + "operator": "eq", + "value": "513" + } + ], + "list_id": 4, + "name": "Last Name Miller" +} +response = sg.client.contactdb.segments.post(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve all segments # +# GET /contactdb/segments # + +response = sg.client.contactdb.segments.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Update a segment # +# PATCH /contactdb/segments/{segment_id} # + +data = { + "conditions": [ + { + "and_or": "", + "field": "last_name", + "operator": "eq", + "value": "Miller" + } + ], + "list_id": 5, + "name": "The Millers" +} +params = {'segment_id': 'test_string'} +segment_id = "test_url_param" +response = sg.client.contactdb.segments._(segment_id).patch(request_body=data, query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve a segment # +# GET /contactdb/segments/{segment_id} # + +params = {'segment_id': 1} +segment_id = "test_url_param" +response = sg.client.contactdb.segments._(segment_id).get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Delete a segment # +# DELETE /contactdb/segments/{segment_id} # + +params = {'delete_contacts': 'true'} +segment_id = "test_url_param" +response = sg.client.contactdb.segments._(segment_id).delete(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve recipients on a segment # +# GET /contactdb/segments/{segment_id}/recipients # + +params = {'page': 1, 'page_size': 1} +segment_id = "test_url_param" +response = sg.client.contactdb.segments._(segment_id).recipients.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + diff --git a/docker/examples/devices/devices.py b/docker/examples/devices/devices.py new file mode 100644 index 000000000..108e98452 --- /dev/null +++ b/docker/examples/devices/devices.py @@ -0,0 +1,17 @@ +import sendgrid +import json +import os + + +sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) + +################################################## +# Retrieve email statistics by device type. # +# GET /devices/stats # + +params = {'aggregated_by': 'day', 'limit': 1, 'start_date': '2016-01-01', 'end_date': '2016-04-01', 'offset': 1} +response = sg.client.devices.stats.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + diff --git a/docker/examples/geo/geo.py b/docker/examples/geo/geo.py new file mode 100644 index 000000000..7d58ec085 --- /dev/null +++ b/docker/examples/geo/geo.py @@ -0,0 +1,17 @@ +import sendgrid +import json +import os + + +sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) + +################################################## +# Retrieve email statistics by country and state/province. # +# GET /geo/stats # + +params = {'end_date': '2016-04-01', 'country': 'US', 'aggregated_by': 'day', 'limit': 1, 'offset': 1, 'start_date': '2016-01-01'} +response = sg.client.geo.stats.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + diff --git a/docker/examples/helpers/mail/mail_example.py b/docker/examples/helpers/mail/mail_example.py new file mode 100644 index 000000000..bfd8ea718 --- /dev/null +++ b/docker/examples/helpers/mail/mail_example.py @@ -0,0 +1,219 @@ +import json +import os +import urllib2 +from sendgrid.helpers.mail import * +from sendgrid import * + +# NOTE: you will need move this file to the root +# directory of this project to execute properly. + + +def build_hello_email(): + """Minimum required to send an email""" + from_email = Email("test@example.com") + subject = "Hello World from the SendGrid Python Library" + to_email = Email("test@example.com") + content = Content("text/plain", "some text here") + mail = Mail(from_email, subject, to_email, content) + mail.personalizations[0].add_to(Email("test2@example.com")) + + return mail.get() + + +def build_personalization(personalization): + """Build personalization mock instance from a mock dict""" + mock_personalization = Personalization() + for to_addr in personalization['to_list']: + personalization.add_to(to_addr) + + for cc_addr in personalization['cc_list']: + personalization.add_to(cc_addr) + + for bcc_addr in personalization['bcc_list']: + personalization.add_bc(bcc_addr) + + for header in personalization['headers']: + personalization.add_header(header) + + for substitution in personalization['substitutions']: + personalization.add_substitution(substitution) + + for arg in personalization['custom_args']: + personalization.add_custom_arg(arg) + + personalization.subject = personalization['subject'] + personalization.send_at = personalization['send_at'] + return mock_personalization + + +def get_mock_personalization_dict(): + """Get a dict of personalization mock.""" + mock_pers = dict() + + mock_pers['to_list'] = [Email("test1@example.com", + "Example User"), + Email("test2@example.com", + "Example User")] + + mock_pers['cc_list'] = [Email("test3@example.com", + "Example User"), + Email("test4@example.com", + "Example User")] + + mock_pers['bcc_list'] = [Email("test5@example.com"), + Email("test6@example.com")] + + mock_pers['subject'] = ("Hello World from the Personalized " + "SendGrid Python Library") + + mock_pers['headers'] = [Header("X-Test", "test"), + Header("X-Mock", "true")] + + mock_pers['substitutions'] = [Substitution("%name%", "Example User"), + Substitution("%city%", "Denver")] + + mock_pers['custom_args'] = [CustomArg("user_id", "343"), + CustomArg("type", "marketing")] + + mock_pers['send_at'] = 1443636843 + return mock_pers + + +def build_attachment1(): + """Build attachment mock.""" + attachment = Attachment() + attachment.content = ("TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNl" + "Y3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12") + attachment.type = "application/pdf" + attachment.filename = "balance_001.pdf" + attachment.disposition = "attachment" + attachment.content_id = "Balance Sheet" + return attachment + + +def build_attachment2(): + """Build attachment mock.""" + attachment = Attachment() + attachment.content = "BwdW" + attachment.type = "image/png" + attachment.filename = "banner.png" + attachment.disposition = "inline" + attachment.content_id = "Banner" + return attachment + + +def build_mail_settings(): + """Build mail settings mock.""" + mail_settings = MailSettings() + mail_settings.bcc_settings = BCCSettings(True, Email("test@example.com")) + mail_settings.bypass_list_management = BypassListManagement(True) + mail_settings.footer_settings = FooterSettings(True, "Footer Text", + ("Footer " + "Text")) + mail_settings.sandbox_mode = SandBoxMode(True) + mail_settings.spam_check = SpamCheck(True, 1, + "https://spamcatcher.sendgrid.com") + return mail_settings + + +def build_tracking_settings(): + """Build tracking settings mock.""" + tracking_settings = TrackingSettings() + tracking_settings.click_tracking = ClickTracking(True, True) + tracking_settings.open_tracking = OpenTracking(True, + ("Optional tag to " + "replace with the" + "open image in the " + "body of the message")) + + subs_track = SubscriptionTracking(True, + ("text to insert into the " + "text/plain portion of the" + " message"), + ("html to insert " + "into the text/html portion of " + "the message"), + ("Optional tag to replace with " + "the open image in the body of " + "the message")) + + tracking_settings.subscription_tracking = subs_track + tracking_settings.ganalytics = Ganalytics(True, "some source", + "some medium", "some term", + "some_content", "some_campaign") + return tracking_settings + + +def build_kitchen_sink(): + """All settings set""" + mail = Mail() + + mail.from_email = Email("test@example.com", "Example User") + mail.subject = "Hello World from the SendGrid Python Library" + + personalization = get_mock_personalization_dict() + mail.add_personalization(build_personalization(personalization)) + mail.add_personalization(build_personalization(personalization)) + + mail.add_content(Content("text/plain", "some text here")) + mail.add_content(Content("text/html", ("some text " + "here"))) + + mail.add_attachment(build_attachment1()) + mail.add_attachment(build_attachment2()) + + mail.template_id = "13b8f94f-bcae-4ec6-b752-70d6cb59f932" + + mail.add_section(Section("%section1%", "Substitution Text for Section 1")) + mail.add_section(Section("%section2%", "Substitution Text for Section 2")) + + mail.add_header(Header("X-Test1", "test1")) + mail.add_header(Header("X-Test3", "test2")) + + mail.add_category(Category("May")) + mail.add_category(Category("2016")) + + mail.add_custom_arg(CustomArg("campaign", "welcome")) + mail.add_custom_arg(CustomArg("weekday", "morning")) + + mail.send_at = 1443636842 + + # This must be a valid [batch ID] + # (https://sendgrid.com/docs/API_Reference/SMTP_API/scheduling_parameters.html) to work + # mail.set_batch_id("N2VkYjBjYWItMGU4OC0xMWU2LWJhMzYtZjQ1Yzg5OTBkNzkxLWM5ZTUyZjNhOA") + mail.asm = ASM(99, [4, 5, 6, 7, 8]) + mail.ip_pool_name = "24" + mail.mail_settings = build_mail_settings() + mail.tracking_settings = build_tracking_settings() + mail.reply_to = Email("test@example.com") + + return mail.get() + + +def send_hello_email(): + # Assumes you set your environment variable: + # https://github.com/sendgrid/sendgrid-python/blob/master/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key + sg = SendGridAPIClient() + data = build_hello_email() + response = sg.client.mail.send.post(request_body=data) + print(response.status_code) + print(response.headers) + print(response.body) + + +def send_kitchen_sink(): + # Assumes you set your environment variable: + # https://github.com/sendgrid/sendgrid-python/blob/master/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key + sg = SendGridAPIClient() + data = build_kitchen_sink() + response = sg.client.mail.send.post(request_body=data) + print(response.status_code) + print(response.headers) + print(response.body) + + +# this will actually send an email +send_hello_email() + +# this will only send an email if you set SandBox Mode to False +send_kitchen_sink() diff --git a/docker/examples/ips/ips.py b/docker/examples/ips/ips.py new file mode 100644 index 000000000..6c48ae306 --- /dev/null +++ b/docker/examples/ips/ips.py @@ -0,0 +1,155 @@ +import sendgrid +import json +import os + + +sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) + +################################################## +# Retrieve all IP addresses # +# GET /ips # + +params = {'subuser': 'test_string', 'ip': 'test_string', 'limit': 1, 'exclude_whitelabels': 'true', 'offset': 1} +response = sg.client.ips.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve all assigned IPs # +# GET /ips/assigned # + +response = sg.client.ips.assigned.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Create an IP pool. # +# POST /ips/pools # + +data = { + "name": "marketing" +} +response = sg.client.ips.pools.post(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve all IP pools. # +# GET /ips/pools # + +response = sg.client.ips.pools.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Update an IP pools name. # +# PUT /ips/pools/{pool_name} # + +data = { + "name": "new_pool_name" +} +pool_name = "test_url_param" +response = sg.client.ips.pools._(pool_name).put(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve all IPs in a specified pool. # +# GET /ips/pools/{pool_name} # + +pool_name = "test_url_param" +response = sg.client.ips.pools._(pool_name).get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Delete an IP pool. # +# DELETE /ips/pools/{pool_name} # + +pool_name = "test_url_param" +response = sg.client.ips.pools._(pool_name).delete() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Add an IP address to a pool # +# POST /ips/pools/{pool_name}/ips # + +data = { + "ip": "0.0.0.0" +} +pool_name = "test_url_param" +response = sg.client.ips.pools._(pool_name).ips.post(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Remove an IP address from a pool. # +# DELETE /ips/pools/{pool_name}/ips/{ip} # + +pool_name = "test_url_param" +ip = "test_url_param" +response = sg.client.ips.pools._(pool_name).ips._(ip).delete() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Add an IP to warmup # +# POST /ips/warmup # + +data = { + "ip": "0.0.0.0" +} +response = sg.client.ips.warmup.post(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve all IPs currently in warmup # +# GET /ips/warmup # + +response = sg.client.ips.warmup.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve warmup status for a specific IP address # +# GET /ips/warmup/{ip_address} # + +ip_address = "test_url_param" +response = sg.client.ips.warmup._(ip_address).get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Remove an IP from warmup # +# DELETE /ips/warmup/{ip_address} # + +ip_address = "test_url_param" +response = sg.client.ips.warmup._(ip_address).delete() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve all IP pools an IP address belongs to # +# GET /ips/{ip_address} # + +ip_address = "test_url_param" +response = sg.client.ips._(ip_address).get() +print(response.status_code) +print(response.body) +print(response.headers) + diff --git a/docker/examples/mail/mail.py b/docker/examples/mail/mail.py new file mode 100644 index 000000000..fef420e87 --- /dev/null +++ b/docker/examples/mail/mail.py @@ -0,0 +1,174 @@ +import sendgrid +import json +import os + + +sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) + +################################################## +# Create a batch ID # +# POST /mail/batch # + +response = sg.client.mail.batch.post() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Validate batch ID # +# GET /mail/batch/{batch_id} # + +batch_id = "test_url_param" +response = sg.client.mail.batch._(batch_id).get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# v3 Mail Send # +# POST /mail/send # +# This endpoint has a helper, check it out [here](https://github.com/sendgrid/sendgrid-python/blob/master/sendgrid/helpers/mail/README.md). + +data = { + "asm": { + "group_id": 1, + "groups_to_display": [ + 1, + 2, + 3 + ] + }, + "attachments": [ + { + "content": "[BASE64 encoded content block here]", + "content_id": "ii_139db99fdb5c3704", + "disposition": "inline", + "filename": "file1.jpg", + "name": "file1", + "type": "jpg" + } + ], + "batch_id": "[YOUR BATCH ID GOES HERE]", + "categories": [ + "category1", + "category2" + ], + "content": [ + { + "type": "text/html", + "value": "

Hello, world!

" + } + ], + "custom_args": { + "New Argument 1": "New Value 1", + "activationAttempt": "1", + "customerAccountNumber": "[CUSTOMER ACCOUNT NUMBER GOES HERE]" + }, + "from": { + "email": "sam.smith@example.com", + "name": "Sam Smith" + }, + "headers": {}, + "ip_pool_name": "[YOUR POOL NAME GOES HERE]", + "mail_settings": { + "bcc": { + "email": "ben.doe@example.com", + "enable": True + }, + "bypass_list_management": { + "enable": True + }, + "footer": { + "enable": True, + "html": "

Thanks
The SendGrid Team

", + "text": "Thanks,/n The SendGrid Team" + }, + "sandbox_mode": { + "enable": False + }, + "spam_check": { + "enable": True, + "post_to_url": "http://example.com/compliance", + "threshold": 3 + } + }, + "personalizations": [ + { + "bcc": [ + { + "email": "sam.doe@example.com", + "name": "Sam Doe" + } + ], + "cc": [ + { + "email": "jane.doe@example.com", + "name": "Jane Doe" + } + ], + "custom_args": { + "New Argument 1": "New Value 1", + "activationAttempt": "1", + "customerAccountNumber": "[CUSTOMER ACCOUNT NUMBER GOES HERE]" + }, + "headers": { + "X-Accept-Language": "en", + "X-Mailer": "MyApp" + }, + "send_at": 1409348513, + "subject": "Hello, World!", + "substitutions": { + "id": "substitutions", + "type": "object" + }, + "to": [ + { + "email": "john.doe@example.com", + "name": "John Doe" + } + ] + } + ], + "reply_to": { + "email": "sam.smith@example.com", + "name": "Sam Smith" + }, + "sections": { + "section": { + ":sectionName1": "section 1 text", + ":sectionName2": "section 2 text" + } + }, + "send_at": 1409348513, + "subject": "Hello, World!", + "template_id": "[YOUR TEMPLATE ID GOES HERE]", + "tracking_settings": { + "click_tracking": { + "enable": True, + "enable_text": True + }, + "ganalytics": { + "enable": True, + "utm_campaign": "[NAME OF YOUR REFERRER SOURCE]", + "utm_content": "[USE THIS SPACE TO DIFFERENTIATE YOUR EMAIL FROM ADS]", + "utm_medium": "[NAME OF YOUR MARKETING MEDIUM e.g. email]", + "utm_name": "[NAME OF YOUR CAMPAIGN]", + "utm_term": "[IDENTIFY PAID KEYWORDS HERE]" + }, + "open_tracking": { + "enable": True, + "substitution_tag": "%opentrack" + }, + "subscription_tracking": { + "enable": True, + "html": "If you would like to unsubscribe and stop receiving these emails <% clickhere %>.", + "substitution_tag": "<%click here%>", + "text": "If you would like to unsubscribe and stop receiveing these emails <% click here %>." + } + } +} +response = sg.client.mail.send.post(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + diff --git a/docker/examples/mailboxproviders/mailboxproviders.py b/docker/examples/mailboxproviders/mailboxproviders.py new file mode 100644 index 000000000..1b75ecac5 --- /dev/null +++ b/docker/examples/mailboxproviders/mailboxproviders.py @@ -0,0 +1,17 @@ +import sendgrid +import json +import os + + +sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) + +################################################## +# Retrieve email statistics by mailbox provider. # +# GET /mailbox_providers/stats # + +params = {'end_date': '2016-04-01', 'mailbox_providers': 'test_string', 'aggregated_by': 'day', 'limit': 1, 'offset': 1, 'start_date': '2016-01-01'} +response = sg.client.mailbox_providers.stats.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + diff --git a/docker/examples/mailsettings/mailsettings.py b/docker/examples/mailsettings/mailsettings.py new file mode 100644 index 000000000..18c57b960 --- /dev/null +++ b/docker/examples/mailsettings/mailsettings.py @@ -0,0 +1,220 @@ +import sendgrid +import json +import os + + +sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) + +################################################## +# Retrieve all mail settings # +# GET /mail_settings # + +params = {'limit': 1, 'offset': 1} +response = sg.client.mail_settings.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Update address whitelist mail settings # +# PATCH /mail_settings/address_whitelist # + +data = { + "enabled": True, + "list": [ + "email1@example.com", + "example.com" + ] +} +response = sg.client.mail_settings.address_whitelist.patch(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve address whitelist mail settings # +# GET /mail_settings/address_whitelist # + +response = sg.client.mail_settings.address_whitelist.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Update BCC mail settings # +# PATCH /mail_settings/bcc # + +data = { + "email": "email@example.com", + "enabled": False +} +response = sg.client.mail_settings.bcc.patch(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve all BCC mail settings # +# GET /mail_settings/bcc # + +response = sg.client.mail_settings.bcc.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Update bounce purge mail settings # +# PATCH /mail_settings/bounce_purge # + +data = { + "enabled": True, + "hard_bounces": 5, + "soft_bounces": 5 +} +response = sg.client.mail_settings.bounce_purge.patch(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve bounce purge mail settings # +# GET /mail_settings/bounce_purge # + +response = sg.client.mail_settings.bounce_purge.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Update footer mail settings # +# PATCH /mail_settings/footer # + +data = { + "enabled": True, + "html_content": "...", + "plain_content": "..." +} +response = sg.client.mail_settings.footer.patch(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve footer mail settings # +# GET /mail_settings/footer # + +response = sg.client.mail_settings.footer.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Update forward bounce mail settings # +# PATCH /mail_settings/forward_bounce # + +data = { + "email": "example@example.com", + "enabled": True +} +response = sg.client.mail_settings.forward_bounce.patch(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve forward bounce mail settings # +# GET /mail_settings/forward_bounce # + +response = sg.client.mail_settings.forward_bounce.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Update forward spam mail settings # +# PATCH /mail_settings/forward_spam # + +data = { + "email": "", + "enabled": False +} +response = sg.client.mail_settings.forward_spam.patch(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve forward spam mail settings # +# GET /mail_settings/forward_spam # + +response = sg.client.mail_settings.forward_spam.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Update plain content mail settings # +# PATCH /mail_settings/plain_content # + +data = { + "enabled": False +} +response = sg.client.mail_settings.plain_content.patch(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve plain content mail settings # +# GET /mail_settings/plain_content # + +response = sg.client.mail_settings.plain_content.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Update spam check mail settings # +# PATCH /mail_settings/spam_check # + +data = { + "enabled": True, + "max_score": 5, + "url": "url" +} +response = sg.client.mail_settings.spam_check.patch(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve spam check mail settings # +# GET /mail_settings/spam_check # + +response = sg.client.mail_settings.spam_check.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Update template mail settings # +# PATCH /mail_settings/template # + +data = { + "enabled": True, + "html_content": "<% body %>" +} +response = sg.client.mail_settings.template.patch(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve legacy template mail settings # +# GET /mail_settings/template # + +response = sg.client.mail_settings.template.get() +print(response.status_code) +print(response.body) +print(response.headers) + diff --git a/docker/examples/partnersettings/partnersettings.py b/docker/examples/partnersettings/partnersettings.py new file mode 100644 index 000000000..37f77f4e6 --- /dev/null +++ b/docker/examples/partnersettings/partnersettings.py @@ -0,0 +1,40 @@ +import sendgrid +import json +import os + + +sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) + +################################################## +# Returns a list of all partner settings. # +# GET /partner_settings # + +params = {'limit': 1, 'offset': 1} +response = sg.client.partner_settings.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Updates New Relic partner settings. # +# PATCH /partner_settings/new_relic # + +data = { + "enable_subuser_statistics": True, + "enabled": True, + "license_key": "" +} +response = sg.client.partner_settings.new_relic.patch(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Returns all New Relic partner settings. # +# GET /partner_settings/new_relic # + +response = sg.client.partner_settings.new_relic.get() +print(response.status_code) +print(response.body) +print(response.headers) + diff --git a/docker/examples/scopes/scopes.py b/docker/examples/scopes/scopes.py new file mode 100644 index 000000000..124f77d39 --- /dev/null +++ b/docker/examples/scopes/scopes.py @@ -0,0 +1,16 @@ +import sendgrid +import json +import os + + +sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) + +################################################## +# Retrieve a list of scopes for which this user has access. # +# GET /scopes # + +response = sg.client.scopes.get() +print(response.status_code) +print(response.body) +print(response.headers) + diff --git a/docker/examples/senders/senders.py b/docker/examples/senders/senders.py new file mode 100644 index 000000000..f21459b71 --- /dev/null +++ b/docker/examples/senders/senders.py @@ -0,0 +1,99 @@ +import sendgrid +import json +import os + + +sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) + +################################################## +# Create a Sender Identity # +# POST /senders # + +data = { + "address": "123 Elm St.", + "address_2": "Apt. 456", + "city": "Denver", + "country": "United States", + "from": { + "email": "from@example.com", + "name": "Example INC" + }, + "nickname": "My Sender ID", + "reply_to": { + "email": "replyto@example.com", + "name": "Example INC" + }, + "state": "Colorado", + "zip": "80202" +} +response = sg.client.senders.post(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Get all Sender Identities # +# GET /senders # + +response = sg.client.senders.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Update a Sender Identity # +# PATCH /senders/{sender_id} # + +data = { + "address": "123 Elm St.", + "address_2": "Apt. 456", + "city": "Denver", + "country": "United States", + "from": { + "email": "from@example.com", + "name": "Example INC" + }, + "nickname": "My Sender ID", + "reply_to": { + "email": "replyto@example.com", + "name": "Example INC" + }, + "state": "Colorado", + "zip": "80202" +} +sender_id = "test_url_param" +response = sg.client.senders._(sender_id).patch(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# View a Sender Identity # +# GET /senders/{sender_id} # + +sender_id = "test_url_param" +response = sg.client.senders._(sender_id).get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Delete a Sender Identity # +# DELETE /senders/{sender_id} # + +sender_id = "test_url_param" +response = sg.client.senders._(sender_id).delete() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Resend Sender Identity Verification # +# POST /senders/{sender_id}/resend_verification # + +sender_id = "test_url_param" +response = sg.client.senders._(sender_id).resend_verification.post() +print(response.status_code) +print(response.body) +print(response.headers) + diff --git a/docker/examples/stats/stats.py b/docker/examples/stats/stats.py new file mode 100644 index 000000000..a7bf3362e --- /dev/null +++ b/docker/examples/stats/stats.py @@ -0,0 +1,17 @@ +import sendgrid +import json +import os + + +sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) + +################################################## +# Retrieve global email statistics # +# GET /stats # + +params = {'aggregated_by': 'day', 'limit': 1, 'start_date': '2016-01-01', 'end_date': '2016-04-01', 'offset': 1} +response = sg.client.stats.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + diff --git a/docker/examples/subusers/subusers.py b/docker/examples/subusers/subusers.py new file mode 100644 index 000000000..6aa91e535 --- /dev/null +++ b/docker/examples/subusers/subusers.py @@ -0,0 +1,170 @@ +import sendgrid +import json +import os + + +sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) + +################################################## +# Create Subuser # +# POST /subusers # + +data = { + "email": "John@example.com", + "ips": [ + "1.1.1.1", + "2.2.2.2" + ], + "password": "johns_password", + "username": "John@example.com" +} +response = sg.client.subusers.post(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# List all Subusers # +# GET /subusers # + +params = {'username': 'test_string', 'limit': 1, 'offset': 1} +response = sg.client.subusers.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve Subuser Reputations # +# GET /subusers/reputations # + +params = {'usernames': 'test_string'} +response = sg.client.subusers.reputations.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve email statistics for your subusers. # +# GET /subusers/stats # + +params = {'end_date': '2016-04-01', 'aggregated_by': 'day', 'limit': 1, 'offset': 1, 'start_date': '2016-01-01', 'subusers': 'test_string'} +response = sg.client.subusers.stats.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve monthly stats for all subusers # +# GET /subusers/stats/monthly # + +params = {'subuser': 'test_string', 'limit': 1, 'sort_by_metric': 'test_string', 'offset': 1, 'date': 'test_string', 'sort_by_direction': 'asc'} +response = sg.client.subusers.stats.monthly.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve the totals for each email statistic metric for all subusers. # +# GET /subusers/stats/sums # + +params = {'end_date': '2016-04-01', 'aggregated_by': 'day', 'limit': 1, 'sort_by_metric': 'test_string', 'offset': 1, 'start_date': '2016-01-01', 'sort_by_direction': 'asc'} +response = sg.client.subusers.stats.sums.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Enable/disable a subuser # +# PATCH /subusers/{subuser_name} # + +data = { + "disabled": False +} +subuser_name = "test_url_param" +response = sg.client.subusers._(subuser_name).patch(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Delete a subuser # +# DELETE /subusers/{subuser_name} # + +subuser_name = "test_url_param" +response = sg.client.subusers._(subuser_name).delete() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Update IPs assigned to a subuser # +# PUT /subusers/{subuser_name}/ips # + +data = [ + "127.0.0.1" +] +subuser_name = "test_url_param" +response = sg.client.subusers._(subuser_name).ips.put(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Update Monitor Settings for a subuser # +# PUT /subusers/{subuser_name}/monitor # + +data = { + "email": "example@example.com", + "frequency": 500 +} +subuser_name = "test_url_param" +response = sg.client.subusers._(subuser_name).monitor.put(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Create monitor settings # +# POST /subusers/{subuser_name}/monitor # + +data = { + "email": "example@example.com", + "frequency": 50000 +} +subuser_name = "test_url_param" +response = sg.client.subusers._(subuser_name).monitor.post(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve monitor settings for a subuser # +# GET /subusers/{subuser_name}/monitor # + +subuser_name = "test_url_param" +response = sg.client.subusers._(subuser_name).monitor.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Delete monitor settings # +# DELETE /subusers/{subuser_name}/monitor # + +subuser_name = "test_url_param" +response = sg.client.subusers._(subuser_name).monitor.delete() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve the monthly email statistics for a single subuser # +# GET /subusers/{subuser_name}/stats/monthly # + +params = {'date': 'test_string', 'sort_by_direction': 'asc', 'limit': 1, 'sort_by_metric': 'test_string', 'offset': 1} +subuser_name = "test_url_param" +response = sg.client.subusers._(subuser_name).stats.monthly.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + diff --git a/docker/examples/suppression/suppression.py b/docker/examples/suppression/suppression.py new file mode 100644 index 000000000..abdaef76d --- /dev/null +++ b/docker/examples/suppression/suppression.py @@ -0,0 +1,202 @@ +import sendgrid +import json +import os + + +sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) + +################################################## +# Retrieve all blocks # +# GET /suppression/blocks # + +params = {'start_time': 1, 'limit': 1, 'end_time': 1, 'offset': 1} +response = sg.client.suppression.blocks.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Delete blocks # +# DELETE /suppression/blocks # + +data = { + "delete_all": False, + "emails": [ + "example1@example.com", + "example2@example.com" + ] +} +response = sg.client.suppression.blocks.delete(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve a specific block # +# GET /suppression/blocks/{email} # + +email = "test_url_param" +response = sg.client.suppression.blocks._(email).get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Delete a specific block # +# DELETE /suppression/blocks/{email} # + +email = "test_url_param" +response = sg.client.suppression.blocks._(email).delete() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve all bounces # +# GET /suppression/bounces # + +params = {'start_time': 1, 'end_time': 1} +response = sg.client.suppression.bounces.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Delete bounces # +# DELETE /suppression/bounces # + +data = { + "delete_all": True, + "emails": [ + "example@example.com", + "example2@example.com" + ] +} +response = sg.client.suppression.bounces.delete(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve a Bounce # +# GET /suppression/bounces/{email} # + +email = "test_url_param" +response = sg.client.suppression.bounces._(email).get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Delete a bounce # +# DELETE /suppression/bounces/{email} # + +params = {'email_address': 'example@example.com'} +email = "test_url_param" +response = sg.client.suppression.bounces._(email).delete(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve all invalid emails # +# GET /suppression/invalid_emails # + +params = {'start_time': 1, 'limit': 1, 'end_time': 1, 'offset': 1} +response = sg.client.suppression.invalid_emails.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Delete invalid emails # +# DELETE /suppression/invalid_emails # + +data = { + "delete_all": False, + "emails": [ + "example1@example.com", + "example2@example.com" + ] +} +response = sg.client.suppression.invalid_emails.delete(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve a specific invalid email # +# GET /suppression/invalid_emails/{email} # + +email = "test_url_param" +response = sg.client.suppression.invalid_emails._(email).get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Delete a specific invalid email # +# DELETE /suppression/invalid_emails/{email} # + +email = "test_url_param" +response = sg.client.suppression.invalid_emails._(email).delete() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve a specific spam report # +# GET /suppression/spam_report/{email} # + +email = "test_url_param" +response = sg.client.suppression.spam_report._(email).get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Delete a specific spam report # +# DELETE /suppression/spam_report/{email} # + +email = "test_url_param" +response = sg.client.suppression.spam_report._(email).delete() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve all spam reports # +# GET /suppression/spam_reports # + +params = {'start_time': 1, 'limit': 1, 'end_time': 1, 'offset': 1} +response = sg.client.suppression.spam_reports.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Delete spam reports # +# DELETE /suppression/spam_reports # + +data = { + "delete_all": False, + "emails": [ + "example1@example.com", + "example2@example.com" + ] +} +response = sg.client.suppression.spam_reports.delete(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve all global suppressions # +# GET /suppression/unsubscribes # + +params = {'start_time': 1, 'limit': 1, 'end_time': 1, 'offset': 1} +response = sg.client.suppression.unsubscribes.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + diff --git a/docker/examples/trackingsettings/trackingsettings.py b/docker/examples/trackingsettings/trackingsettings.py new file mode 100644 index 000000000..80dbe243a --- /dev/null +++ b/docker/examples/trackingsettings/trackingsettings.py @@ -0,0 +1,111 @@ +import sendgrid +import json +import os + + +sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) + +################################################## +# Retrieve Tracking Settings # +# GET /tracking_settings # + +params = {'limit': 1, 'offset': 1} +response = sg.client.tracking_settings.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Update Click Tracking Settings # +# PATCH /tracking_settings/click # + +data = { + "enabled": True +} +response = sg.client.tracking_settings.click.patch(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve Click Track Settings # +# GET /tracking_settings/click # + +response = sg.client.tracking_settings.click.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Update Google Analytics Settings # +# PATCH /tracking_settings/google_analytics # + +data = { + "enabled": True, + "utm_campaign": "website", + "utm_content": "", + "utm_medium": "email", + "utm_source": "sendgrid.com", + "utm_term": "" +} +response = sg.client.tracking_settings.google_analytics.patch(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve Google Analytics Settings # +# GET /tracking_settings/google_analytics # + +response = sg.client.tracking_settings.google_analytics.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Update Open Tracking Settings # +# PATCH /tracking_settings/open # + +data = { + "enabled": True +} +response = sg.client.tracking_settings.open.patch(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Get Open Tracking Settings # +# GET /tracking_settings/open # + +response = sg.client.tracking_settings.open.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Update Subscription Tracking Settings # +# PATCH /tracking_settings/subscription # + +data = { + "enabled": True, + "html_content": "html content", + "landing": "landing page html", + "plain_content": "text content", + "replace": "replacement tag", + "url": "url" +} +response = sg.client.tracking_settings.subscription.patch(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve Subscription Tracking Settings # +# GET /tracking_settings/subscription # + +response = sg.client.tracking_settings.subscription.get() +print(response.status_code) +print(response.body) +print(response.headers) + diff --git a/docker/examples/user/user.py b/docker/examples/user/user.py new file mode 100644 index 000000000..9e3f24766 --- /dev/null +++ b/docker/examples/user/user.py @@ -0,0 +1,294 @@ +import sendgrid +import json +import os + + +sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) + +################################################## +# Get a user's account information. # +# GET /user/account # + +response = sg.client.user.account.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve your credit balance # +# GET /user/credits # + +response = sg.client.user.credits.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Update your account email address # +# PUT /user/email # + +data = { + "email": "example@example.com" +} +response = sg.client.user.email.put(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve your account email address # +# GET /user/email # + +response = sg.client.user.email.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Update your password # +# PUT /user/password # + +data = { + "new_password": "new_password", + "old_password": "old_password" +} +response = sg.client.user.password.put(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Update a user's profile # +# PATCH /user/profile # + +data = { + "city": "Orange", + "first_name": "Example", + "last_name": "User" +} +response = sg.client.user.profile.patch(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Get a user's profile # +# GET /user/profile # + +response = sg.client.user.profile.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Cancel or pause a scheduled send # +# POST /user/scheduled_sends # + +data = { + "batch_id": "YOUR_BATCH_ID", + "status": "pause" +} +response = sg.client.user.scheduled_sends.post(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve all scheduled sends # +# GET /user/scheduled_sends # + +response = sg.client.user.scheduled_sends.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Update user scheduled send information # +# PATCH /user/scheduled_sends/{batch_id} # + +data = { + "status": "pause" +} +batch_id = "test_url_param" +response = sg.client.user.scheduled_sends._(batch_id).patch(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve scheduled send # +# GET /user/scheduled_sends/{batch_id} # + +batch_id = "test_url_param" +response = sg.client.user.scheduled_sends._(batch_id).get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Delete a cancellation or pause of a scheduled send # +# DELETE /user/scheduled_sends/{batch_id} # + +batch_id = "test_url_param" +response = sg.client.user.scheduled_sends._(batch_id).delete() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Update Enforced TLS settings # +# PATCH /user/settings/enforced_tls # + +data = { + "require_tls": True, + "require_valid_cert": False +} +response = sg.client.user.settings.enforced_tls.patch(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve current Enforced TLS settings. # +# GET /user/settings/enforced_tls # + +response = sg.client.user.settings.enforced_tls.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Update your username # +# PUT /user/username # + +data = { + "username": "test_username" +} +response = sg.client.user.username.put(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve your username # +# GET /user/username # + +response = sg.client.user.username.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Update Event Notification Settings # +# PATCH /user/webhooks/event/settings # + +data = { + "bounce": True, + "click": True, + "deferred": True, + "delivered": True, + "dropped": True, + "enabled": True, + "group_resubscribe": True, + "group_unsubscribe": True, + "open": True, + "processed": True, + "spam_report": True, + "unsubscribe": True, + "url": "url" +} +response = sg.client.user.webhooks.event.settings.patch(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve Event Webhook settings # +# GET /user/webhooks/event/settings # + +response = sg.client.user.webhooks.event.settings.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Test Event Notification Settings # +# POST /user/webhooks/event/test # + +data = { + "url": "url" +} +response = sg.client.user.webhooks.event.test.post(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Create a parse setting # +# POST /user/webhooks/parse/settings # + +data = { + "hostname": "myhostname.com", + "send_raw": False, + "spam_check": True, + "url": "http://email.myhosthame.com" +} +response = sg.client.user.webhooks.parse.settings.post(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve all parse settings # +# GET /user/webhooks/parse/settings # + +response = sg.client.user.webhooks.parse.settings.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Update a parse setting # +# PATCH /user/webhooks/parse/settings/{hostname} # + +data = { + "send_raw": True, + "spam_check": False, + "url": "http://newdomain.com/parse" +} +hostname = "test_url_param" +response = sg.client.user.webhooks.parse.settings._(hostname).patch(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve a specific parse setting # +# GET /user/webhooks/parse/settings/{hostname} # + +hostname = "test_url_param" +response = sg.client.user.webhooks.parse.settings._(hostname).get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Delete a parse setting # +# DELETE /user/webhooks/parse/settings/{hostname} # + +hostname = "test_url_param" +response = sg.client.user.webhooks.parse.settings._(hostname).delete() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieves Inbound Parse Webhook statistics. # +# GET /user/webhooks/parse/stats # + +params = {'aggregated_by': 'day', 'limit': 'test_string', 'start_date': '2016-01-01', 'end_date': '2016-04-01', 'offset': 'test_string'} +response = sg.client.user.webhooks.parse.stats.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + diff --git a/docker/examples/whitelabel/whitelabel.py b/docker/examples/whitelabel/whitelabel.py new file mode 100644 index 000000000..f529d3ed2 --- /dev/null +++ b/docker/examples/whitelabel/whitelabel.py @@ -0,0 +1,311 @@ +import sendgrid +import json +import os + + +sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) + +################################################## +# Create a domain whitelabel. # +# POST /whitelabel/domains # + +data = { + "automatic_security": False, + "custom_spf": True, + "default": True, + "domain": "example.com", + "ips": [ + "192.168.1.1", + "192.168.1.2" + ], + "subdomain": "news", + "username": "john@example.com" +} +response = sg.client.whitelabel.domains.post(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# List all domain whitelabels. # +# GET /whitelabel/domains # + +params = {'username': 'test_string', 'domain': 'test_string', 'exclude_subusers': 'true', 'limit': 1, 'offset': 1} +response = sg.client.whitelabel.domains.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Get the default domain whitelabel. # +# GET /whitelabel/domains/default # + +response = sg.client.whitelabel.domains.default.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# List the domain whitelabel associated with the given user. # +# GET /whitelabel/domains/subuser # + +response = sg.client.whitelabel.domains.subuser.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Disassociate a domain whitelabel from a given user. # +# DELETE /whitelabel/domains/subuser # + +response = sg.client.whitelabel.domains.subuser.delete() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Update a domain whitelabel. # +# PATCH /whitelabel/domains/{domain_id} # + +data = { + "custom_spf": True, + "default": False +} +domain_id = "test_url_param" +response = sg.client.whitelabel.domains._(domain_id).patch(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve a domain whitelabel. # +# GET /whitelabel/domains/{domain_id} # + +domain_id = "test_url_param" +response = sg.client.whitelabel.domains._(domain_id).get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Delete a domain whitelabel. # +# DELETE /whitelabel/domains/{domain_id} # + +domain_id = "test_url_param" +response = sg.client.whitelabel.domains._(domain_id).delete() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Associate a domain whitelabel with a given user. # +# POST /whitelabel/domains/{domain_id}/subuser # + +data = { + "username": "jane@example.com" +} +domain_id = "test_url_param" +response = sg.client.whitelabel.domains._(domain_id).subuser.post(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Add an IP to a domain whitelabel. # +# POST /whitelabel/domains/{id}/ips # + +data = { + "ip": "192.168.0.1" +} +id = "test_url_param" +response = sg.client.whitelabel.domains._(id).ips.post(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Remove an IP from a domain whitelabel. # +# DELETE /whitelabel/domains/{id}/ips/{ip} # + +id = "test_url_param" +ip = "test_url_param" +response = sg.client.whitelabel.domains._(id).ips._(ip).delete() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Validate a domain whitelabel. # +# POST /whitelabel/domains/{id}/validate # + +id = "test_url_param" +response = sg.client.whitelabel.domains._(id).validate.post() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Create an IP whitelabel # +# POST /whitelabel/ips # + +data = { + "domain": "example.com", + "ip": "192.168.1.1", + "subdomain": "email" +} +response = sg.client.whitelabel.ips.post(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve all IP whitelabels # +# GET /whitelabel/ips # + +params = {'ip': 'test_string', 'limit': 1, 'offset': 1} +response = sg.client.whitelabel.ips.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve an IP whitelabel # +# GET /whitelabel/ips/{id} # + +id = "test_url_param" +response = sg.client.whitelabel.ips._(id).get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Delete an IP whitelabel # +# DELETE /whitelabel/ips/{id} # + +id = "test_url_param" +response = sg.client.whitelabel.ips._(id).delete() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Validate an IP whitelabel # +# POST /whitelabel/ips/{id}/validate # + +id = "test_url_param" +response = sg.client.whitelabel.ips._(id).validate.post() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Create a Link Whitelabel # +# POST /whitelabel/links # + +data = { + "default": True, + "domain": "example.com", + "subdomain": "mail" +} +params = {'limit': 1, 'offset': 1} +response = sg.client.whitelabel.links.post(request_body=data, query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve all link whitelabels # +# GET /whitelabel/links # + +params = {'limit': 1} +response = sg.client.whitelabel.links.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve a Default Link Whitelabel # +# GET /whitelabel/links/default # + +params = {'domain': 'test_string'} +response = sg.client.whitelabel.links.default.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve Associated Link Whitelabel # +# GET /whitelabel/links/subuser # + +params = {'username': 'test_string'} +response = sg.client.whitelabel.links.subuser.get(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Disassociate a Link Whitelabel # +# DELETE /whitelabel/links/subuser # + +params = {'username': 'test_string'} +response = sg.client.whitelabel.links.subuser.delete(query_params=params) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Update a Link Whitelabel # +# PATCH /whitelabel/links/{id} # + +data = { + "default": True +} +id = "test_url_param" +response = sg.client.whitelabel.links._(id).patch(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve a Link Whitelabel # +# GET /whitelabel/links/{id} # + +id = "test_url_param" +response = sg.client.whitelabel.links._(id).get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Delete a Link Whitelabel # +# DELETE /whitelabel/links/{id} # + +id = "test_url_param" +response = sg.client.whitelabel.links._(id).delete() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Validate a Link Whitelabel # +# POST /whitelabel/links/{id}/validate # + +id = "test_url_param" +response = sg.client.whitelabel.links._(id).validate.post() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Associate a Link Whitelabel # +# POST /whitelabel/links/{link_id}/subuser # + +data = { + "username": "jane@example.com" +} +link_id = "test_url_param" +response = sg.client.whitelabel.links._(link_id).subuser.post(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + From b916a6daf669a5c4f68e5133d4a150abcc15d4b3 Mon Sep 17 00:00:00 2001 From: heisendumb Date: Mon, 30 Oct 2017 00:31:16 -0200 Subject: [PATCH 15/52] added args to Dockerfile --- docker/Dockerfile | 7 +++++-- docker/Makefile | 8 -------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index 393d91bfe..a0e5f4885 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -2,6 +2,9 @@ FROM ubuntu:xenial ENV PYTHON_VERSIONS='python2.6 python2.7 python3.4 python3.5 python3.6' \ OAI_SPEC_URL="https://raw.githubusercontent.com/sendgrid/sendgrid-oai/master/oai_stoplight.json" +ARG SENDGRID-PYTHON_VERSION +ARG BRANCH_HTTP_CLIENT + # install testing versions of python, including old versions, from deadsnakes RUN set -x \ && apt-get update \ @@ -30,8 +33,8 @@ RUN python2.7 get-pip.py && \ # set up default sendgrid env WORKDIR /root/sources -RUN git clone https://github.com/sendgrid/sendgrid-python.git --branch && \ - git clone https://github.com/sendgrid/python-http-client.git --branch +RUN git clone https://github.com/sendgrid/sendgrid-python.git --branch $SENDGRID-PYTHON_VERSION && \ + git clone https://github.com/sendgrid/python-http-client.git --branch $HTTP-CLIENT_VERSION WORKDIR /root RUN ln -s /root/sources/sendgrid-python/sendgrid && \ ln -s /root/sources/python-http-client/python_http_client diff --git a/docker/Makefile b/docker/Makefile index 6ec47459d..1afc31189 100644 --- a/docker/Makefile +++ b/docker/Makefile @@ -1,11 +1,3 @@ -# import deploy config -deployfile=deploy.env -ifdef dpl -deployfile=$(dpl) -endif -include $(deployfile) -export $(shell sed 's/=.*//' $(deployfile)) - stop: docker-compose stop From af559cfbcc532048eba35468e9c494b9c05c8a1d Mon Sep 17 00:00:00 2001 From: heisendumb Date: Mon, 30 Oct 2017 00:44:00 -0200 Subject: [PATCH 16/52] edit USAGE.md --- docker/USAGE.md | 18 ++++++++++++++++++ docker/docker-compose.yml | 4 ++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/docker/USAGE.md b/docker/USAGE.md index cd543c402..3c6ff700f 100644 --- a/docker/USAGE.md +++ b/docker/USAGE.md @@ -70,6 +70,24 @@ $ docker run -it -v /path/to/cool-sendgrid-python:/mnt/sendgrid-python sendgrid/ Note that the paths you specify in `-v` must be absolute. +# Docker Compose + +## Using tag's for versions - DockerHub + +### Edit variable TAG on .env/env_sample file + +```sh-session +$ sed -ie 's/TAG=latest/TAG=choice_a_version/g' +``` +### Run service using tags + +```sh-session +$ cd /path/to/sendgrid-python/docker +$ docker-compose up -d +``` + +# Specifying specific versions + # Testing Testing is easy! Run the container, `cd sendgrid`, and run `tox`. diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index d35772fd2..ed78e91ec 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -20,5 +20,5 @@ services: container_name: sendgrid-dev env_file: .env volumes: - - ${PATH_TO_SENDGRID-PYTHON}:/mnt/sendgrid-python - - ${PATH_TO_HTTP-CLIENT}:/mnt/python-http-client + - ${PATH_TO_SENDGRID-PYTHON_DEV}:/mnt/sendgrid-python + - ${PATH_TO_HTTP-CLIENT_DEV}:/mnt/python-http-client From 3c2724c73b62fe2c86a707531794011d619b842b Mon Sep 17 00:00:00 2001 From: heisendumb Date: Mon, 30 Oct 2017 02:11:14 -0200 Subject: [PATCH 17/52] issue #444 done --- docker/Makefile | 2 +- docker/USAGE.md | 45 ++++++++- docker/docker-compose.yml | 29 ++++-- docker/env/python-dev/sendgrid-python | 1 + docker/examples/templates/templates.py | 130 +++++++++++++++++++++++++ docker/sendgrid.env | 8 ++ 6 files changed, 202 insertions(+), 13 deletions(-) create mode 160000 docker/env/python-dev/sendgrid-python create mode 100644 docker/examples/templates/templates.py create mode 100644 docker/sendgrid.env diff --git a/docker/Makefile b/docker/Makefile index 1afc31189..76ccb73af 100644 --- a/docker/Makefile +++ b/docker/Makefile @@ -17,4 +17,4 @@ build-build: docker-compose up --build -d up: rm clean build-build - echo "Sendgrid dev environment is alive :D" + echo "Sendgrid-python environment is alive :D" diff --git a/docker/USAGE.md b/docker/USAGE.md index 3c6ff700f..d869a77ce 100644 --- a/docker/USAGE.md +++ b/docker/USAGE.md @@ -72,7 +72,16 @@ Note that the paths you specify in `-v` must be absolute. # Docker Compose -## Using tag's for versions - DockerHub + +# Quickstart + +1. Install docker-compose on your machine. +2. Must copy sendgrid.env to .env file. +3. Edit .env file for yours versions and paths. +4. Must create env folder for clone yours repo. +5. Have fun! :D + +## Using tag's for versions - DockerHub: ### Edit variable TAG on .env/env_sample file @@ -83,10 +92,40 @@ $ sed -ie 's/TAG=latest/TAG=choice_a_version/g' ```sh-session $ cd /path/to/sendgrid-python/docker -$ docker-compose up -d +$ docker-compose up -d sendgrid +``` + +## Specifying specific versions: + +### Edit variable TAG on .env/env_sample file + +```sh-session +$ sed -ie 's/SENDGRID_PYTHON_VERSION=vy.x.z/SENDGRID_PYTHON_VERSION=vx.y.z/g' +$ sed -ie 's/HTTP_CLIENT_VERSION=vy.x.z/HTTP_CLIENT_VERSION=vx.y.z/g' ``` -# Specifying specific versions +### Run service + +```sh-session +$ cd /path/to/sendgrid-python/docker +$ docker-compose up -d sendgrid-dev +``` + +## Specifying your own fork: + +### Edit variable TAG on .env/env_sample file + +```sh-session +$ sed -ie 's/TAG=latest/TAG=choice_a_version/g' +$ sed -ie 's/SENDGRID_PYTHON_VERSION=vy.x.z/SENDGRID_PYTHON_VERSION=vx.y.z/g' +``` + +### Run service + +```sh-session +$ cd /path/to/sendgrid-python/docker +$ docker-compose up -d sendgrid-beta +``` # Testing diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index ed78e91ec..2a435b39f 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -5,20 +5,31 @@ services: image: sendgrid/sendgrid-python:${TAG} restart: unless-stopped container_name: sendgrid-prod - volumes: - - ${PATH_TO_SENDGRID-PYTHON_PROD}:/mnt/sendgrid-python - - ${PATH_TO_HTTP-CLIENT_PROD}:/mnt/python-http-client - env_file: .env + tty: true + env_file: + - .env sendgrid-dev: build: context: . args: - - SENDGRID-PYTHON_VERSION: {SENDGRID-PYTHON_VERSION} - - HTTP-CLIENT_VERSION: {HTTP-CLIENT_VERSION} + - SENDGRID-PYTHON_VERSION=${SENDGRID_PYTHON_VERSION} + - HTTP-CLIENT_VERSION=${HTTP_CLIENT_VERSION} restart: unless-stopped container_name: sendgrid-dev - env_file: .env + tty: true + env_file: + - .env volumes: - - ${PATH_TO_SENDGRID-PYTHON_DEV}:/mnt/sendgrid-python - - ${PATH_TO_HTTP-CLIENT_DEV}:/mnt/python-http-client + - ${PATH_TO_SENDGRID_PYTHON_DEV}:/mnt/sendgrid-python + - ${PATH_TO_HTTP_CLIENT_DEV}:/mnt/python-http-client + + sendgrid-beta: + image: sendgrid/sendgrid-python:${TAG} + restart: unless-stopped + container_name: sendgrid-beta + tty: true + env_file: + - .env + volumes: + - ${PATH_TO_SENDGRID_PYTHON_FORK}:/root/sources/sendgrid-python/sendgrid diff --git a/docker/env/python-dev/sendgrid-python b/docker/env/python-dev/sendgrid-python new file mode 160000 index 000000000..28cf42f6d --- /dev/null +++ b/docker/env/python-dev/sendgrid-python @@ -0,0 +1 @@ +Subproject commit 28cf42f6d590695de7e7ecdedcb67e9d8d4729ac diff --git a/docker/examples/templates/templates.py b/docker/examples/templates/templates.py new file mode 100644 index 000000000..9d3d5dd4b --- /dev/null +++ b/docker/examples/templates/templates.py @@ -0,0 +1,130 @@ +import sendgrid +import json +import os + + +sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) + +################################################## +# Create a transactional template. # +# POST /templates # + +data = { + "name": "example_name" +} +response = sg.client.templates.post(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve all transactional templates. # +# GET /templates # + +response = sg.client.templates.get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Edit a transactional template. # +# PATCH /templates/{template_id} # + +data = { + "name": "new_example_name" +} +template_id = "test_url_param" +response = sg.client.templates._(template_id).patch(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve a single transactional template. # +# GET /templates/{template_id} # + +template_id = "test_url_param" +response = sg.client.templates._(template_id).get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Delete a template. # +# DELETE /templates/{template_id} # + +template_id = "test_url_param" +response = sg.client.templates._(template_id).delete() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Create a new transactional template version. # +# POST /templates/{template_id}/versions # + +data = { + "active": 1, + "html_content": "<%body%>", + "name": "example_version_name", + "plain_content": "<%body%>", + "subject": "<%subject%>", + "template_id": "ddb96bbc-9b92-425e-8979-99464621b543" +} +template_id = "test_url_param" +response = sg.client.templates._(template_id).versions.post(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Edit a transactional template version. # +# PATCH /templates/{template_id}/versions/{version_id} # + +data = { + "active": 1, + "html_content": "<%body%>", + "name": "updated_example_name", + "plain_content": "<%body%>", + "subject": "<%subject%>" +} +template_id = "test_url_param" +version_id = "test_url_param" +response = sg.client.templates._(template_id).versions._(version_id).patch(request_body=data) +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Retrieve a specific transactional template version. # +# GET /templates/{template_id}/versions/{version_id} # + +template_id = "test_url_param" +version_id = "test_url_param" +response = sg.client.templates._(template_id).versions._(version_id).get() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Delete a transactional template version. # +# DELETE /templates/{template_id}/versions/{version_id} # + +template_id = "test_url_param" +version_id = "test_url_param" +response = sg.client.templates._(template_id).versions._(version_id).delete() +print(response.status_code) +print(response.body) +print(response.headers) + +################################################## +# Activate a transactional template version. # +# POST /templates/{template_id}/versions/{version_id}/activate # + +template_id = "test_url_param" +version_id = "test_url_param" +response = sg.client.templates._(template_id).versions._(version_id).activate.post() +print(response.status_code) +print(response.body) +print(response.headers) + diff --git a/docker/sendgrid.env b/docker/sendgrid.env new file mode 100644 index 000000000..ace58fafa --- /dev/null +++ b/docker/sendgrid.env @@ -0,0 +1,8 @@ +TAG=latest +SENDGRID_PYTHON_VERSION="v3.6.1" +HTTP_CLIENT_VERSION="v1.2.4" +PATH_TO_SENDGRID_PYTHON_DEV=../env/python-dev/sendgrid-python +PATH_TO_HTTP_CLIENT_DEV=../env/python-dev/python-http-client +PATH_TO_SENDGRID_PYTHON_PROD=../env/python-prod/sendgrid-python +PATH_TO_HTTP_CLIENT_PROD=../env/python-prod/python-http-client +PATH_TO_SENDGRID_PYTHON_FORK=../env/python-fork/sendgrid-python From 2a9b4040ba8ac41c7cf48f3e1e995d85f40d5256 Mon Sep 17 00:00:00 2001 From: d grossman Date: Fri, 3 Nov 2017 14:55:02 -0700 Subject: [PATCH 18/52] moved file, updated import --- {sendgrid/helpers/endpoints/ip => test}/test_unassigned.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) rename {sendgrid/helpers/endpoints/ip => test}/test_unassigned.py (96%) diff --git a/sendgrid/helpers/endpoints/ip/test_unassigned.py b/test/test_unassigned.py similarity index 96% rename from sendgrid/helpers/endpoints/ip/test_unassigned.py rename to test/test_unassigned.py index be5904018..d13451277 100644 --- a/sendgrid/helpers/endpoints/ip/test_unassigned.py +++ b/test/test_unassigned.py @@ -1,7 +1,8 @@ import json import pytest -from .unassigned import unassigned +from sendgrid.helpers.endpoints.ip.unassigned import unassigned + ret_json = '''[ { "ip": "167.89.21.3", From 955cac6889c6d7e113f48a3f6a9286cc215cb114 Mon Sep 17 00:00:00 2001 From: mbernier Date: Tue, 12 Dec 2017 08:20:58 -0700 Subject: [PATCH 19/52] removed extra examples dir --- .../examples/accesssettings/accesssettings.py | 84 ---- docker/examples/alerts/alerts.py | 63 --- docker/examples/apikeys/apikeys.py | 85 ---- docker/examples/asm/asm.py | 174 -------- docker/examples/browsers/browsers.py | 17 - docker/examples/campaigns/campaigns.py | 154 ------- docker/examples/categories/categories.py | 37 -- docker/examples/clients/clients.py | 28 -- docker/examples/contactdb/contactdb.py | 396 ------------------ docker/examples/devices/devices.py | 17 - docker/examples/geo/geo.py | 17 - docker/examples/helpers/mail/mail_example.py | 219 ---------- docker/examples/ips/ips.py | 155 ------- docker/examples/mail/mail.py | 174 -------- .../mailboxproviders/mailboxproviders.py | 17 - docker/examples/mailsettings/mailsettings.py | 220 ---------- .../partnersettings/partnersettings.py | 40 -- docker/examples/scopes/scopes.py | 16 - docker/examples/senders/senders.py | 99 ----- docker/examples/stats/stats.py | 17 - docker/examples/subusers/subusers.py | 170 -------- docker/examples/suppression/suppression.py | 202 --------- docker/examples/templates/templates.py | 130 ------ .../trackingsettings/trackingsettings.py | 111 ----- docker/examples/user/user.py | 294 ------------- docker/examples/whitelabel/whitelabel.py | 311 -------------- 26 files changed, 3247 deletions(-) delete mode 100644 docker/examples/accesssettings/accesssettings.py delete mode 100644 docker/examples/alerts/alerts.py delete mode 100644 docker/examples/apikeys/apikeys.py delete mode 100644 docker/examples/asm/asm.py delete mode 100644 docker/examples/browsers/browsers.py delete mode 100644 docker/examples/campaigns/campaigns.py delete mode 100644 docker/examples/categories/categories.py delete mode 100644 docker/examples/clients/clients.py delete mode 100644 docker/examples/contactdb/contactdb.py delete mode 100644 docker/examples/devices/devices.py delete mode 100644 docker/examples/geo/geo.py delete mode 100644 docker/examples/helpers/mail/mail_example.py delete mode 100644 docker/examples/ips/ips.py delete mode 100644 docker/examples/mail/mail.py delete mode 100644 docker/examples/mailboxproviders/mailboxproviders.py delete mode 100644 docker/examples/mailsettings/mailsettings.py delete mode 100644 docker/examples/partnersettings/partnersettings.py delete mode 100644 docker/examples/scopes/scopes.py delete mode 100644 docker/examples/senders/senders.py delete mode 100644 docker/examples/stats/stats.py delete mode 100644 docker/examples/subusers/subusers.py delete mode 100644 docker/examples/suppression/suppression.py delete mode 100644 docker/examples/templates/templates.py delete mode 100644 docker/examples/trackingsettings/trackingsettings.py delete mode 100644 docker/examples/user/user.py delete mode 100644 docker/examples/whitelabel/whitelabel.py diff --git a/docker/examples/accesssettings/accesssettings.py b/docker/examples/accesssettings/accesssettings.py deleted file mode 100644 index aac0e4a54..000000000 --- a/docker/examples/accesssettings/accesssettings.py +++ /dev/null @@ -1,84 +0,0 @@ -import sendgrid -import json -import os - - -sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) - -################################################## -# Retrieve all recent access attempts # -# GET /access_settings/activity # - -params = {'limit': 1} -response = sg.client.access_settings.activity.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Add one or more IPs to the whitelist # -# POST /access_settings/whitelist # - -data = { - "ips": [ - { - "ip": "192.168.1.1" - }, - { - "ip": "192.*.*.*" - }, - { - "ip": "192.168.1.3/32" - } - ] -} -response = sg.client.access_settings.whitelist.post(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve a list of currently whitelisted IPs # -# GET /access_settings/whitelist # - -response = sg.client.access_settings.whitelist.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Remove one or more IPs from the whitelist # -# DELETE /access_settings/whitelist # - -data = { - "ids": [ - 1, - 2, - 3 - ] -} -response = sg.client.access_settings.whitelist.delete(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve a specific whitelisted IP # -# GET /access_settings/whitelist/{rule_id} # - -rule_id = "test_url_param" -response = sg.client.access_settings.whitelist._(rule_id).get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Remove a specific IP from the whitelist # -# DELETE /access_settings/whitelist/{rule_id} # - -rule_id = "test_url_param" -response = sg.client.access_settings.whitelist._(rule_id).delete() -print(response.status_code) -print(response.body) -print(response.headers) - diff --git a/docker/examples/alerts/alerts.py b/docker/examples/alerts/alerts.py deleted file mode 100644 index e30d48748..000000000 --- a/docker/examples/alerts/alerts.py +++ /dev/null @@ -1,63 +0,0 @@ -import sendgrid -import json -import os - - -sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) - -################################################## -# Create a new Alert # -# POST /alerts # - -data = { - "email_to": "example@example.com", - "frequency": "daily", - "type": "stats_notification" -} -response = sg.client.alerts.post(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve all alerts # -# GET /alerts # - -response = sg.client.alerts.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Update an alert # -# PATCH /alerts/{alert_id} # - -data = { - "email_to": "example@example.com" -} -alert_id = "test_url_param" -response = sg.client.alerts._(alert_id).patch(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve a specific alert # -# GET /alerts/{alert_id} # - -alert_id = "test_url_param" -response = sg.client.alerts._(alert_id).get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Delete an alert # -# DELETE /alerts/{alert_id} # - -alert_id = "test_url_param" -response = sg.client.alerts._(alert_id).delete() -print(response.status_code) -print(response.body) -print(response.headers) - diff --git a/docker/examples/apikeys/apikeys.py b/docker/examples/apikeys/apikeys.py deleted file mode 100644 index 42c3afa10..000000000 --- a/docker/examples/apikeys/apikeys.py +++ /dev/null @@ -1,85 +0,0 @@ -import sendgrid -import json -import os - - -sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) - -################################################## -# Create API keys # -# POST /api_keys # - -data = { - "name": "My API Key", - "sample": "data", - "scopes": [ - "mail.send", - "alerts.create", - "alerts.read" - ] -} -response = sg.client.api_keys.post(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve all API Keys belonging to the authenticated user # -# GET /api_keys # - -params = {'limit': 1} -response = sg.client.api_keys.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Update the name & scopes of an API Key # -# PUT /api_keys/{api_key_id} # - -data = { - "name": "A New Hope", - "scopes": [ - "user.profile.read", - "user.profile.update" - ] -} -api_key_id = "test_url_param" -response = sg.client.api_keys._(api_key_id).put(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Update API keys # -# PATCH /api_keys/{api_key_id} # - -data = { - "name": "A New Hope" -} -api_key_id = "test_url_param" -response = sg.client.api_keys._(api_key_id).patch(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve an existing API Key # -# GET /api_keys/{api_key_id} # - -api_key_id = "test_url_param" -response = sg.client.api_keys._(api_key_id).get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Delete API keys # -# DELETE /api_keys/{api_key_id} # - -api_key_id = "test_url_param" -response = sg.client.api_keys._(api_key_id).delete() -print(response.status_code) -print(response.body) -print(response.headers) - diff --git a/docker/examples/asm/asm.py b/docker/examples/asm/asm.py deleted file mode 100644 index 43130cf06..000000000 --- a/docker/examples/asm/asm.py +++ /dev/null @@ -1,174 +0,0 @@ -import sendgrid -import json -import os - - -sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) - -################################################## -# Create a new suppression group # -# POST /asm/groups # - -data = { - "description": "Suggestions for products our users might like.", - "is_default": True, - "name": "Product Suggestions" -} -response = sg.client.asm.groups.post(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve information about multiple suppression groups # -# GET /asm/groups # - -params = {'id': 1} -response = sg.client.asm.groups.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Update a suppression group. # -# PATCH /asm/groups/{group_id} # - -data = { - "description": "Suggestions for items our users might like.", - "id": 103, - "name": "Item Suggestions" -} -group_id = "test_url_param" -response = sg.client.asm.groups._(group_id).patch(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Get information on a single suppression group. # -# GET /asm/groups/{group_id} # - -group_id = "test_url_param" -response = sg.client.asm.groups._(group_id).get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Delete a suppression group. # -# DELETE /asm/groups/{group_id} # - -group_id = "test_url_param" -response = sg.client.asm.groups._(group_id).delete() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Add suppressions to a suppression group # -# POST /asm/groups/{group_id}/suppressions # - -data = { - "recipient_emails": [ - "test1@example.com", - "test2@example.com" - ] -} -group_id = "test_url_param" -response = sg.client.asm.groups._(group_id).suppressions.post(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve all suppressions for a suppression group # -# GET /asm/groups/{group_id}/suppressions # - -group_id = "test_url_param" -response = sg.client.asm.groups._(group_id).suppressions.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Search for suppressions within a group # -# POST /asm/groups/{group_id}/suppressions/search # - -data = { - "recipient_emails": [ - "exists1@example.com", - "exists2@example.com", - "doesnotexists@example.com" - ] -} -group_id = "test_url_param" -response = sg.client.asm.groups._(group_id).suppressions.search.post(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Delete a suppression from a suppression group # -# DELETE /asm/groups/{group_id}/suppressions/{email} # - -group_id = "test_url_param" -email = "test_url_param" -response = sg.client.asm.groups._(group_id).suppressions._(email).delete() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve all suppressions # -# GET /asm/suppressions # - -response = sg.client.asm.suppressions.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Add recipient addresses to the global suppression group. # -# POST /asm/suppressions/global # - -data = { - "recipient_emails": [ - "test1@example.com", - "test2@example.com" - ] -} -response = sg.client.asm.suppressions._("global").post(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve a Global Suppression # -# GET /asm/suppressions/global/{email} # - -email = "test_url_param" -response = sg.client.asm.suppressions._("global")._(email).get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Delete a Global Suppression # -# DELETE /asm/suppressions/global/{email} # - -email = "test_url_param" -response = sg.client.asm.suppressions._("global")._(email).delete() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve all suppression groups for an email address # -# GET /asm/suppressions/{email} # - -email = "test_url_param" -response = sg.client.asm.suppressions._(email).get() -print(response.status_code) -print(response.body) -print(response.headers) - diff --git a/docker/examples/browsers/browsers.py b/docker/examples/browsers/browsers.py deleted file mode 100644 index c123c12e5..000000000 --- a/docker/examples/browsers/browsers.py +++ /dev/null @@ -1,17 +0,0 @@ -import sendgrid -import json -import os - - -sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) - -################################################## -# Retrieve email statistics by browser. # -# GET /browsers/stats # - -params = {'end_date': '2016-04-01', 'aggregated_by': 'day', 'browsers': 'test_string', 'limit': 'test_string', 'offset': 'test_string', 'start_date': '2016-01-01'} -response = sg.client.browsers.stats.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - diff --git a/docker/examples/campaigns/campaigns.py b/docker/examples/campaigns/campaigns.py deleted file mode 100644 index c77fc878b..000000000 --- a/docker/examples/campaigns/campaigns.py +++ /dev/null @@ -1,154 +0,0 @@ -import sendgrid -import json -import os - - -sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) - -################################################## -# Create a Campaign # -# POST /campaigns # - -data = { - "categories": [ - "spring line" - ], - "custom_unsubscribe_url": "", - "html_content": "

Check out our spring line!

", - "ip_pool": "marketing", - "list_ids": [ - 110, - 124 - ], - "plain_content": "Check out our spring line!", - "segment_ids": [ - 110 - ], - "sender_id": 124451, - "subject": "New Products for Spring!", - "suppression_group_id": 42, - "title": "March Newsletter" -} -response = sg.client.campaigns.post(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve all Campaigns # -# GET /campaigns # - -params = {'limit': 1, 'offset': 1} -response = sg.client.campaigns.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Update a Campaign # -# PATCH /campaigns/{campaign_id} # - -data = { - "categories": [ - "summer line" - ], - "html_content": "

Check out our summer line!

", - "plain_content": "Check out our summer line!", - "subject": "New Products for Summer!", - "title": "May Newsletter" -} -campaign_id = "test_url_param" -response = sg.client.campaigns._(campaign_id).patch(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve a single campaign # -# GET /campaigns/{campaign_id} # - -campaign_id = "test_url_param" -response = sg.client.campaigns._(campaign_id).get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Delete a Campaign # -# DELETE /campaigns/{campaign_id} # - -campaign_id = "test_url_param" -response = sg.client.campaigns._(campaign_id).delete() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Update a Scheduled Campaign # -# PATCH /campaigns/{campaign_id}/schedules # - -data = { - "send_at": 1489451436 -} -campaign_id = "test_url_param" -response = sg.client.campaigns._(campaign_id).schedules.patch(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Schedule a Campaign # -# POST /campaigns/{campaign_id}/schedules # - -data = { - "send_at": 1489771528 -} -campaign_id = "test_url_param" -response = sg.client.campaigns._(campaign_id).schedules.post(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# View Scheduled Time of a Campaign # -# GET /campaigns/{campaign_id}/schedules # - -campaign_id = "test_url_param" -response = sg.client.campaigns._(campaign_id).schedules.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Unschedule a Scheduled Campaign # -# DELETE /campaigns/{campaign_id}/schedules # - -campaign_id = "test_url_param" -response = sg.client.campaigns._(campaign_id).schedules.delete() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Send a Campaign # -# POST /campaigns/{campaign_id}/schedules/now # - -campaign_id = "test_url_param" -response = sg.client.campaigns._(campaign_id).schedules.now.post() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Send a Test Campaign # -# POST /campaigns/{campaign_id}/schedules/test # - -data = { - "to": "your.email@example.com" -} -campaign_id = "test_url_param" -response = sg.client.campaigns._(campaign_id).schedules.test.post(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - diff --git a/docker/examples/categories/categories.py b/docker/examples/categories/categories.py deleted file mode 100644 index 7984f0fe0..000000000 --- a/docker/examples/categories/categories.py +++ /dev/null @@ -1,37 +0,0 @@ -import sendgrid -import json -import os - - -sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) - -################################################## -# Retrieve all categories # -# GET /categories # - -params = {'category': 'test_string', 'limit': 1, 'offset': 1} -response = sg.client.categories.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve Email Statistics for Categories # -# GET /categories/stats # - -params = {'end_date': '2016-04-01', 'aggregated_by': 'day', 'limit': 1, 'offset': 1, 'start_date': '2016-01-01', 'categories': 'test_string'} -response = sg.client.categories.stats.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve sums of email stats for each category [Needs: Stats object defined, has category ID?] # -# GET /categories/stats/sums # - -params = {'end_date': '2016-04-01', 'aggregated_by': 'day', 'limit': 1, 'sort_by_metric': 'test_string', 'offset': 1, 'start_date': '2016-01-01', 'sort_by_direction': 'asc'} -response = sg.client.categories.stats.sums.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - diff --git a/docker/examples/clients/clients.py b/docker/examples/clients/clients.py deleted file mode 100644 index 7831ef78f..000000000 --- a/docker/examples/clients/clients.py +++ /dev/null @@ -1,28 +0,0 @@ -import sendgrid -import json -import os - - -sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) - -################################################## -# Retrieve email statistics by client type. # -# GET /clients/stats # - -params = {'aggregated_by': 'day', 'start_date': '2016-01-01', 'end_date': '2016-04-01'} -response = sg.client.clients.stats.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve stats by a specific client type. # -# GET /clients/{client_type}/stats # - -params = {'aggregated_by': 'day', 'start_date': '2016-01-01', 'end_date': '2016-04-01'} -client_type = "test_url_param" -response = sg.client.clients._(client_type).stats.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - diff --git a/docker/examples/contactdb/contactdb.py b/docker/examples/contactdb/contactdb.py deleted file mode 100644 index c234d7724..000000000 --- a/docker/examples/contactdb/contactdb.py +++ /dev/null @@ -1,396 +0,0 @@ -import sendgrid -import json -import os - - -sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) - -################################################## -# Create a Custom Field # -# POST /contactdb/custom_fields # - -data = { - "name": "pet", - "type": "text" -} -response = sg.client.contactdb.custom_fields.post(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve all custom fields # -# GET /contactdb/custom_fields # - -response = sg.client.contactdb.custom_fields.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve a Custom Field # -# GET /contactdb/custom_fields/{custom_field_id} # - -custom_field_id = "test_url_param" -response = sg.client.contactdb.custom_fields._(custom_field_id).get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Delete a Custom Field # -# DELETE /contactdb/custom_fields/{custom_field_id} # - -custom_field_id = "test_url_param" -response = sg.client.contactdb.custom_fields._(custom_field_id).delete() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Create a List # -# POST /contactdb/lists # - -data = { - "name": "your list name" -} -response = sg.client.contactdb.lists.post(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve all lists # -# GET /contactdb/lists # - -response = sg.client.contactdb.lists.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Delete Multiple lists # -# DELETE /contactdb/lists # - -data = [ - 1, - 2, - 3, - 4 -] -response = sg.client.contactdb.lists.delete(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Update a List # -# PATCH /contactdb/lists/{list_id} # - -data = { - "name": "newlistname" -} -params = {'list_id': 1} -list_id = "test_url_param" -response = sg.client.contactdb.lists._(list_id).patch(request_body=data, query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve a single list # -# GET /contactdb/lists/{list_id} # - -params = {'list_id': 1} -list_id = "test_url_param" -response = sg.client.contactdb.lists._(list_id).get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Delete a List # -# DELETE /contactdb/lists/{list_id} # - -params = {'delete_contacts': 'true'} -list_id = "test_url_param" -response = sg.client.contactdb.lists._(list_id).delete(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Add Multiple Recipients to a List # -# POST /contactdb/lists/{list_id}/recipients # - -data = [ - "recipient_id1", - "recipient_id2" -] -list_id = "test_url_param" -response = sg.client.contactdb.lists._(list_id).recipients.post(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve all recipients on a List # -# GET /contactdb/lists/{list_id}/recipients # - -params = {'page': 1, 'page_size': 1} -list_id = "test_url_param" -response = sg.client.contactdb.lists._(list_id).recipients.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Add a Single Recipient to a List # -# POST /contactdb/lists/{list_id}/recipients/{recipient_id} # - -list_id = "test_url_param" -recipient_id = "test_url_param" -response = sg.client.contactdb.lists._(list_id).recipients._(recipient_id).post() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Delete a Single Recipient from a Single List # -# DELETE /contactdb/lists/{list_id}/recipients/{recipient_id} # - -params = {'recipient_id': 1, 'list_id': 1} -list_id = "test_url_param" -recipient_id = "test_url_param" -response = sg.client.contactdb.lists._(list_id).recipients._(recipient_id).delete(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Update Recipient # -# PATCH /contactdb/recipients # - -data = [ - { - "email": "jones@example.com", - "first_name": "Guy", - "last_name": "Jones" - } -] -response = sg.client.contactdb.recipients.patch(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Add recipients # -# POST /contactdb/recipients # - -data = [ - { - "age": 25, - "email": "example@example.com", - "first_name": "", - "last_name": "User" - }, - { - "age": 25, - "email": "example2@example.com", - "first_name": "Example", - "last_name": "User" - } -] -response = sg.client.contactdb.recipients.post(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve recipients # -# GET /contactdb/recipients # - -params = {'page': 1, 'page_size': 1} -response = sg.client.contactdb.recipients.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Delete Recipient # -# DELETE /contactdb/recipients # - -data = [ - "recipient_id1", - "recipient_id2" -] -response = sg.client.contactdb.recipients.delete(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve the count of billable recipients # -# GET /contactdb/recipients/billable_count # - -response = sg.client.contactdb.recipients.billable_count.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve a Count of Recipients # -# GET /contactdb/recipients/count # - -response = sg.client.contactdb.recipients.count.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve recipients matching search criteria # -# GET /contactdb/recipients/search # - -params = {'{field_name}': 'test_string'} -response = sg.client.contactdb.recipients.search.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve a single recipient # -# GET /contactdb/recipients/{recipient_id} # - -recipient_id = "test_url_param" -response = sg.client.contactdb.recipients._(recipient_id).get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Delete a Recipient # -# DELETE /contactdb/recipients/{recipient_id} # - -recipient_id = "test_url_param" -response = sg.client.contactdb.recipients._(recipient_id).delete() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve the lists that a recipient is on # -# GET /contactdb/recipients/{recipient_id}/lists # - -recipient_id = "test_url_param" -response = sg.client.contactdb.recipients._(recipient_id).lists.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve reserved fields # -# GET /contactdb/reserved_fields # - -response = sg.client.contactdb.reserved_fields.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Create a Segment # -# POST /contactdb/segments # - -data = { - "conditions": [ - { - "and_or": "", - "field": "last_name", - "operator": "eq", - "value": "Miller" - }, - { - "and_or": "and", - "field": "last_clicked", - "operator": "gt", - "value": "01/02/2015" - }, - { - "and_or": "or", - "field": "clicks.campaign_identifier", - "operator": "eq", - "value": "513" - } - ], - "list_id": 4, - "name": "Last Name Miller" -} -response = sg.client.contactdb.segments.post(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve all segments # -# GET /contactdb/segments # - -response = sg.client.contactdb.segments.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Update a segment # -# PATCH /contactdb/segments/{segment_id} # - -data = { - "conditions": [ - { - "and_or": "", - "field": "last_name", - "operator": "eq", - "value": "Miller" - } - ], - "list_id": 5, - "name": "The Millers" -} -params = {'segment_id': 'test_string'} -segment_id = "test_url_param" -response = sg.client.contactdb.segments._(segment_id).patch(request_body=data, query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve a segment # -# GET /contactdb/segments/{segment_id} # - -params = {'segment_id': 1} -segment_id = "test_url_param" -response = sg.client.contactdb.segments._(segment_id).get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Delete a segment # -# DELETE /contactdb/segments/{segment_id} # - -params = {'delete_contacts': 'true'} -segment_id = "test_url_param" -response = sg.client.contactdb.segments._(segment_id).delete(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve recipients on a segment # -# GET /contactdb/segments/{segment_id}/recipients # - -params = {'page': 1, 'page_size': 1} -segment_id = "test_url_param" -response = sg.client.contactdb.segments._(segment_id).recipients.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - diff --git a/docker/examples/devices/devices.py b/docker/examples/devices/devices.py deleted file mode 100644 index 108e98452..000000000 --- a/docker/examples/devices/devices.py +++ /dev/null @@ -1,17 +0,0 @@ -import sendgrid -import json -import os - - -sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) - -################################################## -# Retrieve email statistics by device type. # -# GET /devices/stats # - -params = {'aggregated_by': 'day', 'limit': 1, 'start_date': '2016-01-01', 'end_date': '2016-04-01', 'offset': 1} -response = sg.client.devices.stats.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - diff --git a/docker/examples/geo/geo.py b/docker/examples/geo/geo.py deleted file mode 100644 index 7d58ec085..000000000 --- a/docker/examples/geo/geo.py +++ /dev/null @@ -1,17 +0,0 @@ -import sendgrid -import json -import os - - -sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) - -################################################## -# Retrieve email statistics by country and state/province. # -# GET /geo/stats # - -params = {'end_date': '2016-04-01', 'country': 'US', 'aggregated_by': 'day', 'limit': 1, 'offset': 1, 'start_date': '2016-01-01'} -response = sg.client.geo.stats.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - diff --git a/docker/examples/helpers/mail/mail_example.py b/docker/examples/helpers/mail/mail_example.py deleted file mode 100644 index bfd8ea718..000000000 --- a/docker/examples/helpers/mail/mail_example.py +++ /dev/null @@ -1,219 +0,0 @@ -import json -import os -import urllib2 -from sendgrid.helpers.mail import * -from sendgrid import * - -# NOTE: you will need move this file to the root -# directory of this project to execute properly. - - -def build_hello_email(): - """Minimum required to send an email""" - from_email = Email("test@example.com") - subject = "Hello World from the SendGrid Python Library" - to_email = Email("test@example.com") - content = Content("text/plain", "some text here") - mail = Mail(from_email, subject, to_email, content) - mail.personalizations[0].add_to(Email("test2@example.com")) - - return mail.get() - - -def build_personalization(personalization): - """Build personalization mock instance from a mock dict""" - mock_personalization = Personalization() - for to_addr in personalization['to_list']: - personalization.add_to(to_addr) - - for cc_addr in personalization['cc_list']: - personalization.add_to(cc_addr) - - for bcc_addr in personalization['bcc_list']: - personalization.add_bc(bcc_addr) - - for header in personalization['headers']: - personalization.add_header(header) - - for substitution in personalization['substitutions']: - personalization.add_substitution(substitution) - - for arg in personalization['custom_args']: - personalization.add_custom_arg(arg) - - personalization.subject = personalization['subject'] - personalization.send_at = personalization['send_at'] - return mock_personalization - - -def get_mock_personalization_dict(): - """Get a dict of personalization mock.""" - mock_pers = dict() - - mock_pers['to_list'] = [Email("test1@example.com", - "Example User"), - Email("test2@example.com", - "Example User")] - - mock_pers['cc_list'] = [Email("test3@example.com", - "Example User"), - Email("test4@example.com", - "Example User")] - - mock_pers['bcc_list'] = [Email("test5@example.com"), - Email("test6@example.com")] - - mock_pers['subject'] = ("Hello World from the Personalized " - "SendGrid Python Library") - - mock_pers['headers'] = [Header("X-Test", "test"), - Header("X-Mock", "true")] - - mock_pers['substitutions'] = [Substitution("%name%", "Example User"), - Substitution("%city%", "Denver")] - - mock_pers['custom_args'] = [CustomArg("user_id", "343"), - CustomArg("type", "marketing")] - - mock_pers['send_at'] = 1443636843 - return mock_pers - - -def build_attachment1(): - """Build attachment mock.""" - attachment = Attachment() - attachment.content = ("TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNl" - "Y3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12") - attachment.type = "application/pdf" - attachment.filename = "balance_001.pdf" - attachment.disposition = "attachment" - attachment.content_id = "Balance Sheet" - return attachment - - -def build_attachment2(): - """Build attachment mock.""" - attachment = Attachment() - attachment.content = "BwdW" - attachment.type = "image/png" - attachment.filename = "banner.png" - attachment.disposition = "inline" - attachment.content_id = "Banner" - return attachment - - -def build_mail_settings(): - """Build mail settings mock.""" - mail_settings = MailSettings() - mail_settings.bcc_settings = BCCSettings(True, Email("test@example.com")) - mail_settings.bypass_list_management = BypassListManagement(True) - mail_settings.footer_settings = FooterSettings(True, "Footer Text", - ("Footer " - "Text")) - mail_settings.sandbox_mode = SandBoxMode(True) - mail_settings.spam_check = SpamCheck(True, 1, - "https://spamcatcher.sendgrid.com") - return mail_settings - - -def build_tracking_settings(): - """Build tracking settings mock.""" - tracking_settings = TrackingSettings() - tracking_settings.click_tracking = ClickTracking(True, True) - tracking_settings.open_tracking = OpenTracking(True, - ("Optional tag to " - "replace with the" - "open image in the " - "body of the message")) - - subs_track = SubscriptionTracking(True, - ("text to insert into the " - "text/plain portion of the" - " message"), - ("html to insert " - "into the text/html portion of " - "the message"), - ("Optional tag to replace with " - "the open image in the body of " - "the message")) - - tracking_settings.subscription_tracking = subs_track - tracking_settings.ganalytics = Ganalytics(True, "some source", - "some medium", "some term", - "some_content", "some_campaign") - return tracking_settings - - -def build_kitchen_sink(): - """All settings set""" - mail = Mail() - - mail.from_email = Email("test@example.com", "Example User") - mail.subject = "Hello World from the SendGrid Python Library" - - personalization = get_mock_personalization_dict() - mail.add_personalization(build_personalization(personalization)) - mail.add_personalization(build_personalization(personalization)) - - mail.add_content(Content("text/plain", "some text here")) - mail.add_content(Content("text/html", ("some text " - "here"))) - - mail.add_attachment(build_attachment1()) - mail.add_attachment(build_attachment2()) - - mail.template_id = "13b8f94f-bcae-4ec6-b752-70d6cb59f932" - - mail.add_section(Section("%section1%", "Substitution Text for Section 1")) - mail.add_section(Section("%section2%", "Substitution Text for Section 2")) - - mail.add_header(Header("X-Test1", "test1")) - mail.add_header(Header("X-Test3", "test2")) - - mail.add_category(Category("May")) - mail.add_category(Category("2016")) - - mail.add_custom_arg(CustomArg("campaign", "welcome")) - mail.add_custom_arg(CustomArg("weekday", "morning")) - - mail.send_at = 1443636842 - - # This must be a valid [batch ID] - # (https://sendgrid.com/docs/API_Reference/SMTP_API/scheduling_parameters.html) to work - # mail.set_batch_id("N2VkYjBjYWItMGU4OC0xMWU2LWJhMzYtZjQ1Yzg5OTBkNzkxLWM5ZTUyZjNhOA") - mail.asm = ASM(99, [4, 5, 6, 7, 8]) - mail.ip_pool_name = "24" - mail.mail_settings = build_mail_settings() - mail.tracking_settings = build_tracking_settings() - mail.reply_to = Email("test@example.com") - - return mail.get() - - -def send_hello_email(): - # Assumes you set your environment variable: - # https://github.com/sendgrid/sendgrid-python/blob/master/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key - sg = SendGridAPIClient() - data = build_hello_email() - response = sg.client.mail.send.post(request_body=data) - print(response.status_code) - print(response.headers) - print(response.body) - - -def send_kitchen_sink(): - # Assumes you set your environment variable: - # https://github.com/sendgrid/sendgrid-python/blob/master/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key - sg = SendGridAPIClient() - data = build_kitchen_sink() - response = sg.client.mail.send.post(request_body=data) - print(response.status_code) - print(response.headers) - print(response.body) - - -# this will actually send an email -send_hello_email() - -# this will only send an email if you set SandBox Mode to False -send_kitchen_sink() diff --git a/docker/examples/ips/ips.py b/docker/examples/ips/ips.py deleted file mode 100644 index 6c48ae306..000000000 --- a/docker/examples/ips/ips.py +++ /dev/null @@ -1,155 +0,0 @@ -import sendgrid -import json -import os - - -sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) - -################################################## -# Retrieve all IP addresses # -# GET /ips # - -params = {'subuser': 'test_string', 'ip': 'test_string', 'limit': 1, 'exclude_whitelabels': 'true', 'offset': 1} -response = sg.client.ips.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve all assigned IPs # -# GET /ips/assigned # - -response = sg.client.ips.assigned.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Create an IP pool. # -# POST /ips/pools # - -data = { - "name": "marketing" -} -response = sg.client.ips.pools.post(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve all IP pools. # -# GET /ips/pools # - -response = sg.client.ips.pools.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Update an IP pools name. # -# PUT /ips/pools/{pool_name} # - -data = { - "name": "new_pool_name" -} -pool_name = "test_url_param" -response = sg.client.ips.pools._(pool_name).put(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve all IPs in a specified pool. # -# GET /ips/pools/{pool_name} # - -pool_name = "test_url_param" -response = sg.client.ips.pools._(pool_name).get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Delete an IP pool. # -# DELETE /ips/pools/{pool_name} # - -pool_name = "test_url_param" -response = sg.client.ips.pools._(pool_name).delete() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Add an IP address to a pool # -# POST /ips/pools/{pool_name}/ips # - -data = { - "ip": "0.0.0.0" -} -pool_name = "test_url_param" -response = sg.client.ips.pools._(pool_name).ips.post(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Remove an IP address from a pool. # -# DELETE /ips/pools/{pool_name}/ips/{ip} # - -pool_name = "test_url_param" -ip = "test_url_param" -response = sg.client.ips.pools._(pool_name).ips._(ip).delete() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Add an IP to warmup # -# POST /ips/warmup # - -data = { - "ip": "0.0.0.0" -} -response = sg.client.ips.warmup.post(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve all IPs currently in warmup # -# GET /ips/warmup # - -response = sg.client.ips.warmup.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve warmup status for a specific IP address # -# GET /ips/warmup/{ip_address} # - -ip_address = "test_url_param" -response = sg.client.ips.warmup._(ip_address).get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Remove an IP from warmup # -# DELETE /ips/warmup/{ip_address} # - -ip_address = "test_url_param" -response = sg.client.ips.warmup._(ip_address).delete() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve all IP pools an IP address belongs to # -# GET /ips/{ip_address} # - -ip_address = "test_url_param" -response = sg.client.ips._(ip_address).get() -print(response.status_code) -print(response.body) -print(response.headers) - diff --git a/docker/examples/mail/mail.py b/docker/examples/mail/mail.py deleted file mode 100644 index fef420e87..000000000 --- a/docker/examples/mail/mail.py +++ /dev/null @@ -1,174 +0,0 @@ -import sendgrid -import json -import os - - -sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) - -################################################## -# Create a batch ID # -# POST /mail/batch # - -response = sg.client.mail.batch.post() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Validate batch ID # -# GET /mail/batch/{batch_id} # - -batch_id = "test_url_param" -response = sg.client.mail.batch._(batch_id).get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# v3 Mail Send # -# POST /mail/send # -# This endpoint has a helper, check it out [here](https://github.com/sendgrid/sendgrid-python/blob/master/sendgrid/helpers/mail/README.md). - -data = { - "asm": { - "group_id": 1, - "groups_to_display": [ - 1, - 2, - 3 - ] - }, - "attachments": [ - { - "content": "[BASE64 encoded content block here]", - "content_id": "ii_139db99fdb5c3704", - "disposition": "inline", - "filename": "file1.jpg", - "name": "file1", - "type": "jpg" - } - ], - "batch_id": "[YOUR BATCH ID GOES HERE]", - "categories": [ - "category1", - "category2" - ], - "content": [ - { - "type": "text/html", - "value": "

Hello, world!

" - } - ], - "custom_args": { - "New Argument 1": "New Value 1", - "activationAttempt": "1", - "customerAccountNumber": "[CUSTOMER ACCOUNT NUMBER GOES HERE]" - }, - "from": { - "email": "sam.smith@example.com", - "name": "Sam Smith" - }, - "headers": {}, - "ip_pool_name": "[YOUR POOL NAME GOES HERE]", - "mail_settings": { - "bcc": { - "email": "ben.doe@example.com", - "enable": True - }, - "bypass_list_management": { - "enable": True - }, - "footer": { - "enable": True, - "html": "

Thanks
The SendGrid Team

", - "text": "Thanks,/n The SendGrid Team" - }, - "sandbox_mode": { - "enable": False - }, - "spam_check": { - "enable": True, - "post_to_url": "http://example.com/compliance", - "threshold": 3 - } - }, - "personalizations": [ - { - "bcc": [ - { - "email": "sam.doe@example.com", - "name": "Sam Doe" - } - ], - "cc": [ - { - "email": "jane.doe@example.com", - "name": "Jane Doe" - } - ], - "custom_args": { - "New Argument 1": "New Value 1", - "activationAttempt": "1", - "customerAccountNumber": "[CUSTOMER ACCOUNT NUMBER GOES HERE]" - }, - "headers": { - "X-Accept-Language": "en", - "X-Mailer": "MyApp" - }, - "send_at": 1409348513, - "subject": "Hello, World!", - "substitutions": { - "id": "substitutions", - "type": "object" - }, - "to": [ - { - "email": "john.doe@example.com", - "name": "John Doe" - } - ] - } - ], - "reply_to": { - "email": "sam.smith@example.com", - "name": "Sam Smith" - }, - "sections": { - "section": { - ":sectionName1": "section 1 text", - ":sectionName2": "section 2 text" - } - }, - "send_at": 1409348513, - "subject": "Hello, World!", - "template_id": "[YOUR TEMPLATE ID GOES HERE]", - "tracking_settings": { - "click_tracking": { - "enable": True, - "enable_text": True - }, - "ganalytics": { - "enable": True, - "utm_campaign": "[NAME OF YOUR REFERRER SOURCE]", - "utm_content": "[USE THIS SPACE TO DIFFERENTIATE YOUR EMAIL FROM ADS]", - "utm_medium": "[NAME OF YOUR MARKETING MEDIUM e.g. email]", - "utm_name": "[NAME OF YOUR CAMPAIGN]", - "utm_term": "[IDENTIFY PAID KEYWORDS HERE]" - }, - "open_tracking": { - "enable": True, - "substitution_tag": "%opentrack" - }, - "subscription_tracking": { - "enable": True, - "html": "If you would like to unsubscribe and stop receiving these emails <% clickhere %>.", - "substitution_tag": "<%click here%>", - "text": "If you would like to unsubscribe and stop receiveing these emails <% click here %>." - } - } -} -response = sg.client.mail.send.post(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - diff --git a/docker/examples/mailboxproviders/mailboxproviders.py b/docker/examples/mailboxproviders/mailboxproviders.py deleted file mode 100644 index 1b75ecac5..000000000 --- a/docker/examples/mailboxproviders/mailboxproviders.py +++ /dev/null @@ -1,17 +0,0 @@ -import sendgrid -import json -import os - - -sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) - -################################################## -# Retrieve email statistics by mailbox provider. # -# GET /mailbox_providers/stats # - -params = {'end_date': '2016-04-01', 'mailbox_providers': 'test_string', 'aggregated_by': 'day', 'limit': 1, 'offset': 1, 'start_date': '2016-01-01'} -response = sg.client.mailbox_providers.stats.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - diff --git a/docker/examples/mailsettings/mailsettings.py b/docker/examples/mailsettings/mailsettings.py deleted file mode 100644 index 18c57b960..000000000 --- a/docker/examples/mailsettings/mailsettings.py +++ /dev/null @@ -1,220 +0,0 @@ -import sendgrid -import json -import os - - -sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) - -################################################## -# Retrieve all mail settings # -# GET /mail_settings # - -params = {'limit': 1, 'offset': 1} -response = sg.client.mail_settings.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Update address whitelist mail settings # -# PATCH /mail_settings/address_whitelist # - -data = { - "enabled": True, - "list": [ - "email1@example.com", - "example.com" - ] -} -response = sg.client.mail_settings.address_whitelist.patch(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve address whitelist mail settings # -# GET /mail_settings/address_whitelist # - -response = sg.client.mail_settings.address_whitelist.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Update BCC mail settings # -# PATCH /mail_settings/bcc # - -data = { - "email": "email@example.com", - "enabled": False -} -response = sg.client.mail_settings.bcc.patch(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve all BCC mail settings # -# GET /mail_settings/bcc # - -response = sg.client.mail_settings.bcc.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Update bounce purge mail settings # -# PATCH /mail_settings/bounce_purge # - -data = { - "enabled": True, - "hard_bounces": 5, - "soft_bounces": 5 -} -response = sg.client.mail_settings.bounce_purge.patch(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve bounce purge mail settings # -# GET /mail_settings/bounce_purge # - -response = sg.client.mail_settings.bounce_purge.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Update footer mail settings # -# PATCH /mail_settings/footer # - -data = { - "enabled": True, - "html_content": "...", - "plain_content": "..." -} -response = sg.client.mail_settings.footer.patch(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve footer mail settings # -# GET /mail_settings/footer # - -response = sg.client.mail_settings.footer.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Update forward bounce mail settings # -# PATCH /mail_settings/forward_bounce # - -data = { - "email": "example@example.com", - "enabled": True -} -response = sg.client.mail_settings.forward_bounce.patch(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve forward bounce mail settings # -# GET /mail_settings/forward_bounce # - -response = sg.client.mail_settings.forward_bounce.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Update forward spam mail settings # -# PATCH /mail_settings/forward_spam # - -data = { - "email": "", - "enabled": False -} -response = sg.client.mail_settings.forward_spam.patch(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve forward spam mail settings # -# GET /mail_settings/forward_spam # - -response = sg.client.mail_settings.forward_spam.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Update plain content mail settings # -# PATCH /mail_settings/plain_content # - -data = { - "enabled": False -} -response = sg.client.mail_settings.plain_content.patch(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve plain content mail settings # -# GET /mail_settings/plain_content # - -response = sg.client.mail_settings.plain_content.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Update spam check mail settings # -# PATCH /mail_settings/spam_check # - -data = { - "enabled": True, - "max_score": 5, - "url": "url" -} -response = sg.client.mail_settings.spam_check.patch(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve spam check mail settings # -# GET /mail_settings/spam_check # - -response = sg.client.mail_settings.spam_check.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Update template mail settings # -# PATCH /mail_settings/template # - -data = { - "enabled": True, - "html_content": "<% body %>" -} -response = sg.client.mail_settings.template.patch(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve legacy template mail settings # -# GET /mail_settings/template # - -response = sg.client.mail_settings.template.get() -print(response.status_code) -print(response.body) -print(response.headers) - diff --git a/docker/examples/partnersettings/partnersettings.py b/docker/examples/partnersettings/partnersettings.py deleted file mode 100644 index 37f77f4e6..000000000 --- a/docker/examples/partnersettings/partnersettings.py +++ /dev/null @@ -1,40 +0,0 @@ -import sendgrid -import json -import os - - -sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) - -################################################## -# Returns a list of all partner settings. # -# GET /partner_settings # - -params = {'limit': 1, 'offset': 1} -response = sg.client.partner_settings.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Updates New Relic partner settings. # -# PATCH /partner_settings/new_relic # - -data = { - "enable_subuser_statistics": True, - "enabled": True, - "license_key": "" -} -response = sg.client.partner_settings.new_relic.patch(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Returns all New Relic partner settings. # -# GET /partner_settings/new_relic # - -response = sg.client.partner_settings.new_relic.get() -print(response.status_code) -print(response.body) -print(response.headers) - diff --git a/docker/examples/scopes/scopes.py b/docker/examples/scopes/scopes.py deleted file mode 100644 index 124f77d39..000000000 --- a/docker/examples/scopes/scopes.py +++ /dev/null @@ -1,16 +0,0 @@ -import sendgrid -import json -import os - - -sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) - -################################################## -# Retrieve a list of scopes for which this user has access. # -# GET /scopes # - -response = sg.client.scopes.get() -print(response.status_code) -print(response.body) -print(response.headers) - diff --git a/docker/examples/senders/senders.py b/docker/examples/senders/senders.py deleted file mode 100644 index f21459b71..000000000 --- a/docker/examples/senders/senders.py +++ /dev/null @@ -1,99 +0,0 @@ -import sendgrid -import json -import os - - -sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) - -################################################## -# Create a Sender Identity # -# POST /senders # - -data = { - "address": "123 Elm St.", - "address_2": "Apt. 456", - "city": "Denver", - "country": "United States", - "from": { - "email": "from@example.com", - "name": "Example INC" - }, - "nickname": "My Sender ID", - "reply_to": { - "email": "replyto@example.com", - "name": "Example INC" - }, - "state": "Colorado", - "zip": "80202" -} -response = sg.client.senders.post(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Get all Sender Identities # -# GET /senders # - -response = sg.client.senders.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Update a Sender Identity # -# PATCH /senders/{sender_id} # - -data = { - "address": "123 Elm St.", - "address_2": "Apt. 456", - "city": "Denver", - "country": "United States", - "from": { - "email": "from@example.com", - "name": "Example INC" - }, - "nickname": "My Sender ID", - "reply_to": { - "email": "replyto@example.com", - "name": "Example INC" - }, - "state": "Colorado", - "zip": "80202" -} -sender_id = "test_url_param" -response = sg.client.senders._(sender_id).patch(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# View a Sender Identity # -# GET /senders/{sender_id} # - -sender_id = "test_url_param" -response = sg.client.senders._(sender_id).get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Delete a Sender Identity # -# DELETE /senders/{sender_id} # - -sender_id = "test_url_param" -response = sg.client.senders._(sender_id).delete() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Resend Sender Identity Verification # -# POST /senders/{sender_id}/resend_verification # - -sender_id = "test_url_param" -response = sg.client.senders._(sender_id).resend_verification.post() -print(response.status_code) -print(response.body) -print(response.headers) - diff --git a/docker/examples/stats/stats.py b/docker/examples/stats/stats.py deleted file mode 100644 index a7bf3362e..000000000 --- a/docker/examples/stats/stats.py +++ /dev/null @@ -1,17 +0,0 @@ -import sendgrid -import json -import os - - -sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) - -################################################## -# Retrieve global email statistics # -# GET /stats # - -params = {'aggregated_by': 'day', 'limit': 1, 'start_date': '2016-01-01', 'end_date': '2016-04-01', 'offset': 1} -response = sg.client.stats.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - diff --git a/docker/examples/subusers/subusers.py b/docker/examples/subusers/subusers.py deleted file mode 100644 index 6aa91e535..000000000 --- a/docker/examples/subusers/subusers.py +++ /dev/null @@ -1,170 +0,0 @@ -import sendgrid -import json -import os - - -sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) - -################################################## -# Create Subuser # -# POST /subusers # - -data = { - "email": "John@example.com", - "ips": [ - "1.1.1.1", - "2.2.2.2" - ], - "password": "johns_password", - "username": "John@example.com" -} -response = sg.client.subusers.post(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# List all Subusers # -# GET /subusers # - -params = {'username': 'test_string', 'limit': 1, 'offset': 1} -response = sg.client.subusers.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve Subuser Reputations # -# GET /subusers/reputations # - -params = {'usernames': 'test_string'} -response = sg.client.subusers.reputations.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve email statistics for your subusers. # -# GET /subusers/stats # - -params = {'end_date': '2016-04-01', 'aggregated_by': 'day', 'limit': 1, 'offset': 1, 'start_date': '2016-01-01', 'subusers': 'test_string'} -response = sg.client.subusers.stats.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve monthly stats for all subusers # -# GET /subusers/stats/monthly # - -params = {'subuser': 'test_string', 'limit': 1, 'sort_by_metric': 'test_string', 'offset': 1, 'date': 'test_string', 'sort_by_direction': 'asc'} -response = sg.client.subusers.stats.monthly.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve the totals for each email statistic metric for all subusers. # -# GET /subusers/stats/sums # - -params = {'end_date': '2016-04-01', 'aggregated_by': 'day', 'limit': 1, 'sort_by_metric': 'test_string', 'offset': 1, 'start_date': '2016-01-01', 'sort_by_direction': 'asc'} -response = sg.client.subusers.stats.sums.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Enable/disable a subuser # -# PATCH /subusers/{subuser_name} # - -data = { - "disabled": False -} -subuser_name = "test_url_param" -response = sg.client.subusers._(subuser_name).patch(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Delete a subuser # -# DELETE /subusers/{subuser_name} # - -subuser_name = "test_url_param" -response = sg.client.subusers._(subuser_name).delete() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Update IPs assigned to a subuser # -# PUT /subusers/{subuser_name}/ips # - -data = [ - "127.0.0.1" -] -subuser_name = "test_url_param" -response = sg.client.subusers._(subuser_name).ips.put(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Update Monitor Settings for a subuser # -# PUT /subusers/{subuser_name}/monitor # - -data = { - "email": "example@example.com", - "frequency": 500 -} -subuser_name = "test_url_param" -response = sg.client.subusers._(subuser_name).monitor.put(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Create monitor settings # -# POST /subusers/{subuser_name}/monitor # - -data = { - "email": "example@example.com", - "frequency": 50000 -} -subuser_name = "test_url_param" -response = sg.client.subusers._(subuser_name).monitor.post(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve monitor settings for a subuser # -# GET /subusers/{subuser_name}/monitor # - -subuser_name = "test_url_param" -response = sg.client.subusers._(subuser_name).monitor.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Delete monitor settings # -# DELETE /subusers/{subuser_name}/monitor # - -subuser_name = "test_url_param" -response = sg.client.subusers._(subuser_name).monitor.delete() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve the monthly email statistics for a single subuser # -# GET /subusers/{subuser_name}/stats/monthly # - -params = {'date': 'test_string', 'sort_by_direction': 'asc', 'limit': 1, 'sort_by_metric': 'test_string', 'offset': 1} -subuser_name = "test_url_param" -response = sg.client.subusers._(subuser_name).stats.monthly.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - diff --git a/docker/examples/suppression/suppression.py b/docker/examples/suppression/suppression.py deleted file mode 100644 index abdaef76d..000000000 --- a/docker/examples/suppression/suppression.py +++ /dev/null @@ -1,202 +0,0 @@ -import sendgrid -import json -import os - - -sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) - -################################################## -# Retrieve all blocks # -# GET /suppression/blocks # - -params = {'start_time': 1, 'limit': 1, 'end_time': 1, 'offset': 1} -response = sg.client.suppression.blocks.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Delete blocks # -# DELETE /suppression/blocks # - -data = { - "delete_all": False, - "emails": [ - "example1@example.com", - "example2@example.com" - ] -} -response = sg.client.suppression.blocks.delete(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve a specific block # -# GET /suppression/blocks/{email} # - -email = "test_url_param" -response = sg.client.suppression.blocks._(email).get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Delete a specific block # -# DELETE /suppression/blocks/{email} # - -email = "test_url_param" -response = sg.client.suppression.blocks._(email).delete() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve all bounces # -# GET /suppression/bounces # - -params = {'start_time': 1, 'end_time': 1} -response = sg.client.suppression.bounces.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Delete bounces # -# DELETE /suppression/bounces # - -data = { - "delete_all": True, - "emails": [ - "example@example.com", - "example2@example.com" - ] -} -response = sg.client.suppression.bounces.delete(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve a Bounce # -# GET /suppression/bounces/{email} # - -email = "test_url_param" -response = sg.client.suppression.bounces._(email).get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Delete a bounce # -# DELETE /suppression/bounces/{email} # - -params = {'email_address': 'example@example.com'} -email = "test_url_param" -response = sg.client.suppression.bounces._(email).delete(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve all invalid emails # -# GET /suppression/invalid_emails # - -params = {'start_time': 1, 'limit': 1, 'end_time': 1, 'offset': 1} -response = sg.client.suppression.invalid_emails.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Delete invalid emails # -# DELETE /suppression/invalid_emails # - -data = { - "delete_all": False, - "emails": [ - "example1@example.com", - "example2@example.com" - ] -} -response = sg.client.suppression.invalid_emails.delete(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve a specific invalid email # -# GET /suppression/invalid_emails/{email} # - -email = "test_url_param" -response = sg.client.suppression.invalid_emails._(email).get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Delete a specific invalid email # -# DELETE /suppression/invalid_emails/{email} # - -email = "test_url_param" -response = sg.client.suppression.invalid_emails._(email).delete() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve a specific spam report # -# GET /suppression/spam_report/{email} # - -email = "test_url_param" -response = sg.client.suppression.spam_report._(email).get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Delete a specific spam report # -# DELETE /suppression/spam_report/{email} # - -email = "test_url_param" -response = sg.client.suppression.spam_report._(email).delete() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve all spam reports # -# GET /suppression/spam_reports # - -params = {'start_time': 1, 'limit': 1, 'end_time': 1, 'offset': 1} -response = sg.client.suppression.spam_reports.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Delete spam reports # -# DELETE /suppression/spam_reports # - -data = { - "delete_all": False, - "emails": [ - "example1@example.com", - "example2@example.com" - ] -} -response = sg.client.suppression.spam_reports.delete(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve all global suppressions # -# GET /suppression/unsubscribes # - -params = {'start_time': 1, 'limit': 1, 'end_time': 1, 'offset': 1} -response = sg.client.suppression.unsubscribes.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - diff --git a/docker/examples/templates/templates.py b/docker/examples/templates/templates.py deleted file mode 100644 index 9d3d5dd4b..000000000 --- a/docker/examples/templates/templates.py +++ /dev/null @@ -1,130 +0,0 @@ -import sendgrid -import json -import os - - -sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) - -################################################## -# Create a transactional template. # -# POST /templates # - -data = { - "name": "example_name" -} -response = sg.client.templates.post(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve all transactional templates. # -# GET /templates # - -response = sg.client.templates.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Edit a transactional template. # -# PATCH /templates/{template_id} # - -data = { - "name": "new_example_name" -} -template_id = "test_url_param" -response = sg.client.templates._(template_id).patch(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve a single transactional template. # -# GET /templates/{template_id} # - -template_id = "test_url_param" -response = sg.client.templates._(template_id).get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Delete a template. # -# DELETE /templates/{template_id} # - -template_id = "test_url_param" -response = sg.client.templates._(template_id).delete() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Create a new transactional template version. # -# POST /templates/{template_id}/versions # - -data = { - "active": 1, - "html_content": "<%body%>", - "name": "example_version_name", - "plain_content": "<%body%>", - "subject": "<%subject%>", - "template_id": "ddb96bbc-9b92-425e-8979-99464621b543" -} -template_id = "test_url_param" -response = sg.client.templates._(template_id).versions.post(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Edit a transactional template version. # -# PATCH /templates/{template_id}/versions/{version_id} # - -data = { - "active": 1, - "html_content": "<%body%>", - "name": "updated_example_name", - "plain_content": "<%body%>", - "subject": "<%subject%>" -} -template_id = "test_url_param" -version_id = "test_url_param" -response = sg.client.templates._(template_id).versions._(version_id).patch(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve a specific transactional template version. # -# GET /templates/{template_id}/versions/{version_id} # - -template_id = "test_url_param" -version_id = "test_url_param" -response = sg.client.templates._(template_id).versions._(version_id).get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Delete a transactional template version. # -# DELETE /templates/{template_id}/versions/{version_id} # - -template_id = "test_url_param" -version_id = "test_url_param" -response = sg.client.templates._(template_id).versions._(version_id).delete() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Activate a transactional template version. # -# POST /templates/{template_id}/versions/{version_id}/activate # - -template_id = "test_url_param" -version_id = "test_url_param" -response = sg.client.templates._(template_id).versions._(version_id).activate.post() -print(response.status_code) -print(response.body) -print(response.headers) - diff --git a/docker/examples/trackingsettings/trackingsettings.py b/docker/examples/trackingsettings/trackingsettings.py deleted file mode 100644 index 80dbe243a..000000000 --- a/docker/examples/trackingsettings/trackingsettings.py +++ /dev/null @@ -1,111 +0,0 @@ -import sendgrid -import json -import os - - -sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) - -################################################## -# Retrieve Tracking Settings # -# GET /tracking_settings # - -params = {'limit': 1, 'offset': 1} -response = sg.client.tracking_settings.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Update Click Tracking Settings # -# PATCH /tracking_settings/click # - -data = { - "enabled": True -} -response = sg.client.tracking_settings.click.patch(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve Click Track Settings # -# GET /tracking_settings/click # - -response = sg.client.tracking_settings.click.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Update Google Analytics Settings # -# PATCH /tracking_settings/google_analytics # - -data = { - "enabled": True, - "utm_campaign": "website", - "utm_content": "", - "utm_medium": "email", - "utm_source": "sendgrid.com", - "utm_term": "" -} -response = sg.client.tracking_settings.google_analytics.patch(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve Google Analytics Settings # -# GET /tracking_settings/google_analytics # - -response = sg.client.tracking_settings.google_analytics.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Update Open Tracking Settings # -# PATCH /tracking_settings/open # - -data = { - "enabled": True -} -response = sg.client.tracking_settings.open.patch(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Get Open Tracking Settings # -# GET /tracking_settings/open # - -response = sg.client.tracking_settings.open.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Update Subscription Tracking Settings # -# PATCH /tracking_settings/subscription # - -data = { - "enabled": True, - "html_content": "html content", - "landing": "landing page html", - "plain_content": "text content", - "replace": "replacement tag", - "url": "url" -} -response = sg.client.tracking_settings.subscription.patch(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve Subscription Tracking Settings # -# GET /tracking_settings/subscription # - -response = sg.client.tracking_settings.subscription.get() -print(response.status_code) -print(response.body) -print(response.headers) - diff --git a/docker/examples/user/user.py b/docker/examples/user/user.py deleted file mode 100644 index 9e3f24766..000000000 --- a/docker/examples/user/user.py +++ /dev/null @@ -1,294 +0,0 @@ -import sendgrid -import json -import os - - -sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) - -################################################## -# Get a user's account information. # -# GET /user/account # - -response = sg.client.user.account.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve your credit balance # -# GET /user/credits # - -response = sg.client.user.credits.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Update your account email address # -# PUT /user/email # - -data = { - "email": "example@example.com" -} -response = sg.client.user.email.put(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve your account email address # -# GET /user/email # - -response = sg.client.user.email.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Update your password # -# PUT /user/password # - -data = { - "new_password": "new_password", - "old_password": "old_password" -} -response = sg.client.user.password.put(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Update a user's profile # -# PATCH /user/profile # - -data = { - "city": "Orange", - "first_name": "Example", - "last_name": "User" -} -response = sg.client.user.profile.patch(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Get a user's profile # -# GET /user/profile # - -response = sg.client.user.profile.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Cancel or pause a scheduled send # -# POST /user/scheduled_sends # - -data = { - "batch_id": "YOUR_BATCH_ID", - "status": "pause" -} -response = sg.client.user.scheduled_sends.post(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve all scheduled sends # -# GET /user/scheduled_sends # - -response = sg.client.user.scheduled_sends.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Update user scheduled send information # -# PATCH /user/scheduled_sends/{batch_id} # - -data = { - "status": "pause" -} -batch_id = "test_url_param" -response = sg.client.user.scheduled_sends._(batch_id).patch(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve scheduled send # -# GET /user/scheduled_sends/{batch_id} # - -batch_id = "test_url_param" -response = sg.client.user.scheduled_sends._(batch_id).get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Delete a cancellation or pause of a scheduled send # -# DELETE /user/scheduled_sends/{batch_id} # - -batch_id = "test_url_param" -response = sg.client.user.scheduled_sends._(batch_id).delete() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Update Enforced TLS settings # -# PATCH /user/settings/enforced_tls # - -data = { - "require_tls": True, - "require_valid_cert": False -} -response = sg.client.user.settings.enforced_tls.patch(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve current Enforced TLS settings. # -# GET /user/settings/enforced_tls # - -response = sg.client.user.settings.enforced_tls.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Update your username # -# PUT /user/username # - -data = { - "username": "test_username" -} -response = sg.client.user.username.put(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve your username # -# GET /user/username # - -response = sg.client.user.username.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Update Event Notification Settings # -# PATCH /user/webhooks/event/settings # - -data = { - "bounce": True, - "click": True, - "deferred": True, - "delivered": True, - "dropped": True, - "enabled": True, - "group_resubscribe": True, - "group_unsubscribe": True, - "open": True, - "processed": True, - "spam_report": True, - "unsubscribe": True, - "url": "url" -} -response = sg.client.user.webhooks.event.settings.patch(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve Event Webhook settings # -# GET /user/webhooks/event/settings # - -response = sg.client.user.webhooks.event.settings.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Test Event Notification Settings # -# POST /user/webhooks/event/test # - -data = { - "url": "url" -} -response = sg.client.user.webhooks.event.test.post(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Create a parse setting # -# POST /user/webhooks/parse/settings # - -data = { - "hostname": "myhostname.com", - "send_raw": False, - "spam_check": True, - "url": "http://email.myhosthame.com" -} -response = sg.client.user.webhooks.parse.settings.post(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve all parse settings # -# GET /user/webhooks/parse/settings # - -response = sg.client.user.webhooks.parse.settings.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Update a parse setting # -# PATCH /user/webhooks/parse/settings/{hostname} # - -data = { - "send_raw": True, - "spam_check": False, - "url": "http://newdomain.com/parse" -} -hostname = "test_url_param" -response = sg.client.user.webhooks.parse.settings._(hostname).patch(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve a specific parse setting # -# GET /user/webhooks/parse/settings/{hostname} # - -hostname = "test_url_param" -response = sg.client.user.webhooks.parse.settings._(hostname).get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Delete a parse setting # -# DELETE /user/webhooks/parse/settings/{hostname} # - -hostname = "test_url_param" -response = sg.client.user.webhooks.parse.settings._(hostname).delete() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieves Inbound Parse Webhook statistics. # -# GET /user/webhooks/parse/stats # - -params = {'aggregated_by': 'day', 'limit': 'test_string', 'start_date': '2016-01-01', 'end_date': '2016-04-01', 'offset': 'test_string'} -response = sg.client.user.webhooks.parse.stats.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - diff --git a/docker/examples/whitelabel/whitelabel.py b/docker/examples/whitelabel/whitelabel.py deleted file mode 100644 index f529d3ed2..000000000 --- a/docker/examples/whitelabel/whitelabel.py +++ /dev/null @@ -1,311 +0,0 @@ -import sendgrid -import json -import os - - -sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) - -################################################## -# Create a domain whitelabel. # -# POST /whitelabel/domains # - -data = { - "automatic_security": False, - "custom_spf": True, - "default": True, - "domain": "example.com", - "ips": [ - "192.168.1.1", - "192.168.1.2" - ], - "subdomain": "news", - "username": "john@example.com" -} -response = sg.client.whitelabel.domains.post(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# List all domain whitelabels. # -# GET /whitelabel/domains # - -params = {'username': 'test_string', 'domain': 'test_string', 'exclude_subusers': 'true', 'limit': 1, 'offset': 1} -response = sg.client.whitelabel.domains.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Get the default domain whitelabel. # -# GET /whitelabel/domains/default # - -response = sg.client.whitelabel.domains.default.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# List the domain whitelabel associated with the given user. # -# GET /whitelabel/domains/subuser # - -response = sg.client.whitelabel.domains.subuser.get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Disassociate a domain whitelabel from a given user. # -# DELETE /whitelabel/domains/subuser # - -response = sg.client.whitelabel.domains.subuser.delete() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Update a domain whitelabel. # -# PATCH /whitelabel/domains/{domain_id} # - -data = { - "custom_spf": True, - "default": False -} -domain_id = "test_url_param" -response = sg.client.whitelabel.domains._(domain_id).patch(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve a domain whitelabel. # -# GET /whitelabel/domains/{domain_id} # - -domain_id = "test_url_param" -response = sg.client.whitelabel.domains._(domain_id).get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Delete a domain whitelabel. # -# DELETE /whitelabel/domains/{domain_id} # - -domain_id = "test_url_param" -response = sg.client.whitelabel.domains._(domain_id).delete() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Associate a domain whitelabel with a given user. # -# POST /whitelabel/domains/{domain_id}/subuser # - -data = { - "username": "jane@example.com" -} -domain_id = "test_url_param" -response = sg.client.whitelabel.domains._(domain_id).subuser.post(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Add an IP to a domain whitelabel. # -# POST /whitelabel/domains/{id}/ips # - -data = { - "ip": "192.168.0.1" -} -id = "test_url_param" -response = sg.client.whitelabel.domains._(id).ips.post(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Remove an IP from a domain whitelabel. # -# DELETE /whitelabel/domains/{id}/ips/{ip} # - -id = "test_url_param" -ip = "test_url_param" -response = sg.client.whitelabel.domains._(id).ips._(ip).delete() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Validate a domain whitelabel. # -# POST /whitelabel/domains/{id}/validate # - -id = "test_url_param" -response = sg.client.whitelabel.domains._(id).validate.post() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Create an IP whitelabel # -# POST /whitelabel/ips # - -data = { - "domain": "example.com", - "ip": "192.168.1.1", - "subdomain": "email" -} -response = sg.client.whitelabel.ips.post(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve all IP whitelabels # -# GET /whitelabel/ips # - -params = {'ip': 'test_string', 'limit': 1, 'offset': 1} -response = sg.client.whitelabel.ips.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve an IP whitelabel # -# GET /whitelabel/ips/{id} # - -id = "test_url_param" -response = sg.client.whitelabel.ips._(id).get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Delete an IP whitelabel # -# DELETE /whitelabel/ips/{id} # - -id = "test_url_param" -response = sg.client.whitelabel.ips._(id).delete() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Validate an IP whitelabel # -# POST /whitelabel/ips/{id}/validate # - -id = "test_url_param" -response = sg.client.whitelabel.ips._(id).validate.post() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Create a Link Whitelabel # -# POST /whitelabel/links # - -data = { - "default": True, - "domain": "example.com", - "subdomain": "mail" -} -params = {'limit': 1, 'offset': 1} -response = sg.client.whitelabel.links.post(request_body=data, query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve all link whitelabels # -# GET /whitelabel/links # - -params = {'limit': 1} -response = sg.client.whitelabel.links.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve a Default Link Whitelabel # -# GET /whitelabel/links/default # - -params = {'domain': 'test_string'} -response = sg.client.whitelabel.links.default.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve Associated Link Whitelabel # -# GET /whitelabel/links/subuser # - -params = {'username': 'test_string'} -response = sg.client.whitelabel.links.subuser.get(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Disassociate a Link Whitelabel # -# DELETE /whitelabel/links/subuser # - -params = {'username': 'test_string'} -response = sg.client.whitelabel.links.subuser.delete(query_params=params) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Update a Link Whitelabel # -# PATCH /whitelabel/links/{id} # - -data = { - "default": True -} -id = "test_url_param" -response = sg.client.whitelabel.links._(id).patch(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Retrieve a Link Whitelabel # -# GET /whitelabel/links/{id} # - -id = "test_url_param" -response = sg.client.whitelabel.links._(id).get() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Delete a Link Whitelabel # -# DELETE /whitelabel/links/{id} # - -id = "test_url_param" -response = sg.client.whitelabel.links._(id).delete() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Validate a Link Whitelabel # -# POST /whitelabel/links/{id}/validate # - -id = "test_url_param" -response = sg.client.whitelabel.links._(id).validate.post() -print(response.status_code) -print(response.body) -print(response.headers) - -################################################## -# Associate a Link Whitelabel # -# POST /whitelabel/links/{link_id}/subuser # - -data = { - "username": "jane@example.com" -} -link_id = "test_url_param" -response = sg.client.whitelabel.links._(link_id).subuser.post(request_body=data) -print(response.status_code) -print(response.body) -print(response.headers) - From 7bde27aed7845908bb2b6a799aa366d4f0c64a32 Mon Sep 17 00:00:00 2001 From: mbernier Date: Thu, 21 Dec 2017 14:14:38 -0700 Subject: [PATCH 20/52] fixed tests for docker files --- test/test_project.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_project.py b/test/test_project.py index 861f1ffe0..e5f05de36 100644 --- a/test/test_project.py +++ b/test/test_project.py @@ -13,11 +13,11 @@ def test_docker_dir(self): # ./docker-compose.yml or ./docker/docker-compose.yml def test_docker_compose(self): - self.assertTrue(os.path.isfile('docker-compose.yml')) + self.assertTrue(os.path.isfile('./docker/docker-compose.yml')) # ./.env_sample def test_env(self): - self.assertTrue(os.path.isfile('./env_sample')) + self.assertTrue(os.path.isfile('./.env_sample')) # ./.gitignore def test_gitignore(self): From 7ccbf5d4097878c77bf6a9f8731e9bc3c7bd3488 Mon Sep 17 00:00:00 2001 From: Bhargav Chandaka Date: Sun, 8 Jul 2018 18:57:21 -0500 Subject: [PATCH 21/52] Update Readme Added windows environment variable setup --- README.md | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index dbcebafdc..0b6a3b483 100644 --- a/README.md +++ b/README.md @@ -46,19 +46,26 @@ We appreciate your continued support, thank you! - The SendGrid service, starting at the [free level](https://sendgrid.com/free?source=sendgrid-python) ## Setup Environment Variables - Update the development environment with your [SENDGRID_API_KEY](https://app.sendgrid.com/settings/api_keys), for example: - +### Mac ```bash echo "export SENDGRID_API_KEY='YOUR_API_KEY'" > sendgrid.env echo "sendgrid.env" >> .gitignore source ./sendgrid.env ``` - Sendgrid also supports local environment file `.env`. Copy or rename `.env_sample` into `.env` and update [SENDGRID_API_KEY](https://app.sendgrid.com/settings/api_keys) with your key. -## Install Package +### Windows +Temporarily set the environment variable(accesible only during the current cli session): +```bash +set SENDGRID_API_KEY=YOUR_API_KEY +``` +Permanently set the environment variable: +```bash +setx SENDGRID_API_KEY "YOUR_API_KEY" +``` +## Install Package ```bash pip install sendgrid ``` From 2da51c238fd6ecc1caaa343cf061437ff8bb2a2a Mon Sep 17 00:00:00 2001 From: Bhargav Chandaka Date: Sun, 8 Jul 2018 18:59:56 -0500 Subject: [PATCH 22/52] Update README.md Added environmental variable setup in windows --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0b6a3b483..f34a46235 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,7 @@ Temporarily set the environment variable(accesible only during the current cli s ```bash set SENDGRID_API_KEY=YOUR_API_KEY ``` -Permanently set the environment variable: +Permanently set the environment variable(accessible in all subsequent cli sessions): ```bash setx SENDGRID_API_KEY "YOUR_API_KEY" ``` From 6d5e9908cd074dbcb9dd6ac24df8de25245e318d Mon Sep 17 00:00:00 2001 From: Swapnil Agarwal Date: Fri, 27 Oct 2017 00:42:03 +0530 Subject: [PATCH 23/52] Add a tutorial to deploy a simple 'Hello Email' app on Heroku See #356 --- use_cases/README.md | 1 + use_cases/flask_heroku.md | 9 +++++++++ 2 files changed, 10 insertions(+) create mode 100644 use_cases/flask_heroku.md diff --git a/use_cases/README.md b/use_cases/README.md index 188464d09..4e0fc24d6 100644 --- a/use_cases/README.md +++ b/use_cases/README.md @@ -8,6 +8,7 @@ This directory provides examples for specific use cases of this library. Please * [How to Create a Django app, Deployed on Heroku, to Send Email with SendGrid](django.md) * [How to Deploy A Simple Hello Email App on AWS](aws.md) +* [How to Deploy a simple Flask app, to send Email with SendGrid, on Heroku](flask_heroku.md) * [How to Setup a Domain Whitelabel](domain_whitelabel.md) * [How to View Email Statistics](email_stats.md) diff --git a/use_cases/flask_heroku.md b/use_cases/flask_heroku.md new file mode 100644 index 000000000..ef0fa599a --- /dev/null +++ b/use_cases/flask_heroku.md @@ -0,0 +1,9 @@ +# Create a Flask app to send email with SendGrid + +This tutorial explains how to deploy a simple Flask app, to send an email using the SendGrid Python SDK, on Heroku. + +1. Create a SendGrid API key at https://app.sendgrid.com/settings/api_keys +1. Go to https://github.com/swapagarwal/sendgrid-flask-heroku +1. Click on `Deploy to Heroku` button, and follow the instructions. + +That's all. You'll be sending your first email within seconds! From b00219775f23d7d934e0bc6787482aa7d7e74679 Mon Sep 17 00:00:00 2001 From: Slam <3lnc.slam@gmail.com> Date: Wed, 1 Aug 2018 20:16:35 +0300 Subject: [PATCH 24/52] Adds support for dynamic template data in personalizations --- examples/helpers/{mail => }/mail_example.py | 26 +++++++++++++++++---- sendgrid/helpers/mail/personalization.py | 9 +++++++ test/test_mail.py | 14 +++++++++-- 3 files changed, 43 insertions(+), 6 deletions(-) rename examples/helpers/{mail => }/mail_example.py (91%) diff --git a/examples/helpers/mail/mail_example.py b/examples/helpers/mail_example.py similarity index 91% rename from examples/helpers/mail/mail_example.py rename to examples/helpers/mail_example.py index b2de7f0a0..e227b22cc 100644 --- a/examples/helpers/mail/mail_example.py +++ b/examples/helpers/mail_example.py @@ -1,8 +1,6 @@ -import json -import os -import urllib2 +from sendgrid import SendGridAPIClient from sendgrid.helpers.mail import * -from sendgrid import * + # NOTE: you will need move this file to the root # directory of this project to execute properly. @@ -217,3 +215,23 @@ def send_kitchen_sink(): # this will only send an email if you set SandBox Mode to False send_kitchen_sink() + + +def dynamic_template_usage(): + """ + Sample usage of dynamic (handlebars) transactional templates. + To make this work, you should have dynamic template created within your + SendGrid account. For this particular example, template may be like:: + +

Hello, {{name}}! Your current balance is {{balance}}

+ + """ + mail = Mail(from_email='templates@sendgrid.com') + mail.template_id = 'd-your-dynamic-template-uid' + p = Personalization() + p.add_to(Email('user@example.com')) + p.dynamic_template_data = {'name': 'Bob', 'balance': 42} + mail.add_personalization(p) + + sg = SendGridAPIClient(apikey='SG.your-api-key') + sg.client.mail.send.post(request_body=mail.get()) diff --git a/sendgrid/helpers/mail/personalization.py b/sendgrid/helpers/mail/personalization.py index 8bb4bed0b..8032af958 100644 --- a/sendgrid/helpers/mail/personalization.py +++ b/sendgrid/helpers/mail/personalization.py @@ -1,6 +1,10 @@ class Personalization(object): """A Personalization defines who should receive an individual message and how that message should be handled. + + :var dynamic_template_data: data for dynamic transactional template. + Should be JSON-serializeable structure. No pre-processing sill be done + prior to sending this via http client. """ def __init__(self): @@ -13,6 +17,7 @@ def __init__(self): self._substitutions = [] self._custom_args = [] self._send_at = None + self.dynamic_template_data = None @property def tos(self): @@ -198,4 +203,8 @@ def get(self): if self.send_at is not None: personalization["send_at"] = self.send_at + + if self.dynamic_template_data is not None: + personalization['dynamic_template_data'] = self.dynamic_template_data + return personalization diff --git a/test/test_mail.py b/test/test_mail.py index 7721b5205..fb46d34c2 100644 --- a/test/test_mail.py +++ b/test/test_mail.py @@ -80,7 +80,6 @@ def test_sendgridAPIKey(self): else: self.fail("Should have failed as SendGrid API key included") - def test_helloEmail(self): self.max_diff = None @@ -130,7 +129,7 @@ def test_helloEmailAdditionalContent(self): personalization = Personalization() personalization.add_to(Email("test@example.com")) mail.add_personalization(personalization) - + mail.add_content(Content("text/html", "some text here")) mail.add_content(Content("text/plain", "some text here")) @@ -562,3 +561,14 @@ def test_disable_tracking(self): def test_directly_setting_substitutions(self): personalization = Personalization() personalization.substitutions = [{'a': 0}] + + def test_dynamic_template_data(self): + p = Personalization() + p.add_to(Email('test@sendgrid.com')) + p.dynamic_template_data = {'customer': {'name': 'Bob', 'returning': True}, 'total': 42} + + expected = { + 'to': [{'email': 'test@sendgrid.com'}], + 'dynamic_template_data': {'customer': {'name': 'Bob', 'returning': True}, 'total': 42} + } + self.assertDictEqual(p.get(), expected) From 210a4fa63a10ad88a2b09ae716344155a180d73a Mon Sep 17 00:00:00 2001 From: Slam <3lnc.slam@gmail.com> Date: Wed, 1 Aug 2018 20:30:30 +0300 Subject: [PATCH 25/52] Example for .md usecase --- use_cases/transational_templates.md | 32 ++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/use_cases/transational_templates.md b/use_cases/transational_templates.md index d3e3a005d..1e89e08a9 100644 --- a/use_cases/transational_templates.md +++ b/use_cases/transational_templates.md @@ -66,6 +66,36 @@ print(response.body) print(response.headers) ``` +### With dynamic templates + +Sendgrid dynamic templates let you leverage power of [handlebars](https://handlebarsjs.com/) +syntax to easily manage complex dynamic content in transactional emails. + +To check this example snippet, create transactional email with code like +```html +

Hello, {{name}}! Your current balance is {{balance}}

+``` + +Than send email based on it, providing context for substitutions: +```python +from sendgrid import SendGridAPIClient +from sendgrid.helpers.mail import Email, Personalization + + +sg = SendGridAPIClient(apikey='SG.your-api-key') + +mail = Mail(from_email='templates@example.com') +mail.template_id = 'd-your-dynamic-template-uid' +p = Personalization() +p.add_to(Email('user@example.com')) +p.dynamic_template_data = {'name': 'Bob', 'balance': 42} +mail.add_personalization(p) + +sg.client.mail.send.post(request_body=mail.get()) +``` + +Read more about dynamic templates in [docs](https://sendgrid.com/docs/User_Guide/Transactional_Templates/how_to_send_an_email_with_transactional_templates.html) + ## Without Mail Helper Class ```python @@ -113,4 +143,4 @@ except urllib.HTTPError as e: print(response.status_code) print(response.body) print(response.headers) -``` \ No newline at end of file +``` From 8ff82a83abe14c2d5910b56fad8ccc3cbf53bc74 Mon Sep 17 00:00:00 2001 From: Slam <3lnc.slam@gmail.com> Date: Wed, 1 Aug 2018 20:35:15 +0300 Subject: [PATCH 26/52] Linking SG dashboard --- use_cases/transational_templates.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/use_cases/transational_templates.md b/use_cases/transational_templates.md index 1e89e08a9..d88ffae76 100644 --- a/use_cases/transational_templates.md +++ b/use_cases/transational_templates.md @@ -71,7 +71,8 @@ print(response.headers) Sendgrid dynamic templates let you leverage power of [handlebars](https://handlebarsjs.com/) syntax to easily manage complex dynamic content in transactional emails. -To check this example snippet, create transactional email with code like +To check this example snippet, create +[transactional email template](https://sendgrid.com/dynamic_templates) with code like ```html

Hello, {{name}}! Your current balance is {{balance}}

``` From 65f4857a800c379620f216426f0417c9e78d440c Mon Sep 17 00:00:00 2001 From: lifez Date: Fri, 3 Aug 2018 17:56:12 +0700 Subject: [PATCH 27/52] Change type of category in Mail.add_category from string to Category --- sendgrid/helpers/mail/mail.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/sendgrid/helpers/mail/mail.py b/sendgrid/helpers/mail/mail.py index 6554d4508..22a3fad66 100644 --- a/sendgrid/helpers/mail/mail.py +++ b/sendgrid/helpers/mail/mail.py @@ -8,10 +8,10 @@ class Mail(object): Use get() to get the request body. """ - def __init__(self, - from_email=None, - subject=None, - to_email=None, + def __init__(self, + from_email=None, + subject=None, + to_email=None, content=None): """Create a Mail object. @@ -73,7 +73,7 @@ def get(self): if self.from_email is not None: mail["from"] = self.from_email.get() - + if self.subject is not None: mail["subject"] = self.subject @@ -306,7 +306,7 @@ def add_content(self, content): """ if self._contents is None: self._contents = [] - + # Text content should be before HTML content if content._type == "text/plain": self._contents.insert(0, content) @@ -376,9 +376,9 @@ def categories(self): return self._categories def add_category(self, category): - """Add a Category to this Mail. Must be less than 255 characters. + """Add a Category to this Mail. - :type category: string + :type category: Category """ self._categories.append(category) From 0ca2578c19b5626d1a60964e1d347b173f9ee65e Mon Sep 17 00:00:00 2001 From: lifez Date: Fri, 3 Aug 2018 18:18:34 +0700 Subject: [PATCH 28/52] Install pip for python3.6 --- docker/Dockerfile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index e891e497c..cd36ea199 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -25,14 +25,15 @@ RUN chmod +x ./install.sh && sync && \ # install pip, tox ADD https://bootstrap.pypa.io/get-pip.py get-pip.py RUN python2.7 get-pip.py && \ + python3.6 get-pip.py && \ pip install tox && \ rm get-pip.py #install pyyaml, six, werkzeug RUN python3.6 -m pip install pyyaml RUN python3.6 -m pip install six -RUN Python3.6 -m pip install werkzeug -RUN Python3.6 -m pip install flask +RUN python3.6 -m pip install werkzeug +RUN python3.6 -m pip install flask # set up default sendgrid env WORKDIR /root/sources From 382b5e87d53865ffce702e9bbf4d3a05a3d3d2c6 Mon Sep 17 00:00:00 2001 From: Ryan Jarvis Date: Fri, 3 Aug 2018 16:30:39 -0700 Subject: [PATCH 29/52] Update TROUBLESHOOTING.md --- TROUBLESHOOTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TROUBLESHOOTING.md b/TROUBLESHOOTING.md index 29c0c8e89..331aee8e7 100644 --- a/TROUBLESHOOTING.md +++ b/TROUBLESHOOTING.md @@ -50,7 +50,7 @@ import urllib try: response = sg.client.mail.send.post(request_body=mail.get()) except urllib.error.HTTPError as e: - print e.read() + print(e.read()) ``` From f2c48e33f12bedf5f688f1a1617ba1575ad9e812 Mon Sep 17 00:00:00 2001 From: Anshul Singhal Date: Mon, 6 Aug 2018 13:05:57 -0700 Subject: [PATCH 30/52] Updated README for more info --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index dbcebafdc..d8823e2f0 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ We appreciate your continued support, thank you! * [License](#license) + # Installation ## Prerequisites @@ -47,7 +48,7 @@ We appreciate your continued support, thank you! ## Setup Environment Variables -Update the development environment with your [SENDGRID_API_KEY](https://app.sendgrid.com/settings/api_keys), for example: +Update the development environment with your [SENDGRID_API_KEY](https://app.sendgrid.com/settings/api_keys) (more info [here](https://sendgrid.com/docs/User_Guide/Settings/api_keys.html)), for example: ```bash echo "export SENDGRID_API_KEY='YOUR_API_KEY'" > sendgrid.env @@ -67,6 +68,7 @@ pip install sendgrid - [Python-HTTP-Client](https://github.com/sendgrid/python-http-client) + # Quick Start From 6adc94886d389c00dafb005cb56ec292b1bd6af1 Mon Sep 17 00:00:00 2001 From: af4ro <1997ansul@gmail.com> Date: Mon, 6 Aug 2018 16:23:41 -0700 Subject: [PATCH 31/52] fixed assertion error --- test/test_project.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_project.py b/test/test_project.py index e5f05de36..a9edc8195 100644 --- a/test/test_project.py +++ b/test/test_project.py @@ -9,7 +9,7 @@ class ProjectTests(unittest.TestCase): # ./Docker or docker/Docker def test_docker_dir(self): - self.assertTrue(os.path.isdir("./docker/Dockerfile")) + self.assertTrue(os.path.isfile("./docker/Dockerfile")) # ./docker-compose.yml or ./docker/docker-compose.yml def test_docker_compose(self): From 946a683fc04b2fe5a4415bb32fbb6e071bfabc9d Mon Sep 17 00:00:00 2001 From: Anshul Singhal Date: Tue, 7 Aug 2018 11:27:00 -0700 Subject: [PATCH 32/52] readme tag update --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index dbcebafdc..fddcc69d2 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ [![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](./LICENSE.txt) [![Twitter Follow](https://img.shields.io/twitter/follow/sendgrid.svg?style=social&label=Follow)](https://twitter.com/sendgrid) [![GitHub contributors](https://img.shields.io/github/contributors/sendgrid/sendgrid-python.svg)](https://github.com/sendgrid/sendgrid-python/graphs/contributors) +[![Open Source Helpers](https://www.codetriage.com/sendgrid/sendgrid-python/badges/users.svg)](https://www.codetriage.com/sendgrid/sendgrid-python) **NEW:** From e1cc25685ffc8ea0d5c7ebc22c083f62cf0169ab Mon Sep 17 00:00:00 2001 From: af4ro <1997anshul@gmail.com> Date: Thu, 9 Aug 2018 09:39:27 -0700 Subject: [PATCH 33/52] minor formatting --- examples/helpers/mail_example.py | 8 ++++++-- test/test_mail.py | 16 ++++++++++++++-- use_cases/transational_templates.md | 3 ++- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/examples/helpers/mail_example.py b/examples/helpers/mail_example.py index e227b22cc..0a5b868bc 100644 --- a/examples/helpers/mail_example.py +++ b/examples/helpers/mail_example.py @@ -226,11 +226,15 @@ def dynamic_template_usage():

Hello, {{name}}! Your current balance is {{balance}}

""" - mail = Mail(from_email='templates@sendgrid.com') + mail = Mail() + mail.from_email = 'templates@sendgrid.com' mail.template_id = 'd-your-dynamic-template-uid' p = Personalization() p.add_to(Email('user@example.com')) - p.dynamic_template_data = {'name': 'Bob', 'balance': 42} + p.dynamic_template_data = { + 'name': 'Bob', + 'balance': 42 + } mail.add_personalization(p) sg = SendGridAPIClient(apikey='SG.your-api-key') diff --git a/test/test_mail.py b/test/test_mail.py index fb46d34c2..08d0feb8e 100644 --- a/test/test_mail.py +++ b/test/test_mail.py @@ -565,10 +565,22 @@ def test_directly_setting_substitutions(self): def test_dynamic_template_data(self): p = Personalization() p.add_to(Email('test@sendgrid.com')) - p.dynamic_template_data = {'customer': {'name': 'Bob', 'returning': True}, 'total': 42} + p.dynamic_template_data = { + 'customer': { + 'name': 'Bob', + 'returning': True + }, + 'total': 42 + } expected = { 'to': [{'email': 'test@sendgrid.com'}], - 'dynamic_template_data': {'customer': {'name': 'Bob', 'returning': True}, 'total': 42} + 'dynamic_template_data': { + 'customer': { + 'name': 'Bob', + 'returning': True + }, + 'total': 42 + } } self.assertDictEqual(p.get(), expected) diff --git a/use_cases/transational_templates.md b/use_cases/transational_templates.md index d88ffae76..2d74f92a5 100644 --- a/use_cases/transational_templates.md +++ b/use_cases/transational_templates.md @@ -85,7 +85,8 @@ from sendgrid.helpers.mail import Email, Personalization sg = SendGridAPIClient(apikey='SG.your-api-key') -mail = Mail(from_email='templates@example.com') +mail = Mail() +mail.from_email='templates@example.com' mail.template_id = 'd-your-dynamic-template-uid' p = Personalization() p.add_to(Email('user@example.com')) From 54aa6ec05ced0c9c5577abef118f5a2ecb1ed711 Mon Sep 17 00:00:00 2001 From: Anshul Singhal <1997anshul@gmail.com> Date: Fri, 10 Aug 2018 16:03:54 -0700 Subject: [PATCH 34/52] Minor readability update --- CONTRIBUTING.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b38f1c1c4..882e52164 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -191,8 +191,10 @@ Please run your code through: ```bash # Clone your fork of the repo into the current directory git clone https://github.com/sendgrid/sendgrid-python + # Navigate to the newly cloned directory cd sendgrid-python + # Assign the original repo to a remote called "upstream" git remote add upstream https://github.com/sendgrid/sendgrid-python ``` From 0c2163c3f545e7e7a91d05b6b2fcb037768d83af Mon Sep 17 00:00:00 2001 From: Navin Pai Date: Tue, 14 Aug 2018 03:00:04 +0530 Subject: [PATCH 35/52] Add slack API integration to use_cases --- use_cases/README.md | 1 + use_cases/slack_event_api_integration.md | 46 ++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 use_cases/slack_event_api_integration.md diff --git a/use_cases/README.md b/use_cases/README.md index 188464d09..ea8156533 100644 --- a/use_cases/README.md +++ b/use_cases/README.md @@ -15,6 +15,7 @@ This directory provides examples for specific use cases of this library. Please * [Asynchronous Mail Send](asynchronous_mail_send.md) * [Attachment](attachment.md) * [Transactional Templates](transational_templates.md) +* [Integrate with Slack Events API](slack_event_api_integration.md) ### Library Features * [Error Handling](error_handling.md) \ No newline at end of file diff --git a/use_cases/slack_event_api_integration.md b/use_cases/slack_event_api_integration.md new file mode 100644 index 000000000..d0e0f6140 --- /dev/null +++ b/use_cases/slack_event_api_integration.md @@ -0,0 +1,46 @@ +# Integrate with Slack Events API + +It's fairly straightforward to integrate Sendgrid with Slack, to allow emails to be triggered by events happening on Slack. + +For this, we make use of the [Official Slack Events API](https://github.com/slackapi/python-slack-events-api), which can be installed using pip. + +To allow our application to get notifications of slack events, we first create a Slack App with Event Subscriptions as described [here](https://github.com/slackapi/python-slack-events-api#--development-workflow) + +Then, we set `SENDGRID_API_KEY` _(which you can create on the Sendgrid dashboard)_ and `SLACK_VERIFICATION_TOKEN` _(which you can get in the App Credentials section of the Slack App)_ as environment variables. + +Once this is done, we can subscribe to [events on Slack](https://api.slack.com/events) and trigger emails when an event occurs. In the example below, we trigger an email to `test@example.com` whenever someone posts a message on Slack that has the word "_help_" in it. + +``` +from slackeventsapi import SlackEventAdapter +from slackclient import SlackClient +import os +import sendgrid +from sendgrid.helpers.mail import * + +sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) + +SLACK_VERIFICATION_TOKEN = os.environ["SLACK_VERIFICATION_TOKEN"] +slack_events_adapter = SlackEventAdapter(SLACK_VERIFICATION_TOKEN, "/slack/events") + +@slack_events_adapter.on("message") +def handle_message(event_data): + message = event_data["event"] + # If the incoming message contains "help", then send an email using SendGrid + if message.get("subtype") is None and "help" in message.get('text').lower(): + message = "Someone needs your help: \n\n %s" % message["text"] + r = send_email(message) + print(r) + + +def send_email(message): + from_email = Email("slack_integration@example.com") + to_email = Email("test@example.com") + subject = "Psst... Someone needs help!" + content = Content("text/plain", message) + mail = Mail(from_email, subject, to_email, content) + response = sg.client.mail.send.post(request_body=mail.get()) + return response.status_code + +# Start the slack event listener server on port 3000 +slack_events_adapter.start(port=3000) +``` \ No newline at end of file From 5364bf9872b76bc496759b5d8f740e22059e7c43 Mon Sep 17 00:00:00 2001 From: James Purpura Date: Mon, 13 Aug 2018 15:01:44 -0700 Subject: [PATCH 36/52] update TROUBLESHOOTING.md editted "our use cases" link --- TROUBLESHOOTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TROUBLESHOOTING.md b/TROUBLESHOOTING.md index 29c0c8e89..a2332ae1b 100644 --- a/TROUBLESHOOTING.md +++ b/TROUBLESHOOTING.md @@ -111,4 +111,4 @@ print mail.get() # Error Handling -Please review [our use_cases](https://github.com/sendgrid/sendgrid-python/use_cases/README.md) for examples of error handling. +Please review [our use_cases](https://github.com/sendgrid/sendgrid-python/blob/master/use_cases/README.md#use-cases) for examples of error handling. From 07ed3228313eb4379877867fa81ce3fac18c612b Mon Sep 17 00:00:00 2001 From: Agnes Jang Date: Tue, 14 Aug 2018 13:45:04 -0700 Subject: [PATCH 37/52] Added comment for attachments to be base64 encoded --- examples/helpers/mail/mail_example.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/helpers/mail/mail_example.py b/examples/helpers/mail/mail_example.py index b2de7f0a0..3a4a350a8 100644 --- a/examples/helpers/mail/mail_example.py +++ b/examples/helpers/mail/mail_example.py @@ -80,7 +80,8 @@ def get_mock_personalization_dict(): def build_attachment1(): - """Build attachment mock.""" + """Build attachment mock. Make sure your content is base64 encoded before passing into attachment.content. + Another example: https://github.com/sendgrid/sendgrid-python/blob/master/use_cases/attachment.md""" attachment = Attachment() attachment.content = ("TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNl" "Y3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12") From 3ec8e6535baf9a75814aae4cc7f7c539c1af577a Mon Sep 17 00:00:00 2001 From: Elmer Thomas Date: Thu, 16 Aug 2018 09:22:35 -0700 Subject: [PATCH 38/52] Add missing dependencies --- docker-test/entrypoint.sh | 2 +- requirements.txt | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/docker-test/entrypoint.sh b/docker-test/entrypoint.sh index f64d0cccb..abb55b9f2 100644 --- a/docker-test/entrypoint.sh +++ b/docker-test/entrypoint.sh @@ -13,5 +13,5 @@ fi cd sendgrid-python python3.6 setup.py install -pip install pyyaml six werkzeug flask +pip install pyyaml six werkzeug flask python-http-client exec $SHELL diff --git a/requirements.txt b/requirements.txt index 34d770b5b..ceae6cf62 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ Flask==0.10.1 PyYAML==3.11 python-http-client==2.2.1 six==1.10.0 +pytest=3.7.1 \ No newline at end of file From 793aad7c3e520e18b391de4a1333d9ec463abd2f Mon Sep 17 00:00:00 2001 From: Elmer Thomas Date: Thu, 16 Aug 2018 12:03:10 -0700 Subject: [PATCH 39/52] Version Bump v5.5.0: Pre-Dynamic Template Roll Up Release --- CHANGELOG.md | 16 +++++++++++++++- docker/README.md | 3 ++- sendgrid/version.py | 2 +- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c1869666..69c0a5211 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,24 @@ # Change Log All notable changes to this project will be documented in this file. +## [5.5.1] - 2018-08-16 ## +### Added +- [PR #588](https://github.com/sendgrid/sendgrid-python/pull/588): Updates the Readme to include environment variable setup in windows. Big thanks to [Bhargav Chandaka](https://github.com/bchandaka) for the PR! +- [PR #599](https://github.com/sendgrid/sendgrid-python/pull/599): Updates the Readme to include additional API Key instruction. Big thanks to [Anshul Singhal](https://github.com/af4ro) for the PR! +- [PR #600](https://github.com/sendgrid/sendgrid-python/pull/600): Add CodeTriage Badge. Big thanks to [Anshul Singhal](https://github.com/af4ro) for the PR! +- [PR #601](https://github.com/sendgrid/sendgrid-python/pull/601): Readability improvements to CONTRIBUTING.md. Big thanks to [Anshul Singhal](https://github.com/af4ro) for the PR! +- [PR #604](https://github.com/sendgrid/sendgrid-python/pull/604): Readability improvements to CONTRIBUTING.md. Big thanks to [agnesjang98](https://github.com/agnesjang98) for the PR! + +### Fixed +- [PR #595](https://github.com/sendgrid/sendgrid-python/pull/595): Change type of category in Mail.add_category from string to Category. Big thanks to [Phawin Khongkhasawan](https://github.com/lifez) for the PR! +- [PR #596](https://github.com/sendgrid/sendgrid-python/pull/596): Fix Docker build. Big thanks to [Phawin Khongkhasawan](https://github.com/lifez) for the PR! +- [PR #598](https://github.com/sendgrid/sendgrid-python/pull/598): Fix python3 print example in TROUBLESHOOTING.md. Big thanks to [Ryan Jarvis](https://github.com/Cabalist) for the PR! +- [PR #603](https://github.com/sendgrid/sendgrid-python/pull/603): Update TROUBLESHOOTING.md to link to correct use cases page. Big thanks to [James Purpura](https://github.com/jpurpura) for the PR! + ## [5.4.1] - 2018-06-26 ## ### Fixed - [PR #585](https://github.com/sendgrid/sendgrid-python/pull/585): Fix typo in `mail_example.py`. Big thanks to [Anurag Anand](https://github.com/theanuraganand) for the PR! -- [PR #583](https://github.com/sendgrid/sendgrid-python/pull/585): Fix `Personalization.substitutions` setter. Trying to set substitutions directly rather than with add_substitution was causing an infinite regress. Big thanks to [Richard Nias](https://github.com/richardnias) for the PR! +- [PR #583](https://github.com/sendgrid/sendgrid-python/pull/583): Fix `Personalization.substitutions` setter. Trying to set substitutions directly rather than with add_substitution was causing an infinite regress. Big thanks to [Richard Nias](https://github.com/richardnias) for the PR! ## [5.4.0] - 2018-06-07 ## ### Added diff --git a/docker/README.md b/docker/README.md index a523dc93d..41c235fcc 100644 --- a/docker/README.md +++ b/docker/README.md @@ -1,5 +1,6 @@ # Supported tags and respective `Dockerfile` links - - `v5.4.1`, `latest` [(Dockerfile)](https://github.com/sendgrid/sendgrid-python/blob/master/docker/Dockerfile) + - `v5.5.0`, `latest` [(Dockerfile)](https://github.com/sendgrid/sendgrid-python/blob/master/docker/Dockerfile) + - `v5.4.1` - `v5.4.0` - `v5.3.0` - `v5.2.1` diff --git a/sendgrid/version.py b/sendgrid/version.py index c191754f8..30415fcbd 100644 --- a/sendgrid/version.py +++ b/sendgrid/version.py @@ -1,2 +1,2 @@ -version_info = (5, 4, 1) +version_info = (5, 5, 0) __version__ = '.'.join(str(v) for v in version_info) From 28bd2a5d91c0130ffd832bec4cc95356e0b37ce7 Mon Sep 17 00:00:00 2001 From: Elmer Thomas Date: Mon, 20 Aug 2018 17:05:50 -0700 Subject: [PATCH 40/52] Merge #593 --- docker-test/entrypoint.sh | 2 +- examples/helpers/mail_example.py | 14 ++- sendgrid/helpers/mail/mail.py | 3 + sendgrid/helpers/mail/personalization.py | 14 ++- use_cases/transational_templates.md | 103 +++++++++++++++-------- 5 files changed, 95 insertions(+), 41 deletions(-) diff --git a/docker-test/entrypoint.sh b/docker-test/entrypoint.sh index abb55b9f2..6749e8cf3 100644 --- a/docker-test/entrypoint.sh +++ b/docker-test/entrypoint.sh @@ -13,5 +13,5 @@ fi cd sendgrid-python python3.6 setup.py install -pip install pyyaml six werkzeug flask python-http-client +pip install pyyaml six werkzeug flask python-http-client pytest exec $SHELL diff --git a/examples/helpers/mail_example.py b/examples/helpers/mail_example.py index 65176a6ca..c1cd166ad 100644 --- a/examples/helpers/mail_example.py +++ b/examples/helpers/mail_example.py @@ -218,7 +218,10 @@ def send_kitchen_sink(): send_kitchen_sink() -def dynamic_template_usage(): +def transactional_template_usage(): + # Assumes you set your environment variable: + # https://github.com/sendgrid/sendgrid-python/blob/master/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key + """ Sample usage of dynamic (handlebars) transactional templates. To make this work, you should have dynamic template created within your @@ -228,7 +231,7 @@ def dynamic_template_usage(): """ mail = Mail() - mail.from_email = 'templates@sendgrid.com' + mail.from_email = Email('templates@sendgrid.com') mail.template_id = 'd-your-dynamic-template-uid' p = Personalization() p.add_to(Email('user@example.com')) @@ -238,5 +241,8 @@ def dynamic_template_usage(): } mail.add_personalization(p) - sg = SendGridAPIClient(apikey='SG.your-api-key') - sg.client.mail.send.post(request_body=mail.get()) + sg = SendGridAPIClient() + response = sg.client.mail.send.post(request_body=mail.get()) + print(response.status_code) + print(response.headers) + print(response.body) diff --git a/sendgrid/helpers/mail/mail.py b/sendgrid/helpers/mail/mail.py index 22a3fad66..fd606fd18 100644 --- a/sendgrid/helpers/mail/mail.py +++ b/sendgrid/helpers/mail/mail.py @@ -1,6 +1,7 @@ """v3/mail/send response body builder""" from .personalization import Personalization from .header import Header +from .email import Email class Mail(object): @@ -147,6 +148,8 @@ def from_email(self): @from_email.setter def from_email(self, value): + if isinstance(value, str): + value = Email(value) self._from_email = value @property diff --git a/sendgrid/helpers/mail/personalization.py b/sendgrid/helpers/mail/personalization.py index 8032af958..c42694fe9 100644 --- a/sendgrid/helpers/mail/personalization.py +++ b/sendgrid/helpers/mail/personalization.py @@ -17,7 +17,7 @@ def __init__(self): self._substitutions = [] self._custom_args = [] self._send_at = None - self.dynamic_template_data = None + self._dynamic_template_data = None @property def tos(self): @@ -163,6 +163,18 @@ def send_at(self): def send_at(self, value): self._send_at = value + @property + def dynamic_template_data(self): + """Data for dynamic transactional template. + + :rtype: JSON-serializeable structure + """ + return self._dynamic_template_data + + @dynamic_template_data.setter + def dynamic_template_data(self, json): + self._dynamic_template_data = json + def get(self): """ Get a JSON-ready representation of this Personalization. diff --git a/use_cases/transational_templates.md b/use_cases/transational_templates.md index 2d74f92a5..d3d587bc0 100644 --- a/use_cases/transational_templates.md +++ b/use_cases/transational_templates.md @@ -1,6 +1,71 @@ -# Transactional Templates +### Transactional Templates -For this example, we assume you have created a [transactional template](https://sendgrid.com/docs/User_Guide/Transactional_Templates/index.html). Following is the template content we used for testing. +Sendgrid transactional templates let you leverage power of [handlebars](https://handlebarsjs.com/) +syntax to easily manage complex dynamic content in transactional emails. + +For this example, we assume you have created a [transactional template](https://sendgrid.com/docs/User_Guide/Transactional_Templates/create_and_edit_transactional_templates.html). Following is the template content we used for testing. + +This example also assumes you [set your environment variable](https://github.com/sendgrid/sendgrid-python/blob/master/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key) with your SendGrid API Key. + +Template ID (replace with your own): + +```text +d-13b8f94fbcae4ec6b75270d6cb59f932 +``` + +Email Subject: + +```text +{{ subject }} +``` + +Template Body: + +```html + + + + + +Hello {{ name }}, +

+I'm glad you are trying out the template feature! +

+I hope you are having a great day in {{ city }} :) +

+ + +``` + +```python +from sendgrid import SendGridAPIClient +from sendgrid.helpers.mail import Mail, Email, Personalization + + +sg = SendGridAPIClient() +mail = Mail() +mail.from_email = Email('templates@example.com') +mail.template_id = 'd-your-dynamic-template-uid' +p = Personalization() +p.add_to(Email('user@example.com')) +p.dynamic_template_data = { + 'subject': 'Dynamic Templates in Python', + 'name': 'Example User', + 'city': 'Denver' +} +mail.add_personalization(p) + +response = sg.client.mail.send.post(request_body=mail.get()) +print(response.status_code) +print(response.headers) +print(response.body) +``` + +Read more about dynamic templates [here](https://sendgrid.com/docs/User_Guide/Transactional_Templates/how_to_send_an_email_with_transactional_templates.html). + +# Legacy Templates + +For this example, we assume you have created a [Legacy Template](https://sendgrid.com/templates). Following is the template content we used for testing. Template ID (replace with your own): @@ -66,38 +131,6 @@ print(response.body) print(response.headers) ``` -### With dynamic templates - -Sendgrid dynamic templates let you leverage power of [handlebars](https://handlebarsjs.com/) -syntax to easily manage complex dynamic content in transactional emails. - -To check this example snippet, create -[transactional email template](https://sendgrid.com/dynamic_templates) with code like -```html -

Hello, {{name}}! Your current balance is {{balance}}

-``` - -Than send email based on it, providing context for substitutions: -```python -from sendgrid import SendGridAPIClient -from sendgrid.helpers.mail import Email, Personalization - - -sg = SendGridAPIClient(apikey='SG.your-api-key') - -mail = Mail() -mail.from_email='templates@example.com' -mail.template_id = 'd-your-dynamic-template-uid' -p = Personalization() -p.add_to(Email('user@example.com')) -p.dynamic_template_data = {'name': 'Bob', 'balance': 42} -mail.add_personalization(p) - -sg.client.mail.send.post(request_body=mail.get()) -``` - -Read more about dynamic templates in [docs](https://sendgrid.com/docs/User_Guide/Transactional_Templates/how_to_send_an_email_with_transactional_templates.html) - ## Without Mail Helper Class ```python @@ -145,4 +178,4 @@ except urllib.HTTPError as e: print(response.status_code) print(response.body) print(response.headers) -``` +``` \ No newline at end of file From 7438ae17406f6f25cab58f9e807576c94a785d3d Mon Sep 17 00:00:00 2001 From: Elmer Thomas Date: Mon, 20 Aug 2018 17:12:59 -0700 Subject: [PATCH 41/52] Version Bump v5.6.0: Dynamic Template support --- CHANGELOG.md | 6 +++++- docker/README.md | 3 ++- sendgrid/version.py | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 69c0a5211..cde406f18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,11 @@ # Change Log All notable changes to this project will be documented in this file. -## [5.5.1] - 2018-08-16 ## +## [5.6.0] - 2018-08-20 ## +### Added +- [PR #593](https://github.com/sendgrid/sendgrid-python/pull/593): Adds support for dynamic template data. Big thanks to [Wojciech Bartosiak](https://github.com/wojtek-fliposports) for the PR! Also, big thanks to []() for [PR #597](https://github.com/sendgrid/sendgrid-python/pull/597)! + +## [5.5.0] - 2018-08-16 ## ### Added - [PR #588](https://github.com/sendgrid/sendgrid-python/pull/588): Updates the Readme to include environment variable setup in windows. Big thanks to [Bhargav Chandaka](https://github.com/bchandaka) for the PR! - [PR #599](https://github.com/sendgrid/sendgrid-python/pull/599): Updates the Readme to include additional API Key instruction. Big thanks to [Anshul Singhal](https://github.com/af4ro) for the PR! diff --git a/docker/README.md b/docker/README.md index 41c235fcc..b439fe892 100644 --- a/docker/README.md +++ b/docker/README.md @@ -1,5 +1,6 @@ # Supported tags and respective `Dockerfile` links - - `v5.5.0`, `latest` [(Dockerfile)](https://github.com/sendgrid/sendgrid-python/blob/master/docker/Dockerfile) + - `v5.6.0`, `latest` [(Dockerfile)](https://github.com/sendgrid/sendgrid-python/blob/master/docker/Dockerfile) + - `v5.5.0` - `v5.4.1` - `v5.4.0` - `v5.3.0` diff --git a/sendgrid/version.py b/sendgrid/version.py index 30415fcbd..fffe2c469 100644 --- a/sendgrid/version.py +++ b/sendgrid/version.py @@ -1,2 +1,2 @@ -version_info = (5, 5, 0) +version_info = (5, 6, 0) __version__ = '.'.join(str(v) for v in version_info) From fe61c16513c968b2c33ef034f9cbe4759d6d4614 Mon Sep 17 00:00:00 2001 From: Elmer Thomas Date: Mon, 20 Aug 2018 17:15:55 -0700 Subject: [PATCH 42/52] Fix CHANGELOG attribution --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cde406f18..dd2334ba4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ All notable changes to this project will be documented in this file. ## [5.6.0] - 2018-08-20 ## ### Added -- [PR #593](https://github.com/sendgrid/sendgrid-python/pull/593): Adds support for dynamic template data. Big thanks to [Wojciech Bartosiak](https://github.com/wojtek-fliposports) for the PR! Also, big thanks to []() for [PR #597](https://github.com/sendgrid/sendgrid-python/pull/597)! +- [PR #593](https://github.com/sendgrid/sendgrid-python/pull/593): Adds support for dynamic template data. Big thanks to [Slam](https://github.com/3lnc) for the PR! Also, big thanks to [Wojciech Bartosiak](https://github.com/wojtek-fliposports) for [PR #597](https://github.com/sendgrid/sendgrid-python/pull/597)! ## [5.5.0] - 2018-08-16 ## ### Added From 84f7cf258f77f179dce0ef3bee8dc3b90f4af2f3 Mon Sep 17 00:00:00 2001 From: Elmer Thomas Date: Wed, 22 Aug 2018 13:08:57 -0700 Subject: [PATCH 43/52] Typo in comments --- sendgrid/helpers/mail/personalization.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sendgrid/helpers/mail/personalization.py b/sendgrid/helpers/mail/personalization.py index c42694fe9..1ecbb8678 100644 --- a/sendgrid/helpers/mail/personalization.py +++ b/sendgrid/helpers/mail/personalization.py @@ -3,7 +3,7 @@ class Personalization(object): how that message should be handled. :var dynamic_template_data: data for dynamic transactional template. - Should be JSON-serializeable structure. No pre-processing sill be done + Should be JSON-serializeable structure. No pre-processing will be done prior to sending this via http client. """ From 27b302f05d187d1926cfb08b6b365d544eaa8feb Mon Sep 17 00:00:00 2001 From: Corey McCandless Date: Thu, 13 Sep 2018 11:49:12 -0400 Subject: [PATCH 44/52] Fix format of dependency `pytest` --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index ceae6cf62..9756ebf8c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,4 @@ Flask==0.10.1 PyYAML==3.11 python-http-client==2.2.1 six==1.10.0 -pytest=3.7.1 \ No newline at end of file +pytest==3.7.1 \ No newline at end of file From aba7f0449ca22d21be0ae32957e061c6ca64f6f7 Mon Sep 17 00:00:00 2001 From: themousepotato Date: Fri, 28 Sep 2018 09:41:51 +0530 Subject: [PATCH 45/52] fixes #610 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b2b6a1dbd..20bdfbc9b 100644 --- a/README.md +++ b/README.md @@ -105,7 +105,7 @@ print(response.body) print(response.headers) ``` -The `Mail` constructor creates a [personalization object](https://sendgrid.com/docs/Classroom/Send/v3_Mail_Send/personalizations.html) for you. [Here](https://github.com/sendgrid/sendgrid-python/blob/master/examples/helpers/mail/mail_example.py#L16) is an example of how to add it. +The `Mail` constructor creates a [personalization object](https://sendgrid.com/docs/Classroom/Send/v3_Mail_Send/personalizations.html) for you. [Here](https://github.com/sendgrid/sendgrid-python/blob/master/examples/helpers/mail_example.py#L16) is an example of how to add it. ### Without Mail Helper Class From 29fa10d3aaf8363ae51d3be49be0e77199fbe169 Mon Sep 17 00:00:00 2001 From: Bharat Raghunathan Date: Mon, 1 Oct 2018 09:41:45 +0530 Subject: [PATCH 46/52] Update README.md by including email Make it clear that the link is a redirect to an email and not to any members page or developer team page --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b2b6a1dbd..295582eb5 100644 --- a/README.md +++ b/README.md @@ -224,7 +224,8 @@ Please see our [troubleshooting guide](https://github.com/sendgrid/sendgrid-pyth # About -sendgrid-python is guided and supported by the SendGrid [Developer Experience Team](mailto:dx@sendgrid.com). +sendgrid-python is guided and supported by the SendGrid Developer Experience Team. +Email the Developer Experience Team [here](mailto:dx@sendgrid.com) in case of any queries. sendgrid-python is maintained and funded by SendGrid, Inc. The names and logos for sendgrid-python are trademarks of SendGrid, Inc. From a8907d8a68bc05455b874d58c69393d29c08fb31 Mon Sep 17 00:00:00 2001 From: Hugo Date: Mon, 1 Oct 2018 11:14:04 +0300 Subject: [PATCH 47/52] Fix typos --- .github/PULL_REQUEST_TEMPLATE | 2 +- CONTRIBUTING.md | 4 ++-- README.md | 6 +++--- sendgrid/__init__.py | 2 +- sendgrid/helpers/endpoints/ip/unassigned.py | 2 +- sendgrid/sendgrid.py | 2 +- use_cases/aws.md | 10 +++++----- use_cases/slack_event_api_integration.md | 4 ++-- use_cases/transational_templates.md | 2 +- 9 files changed, 17 insertions(+), 17 deletions(-) diff --git a/.github/PULL_REQUEST_TEMPLATE b/.github/PULL_REQUEST_TEMPLATE index 7ad590b42..b3b7a4446 100644 --- a/.github/PULL_REQUEST_TEMPLATE +++ b/.github/PULL_REQUEST_TEMPLATE @@ -21,4 +21,4 @@ Closes #2 - - -If you have questions, please send an email to [Sendgrid](mailto:dx@sendgrid.com), or file a Github Issue in this repository. +If you have questions, please send an email to [SendGrid](mailto:dx@sendgrid.com), or file a GitHub Issue in this repository. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 882e52164..a875bf9c1 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -46,7 +46,7 @@ A software bug is a demonstrable issue in the code base. In order for us to diag Before you decide to create a new issue, please try the following: -1. Check the Github issues tab if the identified issue has already been reported, if so, please add a +1 to the existing post. +1. Check the GitHub issues tab if the identified issue has already been reported, if so, please add a +1 to the existing post. 2. Update to the latest version of this code and check if issue has already been fixed 3. Copy and fill in the Bug Report Template we have provided below @@ -242,4 +242,4 @@ If you have any additional questions, please feel free to [email](mailto:dx@send ## Code Reviews -If you can, please look at open PRs and review them. Give feedback and help us merge these PRs much faster! If you don't know how, Github has some great [information on how to review a Pull Request](https://help.github.com/articles/about-pull-request-reviews/). +If you can, please look at open PRs and review them. Give feedback and help us merge these PRs much faster! If you don't know how, GitHub has some great [information on how to review a Pull Request](https://help.github.com/articles/about-pull-request-reviews/). diff --git a/README.md b/README.md index b2b6a1dbd..a4aa8c343 100644 --- a/README.md +++ b/README.md @@ -57,10 +57,10 @@ echo "export SENDGRID_API_KEY='YOUR_API_KEY'" > sendgrid.env echo "sendgrid.env" >> .gitignore source ./sendgrid.env ``` -Sendgrid also supports local environment file `.env`. Copy or rename `.env_sample` into `.env` and update [SENDGRID_API_KEY](https://app.sendgrid.com/settings/api_keys) with your key. +SendGrid also supports local environment file `.env`. Copy or rename `.env_sample` into `.env` and update [SENDGRID_API_KEY](https://app.sendgrid.com/settings/api_keys) with your key. -### Windows -Temporarily set the environment variable(accesible only during the current cli session): +### Windows +Temporarily set the environment variable(accessible only during the current cli session): ```bash set SENDGRID_API_KEY=YOUR_API_KEY ``` diff --git a/sendgrid/__init__.py b/sendgrid/__init__.py index 2bbd38b59..1b5100c01 100644 --- a/sendgrid/__init__.py +++ b/sendgrid/__init__.py @@ -2,7 +2,7 @@ This library allows you to quickly and easily use the SendGrid Web API v3 via Python. -For more information on this library, see the README on Github. +For more information on this library, see the README on GitHub. http://github.com/sendgrid/sendgrid-python For more information on the SendGrid v3 API, see the v3 docs: http://sendgrid.com/docs/API_Reference/api_v3.html diff --git a/sendgrid/helpers/endpoints/ip/unassigned.py b/sendgrid/helpers/endpoints/ip/unassigned.py index 075f19857..ff5edbd73 100644 --- a/sendgrid/helpers/endpoints/ip/unassigned.py +++ b/sendgrid/helpers/endpoints/ip/unassigned.py @@ -21,7 +21,7 @@ def unassigned(data, as_json=False): and the usernames assigned to an IP unassigned returns a listing of the IP addresses that are allocated - but have 0 usera assigned + but have 0 users assigned data (response.body from sg.client.ips.get()) diff --git a/sendgrid/sendgrid.py b/sendgrid/sendgrid.py index 0f09bd542..cc3450091 100644 --- a/sendgrid/sendgrid.py +++ b/sendgrid/sendgrid.py @@ -2,7 +2,7 @@ This library allows you to quickly and easily use the SendGrid Web API v3 via Python. -For more information on this library, see the README on Github. +For more information on this library, see the README on GitHub. http://github.com/sendgrid/sendgrid-python For more information on the SendGrid v3 API, see the v3 docs: http://sendgrid.com/docs/API_Reference/api_v3.html diff --git a/use_cases/aws.md b/use_cases/aws.md index 2ff04bd1f..d07d5769c 100644 --- a/use_cases/aws.md +++ b/use_cases/aws.md @@ -13,7 +13,7 @@ Python 2.6, 2.7, 3.4, or 3.5 are supported by the sendgrid Python library, howev Before starting this tutorial, you will need to have access to an AWS account in which you are allowed to provision resources. This tutorial also assumes you've already created a SendGrid account with free-tier access. Finally, it is highly recommended you utilize [virtualenv](https://virtualenv.pypa.io/en/stable/). -*DISCLAIMER*: Any resources provisioned here may result in charges being incurred to your account. Sendgrid is in no way responsible for any billing charges. +*DISCLAIMER*: Any resources provisioned here may result in charges being incurred to your account. SendGrid is in no way responsible for any billing charges. ## Getting Started @@ -36,15 +36,15 @@ On the next menu, you have the option to choose what programming language you'll Follow the steps on the next screen. Choose a name for your API key, such as "hello-email". Follow the remaining steps to create an environment variable, install the sendgrid module, and copy the test code. Once that is complete, check the "I've integrated the code above" box, and click the "Next: Verify Integration" button. -Assuming all the steps were completed correctly, you should be greeted with a success message. If not, go back and verify that everything is correct, including your API key environment varible, and Python code. +Assuming all the steps were completed correctly, you should be greeted with a success message. If not, go back and verify that everything is correct, including your API key environment variable, and Python code. ## Deploy hello-world app using CodeStar -For the rest of the tutorial, we'll be working out of the git repository we cloned from AWS earlier: +For the rest of the tutorial, we'll be working out of the Git repository we cloned from AWS earlier: ``` $ cd hello-email ``` -note: this assumes you cloned the git repo inside your current directory. My directory is: +note: this assumes you cloned the Git repo inside your current directory. My directory is: ``` ~/projects/hello-email @@ -139,7 +139,7 @@ def handler(event, context): 'headers': {'Content-Type': 'application/json'}} ``` -Note that for the most part, we've simply copied the intial code from the API verification with SendGrid. Some slight modifications were needed to allow it to run as a lambda function, and for the output to be passed cleanly from the API endpoint. +Note that for the most part, we've simply copied the initial code from the API verification with SendGrid. Some slight modifications were needed to allow it to run as a lambda function, and for the output to be passed cleanly from the API endpoint. Change the `test@example.com` emails appropriately so that you may receive the test email. diff --git a/use_cases/slack_event_api_integration.md b/use_cases/slack_event_api_integration.md index d0e0f6140..ecce40695 100644 --- a/use_cases/slack_event_api_integration.md +++ b/use_cases/slack_event_api_integration.md @@ -1,12 +1,12 @@ # Integrate with Slack Events API -It's fairly straightforward to integrate Sendgrid with Slack, to allow emails to be triggered by events happening on Slack. +It's fairly straightforward to integrate SendGrid with Slack, to allow emails to be triggered by events happening on Slack. For this, we make use of the [Official Slack Events API](https://github.com/slackapi/python-slack-events-api), which can be installed using pip. To allow our application to get notifications of slack events, we first create a Slack App with Event Subscriptions as described [here](https://github.com/slackapi/python-slack-events-api#--development-workflow) -Then, we set `SENDGRID_API_KEY` _(which you can create on the Sendgrid dashboard)_ and `SLACK_VERIFICATION_TOKEN` _(which you can get in the App Credentials section of the Slack App)_ as environment variables. +Then, we set `SENDGRID_API_KEY` _(which you can create on the SendGrid dashboard)_ and `SLACK_VERIFICATION_TOKEN` _(which you can get in the App Credentials section of the Slack App)_ as environment variables. Once this is done, we can subscribe to [events on Slack](https://api.slack.com/events) and trigger emails when an event occurs. In the example below, we trigger an email to `test@example.com` whenever someone posts a message on Slack that has the word "_help_" in it. diff --git a/use_cases/transational_templates.md b/use_cases/transational_templates.md index d3d587bc0..491d528bd 100644 --- a/use_cases/transational_templates.md +++ b/use_cases/transational_templates.md @@ -1,6 +1,6 @@ ### Transactional Templates -Sendgrid transactional templates let you leverage power of [handlebars](https://handlebarsjs.com/) +SendGrid transactional templates let you leverage power of [handlebars](https://handlebarsjs.com/) syntax to easily manage complex dynamic content in transactional emails. For this example, we assume you have created a [transactional template](https://sendgrid.com/docs/User_Guide/Transactional_Templates/create_and_edit_transactional_templates.html). Following is the template content we used for testing. From 1a1d3a50e326f66222ac6e193cfa84b02e7fd365 Mon Sep 17 00:00:00 2001 From: Bharat123Rox Date: Wed, 3 Oct 2018 13:06:39 +0530 Subject: [PATCH 48/52] Update README.md for Email and fix alphabetical order --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f019c1ce3..c08e417fc 100644 --- a/README.md +++ b/README.md @@ -212,9 +212,9 @@ Quick links: - [Feature Request](https://github.com/sendgrid/sendgrid-python/blob/master/CONTRIBUTING.md#feature-request) - [Bug Reports](https://github.com/sendgrid/sendgrid-python/blob/master/CONTRIBUTING.md#submit-a-bug-report) -- [Sign the CLA to Create a Pull Request](https://cla.sendgrid.com/sendgrid/sendgrid-python) - [Improvements to the Codebase](https://github.com/sendgrid/sendgrid-python/blob/master/CONTRIBUTING.md#improvements-to-the-codebase) - [Review Pull Requests](https://github.com/sendgrid/sendgrid-python/blob/master/CONTRIBUTING.md#code-reviews) +- [Sign the CLA to Create a Pull Request](https://cla.sendgrid.com/sendgrid/sendgrid-python) # Troubleshooting @@ -224,7 +224,9 @@ Please see our [troubleshooting guide](https://github.com/sendgrid/sendgrid-pyth # About -sendgrid-python is guided and supported by the SendGrid [Developer Experience Team](mailto:dx@sendgrid.com). +sendgrid-python is guided and supported by the SendGrid Developer Experience Team. + +Email the team [here](mailto:dx@sendgrid.com) in case of any queries. sendgrid-python is maintained and funded by SendGrid, Inc. The names and logos for sendgrid-python are trademarks of SendGrid, Inc. From 80788ede975a874673ad412b1113f83c771ab027 Mon Sep 17 00:00:00 2001 From: Jeremy Yang Date: Wed, 3 Oct 2018 07:37:24 -0700 Subject: [PATCH 49/52] Update README Remove link to a position not found and replaced with general link to the careers page. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f019c1ce3..c70ef1f26 100644 --- a/README.md +++ b/README.md @@ -192,7 +192,7 @@ Please see [our helper](https://github.com/sendgrid/sendgrid-python/tree/master/ # Announcements -Join an experienced and passionate team that focuses on making an impact. Opportunities abound to grow the product - and grow your career! Check out our [Data Platform Engineer role](http://grnh.se/wbx1701) +Join an experienced and passionate team that focuses on making an impact. [Opportunities abound](https://sendgrid.com/careers) to grow the product - and grow your career! Please see our announcement regarding [breaking changes](https://github.com/sendgrid/sendgrid-python/issues/217). Your support is appreciated! From 1bd4a31864670bf284e829b14f04f80816ac73f9 Mon Sep 17 00:00:00 2001 From: Moises Meirelles Date: Sun, 7 Oct 2018 20:04:45 -0300 Subject: [PATCH 50/52] Add PEP8 code compliance according to pycodestyle --- examples/campaigns/campaigns.py | 6 +- examples/categories/categories.py | 3 +- examples/helpers/mail_example.py | 11 ++-- examples/helpers/stats/stats_example.py | 7 ++- examples/mail/mail.py | 12 ++-- register.py | 2 +- sendgrid/helpers/endpoints/ip/unassigned.py | 19 +++++-- sendgrid/helpers/inbound/app.py | 4 +- sendgrid/helpers/inbound/config.py | 4 +- sendgrid/helpers/inbound/send.py | 8 ++- sendgrid/helpers/mail/content.py | 1 + sendgrid/helpers/mail/exceptions.py | 6 +- sendgrid/helpers/mail/mail.py | 6 +- sendgrid/helpers/mail/personalization.py | 6 +- .../helpers/mail/subscription_tracking.py | 6 +- sendgrid/helpers/mail/tracking_settings.py | 3 +- sendgrid/helpers/mail/validators.py | 21 +++---- sendgrid/sendgrid.py | 40 ++++++++----- setup.py | 1 + test/test_app.py | 2 +- test/test_mail.py | 56 +++++++++++++------ test/test_project.py | 2 + test/test_send.py | 24 ++++++-- test/test_sendgrid.py | 40 +++++++++---- test/test_stats.py | 6 +- test/test_unassigned.py | 16 +++--- 26 files changed, 205 insertions(+), 107 deletions(-) diff --git a/examples/campaigns/campaigns.py b/examples/campaigns/campaigns.py index 76bdaeb9e..effda1015 100644 --- a/examples/campaigns/campaigns.py +++ b/examples/campaigns/campaigns.py @@ -14,7 +14,8 @@ "spring line" ], "custom_unsubscribe_url": "", - "html_content": "

Check out our spring line!

", + "html_content": "

Check out our " + "spring line!

", "ip_pool": "marketing", "list_ids": [ 110, @@ -52,7 +53,8 @@ "categories": [ "summer line" ], - "html_content": "

Check out our summer line!

", + "html_content": "

Check out our " + "summer line!

", "plain_content": "Check out our summer line!", "subject": "New Products for Summer!", "title": "May Newsletter" diff --git a/examples/categories/categories.py b/examples/categories/categories.py index 774284ee8..4517645a8 100644 --- a/examples/categories/categories.py +++ b/examples/categories/categories.py @@ -27,7 +27,8 @@ print(response.headers) ################################################## -# Retrieve sums of email stats for each category [Needs: Stats object defined, has category ID?] # +# Retrieve sums of email stats for each category [Needs: Stats object defined, +# has category ID?] # # GET /categories/stats/sums # params = {'end_date': '2016-04-01', diff --git a/examples/helpers/mail_example.py b/examples/helpers/mail_example.py index c1cd166ad..afa2f1ed5 100644 --- a/examples/helpers/mail_example.py +++ b/examples/helpers/mail_example.py @@ -78,8 +78,9 @@ def get_mock_personalization_dict(): def build_attachment1(): - """Build attachment mock. Make sure your content is base64 encoded before passing into attachment.content. - Another example: https://github.com/sendgrid/sendgrid-python/blob/master/use_cases/attachment.md""" + """Build attachment mock. Make sure your content is base64 encoded before + passing into attachment.content. + Another example: https://github.com/sendgrid/sendgrid-python/blob/master/use_cases/attachment.md""" # noqa attachment = Attachment() attachment.content = ("TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNl" "Y3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12") @@ -178,8 +179,8 @@ def build_kitchen_sink(): mail.send_at = 1443636842 # This must be a valid [batch ID] - # (https://sendgrid.com/docs/API_Reference/SMTP_API/scheduling_parameters.html) to work - # mail.set_batch_id("N2VkYjBjYWItMGU4OC0xMWU2LWJhMzYtZjQ1Yzg5OTBkNzkxLWM5ZTUyZjNhOA") + # (https://sendgrid.com/docs/API_Reference/SMTP_API/scheduling_parameters.html) to work # noqa + # mail.set_batch_id("N2VkYjBjYWItMGU4OC0xMWU2LWJhMzYtZjQ1Yzg5OTBkNzkxLWM5ZTUyZjNhOA") # noqa mail.asm = ASM(99, [4, 5, 6, 7, 8]) mail.ip_pool_name = "24" mail.mail_settings = build_mail_settings() @@ -221,7 +222,7 @@ def send_kitchen_sink(): def transactional_template_usage(): # Assumes you set your environment variable: # https://github.com/sendgrid/sendgrid-python/blob/master/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key - + """ Sample usage of dynamic (handlebars) transactional templates. To make this work, you should have dynamic template created within your diff --git a/examples/helpers/stats/stats_example.py b/examples/helpers/stats/stats_example.py index d48664c3f..e54f45354 100644 --- a/examples/helpers/stats/stats_example.py +++ b/examples/helpers/stats/stats_example.py @@ -3,7 +3,8 @@ from sendgrid.helpers.stats import * from sendgrid import * -# NOTE: you will need move this file to the root directory of this project to execute properly. +# NOTE: you will need move this file to the root directory of this project to +# execute properly. # Assumes you set your environment variable: # https://github.com/sendgrid/sendgrid-python/blob/master/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key @@ -39,12 +40,13 @@ def build_category_stats_sums(): def build_subuser_stats(): - subuser_stats = SubuserStats('2017-10-20', ['aaronmakks','foo']) + subuser_stats = SubuserStats('2017-10-20', ['aaronmakks', 'foo']) # subuser_stats.start_date = '2017-10-15' # subuser_stats.add_subuser(Subuser("foo")) # subuser_stats.add_subuser(Subuser("bar")) return subuser_stats.get() + def build_subuser_stats_sums(): subuser_stats = SubuserStats() subuser_stats.start_date = '2017-10-15' @@ -92,6 +94,7 @@ def get_subuser_stats_sums(): print(response.headers) pprint_json(response.body) + get_global_stats() get_category_stats() get_category_stats_sums() diff --git a/examples/mail/mail.py b/examples/mail/mail.py index e853d422c..43d301dfa 100644 --- a/examples/mail/mail.py +++ b/examples/mail/mail.py @@ -57,7 +57,8 @@ "content": [ { "type": "text/html", - "value": "

Hello, world!

" + "value": "

Hello, world!

" + "" } ], "custom_args": { @@ -151,7 +152,8 @@ "ganalytics": { "enable": True, "utm_campaign": "[NAME OF YOUR REFERRER SOURCE]", - "utm_content": "[USE THIS SPACE TO DIFFERENTIATE YOUR EMAIL FROM ADS]", + "utm_content": "[USE THIS SPACE TO DIFFERENTIATE YOUR EMAIL" + " FROM ADS]", "utm_medium": "[NAME OF YOUR MARKETING MEDIUM e.g. email]", "utm_name": "[NAME OF YOUR CAMPAIGN]", "utm_term": "[IDENTIFY PAID KEYWORDS HERE]" @@ -162,9 +164,11 @@ }, "subscription_tracking": { "enable": True, - "html": "If you would like to unsubscribe and stop receiving these emails <% clickhere %>.", + "html": "If you would like to unsubscribe and stop receiving " + "these emails <% clickhere %>.", "substitution_tag": "<%click here%>", - "text": "If you would like to unsubscribe and stop receiving these emails <% click here %>." + "text": "If you would like to unsubscribe and stop receiving " + "these emails <% click here %>." } } } diff --git a/register.py b/register.py index 0a7ffe8d8..00ddca15c 100644 --- a/register.py +++ b/register.py @@ -17,4 +17,4 @@ ''' final_text = readme_rst.replace(replace, replacement) with open('./README.txt', 'w', encoding='utf-8') as f: - f.write(final_text) + f.write(final_text) diff --git a/sendgrid/helpers/endpoints/ip/unassigned.py b/sendgrid/helpers/endpoints/ip/unassigned.py index ff5edbd73..ba07fd747 100644 --- a/sendgrid/helpers/endpoints/ip/unassigned.py +++ b/sendgrid/helpers/endpoints/ip/unassigned.py @@ -20,18 +20,25 @@ def unassigned(data, as_json=False): The /ips rest endpoint returns information about the IP addresses and the usernames assigned to an IP - unassigned returns a listing of the IP addresses that are allocated + unassigned returns a listing of the IP addresses that are allocated but have 0 users assigned - - data (response.body from sg.client.ips.get()) + data (response.body from sg.client.ips.get()) as_json False -> get list of dicts True -> get json object example: - sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) - - params = {'subuser': 'test_string', 'ip': 'test_string', 'limit': 1, 'exclude_whitelabels': 'true', 'offset': 1} + sg = sendgrid.SendGridAPIClient( + apikey=os.environ.get('SENDGRID_API_KEY') + ) + + params = { + 'subuser': 'test_string', + 'ip': 'test_string', + 'limit': 1, + 'exclude_whitelabels': 'true', + 'offset': 1 + } response = sg.client.ips.get(query_params=params) if response.status_code == 201: data = response.body diff --git a/sendgrid/helpers/inbound/app.py b/sendgrid/helpers/inbound/app.py index 0d4435907..c199cb725 100755 --- a/sendgrid/helpers/inbound/app.py +++ b/sendgrid/helpers/inbound/app.py @@ -3,13 +3,13 @@ See README.txt for usage instructions.""" try: from config import Config -except: +except: # noqa # Python 3+, Travis from sendgrid.helpers.inbound.config import Config try: from parse import Parse -except: +except: # noqa # Python 3+, Travis from sendgrid.helpers.inbound.parse import Parse diff --git a/sendgrid/helpers/inbound/config.py b/sendgrid/helpers/inbound/config.py index d0c6517bc..36ba89595 100644 --- a/sendgrid/helpers/inbound/config.py +++ b/sendgrid/helpers/inbound/config.py @@ -10,8 +10,8 @@ def __init__(self, **opts): if os.environ.get('ENV') != 'prod': # We are not in Heroku self.init_environment() - """Allow variables assigned in config.yml available the following variables - via properties""" + """Allow variables assigned in config.yml available the following + variables via properties""" self.path = opts.get( 'path', os.path.abspath(os.path.dirname(__file__)) ) diff --git a/sendgrid/helpers/inbound/send.py b/sendgrid/helpers/inbound/send.py index 6de575aab..d5138b89e 100644 --- a/sendgrid/helpers/inbound/send.py +++ b/sendgrid/helpers/inbound/send.py @@ -37,9 +37,12 @@ def url(self): """URL to send to.""" return self._url + def main(): config = Config() - parser = argparse.ArgumentParser(description='Test data and optional host.') + parser = argparse.ArgumentParser( + description='Test data and optional host.' + ) parser.add_argument('data', type=str, help='path to the sample data') @@ -54,5 +57,6 @@ def main(): print(response.headers) print(response.body) + if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/sendgrid/helpers/mail/content.py b/sendgrid/helpers/mail/content.py index cff8ac498..da4ed8027 100644 --- a/sendgrid/helpers/mail/content.py +++ b/sendgrid/helpers/mail/content.py @@ -1,5 +1,6 @@ from .validators import ValidateAPIKey + class Content(object): """Content to be included in your email. diff --git a/sendgrid/helpers/mail/exceptions.py b/sendgrid/helpers/mail/exceptions.py index ab4dd9c0c..1b5da92fc 100644 --- a/sendgrid/helpers/mail/exceptions.py +++ b/sendgrid/helpers/mail/exceptions.py @@ -2,6 +2,7 @@ # Various types of extensible SendGrid related exceptions ################################################################ + class SendGridException(Exception): """Wrapper/default SendGrid-related exception""" pass @@ -14,9 +15,8 @@ class APIKeyIncludedException(SendGridException): message -- explanation of the error """ - def __init__(self, - expression="Email body", + def __init__(self, + expression="Email body", message="SendGrid API Key detected"): self.expression = expression self.message = message - diff --git a/sendgrid/helpers/mail/mail.py b/sendgrid/helpers/mail/mail.py index fd606fd18..3114e5b21 100644 --- a/sendgrid/helpers/mail/mail.py +++ b/sendgrid/helpers/mail/mail.py @@ -16,7 +16,8 @@ def __init__(self, content=None): """Create a Mail object. - If any parameters are not supplied, they must be set after initialization. + If any parameters are not supplied, they must be set after + initialization. :param from_email: Email address to send from. :type from_email: Email, optional :param subject: Subject line of emails. @@ -203,7 +204,8 @@ def batch_id(self): This represents a batch of emails sent at the same time. Including a batch_id in your request allows you include this email in that batch, and also enables you to cancel or pause the delivery of that batch. - For more information, see https://sendgrid.com/docs/API_Reference/Web_API_v3/cancel_schedule_send.html + For more information, see + https://sendgrid.com/docs/API_Reference/Web_API_v3/cancel_schedule_send.html # noqa :rtype: int """ diff --git a/sendgrid/helpers/mail/personalization.py b/sendgrid/helpers/mail/personalization.py index 1ecbb8678..97499a34c 100644 --- a/sendgrid/helpers/mail/personalization.py +++ b/sendgrid/helpers/mail/personalization.py @@ -60,7 +60,8 @@ def add_cc(self, email): @property def bccs(self): - """A list of recipients who will receive blind carbon copies of this email. + """A list of recipients who will receive blind carbon copies of + this email. :rtype: list(dict) """ @@ -217,6 +218,7 @@ def get(self): personalization["send_at"] = self.send_at if self.dynamic_template_data is not None: - personalization['dynamic_template_data'] = self.dynamic_template_data + personalization['dynamic_template_data'] = \ + self.dynamic_template_data return personalization diff --git a/sendgrid/helpers/mail/subscription_tracking.py b/sendgrid/helpers/mail/subscription_tracking.py index 204e427f0..552642ed8 100644 --- a/sendgrid/helpers/mail/subscription_tracking.py +++ b/sendgrid/helpers/mail/subscription_tracking.py @@ -4,7 +4,8 @@ class SubscriptionTracking(object): location of the link within your email, you may use the substitution_tag. """ - def __init__(self, enable=None, text=None, html=None, substitution_tag=None): + def __init__(self, enable=None, text=None, html=None, + substitution_tag=None): """Create a SubscriptionTracking to customize subscription management. :param enable: Whether this setting is enabled. @@ -13,7 +14,8 @@ def __init__(self, enable=None, text=None, html=None, substitution_tag=None): :type text: string, optional :param html: HTML to be appended to the email with the link as "<% %>". :type html: string, optional - :param substitution_tag: Tag replaced with URL. Overrides text, html params. + :param substitution_tag: Tag replaced with URL. Overrides text, + html params. :type substitution_tag: string, optional """ self.enable = enable diff --git a/sendgrid/helpers/mail/tracking_settings.py b/sendgrid/helpers/mail/tracking_settings.py index cb19e2bae..73642586c 100644 --- a/sendgrid/helpers/mail/tracking_settings.py +++ b/sendgrid/helpers/mail/tracking_settings.py @@ -10,7 +10,8 @@ def __init__(self): @property def click_tracking(self): - """Allows you to track whether a recipient clicked a link in your email. + """Allows you to track whether a recipient clicked a link in your + email. :rtype: ClickTracking """ diff --git a/sendgrid/helpers/mail/validators.py b/sendgrid/helpers/mail/validators.py index b4a69f697..b12d95bd0 100644 --- a/sendgrid/helpers/mail/validators.py +++ b/sendgrid/helpers/mail/validators.py @@ -3,6 +3,7 @@ # Various types of Validators ################################################################ + class ValidateAPIKey(object): """Validates content to ensure SendGrid API key is not present""" @@ -27,15 +28,16 @@ def __init__(self, regex_strings=None, use_default=True): default_regex_string = 'SG\.[0-9a-zA-Z]+\.[0-9a-zA-Z]+' self.regexes.add(re.compile(default_regex_string)) - def validate_message_dict(self, request_body): - """With the JSON dict that will be sent to SendGrid's API, - check the content for SendGrid API keys - throw exception if found + """With the JSON dict that will be sent to SendGrid's API, + check the content for SendGrid API keys - throw exception if found + Args: request_body (:obj:`dict`): message parameter that is an argument to: mail.send.post() Raises: - APIKeyIncludedException: If any content in request_body matches regex + APIKeyIncludedException: If any content in request_body + matches regex """ # Handle string in edge-case @@ -44,9 +46,9 @@ def validate_message_dict(self, request_body): # Default param elif isinstance(request_body, dict): - + contents = request_body.get("content", list()) - + for content in contents: if content is not None: if (content.get("type") == "text/html" or @@ -54,10 +56,10 @@ def validate_message_dict(self, request_body): message_text = content.get("value", "") self.validate_message_text(message_text) - def validate_message_text(self, message_string): - """With a message string, check to see if it contains a SendGrid API Key - If a key is found, throw an exception + """With a message string, check to see if it contains a + SendGrid API Key If a key is found, throw an exception + Args: message_string (str): message that will be sent Raises: @@ -68,4 +70,3 @@ def validate_message_text(self, message_string): for regex in self.regexes: if regex.match(message_string) is not None: raise APIKeyIncludedException() - diff --git a/sendgrid/sendgrid.py b/sendgrid/sendgrid.py index cc3450091..9811c2292 100644 --- a/sendgrid/sendgrid.py +++ b/sendgrid/sendgrid.py @@ -25,7 +25,9 @@ class SendGridAPIClient(object): """The SendGrid API Client. Use this object to interact with the v3 API. For example: - sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) + sg = sendgrid.SendGridAPIClient(apikey=os.environ.get( + 'SENDGRID_API_KEY') + ) ... mail = Mail(from_email, subject, to_email, content) response = sg.client.mail.send.post(request_body=mail.get()) @@ -43,28 +45,34 @@ def __init__( **opts): # TODO: remove **opts for 6.x release """ Construct SendGrid v3 API object. - Note that underlying client being set up during initialization, therefore changing - attributes in runtime will not affect HTTP client behaviour. + Note that underlying client being set up during initialization, + therefore changing attributes in runtime will not affect HTTP + client behaviour. - :param apikey: SendGrid API key to use. If not provided, key will be read from - environment variable "SENDGRID_API_KEY" + :param apikey: SendGrid API key to use. If not provided, key will be + read from environment variable "SENDGRID_API_KEY" :type apikey: basestring - :param api_key: SendGrid API key to use. Provides backward compatibility + :param api_key: SendGrid API key to use. Provides backward + compatibility .. deprecated:: 5.3 Use apikey instead :type api_key: basestring - :param impersonate_subuser: the subuser to impersonate. Will be passed by - "On-Behalf-Of" header by underlying client. - See https://sendgrid.com/docs/User_Guide/Settings/subusers.html for more details + :param impersonate_subuser: the subuser to impersonate. Will be passed + by "On-Behalf-Of" header by underlying client. + See https://sendgrid.com/docs/User_Guide/Settings/subusers.html + for more details :type impersonate_subuser: basestring :param host: base URL for API calls :type host: basestring - :param opts: dispatcher for deprecated arguments. Added for backward-compatibility - with `path` parameter. Should be removed during 6.x release + :param opts: dispatcher for deprecated arguments. Added for + backward-compatibility with `path` parameter. + Should be removed during 6.x release """ if opts: warnings.warn( - 'Unsupported argument(s) provided: {}'.format(list(opts.keys())), + 'Unsupported argument(s) provided: {}'.format( + list(opts.keys()) + ), DeprecationWarning) self.apikey = apikey or api_key or os.environ.get('SENDGRID_API_KEY') self.impersonate_subuser = impersonate_subuser @@ -72,9 +80,11 @@ def __init__( self.useragent = 'sendgrid/{0};python'.format(__version__) self.version = __version__ - self.client = python_http_client.Client(host=self.host, - request_headers=self._default_headers, - version=3) + self.client = python_http_client.Client( + host=self.host, + request_headers=self._default_headers, + version=3 + ) @property def _default_headers(self): diff --git a/setup.py b/setup.py index 014691b61..11aa3a07e 100644 --- a/setup.py +++ b/setup.py @@ -20,6 +20,7 @@ def getRequires(): deps.append('unittest2py3k') return deps + setup( name='sendgrid', version=str(__version__), diff --git a/test/test_app.py b/test/test_app.py index 1a8e4a698..13b0a9522 100644 --- a/test/test_app.py +++ b/test/test_app.py @@ -23,4 +23,4 @@ def test_up_and_running(self): def test_used_port_true(self): if self.config.debug_mode: port = int(os.environ.get("PORT", self.config.port)) - self.assertEqual(port, self.config.port) \ No newline at end of file + self.assertEqual(port, self.config.port) diff --git a/test/test_mail.py b/test/test_mail.py index 08d0feb8e..72cb6b069 100644 --- a/test/test_mail.py +++ b/test/test_mail.py @@ -52,31 +52,40 @@ def test_sendgridAPIKey(self): personalization.add_to(Email("test@example.com")) mail.add_personalization(personalization) - #Try to include SendGrid API key + # Try to include SendGrid API key try: - mail.add_content(Content("text/plain", "some SG.2123b1B.1212lBaC here")) + mail.add_content( + Content( + "text/plain", + "some SG.2123b1B.1212lBaC here" + ) + ) mail.add_content( Content( "text/html", - "some SG.Ba2BlJSDba.232Ln2 here")) + "some SG.Ba2BlJSDba.232Ln2 here" + ) + ) self.assertEqual( json.dumps( mail.get(), sort_keys=True), - '{"content": [{"type": "text/plain", "value": "some text here"}, ' - '{"type": "text/html", ' - '"value": "some text here"}], ' + '{"content": [' + '{"type": "text/plain", "value": "some text here"' + '}, {"type": "text/html", "value": "some text ' + 'here"' + '}], ' '"from": {"email": "test@example.com"}, "personalizations": ' '[{"to": [{"email": "test@example.com"}]}], ' '"subject": "Hello World from the SendGrid Python Library"}' ) - #Exception should be thrown + # Exception should be thrown except Exception as e: pass - #Exception not thrown + # Exception not thrown else: self.fail("Should have failed as SendGrid API key included") @@ -115,7 +124,9 @@ def test_helloEmail(self): self.assertTrue(isinstance(str(mail), str)) def test_helloEmailAdditionalContent(self): - """Tests bug found in Issue-451 with Content ordering causing a crash""" + """ + Tests bug found in Issue-451 with Content ordering causing a crash + """ self.maxDiff = None @@ -130,7 +141,8 @@ def test_helloEmailAdditionalContent(self): personalization.add_to(Email("test@example.com")) mail.add_personalization(personalization) - mail.add_content(Content("text/html", "some text here")) + mail.add_content(Content("text/html", "some text " + "here")) mail.add_content(Content("text/plain", "some text here")) self.assertEqual( @@ -164,7 +176,8 @@ def test_kitchenSink(self): personalization.add_cc(Email("test@example.com", "Example User")) personalization.add_bcc(Email("test@example.com")) personalization.add_bcc(Email("test@example.com")) - personalization.subject = "Hello World from the Personalized SendGrid Python Library" + personalization.subject = "Hello World from the Personalized " \ + "SendGrid Python Library" personalization.add_header(Header("X-Test", "test")) personalization.add_header(Header("X-Mock", "true")) personalization.add_substitution( @@ -182,7 +195,8 @@ def test_kitchenSink(self): personalization2.add_cc(Email("test@example.com", "Example User")) personalization2.add_bcc(Email("test@example.com")) personalization2.add_bcc(Email("test@example.com")) - personalization2.subject = "Hello World from the Personalized SendGrid Python Library" + personalization2.subject = "Hello World from the Personalized " \ + "SendGrid Python Library" personalization2.add_header(Header("X-Test", "test")) personalization2.add_header(Header("X-Mock", "true")) personalization2.add_substitution( @@ -200,7 +214,8 @@ def test_kitchenSink(self): "some text here")) attachment = Attachment() - attachment.content = "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12" + attachment.content = "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY" \ + "3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12" attachment.type = "application/pdf" attachment.filename = "balance_001.pdf" attachment.disposition = "attachment" @@ -263,12 +278,15 @@ def test_kitchenSink(self): True, True) tracking_settings.open_tracking = OpenTracking( True, - "Optional tag to replace with the open image in the body of the message") + "Optional tag to replace with the open image in the body " + "of the message") tracking_settings.subscription_tracking = SubscriptionTracking( True, "text to insert into the text/plain portion of the message", - "html to insert into the text/html portion of the message", - "Optional tag to replace with the open image in the body of the message") + "html to insert into the text/html portion of " + "the message", + "Optional tag to replace with the open image in the body of " + "the message") tracking_settings.ganalytics = Ganalytics( True, "some source", @@ -499,7 +517,8 @@ def test_unicode_values_in_substitutions_helper(self): mail.from_email = Email("test@example.com") - mail.subject = "Testing unicode substitutions with the SendGrid Python Library" + mail.subject = "Testing unicode substitutions with the SendGrid " \ + "Python Library" personalization = Personalization() personalization.add_to(Email("test@example.com")) @@ -538,7 +557,8 @@ def test_unicode_values_in_substitutions_helper(self): ] } ], - "subject": "Testing unicode substitutions with the SendGrid Python Library", + "subject": "Testing unicode substitutions with the " + "SendGrid Python Library", } self.assertEqual( diff --git a/test/test_project.py b/test/test_project.py index 4fbc8147c..5e269ae43 100644 --- a/test/test_project.py +++ b/test/test_project.py @@ -5,6 +5,7 @@ except ImportError: import unittest + class ProjectTests(unittest.TestCase): # ./docker @@ -71,5 +72,6 @@ def test_usage(self): def test_use_cases(self): self.assertTrue(os.path.isfile('./use_cases/README.md')) + if __name__ == '__main__': unittest.main() diff --git a/test/test_send.py b/test/test_send.py index 16d496b85..003214892 100644 --- a/test/test_send.py +++ b/test/test_send.py @@ -30,13 +30,27 @@ def test_send(self): x = send.Send(fake_url) x.test_payload(fake_url) - send.Client.assert_called_once_with(host=fake_url, request_headers={'User-Agent': 'SendGrid-Test', - 'Content-Type': 'multipart/form-data; boundary=xYzZY'}) + send.Client.assert_called_once_with( + host=fake_url, + request_headers={ + 'User-Agent': 'SendGrid-Test', + 'Content-Type': 'multipart/form-data; boundary=xYzZY' + } + ) def test_main_call(self): fake_url = 'https://fake_url' - with mock.patch('argparse.ArgumentParser.parse_args', return_value=argparse.Namespace(host=fake_url, data='test_file.txt')): + with mock.patch( + 'argparse.ArgumentParser.parse_args', + return_value=argparse.Namespace(host=fake_url, + data='test_file.txt') + ): send.main() - send.Client.assert_called_once_with(host=fake_url, request_headers={'User-Agent': 'SendGrid-Test', - 'Content-Type': 'multipart/form-data; boundary=xYzZY'}) + send.Client.assert_called_once_with( + host=fake_url, + request_headers={ + 'User-Agent': 'SendGrid-Test', + 'Content-Type': 'multipart/form-data; boundary=xYzZY' + } + ) diff --git a/test/test_sendgrid.py b/test/test_sendgrid.py index c545cbb2d..68193c9b4 100644 --- a/test/test_sendgrid.py +++ b/test/test_sendgrid.py @@ -25,18 +25,22 @@ def setUpClass(cls): cls.sg = sendgrid.SendGridAPIClient(host=host) cls.devnull = open(os.devnull, 'w') prism_cmd = None - + # try: # # check for prism in the PATH - # if subprocess.call('prism version'.split(), stdout=cls.devnull) == 0: + # if subprocess.call('prism version'.split(), + # stdout=cls.devnull) == 0: # prism_cmd = 'prism' # except OSError: # prism_cmd = None # if not prism_cmd: # # check for known prism locations - # for path in ('/usr/local/bin/prism', os.path.expanduser(os.path.join('~', 'bin', 'prism')), - # os.path.abspath(os.path.join(os.getcwd(), 'prism', 'bin', 'prism'))): + # for path in ('/usr/local/bin/prism', + # os.path.expanduser(os.path.join('~', 'bin', + # 'prism')), + # os.path.abspath(os.path.join(os.getcwd(), 'prism', + # 'bin', 'prism'))): # prism_cmd = path if os.path.isfile(path) else None # if prism_cmd: # break @@ -45,10 +49,14 @@ def setUpClass(cls): # if sys.platform != 'win32': # # try to install with prism.sh # try: - # print("Warning: no prism detected, I will try to install it locally") - # prism_sh = os.path.abspath(os.path.join(cls.path, 'test', 'prism.sh')) + # print("Warning: no prism detected, I will try to install" + # " it locally") + # prism_sh = os.path.abspath(os.path.join(cls.path, 'test', + # 'prism.sh')) # if subprocess.call(prism_sh) == 0: - # prism_cmd = os.path.expanduser(os.path.join('~', 'bin', 'prism')) + # prism_cmd = os.path.expanduser(os.path.join('~', + # 'bin', + # 'prism')) # else: # raise RuntimeError() # except Exception as e: @@ -68,7 +76,8 @@ def setUpClass(cls): # cls.p = subprocess.Popen([ # prism_cmd, "run", "-s", # "https://raw.githubusercontent.com/sendgrid/sendgrid-oai/master/" - # "oai_stoplight.json"], stdout=cls.devnull, stderr=subprocess.STDOUT) + # "oai_stoplight.json"], stdout=cls.devnull, + # stderr=subprocess.STDOUT) # time.sleep(15) # print("Prism Started") @@ -134,7 +143,7 @@ def test_reset_request_headers(self): self.assertNotIn('blah', self.sg.client.request_headers) self.assertNotIn('blah2x', self.sg.client.request_headers) - for k,v in self.sg._default_headers.items(): + for k, v in self.sg._default_headers.items(): self.assertEqual(v, self.sg.client.request_headers[k]) def test_hello_world(self): @@ -144,8 +153,14 @@ def test_hello_world(self): content = Content( "text/plain", "and easy to do anywhere, even with Python") mail = Mail(from_email, subject, to_email, content) - self.assertTrue(mail.get() == {'content': [{'type': 'text/plain', 'value': 'and easy to do anywhere, even with Python'}], 'personalizations': [ - {'to': [{'email': 'test@example.com'}]}], 'from': {'email': 'test@example.com'}, 'subject': 'Sending with SendGrid is Fun'}) + self.assertTrue(mail.get() == {'content': + [{'type': 'text/plain', + 'value': 'and easy to do anywhere, ' + 'even with Python'}], + 'personalizations': [{'to': [{'email': + 'test@example.com'}]}], + 'from': {'email': 'test@example.com'}, + 'subject': 'Sending with SendGrid is Fun'}) def test_access_settings_activity_get(self): params = {'limit': 1} @@ -2373,7 +2388,8 @@ def test_license_year(self): LICENSE_FILE = 'LICENSE.txt' with open(LICENSE_FILE, 'r') as f: copyright_line = f.readline().rstrip() - self.assertEqual('Copyright (c) 2012-%s SendGrid, Inc.' % datetime.datetime.now().year, copyright_line) + self.assertEqual('Copyright (c) 2012-%s SendGrid, Inc.' % + datetime.datetime.now().year, copyright_line) # @classmethod # def tearDownClass(cls): diff --git a/test/test_stats.py b/test/test_stats.py index c71117397..c1bb88d8b 100644 --- a/test/test_stats.py +++ b/test/test_stats.py @@ -45,7 +45,8 @@ def test_Stats(self): def test_categoryStats(self): - category_stats = CategoryStats(start_date='12-09-2017', categories=['foo', 'bar']) + category_stats = CategoryStats(start_date='12-09-2017', + categories=['foo', 'bar']) category_stats.add_category(Category('woo')) category_stats.end_date = '12-10-2017' category_stats.aggregated_by = 'day' @@ -66,7 +67,8 @@ def test_categoryStats(self): def test_subuserStats(self): - subuser_stats = SubuserStats(start_date = '12-09-2017', subusers=['foo', 'bar']) + subuser_stats = SubuserStats(start_date='12-09-2017', + subusers=['foo', 'bar']) subuser_stats.add_subuser(Subuser('blah')) subuser_stats.end_date = '12-10-2017' subuser_stats.aggregated_by = 'day' diff --git a/test/test_unassigned.py b/test/test_unassigned.py index d13451277..80ff9bb1e 100644 --- a/test/test_unassigned.py +++ b/test/test_unassigned.py @@ -5,11 +5,11 @@ ret_json = '''[ { - "ip": "167.89.21.3", + "ip": "167.89.21.3", "pools": [ - "pool1", - "pool2" - ], + "pool1", + "pool2" + ], "whitelabeled": false, "start_date": 1409616000, "subusers": [ @@ -55,6 +55,7 @@ } ] ''' + def get_all_ip(): ret_val = json.loads(ret_json) return ret_val @@ -67,10 +68,9 @@ def make_data(): return data - def test_unassigned_ip_json(): - data = make_data() + data = make_data() as_json = True calculated = unassigned(get_all_ip(), as_json=as_json) @@ -79,9 +79,10 @@ def test_unassigned_ip_json(): for item in calculated: assert item["ip"] in data + def test_unassigned_ip_obj(): - data = make_data() + data = make_data() as_json = False calculated = unassigned(get_all_ip(), as_json=as_json) @@ -89,6 +90,7 @@ def test_unassigned_ip_obj(): for item in calculated: assert item["ip"] in data + def test_unassigned_baddata(): as_json = False calculated = unassigned(dict(), as_json=as_json) From 6665395eb9b7df78ffa83643c6332bf2afdbe1c5 Mon Sep 17 00:00:00 2001 From: Moises Meirelles Date: Sun, 7 Oct 2018 20:05:58 -0300 Subject: [PATCH 51/52] Update .travis.yml to execute pycodestyle validating PEP8 Errors --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index 5ad33bccf..6e2f46b30 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,6 +19,7 @@ install: - pip install pypandoc - pip install coverage - pip install codecov +- pip install pycodestyle # - sudo apt-get install -y pandoc addons: apt_packages: @@ -30,6 +31,7 @@ before_script: - chmod +x ./cc-test-reporter - ./cc-test-reporter before-build script: +- pycodestyle --ignore=W605 --exclude='build' - if [[ $TRAVIS_PYTHON_VERSION == '2.6' ]]; then coverage run -m unittest2 discover; else coverage run -m unittest discover; fi after_script: - codecov From 5a84109cd571e2e320ef0d4c9622ed748ad713dc Mon Sep 17 00:00:00 2001 From: Moises Meirelles Date: Sun, 7 Oct 2018 21:07:32 -0300 Subject: [PATCH 52/52] Add PEP8 code compliance according to pycodestyle --- .travis.yml | 5 ++++- test/test_mail.py | 8 ++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6e2f46b30..1b55a568b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -31,7 +31,10 @@ before_script: - chmod +x ./cc-test-reporter - ./cc-test-reporter before-build script: -- pycodestyle --ignore=W605 --exclude='build' +# errors W605 and W504 added to avoid inconsistent errors happening +# error W605 happening on validators.py:28:40 on a valid Regex +# error W504 happening on validators.py:54:29 on a valid line break operator +- pycodestyle --ignore=W605,W504 --exclude='build' - if [[ $TRAVIS_PYTHON_VERSION == '2.6' ]]; then coverage run -m unittest2 discover; else coverage run -m unittest discover; fi after_script: - codecov diff --git a/test/test_mail.py b/test/test_mail.py index 72cb6b069..711f8e4c9 100644 --- a/test/test_mail.py +++ b/test/test_mail.py @@ -589,9 +589,9 @@ def test_dynamic_template_data(self): 'customer': { 'name': 'Bob', 'returning': True - }, + }, 'total': 42 - } + } expected = { 'to': [{'email': 'test@sendgrid.com'}], @@ -599,8 +599,8 @@ def test_dynamic_template_data(self): 'customer': { 'name': 'Bob', 'returning': True - }, + }, 'total': 42 - } + } } self.assertDictEqual(p.get(), expected)