diff --git a/examples/example.py b/examples/example.py index 07c3b15..332b39f 100644 --- a/examples/example.py +++ b/examples/example.py @@ -19,9 +19,8 @@ # header.add_substitution('key', 'value') header.set_substitutions({'key': ['value1', 'value2']}) -# [Unique Arguments] -# (http://sendgrid.com/docs/API_Reference/SMTP_API/unique_arguments.html) -# header.add_unique_arg('key', 'value') +# [Unique Arguments](http://sendgrid.com/docs/API_Reference/SMTP_API/unique_arguments.html) +#header.add_unique_arg('key', 'value') header.set_unique_args({'key': 'value'}) # [Categories](http://sendgrid.com/docs/API_Reference/SMTP_API/categories.html) @@ -44,10 +43,9 @@ # (https://sendgrid.com/docs/API_Reference/Web_API_v3/IP_Management/ip_pools.html) header.set_ip_pool("testPool") -# [Scheduling Parameters] -# (https://sendgrid.com/docs/API_Reference/SMTP_API/scheduling_parameters.html) -# header.add_send_each_at(unix_timestamp) # must be a unix timestamp -# header.set_send_each_at([]) # must be a unix timestamp +# [Scheduling Parameters](https://sendgrid.com/docs/API_Reference/SMTP_API/scheduling_parameters.html) +# header.add_send_each_at(unix_timestamp) # must be a unix timestamp +# header.set_send_each_at([]) # must be a unix timestamp header.set_send_at(int(time.time())) # must be a unix timestamp print(header.json_string()) diff --git a/smtpapi/__init__.py b/smtpapi/__init__.py index 1ed55a7..44ff7da 100644 --- a/smtpapi/__init__.py +++ b/smtpapi/__init__.py @@ -1,104 +1,101 @@ -import json -import decimal - - -class _CustomJSONEncoder(json.JSONEncoder): - - def default(self, o): - if isinstance(o, decimal.Decimal): - return float(o) - # Provide a fallback to the default encoder if we haven't implemented - # special support for the object's class - return super(_CustomJSONEncoder, self).default(o) - - -class SMTPAPIHeader(object): - - def __init__(self): - self.data = {} - - def add_to(self, to): - if 'to' not in self.data: - self.data['to'] = [] - if type(to) is list: - self.data['to'] += to - else: - self.data['to'].append(to) - - def set_tos(self, tos): - self.data['to'] = tos - - def add_substitution(self, key, value): - if 'sub' not in self.data: - self.data['sub'] = {} - if key not in self.data['sub']: - self.data['sub'][key] = [] - self.data['sub'][key].append(value) - - def set_substitutions(self, subs): - self.data['sub'] = subs - - def _add_key_value(self, index, key, value): - if index not in self.data: - self.data[index] = {} - self.data[index][key] = value - - def _add_key(self, index, key): - if index not in self.data: - self.data[index] = [] - self.data[index].append(key) - - def add_unique_arg(self, key, value): - self._add_key_value('unique_args', key, value) - - def set_unique_args(self, value): - self.data['unique_args'] = value - - def add_category(self, category): - self._add_key('category', category) - - def set_categories(self, category): - self.data['category'] = category - - def add_section(self, key, section): - self._add_key_value('section', key, section) - - def set_sections(self, value): - self.data['section'] = value - - def add_send_each_at(self, time): - self._add_key('send_each_at', time) - - def set_send_each_at(self, time): - self.data['send_each_at'] = time - - def set_send_at(self, time): - self.data['send_at'] = time - - def add_filter(self, app, setting, val): - if 'filters' not in self.data: - self.data['filters'] = {} - if app not in self.data['filters']: - self.data['filters'][app] = {} - if 'settings' not in self.data['filters'][app]: - self.data['filters'][app]['settings'] = {} - self.data['filters'][app]['settings'][setting] = val - - def set_asm_group_id(self, value): - if not bool(value): - self.data['asm_group_id'] = {} - else: - self.data['asm_group_id'] = value - - def set_ip_pool(self, value): - if bool(value): - self.data['ip_pool'] = value - else: - self.data['ip_pool'] = {} - - def json_string(self): - result = {} - for key in self.data.keys(): - if self.data[key] != [] and self.data[key] != {}: - result[key] = self.data[key] - return json.dumps(result, cls=_CustomJSONEncoder) +import json +import decimal + + +class _CustomJSONEncoder(json.JSONEncoder): + + def default(self, o): + if isinstance(o, decimal.Decimal): + return float(o) + # Provide a fallback to the default encoder if we haven't implemented special support for the object's class + return super(_CustomJSONEncoder, self).default(o) + + +class SMTPAPIHeader(object): + + def __init__(self): + self.data = {} + + def add_to(self, to): + if 'to' not in self.data: + self.data['to'] = [] + if type(to) is list: + self.data['to'] += to + else: + self.data['to'].append(to) + + def set_tos(self, tos): + self.data['to'] = tos + + def add_substitution(self, key, value): + if 'sub' not in self.data: + self.data['sub'] = {} + if key not in self.data['sub']: + self.data['sub'][key] = [] + self.data['sub'][key].append(value) + + def set_substitutions(self, subs): + self.data['sub'] = subs + + def add_unique_arg(self, key, value): + if 'unique_args' not in self.data: + self.data['unique_args'] = {} + self.data['unique_args'][key] = value + + def set_unique_args(self, value): + self.data['unique_args'] = value + + def add_category(self, category): + if 'category' not in self.data: + self.data['category'] = [] + self.data['category'].append(category) + + def set_categories(self, category): + self.data['category'] = category + + def add_section(self, key, section): + if 'section' not in self.data: + self.data['section'] = {} + self.data['section'][key] = section + + def set_sections(self, value): + self.data['section'] = value + + def add_send_each_at(self, time): + if 'send_each_at' not in self.data: + self.data['send_each_at'] = [] + self.data['send_each_at'].append(time) + + def set_send_each_at(self, time): + self.data['send_each_at'] = time + + def set_send_at(self, time): + self.data['send_at'] = time + + def add_filter(self, app, setting, val): + if 'filters' not in self.data: + self.data['filters'] = {} + if app not in self.data['filters']: + self.data['filters'][app] = {} + if 'settings' not in self.data['filters'][app]: + self.data['filters'][app]['settings'] = {} + self.data['filters'][app]['settings'][setting] = val + + def set_asm_group_id(self, value): + if not bool(value): + self.data['asm_group_id'] = {} + else: + self.data['asm_group_id'] = value + + def set_ip_pool(self, value): + if bool(value): + self.data['ip_pool'] = value + else: + self.data['ip_pool'] = {} + + def json_string(self): + result = {} + for key in self.data.keys(): + if self.data[key] != [] and self.data[key] != {}: + result[key] = self.data[key] + return json.dumps(result, cls=_CustomJSONEncoder) diff --git a/test/__init__.py b/test/__init__.py index 00ff8b8..5c450e9 100644 --- a/test/__init__.py +++ b/test/__init__.py @@ -1,5 +1,5 @@ -import decimal import json +import decimal import os import datetime