Skip to content
This repository has been archived by the owner on Dec 19, 2020. It is now read-only.

Commit

Permalink
add methods status, balance, checktarif, senders, sign
Browse files Browse the repository at this point in the history
  • Loading branch information
sheregeda committed May 6, 2016
1 parent ce001b4 commit 7625f5e
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 7 deletions.
29 changes: 22 additions & 7 deletions smsaero/api.py
Expand Up @@ -18,10 +18,10 @@ class SmsAero(object):
URL_GATE = 'http://gate.smsaero.ru/'
SIGNATURE = 'NEWS'

def __init__(self, user, passwd, url_gate=URL_GATE, sign=SIGNATURE):
def __init__(self, user, passwd, url_gate=URL_GATE, signature=SIGNATURE):
self.user = user
self.url_gate = url_gate
self.sign = sign
self.signature = signature
self.session = requests.session()

m = hashlib.md5()
Expand All @@ -32,7 +32,7 @@ def _request(self, selector, data):
data.update({
'user': self.user,
'password': self.passwd,
'from': self.sign,
'answer': 'json',
})
url = urljoin(self.url_gate, selector)

Expand All @@ -49,9 +49,9 @@ def _request(self, selector, data):
def _check_response(self, content):
try:
response = json.loads(content)
if response['result'] == u'reject':
if 'result' in response and response['result'] == u'reject':
raise SmsAeroError(response['reason'])
elif response['result'] == u'no credits':
elif 'result' in response and response['result'] == u'no credits':
raise SmsAeroError(response['result'])
return response
except ValueError:
Expand All @@ -61,13 +61,13 @@ def _check_response(self, content):
else:
raise SmsAeroError('unexpected format is received')

def send(self, to, text, date=None, digital=0, type_send=2, answer='json'):
def send(self, to, text, date=None, digital=0, type_send=2):
data = {
'from': self.signature,
'digital': digital,
'type_send': type_send,
'to': to,
'text': text,
'answer': answer,
}

if date is not None:
Expand All @@ -77,3 +77,18 @@ def send(self, to, text, date=None, digital=0, type_send=2, answer='json'):
raise SmsAeroError('param `date` is not datetime object')

return self._request('/send/', data)

def status(self, id):
return self._request('/status/', {'id': id})

def balance(self):
return self._request('/balance/', {})

def checktarif(self):
return self._request('/checktarif/', {})

def senders(self):
return self._request('/senders/', {})

def sign(self, sign):
return self._request('/sign/', {'sign': sign})
69 changes: 69 additions & 0 deletions tests/test_api.py
Expand Up @@ -118,6 +118,75 @@ def test_send(self):
except SmsAeroError:
pass

@httpretty.activate
def test_status(self):
httpretty.register_uri(
httpretty.POST,
urljoin(SmsAero.URL_GATE, '/status/'),
body='{"reason": "empty field", "result": "reject"}',
status=200,
content_type='text/json',
)

try:
self.api.status(0)
self.assertTrue(False)
except SmsAeroError:
pass

@httpretty.activate
def test_balance(self):
httpretty.register_uri(
httpretty.POST,
urljoin(SmsAero.URL_GATE, '/balance/'),
body='{"balance": "48.20"}',
status=200,
content_type='text/json',
)

response = self.api.balance()
self.assertEqual(response['balance'], u'48.20')

@httpretty.activate
def test_checktarif(self):
httpretty.register_uri(
httpretty.POST,
urljoin(SmsAero.URL_GATE, '/checktarif/'),
body='{"reason": {"Digital channel": "0.45", \
"Direct channel": "1.80"}, "result": "accepted"}',
status=200,
content_type='text/json',
)

response = self.api.checktarif()
self.assertEqual(response['reason']['Digital channel'], u'0.45')

@httpretty.activate
def test_sign(self):
httpretty.register_uri(
httpretty.POST,
urljoin(SmsAero.URL_GATE, '/sign/'),
body='{"accepted": "pending"}',
status=200,
content_type='text/json',
)

response = self.api.sign('awesome')
self.assertEqual(response['accepted'], u'pending')

@httpretty.activate
def test_senders(self):
httpretty.register_uri(
httpretty.POST,
urljoin(SmsAero.URL_GATE, '/senders/'),
body='["NEWS", "awesome"]',
status=200,
content_type='text/json',
)

response = self.api.senders()
self.assertEqual(response, [u'NEWS', u'awesome'])


if __name__ == '__main__':
unittest.main()

0 comments on commit 7625f5e

Please sign in to comment.