From e8ca55d8364ff5879e0e00ee6a88e0bbb4472c6c Mon Sep 17 00:00:00 2001 From: Manan Jadhav Date: Tue, 10 Nov 2020 11:33:20 +0530 Subject: [PATCH 1/4] Flake8 lint fixes with autopep8 /?/ --- examples/account.py | 22 +-- examples/call.py | 15 +- examples/default.py | 2 +- examples/general.py | 8 +- examples/hlr.py | 15 +- examples/pay.py | 15 +- examples/sms.py | 16 ++- examples/token.py | 4 +- examples/viber.py | 15 +- examples/voice.py | 15 +- examples/whois.py | 8 +- greensms/__init__.py | 2 +- greensms/api/module.py | 45 +++--- greensms/api/module_loader.py | 105 +++++++------- greensms/api/modules.py | 258 +++++++++++++++++----------------- greensms/api/schema.py | 216 ++++++++++++++-------------- greensms/client.py | 107 +++++++------- greensms/constants.py | 4 +- greensms/http/error.py | 85 +++++------ greensms/http/rest.py | 93 ++++++------ greensms/utils/attr_dict.py | 51 +++---- greensms/utils/dict.py | 2 +- greensms/utils/url.py | 19 +-- greensms/utils/validator.py | 28 ++-- greensms/utils/version.py | 11 +- setup.py | 44 +++--- tests/account_test.py | 41 +++--- tests/call_test.py | 22 +-- tests/default.py | 2 +- tests/general_test.py | 18 +-- tests/hlr_test.py | 20 +-- tests/pay_test.py | 19 +-- tests/sms_test.py | 33 ++--- tests/token_test.py | 30 ++-- tests/utils.py | 6 +- tests/viber_test.py | 32 +++-- tests/voice_test.py | 20 +-- 37 files changed, 747 insertions(+), 701 deletions(-) diff --git a/examples/account.py b/examples/account.py index 068fb20..b310e40 100644 --- a/examples/account.py +++ b/examples/account.py @@ -1,20 +1,24 @@ from default import client + def account_balance(): - res = client.account.balance() - print(res) + res = client.account.balance() + print(res) + def account_token(): - res = client.account.token(expire=100) - print(res) + res = client.account.token(expire=100) + print(res) + def account_tariff(): - res = client.account.tariff() - print(res) + res = client.account.tariff() + print(res) + if __name__ == '__main__': - account_balance() + account_balance() - account_token() + account_token() - account_tariff() \ No newline at end of file + account_tariff() diff --git a/examples/call.py b/examples/call.py index b7a0017..fd18c79 100644 --- a/examples/call.py +++ b/examples/call.py @@ -1,14 +1,17 @@ from default import client + def call_send(): - res = client.call.send(to='919987409698') - print(res) + res = client.call.send(to='919987409698') + print(res) + def call_status(): - res = client.call.status(id='1fd4ac4d-6e4f-4e32-b6e4-8087d3466f55') - print(res) + res = client.call.status(id='1fd4ac4d-6e4f-4e32-b6e4-8087d3466f55') + print(res) + if __name__ == '__main__': - call_send() + call_send() - call_status() \ No newline at end of file + call_status() diff --git a/examples/default.py b/examples/default.py index 66e911e..87a407d 100644 --- a/examples/default.py +++ b/examples/default.py @@ -4,4 +4,4 @@ os.environ['GREENSMS_USER'] = 'username' os.environ['GREENSMS_PASS'] = 'password' -client = GreenSMS() \ No newline at end of file +client = GreenSMS() diff --git a/examples/general.py b/examples/general.py index 443e3b3..c67c76b 100644 --- a/examples/general.py +++ b/examples/general.py @@ -1,8 +1,10 @@ from default import client + def status(): - res = client.status() - print(res) + res = client.status() + print(res) + if __name__ == '__main__': - status() \ No newline at end of file + status() diff --git a/examples/hlr.py b/examples/hlr.py index 92f2592..aba38d8 100644 --- a/examples/hlr.py +++ b/examples/hlr.py @@ -1,14 +1,17 @@ from default import client + def hlr_send(): - res = client.hlr.send(to='919987409698', txt='1920') - print(res) + res = client.hlr.send(to='919987409698', txt='1920') + print(res) + def hlr_status(): - res = client.hlr.status(id='70d296f5-ac52-403d-a27b-24829c2faebc') - print(res) + res = client.hlr.status(id='70d296f5-ac52-403d-a27b-24829c2faebc') + print(res) + if __name__ == '__main__': - hlr_send() + hlr_send() - hlr_status() \ No newline at end of file + hlr_status() diff --git a/examples/pay.py b/examples/pay.py index 744c115..56f10f8 100644 --- a/examples/pay.py +++ b/examples/pay.py @@ -1,14 +1,17 @@ from default import client + def pay_send(): - res = client.pay.send(to='919987409698', amount= 10) - print(res) + res = client.pay.send(to='919987409698', amount=10) + print(res) + def pay_status(): - res = client.pay.status(id='60f231d9-16ec-4313-842e-6e6853063482') - print(res) + res = client.pay.status(id='60f231d9-16ec-4313-842e-6e6853063482') + print(res) + if __name__ == '__main__': - pay_send() + pay_send() - pay_status() \ No newline at end of file + pay_status() diff --git a/examples/sms.py b/examples/sms.py index 4d019a7..b612a37 100644 --- a/examples/sms.py +++ b/examples/sms.py @@ -1,14 +1,18 @@ from default import client + def sms_send(): - res = client.sms.send(to='919987409698', txt='Here is your message for delivery') - print(res) + res = client.sms.send( + to='919987409698', txt='Here is your message for delivery') + print(res) + def sms_status(): - res = client.sms.status(id='dc2bac6d-f375-4e19-9a02-ef0148991635') - print(res) + res = client.sms.status(id='dc2bac6d-f375-4e19-9a02-ef0148991635') + print(res) + if __name__ == '__main__': - sms_send() + sms_send() - sms_status() \ No newline at end of file + sms_status() diff --git a/examples/token.py b/examples/token.py index 8b275d4..dec08a3 100644 --- a/examples/token.py +++ b/examples/token.py @@ -1,6 +1,6 @@ import os from greensms.client import GreenSMS -os.environ['GREENSMS_TOKEN'] = '1234' # Change this value to your actual Token +os.environ['GREENSMS_TOKEN'] = '1234' # Change this value to your actual Token token_client = GreenSMS() -res = token_client.account.balance() \ No newline at end of file +res = token_client.account.balance() diff --git a/examples/viber.py b/examples/viber.py index 7fac0a6..a6ec72e 100644 --- a/examples/viber.py +++ b/examples/viber.py @@ -1,14 +1,17 @@ from default import client + def viber_send(): - res = client.viber.send(to='919987409698',txt='Viber Message') - print(res) + res = client.viber.send(to='919987409698', txt='Viber Message') + print(res) + def viber_status(): - res = client.viber.status(id='0b18fab4-0c5d-4a8b-8ee4-057a59596c7d') - print(res) + res = client.viber.status(id='0b18fab4-0c5d-4a8b-8ee4-057a59596c7d') + print(res) + if __name__ == '__main__': - viber_send() + viber_send() - viber_status() \ No newline at end of file + viber_status() diff --git a/examples/voice.py b/examples/voice.py index 52f7861..13d74e4 100644 --- a/examples/voice.py +++ b/examples/voice.py @@ -1,14 +1,17 @@ from default import client + def voice_send(): - res = client.voice.send(to='919987409698', txt='1234') - print(res) + res = client.voice.send(to='919987409698', txt='1234') + print(res) + def voice_status(): - res = client.voice.status(id='41f23094-deda-4cab-ac9c-3ab4f2fee9e6') - print(res) + res = client.voice.status(id='41f23094-deda-4cab-ac9c-3ab4f2fee9e6') + print(res) + if __name__ == '__main__': - voice_send() + voice_send() - voice_status() \ No newline at end of file + voice_status() diff --git a/examples/whois.py b/examples/whois.py index fe30ff1..9930558 100644 --- a/examples/whois.py +++ b/examples/whois.py @@ -1,8 +1,10 @@ from default import client + def whois_lookup(): - res = client.whois.lookup(to='4477743336335') - print(res) + res = client.whois.lookup(to='4477743336335') + print(res) + if __name__ == '__main__': - whois_lookup() \ No newline at end of file + whois_lookup() diff --git a/greensms/__init__.py b/greensms/__init__.py index 979c51e..82a86ce 100644 --- a/greensms/__init__.py +++ b/greensms/__init__.py @@ -1,2 +1,2 @@ def test(): - return ('Test String') \ No newline at end of file + return ('Test String') diff --git a/greensms/api/module.py b/greensms/api/module.py index f5c54be..64a5f38 100644 --- a/greensms/api/module.py +++ b/greensms/api/module.py @@ -1,34 +1,35 @@ from greensms.utils.validator import validate + class Module: - """ - Module Instance Class + """ + Module Instance Class - Call that inits with default args and has an API function to call the Rest API along with Validation - """ - def __init__(self, rest_client, module_schema, **kwargs): - self.rest_client = rest_client - self.module_schema = module_schema - self.params = {} + Call that inits with default args and has an API function to call the Rest API along with Validation + """ - for key, value in kwargs.items(): - self.params[key] = value + def __init__(self, rest_client, module_schema, **kwargs): + self.rest_client = rest_client + self.module_schema = module_schema + self.params = {} - def api_func(self, **kwargs): + for key, value in kwargs.items(): + self.params[key] = value - if self.module_schema is not None: - errors = validate(self.module_schema, kwargs) - if errors: - return errors + def api_func(self, **kwargs): - api_params={} - for key, value in kwargs.items(): - api_params[key] = value + if self.module_schema is not None: + errors = validate(self.module_schema, kwargs) + if errors: + return errors - request_params = self.params.copy() - request_params['params'] = api_params + api_params = {} + for key, value in kwargs.items(): + api_params[key] = value - response = self.rest_client.request(**request_params) + request_params = self.params.copy() + request_params['params'] = api_params - return response + response = self.rest_client.request(**request_params) + return response diff --git a/greensms/api/module_loader.py b/greensms/api/module_loader.py index 39fbc6e..72ecd40 100644 --- a/greensms/api/module_loader.py +++ b/greensms/api/module_loader.py @@ -5,78 +5,79 @@ class ModuleLoader: - """ Generic Module Loader Class + """ Generic Module Loader Class - Allows us with an ability to set module.version.function based on a dictionary - """ - - def __init__(self): - self.module_map = AttrDict({}) + Allows us with an ability to set module.version.function based on a dictionary + """ - def register_modules(self, shared_options, filters = {}): - if not filters: - filters = {} + def __init__(self): + self.module_map = AttrDict({}) - current_version = shared_options['version'] + def register_modules(self, shared_options, filters={}): + if not filters: + filters = {} - # Layered approach for Module > Version > Function - for module_name, module_info in MODULES.items(): + current_version = shared_options['version'] - if module_name not in self.module_map: - self.module_map[module_name] = AttrDict({}) + # Layered approach for Module > Version > Function + for module_name, module_info in MODULES.items(): - module_versions = module_info['versions'] - module_schema = None + if module_name not in self.module_map: + self.module_map[module_name] = AttrDict({}) - if 'load_static' in filters and 'static' in module_info and filters['load_static'] == True and module_info['static'] == True: - continue + module_versions = module_info['versions'] + module_schema = None - for version, version_functions in module_versions.items(): - if version not in self.module_map[module_name]: - self.module_map[module_name][version] = AttrDict({}) + if 'load_static' in filters and 'static' in module_info and filters['load_static'] == True and module_info['static'] == True: + continue - for function_name, definition in version_functions.items(): + for version, version_functions in module_versions.items(): + if version not in self.module_map[module_name]: + self.module_map[module_name][version] = AttrDict({}) - if function_name not in self.module_map[module_name][version]: - self.module_map[module_name][version][function_name] = AttrDict({}) + for function_name, definition in version_functions.items(): - module_schema = None - if 'schema' in module_info and version in module_info['schema'] and function_name in module_info['schema'][version]: - module_schema = module_info['schema'][version][function_name] + if function_name not in self.module_map[module_name][version]: + self.module_map[module_name][version][function_name] = AttrDict({ + }) - url_args = [] - if 'static' not in module_info or module_info['static'] == False: - url_args.append(module_name) - url_args.append(function_name) + module_schema = None + if 'schema' in module_info and version in module_info['schema'] and function_name in module_info['schema'][version]: + module_schema = module_info['schema'][version][function_name] - api_url = build_url(shared_options['base_url'], url_args) + url_args = [] + if 'static' not in module_info or module_info['static'] == False: + url_args.append(module_name) + url_args.append(function_name) - # Call a Higher Order Function that returns an Module instance function - self.module_map[module_name][version][function_name] = self.module_api(shared_options=shared_options, api_url=api_url, definition=definition, module_schema=module_schema) + api_url = build_url(shared_options['base_url'], url_args) - if version == current_version: - self.module_map[module_name][function_name] = self.module_map[module_name][version][function_name] + # Call a Higher Order Function that returns an Module instance function + self.module_map[module_name][version][function_name] = self.module_api( + shared_options=shared_options, api_url=api_url, definition=definition, module_schema=module_schema) - if 'static' in module_info and module_info['static'] == True: - self.module_map[function_name] = self.module_map[module_name][version][function_name] - del self.module_map[module_name] + if version == current_version: + self.module_map[module_name][function_name] = self.module_map[module_name][version][function_name] - return self.module_map + if 'static' in module_info and module_info['static'] == True: + self.module_map[function_name] = self.module_map[module_name][version][function_name] + del self.module_map[module_name] + return self.module_map - def module_api(self, **kwargs): - """ Module API Function + def module_api(self, **kwargs): + """ Module API Function - Higher Order Function that returns a Function, to be invoked by user later - """ + Higher Order Function that returns a Function, to be invoked by user later + """ - rest_client = kwargs['shared_options']['rest_client'] - module_schema = kwargs['module_schema'] + rest_client = kwargs['shared_options']['rest_client'] + module_schema = kwargs['module_schema'] - request_params = { - 'url': kwargs['api_url'], - 'method': kwargs['definition']['method'], - } + request_params = { + 'url': kwargs['api_url'], + 'method': kwargs['definition']['method'], + } - module = Module(rest_client, module_schema, **request_params) - return module.api_func \ No newline at end of file + module = Module(rest_client, module_schema, **request_params) + return module.api_func diff --git a/greensms/api/modules.py b/greensms/api/modules.py index a80226b..9f6da54 100644 --- a/greensms/api/modules.py +++ b/greensms/api/modules.py @@ -1,136 +1,136 @@ from greensms.api.schema import VALIDATION_SCHEMA MODULES = { - 'account': { - 'schema': VALIDATION_SCHEMA['account'], - 'versions': { - 'v1': { - 'balance': { - 'args': None, - 'method': 'GET' - }, - 'token': { - 'args': ['params'], - 'method': 'POST' - }, - 'tariff': { - 'args': None, - 'method': 'GET' + 'account': { + 'schema': VALIDATION_SCHEMA['account'], + 'versions': { + 'v1': { + 'balance': { + 'args': None, + 'method': 'GET' + }, + 'token': { + 'args': ['params'], + 'method': 'POST' + }, + 'tariff': { + 'args': None, + 'method': 'GET' + } + } } - } - } - }, - 'call': { - 'schema': VALIDATION_SCHEMA['call'], - 'versions': { - 'v1': { - 'send': { - 'args': ['params'], - 'method': 'POST', - }, - 'status': { - 'args': ['params'], - 'method': 'GET', - }, - } - } - }, - 'whois': { - 'schema': VALIDATION_SCHEMA['whois'], - 'versions': { - 'v1': { - 'lookup': { - 'args': ['params'], - 'method': 'GET' + }, + 'call': { + 'schema': VALIDATION_SCHEMA['call'], + 'versions': { + 'v1': { + 'send': { + 'args': ['params'], + 'method': 'POST', + }, + 'status': { + 'args': ['params'], + 'method': 'GET', + }, + } } - } - } - }, - 'general': { - 'schema': VALIDATION_SCHEMA['general'], - 'static': True, - 'versions': { - 'v1': { - 'status': { - 'args': None, - 'method': 'GET', - }, - }, }, - }, - 'voice': { - 'schema': VALIDATION_SCHEMA['voice'], - 'versions': { - 'v1': { - 'send': { - 'args': ['params'], - 'method': 'POST', - }, - 'status': { - 'args': ['params'], - 'method': 'GET', - }, - } - } - }, - 'pay': { - 'schema': VALIDATION_SCHEMA['pay'], - 'versions': { - 'v1': { - 'send': { - 'args': ['params'], - 'method': 'POST', - }, - 'status': { - 'args': ['params'], - 'method': 'GET', - }, - } - } - }, - 'hlr': { - 'schema': VALIDATION_SCHEMA['hlr'], - 'versions': { - 'v1': { - 'send': { - 'args': ['params'], - 'method': 'POST', - }, - 'status': { - 'args': ['params'], - 'method': 'GET', - }, - } - } - }, - 'sms': { - 'schema': VALIDATION_SCHEMA['sms'], - 'versions': { - 'v1': { - 'send': { - 'args': ['params'], - 'method': 'POST', - }, - 'status': { - 'args': ['params'], - 'method': 'GET', - }, - } - } - }, - 'viber': { - 'schema': VALIDATION_SCHEMA['viber'], - 'versions': { - 'v1': { - 'send': { - 'args': ['params'], - 'method': 'POST', - }, - 'status': { - 'args': ['params'], - 'method': 'GET', + 'whois': { + 'schema': VALIDATION_SCHEMA['whois'], + 'versions': { + 'v1': { + 'lookup': { + 'args': ['params'], + 'method': 'GET' + } + } + } + }, + 'general': { + 'schema': VALIDATION_SCHEMA['general'], + 'static': True, + 'versions': { + 'v1': { + 'status': { + 'args': None, + 'method': 'GET', + }, + }, }, - } - } - }, -} \ No newline at end of file + }, + 'voice': { + 'schema': VALIDATION_SCHEMA['voice'], + 'versions': { + 'v1': { + 'send': { + 'args': ['params'], + 'method': 'POST', + }, + 'status': { + 'args': ['params'], + 'method': 'GET', + }, + } + } + }, + 'pay': { + 'schema': VALIDATION_SCHEMA['pay'], + 'versions': { + 'v1': { + 'send': { + 'args': ['params'], + 'method': 'POST', + }, + 'status': { + 'args': ['params'], + 'method': 'GET', + }, + } + } + }, + 'hlr': { + 'schema': VALIDATION_SCHEMA['hlr'], + 'versions': { + 'v1': { + 'send': { + 'args': ['params'], + 'method': 'POST', + }, + 'status': { + 'args': ['params'], + 'method': 'GET', + }, + } + } + }, + 'sms': { + 'schema': VALIDATION_SCHEMA['sms'], + 'versions': { + 'v1': { + 'send': { + 'args': ['params'], + 'method': 'POST', + }, + 'status': { + 'args': ['params'], + 'method': 'GET', + }, + } + } + }, + 'viber': { + 'schema': VALIDATION_SCHEMA['viber'], + 'versions': { + 'v1': { + 'send': { + 'args': ['params'], + 'method': 'POST', + }, + 'status': { + 'args': ['params'], + 'method': 'GET', + }, + } + } + }, +} diff --git a/greensms/api/schema.py b/greensms/api/schema.py index 0bcd357..c3fe2ca 100644 --- a/greensms/api/schema.py +++ b/greensms/api/schema.py @@ -1,126 +1,126 @@ from greensms.utils.dict import deep_merge to_schema = { - 'type': 'string', - 'minlength': 11, - 'maxlength': 14, - 'required': True, - 'regex': '^\d+' + 'type': 'string', + 'minlength': 11, + 'maxlength': 14, + 'required': True, + 'regex': "^[0-9]+" } id_schema = { - 'type': 'string', - 'minlength': 36, - 'maxlength': 36, - 'required': True + 'type': 'string', + 'minlength': 36, + 'maxlength': 36, + 'required': True } common_schema = { - 'v1': { - 'send': { - 'to': to_schema - }, - 'status': { - 'id': id_schema, - 'extended': { - 'type': 'boolean', - 'required': False - } + 'v1': { + 'send': { + 'to': to_schema + }, + 'status': { + 'id': id_schema, + 'extended': { + 'type': 'boolean', + 'required': False + } + } } - } } VALIDATION_SCHEMA = { - 'account': { - 'v1': { - 'token': { - 'expire': { - 'type': 'integer', - 'min': 0 + 'account': { + 'v1': { + 'token': { + 'expire': { + 'type': 'integer', + 'min': 0 + } + } } - } - } - }, - 'call': common_schema , - 'hlr': common_schema, - 'whois': { - 'v1': { - 'lookup': { - 'to': to_schema - } }, - }, - 'general': {}, - 'voice': deep_merge(common_schema, - { - 'v1': { - 'send': { - 'txt': { - 'type': 'string', - 'minlength': 1, - 'maxlength': 5, - 'regex': '^\d+', - 'required': True - }, - 'lang': { - 'type': 'string', - 'allowed': ['ru', 'en'] - } - } - } - }), - 'pay': deep_merge(common_schema, { - 'v1': { - 'send': { - 'amount': { - 'type': 'number', - 'min': 1, - 'required': True + 'call': common_schema, + 'hlr': common_schema, + 'whois': { + 'v1': { + 'lookup': { + 'to': to_schema + } }, - 'tag': { - 'type': 'string' - } - } - } - }), - 'sms': deep_merge(common_schema, - { - 'v1': { - 'send': { - 'txt': { - 'type': 'string', - 'minlength': 1, - 'required': True - }, - 'from': { - 'type': 'string', - }, - 'tag': { - 'type': 'string', - }, - 'hidden': { - 'type': 'string', - } - } - } - }), - 'viber': deep_merge(common_schema, - { - 'v1': { - 'send': { - 'txt': { - 'type': 'string', - 'required': True, - 'minlength': 1 - }, - 'from': { - 'type': 'string', - }, - 'cascade': { - 'type': 'string', - 'allowed': ['sms', 'voice'] - } + }, + 'general': {}, + 'voice': deep_merge(common_schema, + { + 'v1': { + 'send': { + 'txt': { + 'type': 'string', + 'minlength': 1, + 'maxlength': 5, + 'regex': "^[0-9]+", + 'required': True + }, + 'lang': { + 'type': 'string', + 'allowed': ['ru', 'en'] + } + } + } + }), + 'pay': deep_merge(common_schema, { + 'v1': { + 'send': { + 'amount': { + 'type': 'number', + 'min': 1, + 'required': True + }, + 'tag': { + 'type': 'string' + } + } } - } }), -} \ No newline at end of file + 'sms': deep_merge(common_schema, + { + 'v1': { + 'send': { + 'txt': { + 'type': 'string', + 'minlength': 1, + 'required': True + }, + 'from': { + 'type': 'string', + }, + 'tag': { + 'type': 'string', + }, + 'hidden': { + 'type': 'string', + } + } + } + }), + 'viber': deep_merge(common_schema, + { + 'v1': { + 'send': { + 'txt': { + 'type': 'string', + 'required': True, + 'minlength': 1 + }, + 'from': { + 'type': 'string', + }, + 'cascade': { + 'type': 'string', + 'allowed': ['sms', 'voice'] + } + } + } + }), +} diff --git a/greensms/client.py b/greensms/client.py index f72a821..f9145ae 100644 --- a/greensms/client.py +++ b/greensms/client.py @@ -1,75 +1,78 @@ import os from greensms.utils.version import get_version -from greensms.utils.url import base_url, build_url +from greensms.utils.url import base_url from greensms.http.rest import HttpClient from greensms.api.module_loader import ModuleLoader +import types -class GreenSMS(object): - """ Client for accessing the GreenSMS API""" - def __init__(self, user=None, password=None, token=None, version=None, camel_case_response=False, use_token_for_requests=False): - """ Initialize the client +class GreenSMS(object): + """ Client for accessing the GreenSMS API""" - :param str user: Username. Required when AuthToken is not passed - :param str password: Password. Request when AuthToken is not passed - :param str token: AuthToken. Required when Username/Password not passed - :param str version: API version to be used - :param bool use_token_for_requests: Create Auth Token after login and use for subsequent requests - :param bool camel_case_response: Convert all response keys to camelCase + def __init__(self, user=None, password=None, token=None, version=None, camel_case_response=False, use_token_for_requests=False): # noqa: E501 + """ Initialize the client - :returns: Twilio Client - :rtype: greensms.GreenSMS + :param str user: Username. Required when AuthToken is not passed + :param str password: Password. Request when AuthToken is not passed + :param str token: AuthToken. Required when Username/Password not passed + :param str version: API version to be used + :param bool use_token_for_requests: Create Auth Token after login and use for subsequent requests + :param bool camel_case_response: Convert all response keys to camelCase - """ + :returns: Twilio Client + :rtype: greensms.GreenSMS - environment = os.environ + """ - self.token = token or environment.get('GREENSMS_TOKEN') - self.user = user or environment.get('GREENSMS_USER') - self.password = password or environment.get('GREENSMS_PASS') - self.version = version - self.use_camel_case = camel_case_response if type(camel_case_response) == type(True) else False - self.use_token_for_requests = use_token_for_requests if type(use_token_for_requests) == type(True) else False + environment = os.environ - if self.token is not None: - self.user = None - self.password = None + self.token = token or environment.get('GREENSMS_TOKEN') + self.user = user or environment.get('GREENSMS_USER') + self.password = password or environment.get('GREENSMS_PASS') + self.version = version + self.use_camel_case = camel_case_response if isinstance( + camel_case_response, types.BooleanType) else False + self.use_token_for_requests = use_token_for_requests if isinstance( + use_token_for_requests, types.BooleanType) else False - if self.token is None and (self.user is None or self.password is None): - raise Exception('Either User/Pass or Auth Token is required!') + if self.token is not None: + self.user = None + self.password = None - shared_options = { - 'use_token_for_requests': self.use_token_for_requests, - 'version': get_version(version), - 'rest_client': self._http_client(use_camel_case=camel_case_response), - 'base_url': base_url() - } + if self.token is None and (self.user is None or self.password is None): + raise Exception('Either User/Pass or Auth Token is required!') - self.add_modules(shared_options) + shared_options = { + 'use_token_for_requests': self.use_token_for_requests, + 'version': get_version(version), + 'rest_client': self._http_client(use_camel_case=camel_case_response), + 'base_url': base_url() + } + self.add_modules(shared_options) - def add_modules(self, shared_options): - module_loader = ModuleLoader() - modules = module_loader.register_modules(shared_options) - for module, value in modules.items(): - setattr(self, module, value) + def add_modules(self, shared_options): + module_loader = ModuleLoader() + modules = module_loader.register_modules(shared_options) + for module, value in modules.items(): + setattr(self, module, value) - def _http_client(self, **kwargs): + def _http_client(self, **kwargs): - default_params = {} + default_params = {} - if not self.token and self.user: - default_params['user'] = self.user - default_params['pass'] = self.password + if not self.token and self.user: + default_params['user'] = self.user + default_params['pass'] = self.password - http_args = { - 'default_params': default_params, - 'default_data': {}, - 'token': self.token, - } + http_args = { + 'default_params': default_params, + 'default_data': {}, + 'token': self.token, + } - for key, value in kwargs.items(): - http_args[key] = value + for key, value in kwargs.items(): + http_args[key] = value - rest_client = HttpClient(http_args) - return rest_client \ No newline at end of file + rest_client = HttpClient(http_args) + return rest_client diff --git a/greensms/constants.py b/greensms/constants.py index 69ead88..1953fd1 100644 --- a/greensms/constants.py +++ b/greensms/constants.py @@ -1,6 +1,6 @@ BASE_URL = 'https://api3.greensms.ru' VERSIONS = { - 'v1': 'v1', + 'v1': 'v1', } RES_STATUS_ACCEPTED = 'Accepted' -RES_STATUS_DELAYED = 'Delayed' \ No newline at end of file +RES_STATUS_DELAYED = 'Delayed' diff --git a/greensms/http/error.py b/greensms/http/error.py index 448d7c5..1d83fde 100644 --- a/greensms/http/error.py +++ b/greensms/http/error.py @@ -1,44 +1,45 @@ -try: - from builtins import super -except: - pass +# try: +# from builtins import super +# except: +# pass -class RestError(Exception): - """ Custom Error Implementation - - Custom Error Class extending Exception with additional fields like error type - """ - - def __init__(self, error): - - error_message = '' - if type(error) == Exception: - error_message = error.message - else: - error_message = error['error'] - self.name = error['name'] if 'name' in error.keys() else 'RestError' - self.code = error['code'] if 'code' in error.keys() else -1 - - self.error = error_message - self.message = error_message - # super().__init__(self.message) - - error_type = self._get_error_type(self.code) - self.error_type = error_type - - if 'params' in error.keys(): - self.params = error['params'] - - - def _get_error_type(self, code): - if code == 0: - return 'AUTH_DECLINED' - elif code == 1: - return 'MISSING_INPUT_PARAM' - elif code == 2: - return 'INVALID_INPUT_PARAM' - elif code == 404: - return 'NOT_FOUND' - else: - return 'INTERNAL_SERVER_ERROR' \ No newline at end of file +class RestError(Exception): + """ Custom Error Implementation + + Custom Error Class extending Exception with additional fields like error type + """ + + def __init__(self, error): + + error_message = '' + if type(error) == Exception: + error_message = error.message + else: + error_message = error['error'] + self.name = error['name'] if 'name' in error.keys( + ) else 'RestError' + self.code = error['code'] if 'code' in error.keys() else -1 + + self.error = error_message + self.message = error_message + + # super().__init__(self.message) + + error_type = self._get_error_type(self.code) + self.error_type = error_type + + if 'params' in error.keys(): + self.params = error['params'] + + def _get_error_type(self, code): + if code == 0: + return 'AUTH_DECLINED' + elif code == 1: + return 'MISSING_INPUT_PARAM' + elif code == 2: + return 'INVALID_INPUT_PARAM' + elif code == 404: + return 'NOT_FOUND' + else: + return 'INTERNAL_SERVER_ERROR' diff --git a/greensms/http/rest.py b/greensms/http/rest.py index 1401ce1..70f4abe 100644 --- a/greensms/http/rest.py +++ b/greensms/http/rest.py @@ -2,67 +2,70 @@ import humps from greensms.http.error import RestError + class HttpClient: - def __init__(self, opts): + def __init__(self, opts): - self.token = None - self.default_data = {} - self.default_params = {} - self.use_camel_case = False + self.token = None + self.default_data = {} + self.default_params = {} + self.use_camel_case = False - attributes = ['token', 'default_data', 'default_params', 'use_camel_case'] - for attribute in attributes: - if attribute in opts: - self.__dict__[attribute] = opts[attribute] + attributes = ['token', 'default_data', + 'default_params', 'use_camel_case'] + for attribute in attributes: + if attribute in opts: + self.__dict__[attribute] = opts[attribute] - def request(self, **kwargs): + def request(self, **kwargs): # noqa: C901 - if 'method' not in kwargs: - raise Exception('Http Method is required') - method = kwargs['method'] + if 'method' not in kwargs: + raise Exception('Http Method is required') + method = kwargs['method'] - if 'url' not in kwargs: - raise Exception('URL is required') - url = kwargs['url'] + if 'url' not in kwargs: + raise Exception('URL is required') + url = kwargs['url'] - headers = kwargs['headers'] if 'headers' in kwargs else {} + headers = kwargs['headers'] if 'headers' in kwargs else {} - if self.token: - headers['Authorization'] = 'Bearer ' + self.token + if self.token: + headers['Authorization'] = 'Bearer ' + self.token - del kwargs['method'] - del kwargs['url'] - if 'headers' in kwargs: - del kwargs['headers'] + del kwargs['method'] + del kwargs['url'] + if 'headers' in kwargs: + del kwargs['headers'] - params = {} - if bool(self.default_params): - for key, value in self.default_params.items(): - params[key] = value + params = {} + if bool(self.default_params): + for key, value in self.default_params.items(): + params[key] = value - if 'params' in kwargs: - for key, value in kwargs['params'].items(): - params[key] = value + if 'params' in kwargs: + for key, value in kwargs['params'].items(): + params[key] = value - data = {} - if bool(self.default_data): - for key, value in self.default_data.items(): - data[key] = value + data = {} + if bool(self.default_data): + for key, value in self.default_data.items(): + data[key] = value - if 'data' in kwargs: - for key, value in kwargs['data'].items(): - data[key] = value + if 'data' in kwargs: + for key, value in kwargs['data'].items(): + data[key] = value - response = requests.request(method=method, url=url, headers=headers, params=params) + response = requests.request( + method=method, url=url, headers=headers, params=params) - response = response.json() + response = response.json() - if 'error' in response: - response = RestError(response) - # TODO: Decide to raise an Exception or respond with the error json + if 'error' in response: + response = RestError(response) + # TODO: Decide to raise an Exception or respond with the error json - if self.use_camel_case == True: - response = humps.camelize(response) + if self.use_camel_case is True: + response = humps.camelize(response) - return response \ No newline at end of file + return response diff --git a/greensms/utils/attr_dict.py b/greensms/utils/attr_dict.py index f553543..4079706 100644 --- a/greensms/utils/attr_dict.py +++ b/greensms/utils/attr_dict.py @@ -1,31 +1,32 @@ class AttrDict(dict): - """ - Example: - m = AttrDict({'first_name': 'Eduardo'}, last_name='Pool', age=24, sports=['Soccer']) - """ - def __init__(self, *args, **kwargs): - super(AttrDict, self).__init__(*args, **kwargs) - for arg in args: - if isinstance(arg, dict): - for k, v in arg.items(): - self[k] = v - if kwargs: - for k, v in kwargs.items(): - self[k] = v + """ + Example: + m = AttrDict({'first_name': 'Eduardo'}, last_name='Pool', age=24, sports=['Soccer']) + """ - def __getattr__(self, attr): - return self.get(attr) + def __init__(self, *args, **kwargs): + super(AttrDict, self).__init__(*args, **kwargs) + for arg in args: + if isinstance(arg, dict): + for k, v in arg.items(): + self[k] = v + if kwargs: + for k, v in kwargs.items(): + self[k] = v - def __setattr__(self, key, value): - self.__setitem__(key, value) + def __getattr__(self, attr): + return self.get(attr) - def __setitem__(self, key, value): - super(AttrDict, self).__setitem__(key, value) - self.__dict__.update({key: value}) + def __setattr__(self, key, value): + self.__setitem__(key, value) - def __delattr__(self, item): - self.__delitem__(item) + def __setitem__(self, key, value): + super(AttrDict, self).__setitem__(key, value) + self.__dict__.update({key: value}) - def __delitem__(self, key): - super(AttrDict, self).__delitem__(key) - del self.__dict__[key] \ No newline at end of file + def __delattr__(self, item): + self.__delitem__(item) + + def __delitem__(self, key): + super(AttrDict, self).__delitem__(key) + del self.__dict__[key] diff --git a/greensms/utils/dict.py b/greensms/utils/dict.py index bc5b2e8..ff8b994 100644 --- a/greensms/utils/dict.py +++ b/greensms/utils/dict.py @@ -13,4 +13,4 @@ def deep_merge(source, destination): else: destination[key] = value - return destination \ No newline at end of file + return destination diff --git a/greensms/utils/url.py b/greensms/utils/url.py index feddc85..ce3a919 100644 --- a/greensms/utils/url.py +++ b/greensms/utils/url.py @@ -1,18 +1,19 @@ try: - from urllib.parse import urlparse, urljoin + from urllib.parse import urljoin except ImportError: - from urlparse import urlparse, urljoin -from functools import reduce + from urlparse import urljoin from greensms.constants import BASE_URL + def base_url(): - return BASE_URL + return BASE_URL + def build_url(base_url, args): - if not base_url: - raise Exception('Base URL cannot be empty!') + if not base_url: + raise Exception('Base URL cannot be empty!') - path = '/'.join(args) + path = '/'.join(args) - url = urljoin(base_url, path) - return url + url = urljoin(base_url, path) + return url diff --git a/greensms/utils/validator.py b/greensms/utils/validator.py index adf5ea8..8b93ef8 100644 --- a/greensms/utils/validator.py +++ b/greensms/utils/validator.py @@ -1,22 +1,18 @@ from greensms.http.error import RestError from cerberus import Validator + def validate(schema, data): - error_result = None - validator = Validator(schema, allow_unknown=True) + error_result = None + validator = Validator(schema, allow_unknown=True) + + if validator.validate(data): + return error_result + else: + error_result = RestError({ + 'code': 1, + 'error': 'Validation Error', + 'params': validator.errors + }) - if validator.validate(data): return error_result - else: - error_result = RestError({ - 'code': 1, - 'error': 'Validation Error', - 'params': validator.errors - }) - - return error_result - - - - - diff --git a/greensms/utils/version.py b/greensms/utils/version.py index a979026..f3a9fb3 100644 --- a/greensms/utils/version.py +++ b/greensms/utils/version.py @@ -1,8 +1,9 @@ from greensms.constants import VERSIONS + def get_version(version): - version = version or VERSIONS['v1'] - version = version.lower() - if version in VERSIONS.keys(): - return VERSIONS[version] - raise Exception('Invalid Version') \ No newline at end of file + version = version or VERSIONS['v1'] + version = version.lower() + if version in VERSIONS.keys(): + return VERSIONS[version] + raise Exception('Invalid Version') diff --git a/setup.py b/setup.py index b2b3c22..f232443 100644 --- a/setup.py +++ b/setup.py @@ -15,34 +15,34 @@ license='MIT', packages=find_packages(exclude=['tests', 'tests*']), install_requires=[ - 'requests', - 'six', - 'twine', - 'cerberus', - 'pyhumps' + 'requests', + 'six', + 'twine', + 'cerberus', + 'pyhumps' ], extras_require={ - ':python_version<"3.0"': [ - "requests[security] >= 2.0.0", - ], - ':python_version>="3.0"': [ - "requests >= 2.0.0" - ], + ':python_version<"3.0"': [ + "requests[security] >= 2.0.0", + ], + ':python_version>="3.0"': [ + "requests >= 2.0.0" + ], }, classifiers=[ - 'Development Status :: 3 - Alpha', - 'Intended Audience :: Developers', - 'License :: OSI Approved :: MIT License', - 'Programming Language :: Python :: 2.7', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7', - 'Programming Language :: Python :: 3.8', - 'Topic :: Software Development :: Libraries :: Python Modules', + 'Development Status :: 3 - Alpha', + 'Intended Audience :: Developers', + 'License :: OSI Approved :: MIT License', + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3.4', + 'Programming Language :: Python :: 3.5', + 'Programming Language :: Python :: 3.6', + 'Programming Language :: Python :: 3.7', + 'Programming Language :: Python :: 3.8', + 'Topic :: Software Development :: Libraries :: Python Modules', ], include_package_data=True, keywords=['greensms', 'sms', 'rest', 'api', 'viber'], long_description=long_description, long_description_content_type='text/markdown' -) \ No newline at end of file + ) diff --git a/tests/account_test.py b/tests/account_test.py index 4116c14..fb1664d 100644 --- a/tests/account_test.py +++ b/tests/account_test.py @@ -3,33 +3,34 @@ from greensms.client import GreenSMS from tests.utils import random_phone + class TestAccountMethods(unittest.TestCase): - def test_balance(self): - response = client.account.balance() - self.assertIn('balance', response) + def test_balance(self): + response = client.account.balance() + self.assertIn('balance', response) - def test_token(self): - response = client.account.token() - self.assertIn('access_token', response) + def test_token(self): + response = client.account.token() + self.assertIn('access_token', response) - def test_tarrif(self): - response = client.account.tariff() - self.assertGreater(len(response), 0) + def test_tarrif(self): + response = client.account.tariff() + self.assertGreater(len(response), 0) - def test_without_credentials(self): - self.assertRaises(Exception, GreenSMS()) + def test_without_credentials(self): + self.assertRaises(Exception, GreenSMS()) - def test_unauthorized_access(self): - test_client = GreenSMS(user='username', password='password') - response = test_client.account.balance() - self.assertEqual(response.error, 'Authorization declined') + def test_unauthorized_access(self): + test_client = GreenSMS(user='username', password='password') + response = test_client.account.balance() + self.assertEqual(response.error, 'Authorization declined') - def test_insufficient_funds(self): - test_client = GreenSMS(user='test_block_user', password='183456') - response = test_client.sms.send(to=random_phone(), txt='Test Message') - self.assertEqual(response.error, 'Insufficient funds') + def test_insufficient_funds(self): + test_client = GreenSMS(user='test_block_user', password='183456') + response = test_client.sms.send(to=random_phone(), txt='Test Message') + self.assertEqual(response.error, 'Insufficient funds') if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main() diff --git a/tests/call_test.py b/tests/call_test.py index c65f4dc..c49b45b 100644 --- a/tests/call_test.py +++ b/tests/call_test.py @@ -2,20 +2,22 @@ from tests.default import client from tests.utils import random_phone + class TestCallMethods(unittest.TestCase): - def test_send(self): - response = client.call.send(to=random_phone()) - keys_set = set(response.keys()) - self.assertTrue({'request_id', 'code'}.issubset(keys_set)) + def test_send(self): + response = client.call.send(to=random_phone()) + keys_set = set(response.keys()) + self.assertTrue({'request_id', 'code'}.issubset(keys_set)) - def test_mandatory_to(self): - self.assertRaises(Exception, client.call.send()) + def test_mandatory_to(self): + self.assertRaises(Exception, client.call.send()) - def test_status(self): - response = client.call.status(id='1fd4ac4d-6e4f-4e32-b6e4-8087d3466f55', extended=True) - self.assertIn('status', response) + def test_status(self): + response = client.call.status( + id='1fd4ac4d-6e4f-4e32-b6e4-8087d3466f55', extended=True) + self.assertIn('status', response) if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main() diff --git a/tests/default.py b/tests/default.py index 57d29a8..9b86054 100644 --- a/tests/default.py +++ b/tests/default.py @@ -4,4 +4,4 @@ os.environ['GREENSMS_USER'] = 'test' os.environ['GREENSMS_PASS'] = 'test' -client = GreenSMS() \ No newline at end of file +client = GreenSMS() diff --git a/tests/general_test.py b/tests/general_test.py index e1661f0..a274962 100644 --- a/tests/general_test.py +++ b/tests/general_test.py @@ -1,18 +1,18 @@ import unittest from tests.default import client -from tests.utils import random_phone + class TestGeneralMethods(unittest.TestCase): - def test_lookup(self): - response = client.whois.lookup(to='79260000000') - keys_set = set(response.keys()) - self.assertTrue({'def', 'region'}.issubset(keys_set)) + def test_lookup(self): + response = client.whois.lookup(to='79260000000') + keys_set = set(response.keys()) + self.assertTrue({'def', 'region'}.issubset(keys_set)) - def test_status(self): - response = client.status() - self.assertIn('status', response) + def test_status(self): + response = client.status() + self.assertIn('status', response) if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main() diff --git a/tests/hlr_test.py b/tests/hlr_test.py index 015210f..a2a947b 100644 --- a/tests/hlr_test.py +++ b/tests/hlr_test.py @@ -2,19 +2,21 @@ from tests.default import client from tests.utils import random_phone + class TestHlrMethods(unittest.TestCase): - def test_send(self): - response = client.hlr.send(to=random_phone(79260000111, 79260999999)) - self.assertIn('request_id', response) + def test_send(self): + response = client.hlr.send(to=random_phone(79260000111, 79260999999)) + self.assertIn('request_id', response) - def test_mandatory_to(self): - self.assertRaises(Exception, client.hlr.send()) + def test_mandatory_to(self): + self.assertRaises(Exception, client.hlr.send()) - def test_status(self): - response = client.hlr.status(id='70d296f5-ac52-403d-a27b-24829c2faebc', to=random_phone()) - self.assertIn('status', response) + def test_status(self): + response = client.hlr.status( + id='70d296f5-ac52-403d-a27b-24829c2faebc', to=random_phone()) + self.assertIn('status', response) if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main() diff --git a/tests/pay_test.py b/tests/pay_test.py index 9731c09..b1c72ce 100644 --- a/tests/pay_test.py +++ b/tests/pay_test.py @@ -2,19 +2,20 @@ from tests.default import client from tests.utils import random_phone + class TestPayMethods(unittest.TestCase): - def test_send(self): - response = client.pay.send(to=random_phone(), amount=10, tag='PyTest') - self.assertIn('request_id', response) + def test_send(self): + response = client.pay.send(to=random_phone(), amount=10, tag='PyTest') + self.assertIn('request_id', response) - def test_mandatory_to(self): - self.assertRaises(Exception, client.pay.send()) + def test_mandatory_to(self): + self.assertRaises(Exception, client.pay.send()) - def test_status(self): - response = client.pay.status(id='60f231d9-16ec-4313-842e-6e6853063482') - self.assertIn('status', response) + def test_status(self): + response = client.pay.status(id='60f231d9-16ec-4313-842e-6e6853063482') + self.assertIn('status', response) if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main() diff --git a/tests/sms_test.py b/tests/sms_test.py index 7773294..c7d73fa 100644 --- a/tests/sms_test.py +++ b/tests/sms_test.py @@ -2,27 +2,28 @@ from tests.default import client from tests.utils import random_phone + class TestSmsMethods(unittest.TestCase): - def test_send(self): - dict_params = { - 'to': random_phone(), - 'txt': 'Test Message Hampshire', - 'from': 'PyTest', - 'tag': 'PyTest', - 'hidden': 'Hampshire' - } + def test_send(self): + dict_params = { + 'to': random_phone(), + 'txt': 'Test Message Hampshire', + 'from': 'PyTest', + 'tag': 'PyTest', + 'hidden': 'Hampshire' + } - response = client.sms.send(**dict_params) - self.assertIn('request_id', response) + response = client.sms.send(**dict_params) + self.assertIn('request_id', response) - def test_mandatory_to(self): - self.assertRaises(Exception, client.sms.send()) + def test_mandatory_to(self): + self.assertRaises(Exception, client.sms.send()) - def test_status(self): - response = client.sms.status(id='dc2bac6d-f375-4e19-9a02-ef0148991635') - self.assertIn('status', response) + def test_status(self): + response = client.sms.status(id='dc2bac6d-f375-4e19-9a02-ef0148991635') + self.assertIn('status', response) if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main() diff --git a/tests/token_test.py b/tests/token_test.py index 46632d0..2db51e6 100644 --- a/tests/token_test.py +++ b/tests/token_test.py @@ -2,29 +2,27 @@ import time from tests.default import client from greensms.client import GreenSMS -from tests.utils import random_phone +token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoibWFuYW4yNCIsImlhdCI6MTYwMzU2OTgzMCwiaXNzIjoiYXBpLmdyZWVuc21zLnJ1In0.OKiv5itdirS_PuPJj5kgGcR2_9DsC9ALW9c8FvvHSF4' # noqa: E501 -token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoibWFuYW4yNCIsImlhdCI6MTYwMzU2OTgzMCwiaXNzIjoiYXBpLmdyZWVuc21zLnJ1In0.OKiv5itdirS_PuPJj5kgGcR2_9DsC9ALW9c8FvvHSF4' class TestTokenMethods(unittest.TestCase): - def test_balance(self): - token_client = GreenSMS(token=token) - response = token_client.account.balance() - self.assertIn('balance', response) + def test_balance(self): + token_client = GreenSMS(token=token) + response = token_client.account.balance() + self.assertIn('balance', response) + def test_without_token(self): + self.assertRaises(Exception, GreenSMS()) - def test_without_token(self): - self.assertRaises(Exception, GreenSMS()) - - def test_token_expiry(self): - token_response = client.account.token(expire=5) - invalid_token_client = GreenSMS(token=token_response['access_token']) - time.sleep(5) - response = invalid_token_client.account.balance() - self.assertEqual(response.error, 'Authorization declined') + def test_token_expiry(self): + token_response = client.account.token(expire=5) + invalid_token_client = GreenSMS(token=token_response['access_token']) + time.sleep(5) + response = invalid_token_client.account.balance() + self.assertEqual(response.error, 'Authorization declined') if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main() diff --git a/tests/utils.py b/tests/utils.py index 0240cc2..9a86614 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,7 +1,9 @@ import random + def random_number(min, max): - return random.randint(min, max) + return random.randint(min, max) + def random_phone(min_range=70000000111, max_range=70009999999): - return str(random_number(min_range, max_range)) + return str(random_number(min_range, max_range)) diff --git a/tests/viber_test.py b/tests/viber_test.py index abf8fdd..fdf04cd 100644 --- a/tests/viber_test.py +++ b/tests/viber_test.py @@ -2,25 +2,27 @@ from tests.default import client from tests.utils import random_phone + class TestViberMethods(unittest.TestCase): - def test_send(self): - dict_params = { - 'to': random_phone(), - 'txt': 'Test Message Hampshire', - 'from': 'PyTest', - 'cascade': 'sms' - } - response = client.viber.send(**dict_params) - self.assertIn('request_id', response) + def test_send(self): + dict_params = { + 'to': random_phone(), + 'txt': 'Test Message Hampshire', + 'from': 'PyTest', + 'cascade': 'sms' + } + response = client.viber.send(**dict_params) + self.assertIn('request_id', response) - def test_mandatory_to(self): - self.assertRaises(Exception, client.viber.send()) + def test_mandatory_to(self): + self.assertRaises(Exception, client.viber.send()) - def test_status(self): - response = client.viber.status(id='0b18fab4-0c5d-4a8b-8ee4-057a59596c7d', extended=True) - self.assertIn('status', response) + def test_status(self): + response = client.viber.status( + id='0b18fab4-0c5d-4a8b-8ee4-057a59596c7d', extended=True) + self.assertIn('status', response) if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main() diff --git a/tests/voice_test.py b/tests/voice_test.py index 5bf8359..84240c1 100644 --- a/tests/voice_test.py +++ b/tests/voice_test.py @@ -2,19 +2,21 @@ from tests.default import client from tests.utils import random_phone + class TestVoiceMethods(unittest.TestCase): - def test_send(self): - response = client.voice.send(to=random_phone(), txt='1200', lang='en') - self.assertIn('request_id', response) + def test_send(self): + response = client.voice.send(to=random_phone(), txt='1200', lang='en') + self.assertIn('request_id', response) - def test_mandatory_to(self): - self.assertRaises(Exception, client.voice.send()) + def test_mandatory_to(self): + self.assertRaises(Exception, client.voice.send()) - def test_status(self): - response = client.voice.status(id='41f23094-deda-4cab-ac9c-3ab4f2fee9e6', extended=True) - self.assertIn('status', response) + def test_status(self): + response = client.voice.status( + id='41f23094-deda-4cab-ac9c-3ab4f2fee9e6', extended=True) + self.assertIn('status', response) if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main() From 523c16a0ec434dcca542730a3e051dd38b8cf10b Mon Sep 17 00:00:00 2001 From: Manan Jadhav Date: Tue, 10 Nov 2020 11:35:28 +0530 Subject: [PATCH 2/4] flake8 lint fixes for module_loader --- greensms/api/module_loader.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/greensms/api/module_loader.py b/greensms/api/module_loader.py index 72ecd40..65de2e1 100644 --- a/greensms/api/module_loader.py +++ b/greensms/api/module_loader.py @@ -13,7 +13,7 @@ class ModuleLoader: def __init__(self): self.module_map = AttrDict({}) - def register_modules(self, shared_options, filters={}): + def register_modules(self, shared_options, filters={}): # noqa: C901 if not filters: filters = {} @@ -28,7 +28,7 @@ def register_modules(self, shared_options, filters={}): module_versions = module_info['versions'] module_schema = None - if 'load_static' in filters and 'static' in module_info and filters['load_static'] == True and module_info['static'] == True: + if 'load_static' in filters and 'static' in module_info and filters['load_static'] is True and module_info['static'] is True: continue for version, version_functions in module_versions.items(): @@ -46,7 +46,7 @@ def register_modules(self, shared_options, filters={}): module_schema = module_info['schema'][version][function_name] url_args = [] - if 'static' not in module_info or module_info['static'] == False: + if 'static' not in module_info or module_info['static'] is False: url_args.append(module_name) url_args.append(function_name) @@ -59,7 +59,7 @@ def register_modules(self, shared_options, filters={}): if version == current_version: self.module_map[module_name][function_name] = self.module_map[module_name][version][function_name] - if 'static' in module_info and module_info['static'] == True: + if 'static' in module_info and module_info['static'] is True: self.module_map[function_name] = self.module_map[module_name][version][function_name] del self.module_map[module_name] From cbc87c20ca540b1df20d44d9d4837dc9e9042147 Mon Sep 17 00:00:00 2001 From: Manan Jadhav Date: Tue, 10 Nov 2020 11:43:42 +0530 Subject: [PATCH 3/4] Split lines for max length --- greensms/api/module_loader.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/greensms/api/module_loader.py b/greensms/api/module_loader.py index 65de2e1..02ab54e 100644 --- a/greensms/api/module_loader.py +++ b/greensms/api/module_loader.py @@ -28,7 +28,14 @@ def register_modules(self, shared_options, filters={}): # noqa: C901 module_versions = module_info['versions'] module_schema = None - if 'load_static' in filters and 'static' in module_info and filters['load_static'] is True and module_info['static'] is True: + is_static_module = ( + 'load_static' in filters + and 'static' in module_info + and filters['load_static'] is True + and module_info['static'] is True + ) + + if is_static_module: continue for version, version_functions in module_versions.items(): @@ -42,7 +49,12 @@ def register_modules(self, shared_options, filters={}): # noqa: C901 }) module_schema = None - if 'schema' in module_info and version in module_info['schema'] and function_name in module_info['schema'][version]: + scheme_exists = ( + 'schema' in module_info + and version in module_info['schema'] + and function_name in module_info['schema'][version] + ) + if scheme_exists: module_schema = module_info['schema'][version][function_name] url_args = [] From 1c51271d4afda745b27a69a6a6bc651a44a3e39c Mon Sep 17 00:00:00 2001 From: Manan Jadhav Date: Tue, 10 Nov 2020 11:47:50 +0530 Subject: [PATCH 4/4] Type fixes --- greensms/client.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/greensms/client.py b/greensms/client.py index f9145ae..c0f638b 100644 --- a/greensms/client.py +++ b/greensms/client.py @@ -3,7 +3,6 @@ from greensms.utils.url import base_url from greensms.http.rest import HttpClient from greensms.api.module_loader import ModuleLoader -import types class GreenSMS(object): @@ -31,9 +30,9 @@ def __init__(self, user=None, password=None, token=None, version=None, camel_cas self.password = password or environment.get('GREENSMS_PASS') self.version = version self.use_camel_case = camel_case_response if isinstance( - camel_case_response, types.BooleanType) else False + camel_case_response, bool) else False self.use_token_for_requests = use_token_for_requests if isinstance( - use_token_for_requests, types.BooleanType) else False + use_token_for_requests, bool) else False if self.token is not None: self.user = None