Skip to content

Commit

Permalink
Merge pull request #11 from greensms-ru/first-draft
Browse files Browse the repository at this point in the history
Updated Validations, Social API and Attribute handling for Response
  • Loading branch information
mananjadhav authored Nov 12, 2020
2 parents ae0b2ba + 0432dca commit 178acd2
Show file tree
Hide file tree
Showing 16 changed files with 149 additions and 26 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ from greensms.client import GreenSMS
# Register at my.greeensms.ru first
client = GreenSMS(user='test', password='test')

client.sms.send(to='71231234567', txt='Message to deliver')
print(response.request_id)
response = client.sms.send(to='71231234567', txt='Message to deliver')
print(response.request_id) # or print(response['request_id'])

```

Expand All @@ -45,7 +45,7 @@ from greensms.client import GreenSMS
# Register at my.greeensms.ru first
client = GreenSMS(token='yourtoken')

client.account.balance()
response = client.account.balance()
print(response.balance)

```
Expand All @@ -60,6 +60,7 @@ print(response.balance)
- All methods support named \*\*kwargs
- Each API Function is available as `MODULE.FUNCTION()`
- Parameters for each API can be referred from [here][apidocs]
- Response keys can be used as dictionary keys `response['key']` or properties `response.key`
- Response keys by default are available in `snake_case`. If you want to use `camelCase`, then pass `use_camel_case=true`, in the constructor

## Handling Exceptions
Expand Down
25 changes: 25 additions & 0 deletions examples/social.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from default import client


def social_send():
dict_params = {
'to': '71231234567',
'txt': 'Test Message Hampshire',
'from': 'PyTest',
'tag': 'PyTest',
'hidden': 'Hampshire'
}

res = client.social.send(**dict_params)
print(res)


def social_status():
res = client.social.status(id='caf3efb1-8aca-4387-9ed0-e667d315c5c9')
print(res)


if __name__ == '__main__':
social_send()

social_status()
14 changes: 7 additions & 7 deletions greensms/api/module_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ def register_modules(self, shared_options, filters={}): # noqa: C901
module_schema = None

is_static_module = (
'load_static' in filters
and 'static' in module_info
and filters['load_static'] is True
and module_info['static'] is True
'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:
Expand All @@ -50,9 +50,9 @@ def register_modules(self, shared_options, filters={}): # noqa: C901

module_schema = None
scheme_exists = (
'schema' in module_info
and version in module_info['schema']
and function_name in module_info['schema'][version]
'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]
Expand Down
15 changes: 15 additions & 0 deletions greensms/api/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,19 @@
}
}
},
'social': {
'schema': VALIDATION_SCHEMA['social'],
'versions': {
'v1': {
'send': {
'args': ['params'],
'method': 'POST',
},
'status': {
'args': ['params'],
'method': 'GET',
},
}
}
},
}
21 changes: 21 additions & 0 deletions greensms/api/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,25 @@
}
}
}),
'social': deep_merge(common_schema,
{
'v1': {
'send': {
'txt': {
'type': 'string',
'minlength': 1,
'required': True
},
'from': {
'type': 'string',
},
'tag': {
'type': 'string',
},
'hidden': {
'type': 'string',
}
}
}
}),
}
5 changes: 4 additions & 1 deletion greensms/http/rest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import requests
import humps
from greensms.http.error import RestError
from greensms.utils.attr_dict import AttrDict


class HttpClient:
Expand Down Expand Up @@ -62,8 +63,10 @@ def request(self, **kwargs): # noqa: C901
response = response.json()

if 'error' in response:
response = RestError(response)
raise RestError(response)
# TODO: Decide to raise an Exception or respond with the error json
else:
response = AttrDict(response)

if self.use_camel_case is True:
response = humps.camelize(response)
Expand Down
2 changes: 1 addition & 1 deletion greensms/utils/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def validate(schema, data):
if validator.validate(data):
return error_result
else:
error_result = RestError({
raise RestError({
'code': 1,
'error': 'Validation Error',
'params': validator.errors
Expand Down
17 changes: 11 additions & 6 deletions tests/account_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@ 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')
try:
test_client = GreenSMS(user='username', password='password')
test_client.account.balance()
except Exception as e:
self.assertEqual(e.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')
try:
test_client = GreenSMS(user='test_block_user', password='183456')
test_client.sms.send(
to=random_phone(), txt='Test Message')
except Exception as e:
self.assertEqual(e.error, 'Insufficient funds')


if __name__ == '__main__':
Expand Down
5 changes: 4 additions & 1 deletion tests/call_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ def test_send(self):
self.assertTrue({'request_id', 'code'}.issubset(keys_set))

def test_mandatory_to(self):
self.assertRaises(Exception, client.call.send())
try:
client.call.send()
except Exception as e:
self.assertEqual(e.error, 'Validation Error')

def test_status(self):
response = client.call.status(
Expand Down
5 changes: 4 additions & 1 deletion tests/hlr_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ def test_send(self):
self.assertIn('request_id', response)

def test_mandatory_to(self):
self.assertRaises(Exception, client.hlr.send())
try:
client.hlr.send()
except Exception as e:
self.assertEqual(e.error, 'Validation Error')

def test_status(self):
response = client.hlr.status(
Expand Down
5 changes: 4 additions & 1 deletion tests/pay_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ def test_send(self):
self.assertIn('request_id', response)

def test_mandatory_to(self):
self.assertRaises(Exception, client.pay.send())
try:
client.pay.send()
except Exception as e:
self.assertEqual(e.error, 'Validation Error')

def test_status(self):
response = client.pay.status(id='60f231d9-16ec-4313-842e-6e6853063482')
Expand Down
5 changes: 4 additions & 1 deletion tests/sms_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ def test_send(self):
self.assertIn('request_id', response)

def test_mandatory_to(self):
self.assertRaises(Exception, client.sms.send())
try:
client.sms.send()
except Exception as e:
self.assertEqual(e.error, 'Validation Error')

def test_status(self):
response = client.sms.status(id='dc2bac6d-f375-4e19-9a02-ef0148991635')
Expand Down
33 changes: 33 additions & 0 deletions tests/social_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import unittest
from tests.default import client
from tests.utils import random_phone


class TestSocialMethods(unittest.TestCase):

def test_send(self):
dict_params = {
'to': random_phone(),
'txt': 'Test Message Hampshire',
'from': 'PyTest',
'tag': 'PyTest',
'hidden': 'Hampshire'
}

response = client.social.send(**dict_params)
self.assertIn('request_id', response)

def test_mandatory_to(self):
try:
client.social.send()
except Exception as e:
self.assertEqual(e.error, 'Validation Error')

def test_status(self):
response = client.social.status(
id='caf3efb1-8aca-4387-9ed0-e667d315c5c9')
self.assertIn('status', response)


if __name__ == '__main__':
unittest.main()
6 changes: 4 additions & 2 deletions tests/token_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ 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')
try:
invalid_token_client.account.balance()
except Exception as e:
self.assertEqual(e.error, 'Authorization declined')


if __name__ == '__main__':
Expand Down
5 changes: 4 additions & 1 deletion tests/viber_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ def test_send(self):
self.assertIn('request_id', response)

def test_mandatory_to(self):
self.assertRaises(Exception, client.viber.send())
try:
client.viber.send()
except Exception as e:
self.assertEqual(e.error, 'Validation Error')

def test_status(self):
response = client.viber.status(
Expand Down
5 changes: 4 additions & 1 deletion tests/voice_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ def test_send(self):
self.assertIn('request_id', response)

def test_mandatory_to(self):
self.assertRaises(Exception, client.voice.send())
try:
client.voice.send()
except Exception as e:
self.assertEqual(e.error, 'Validation Error')

def test_status(self):
response = client.voice.status(
Expand Down

0 comments on commit 178acd2

Please sign in to comment.