Пример #1
0
 def can_send():
     # send status api method wrapper, doesn't need authentication
     request_handler = MobilyApiJsonRequestHandler()
     request_handler.set_api_method('sendStatus')
     try:
         response = request_handler.handle()
     except MobilyApiError:
         return False
     else:
         return response.get('result') == '1'
Пример #2
0
 def __init__(self, auth, numbers=None, sender='', msg='', delete_key=None, msg_id=None,
              domain_name=None, application_type='24'):
     self.numbers = numbers
     if numbers is None:
         self.numbers = []
     self.auth = auth
     self.sender = sender
     self.msg = msg
     self.date_send = 0
     self.time_send = 0
     self.delete_key = delete_key
     self.msg_id = msg_id
     self.domain_name = domain_name
     self.application_type = application_type
     self.api_method_name = 'msgSend'
     self.request_handler = MobilyApiJsonRequestHandler(self.auth)
Пример #3
0
 def forgot_password(self, send_to_email=True):
     # forgotPassword api method wrapper
     request_handler = MobilyApiJsonRequestHandler(self.auth)
     request_type = 2 if send_to_email else 1
     request_handler.set_api_method('forgetPassword')
     request_handler.add_parameter('type', request_type)
     return request_handler.handle()
Пример #4
0
 def delete(self):
     # send sms api method wrapper
     if self.delete_key is None:
         return
     request_handler = MobilyApiJsonRequestHandler(self.auth)
     request_handler.set_api_method('deleteMsg')
     request_handler.add_parameter('deleteKey', self.delete_key)
     return request_handler.handle()
 def test_json_parsing_success_response(self):
     fake_response = '''{
     "status":1,
     "ResponseStatus":"success",
     "Data":{
     "result":"1",
     "MessageAr":"يمكنك الإرسال الآن",
     "MessageEn":"You can send SMS now"
     },
     "Error":null
     }
     '''
     expected_response = MobilyApiResponse(1, 'success')
     expected_response.add_data('result', '1')
     expected_response.add_data('MessageAr', 'يمكنك الإرسال الآن')
     expected_response.add_data('MessageEn', 'You can send SMS now')
     handler = MobilyApiJsonRequestHandler(
         request=self.MobilyApiRequestStub(fake_response))
     self.assertEqual(expected_response, handler.handle())
Пример #6
0
 def is_mobile_number_license_active(self, sender_id):
     request_handler = MobilyApiJsonRequestHandler(self.auth)
     request_handler.set_api_method('checkSender')
     request_handler.add_parameter('senderId', sender_id.strip('#'))
     try:
         response = request_handler.handle()
     except MobilyApiError:
         return False
     else:
         return response.get('result') == '1'
 def test_json_parsing_throws_on_failure_response(self):
     fake_response = '''{
     "status":1,
     "ResponseStatus":"fail",
     "Data":null,
     "Error":{
     "ErrorCode":0,
     "MessageAr":"بوابة غير معرفة لدينا",
     "MessageEn":"API not exist"
     }
     }'''
     with self.assertRaises(MobilyApiError):
         MobilyApiJsonRequestHandler(
             request=self.MobilyApiRequestStub(fake_response)).handle()
 def test_json_building(self):
     expected_json = '{"Data": {"Method": "balance", "Auth": {"mobile": "test", "password": "******"}}}'
     request = MobilyApiJsonRequestHandler(MobilyApiAuth('test', 'test'))
     request.set_api_method('balance')
     self.assertEqual(expected_json, request.get_request_data())
Пример #9
0
 def get_activation_status_for_all_senders(self):
     request_handler = MobilyApiJsonRequestHandler(self.auth)
     request_handler.set_api_method('checkAlphasSender')
     return request_handler.handle().data
Пример #10
0
 def request_alphabetical_license(self, sender):
     request_handler = MobilyApiJsonRequestHandler(self.auth)
     request_handler.set_api_method('addAlphaSender')
     request_handler.add_parameter('sender', sender)
     return request_handler.handle()
Пример #11
0
 def activate_mobile_number_license(self, sender_id, activation_code):
     request_handler = MobilyApiJsonRequestHandler(self.auth)
     request_handler.set_api_method('activeSender')
     request_handler.add_parameter('senderId', sender_id.strip('#'))
     request_handler.add_parameter('activeKey', activation_code)
     return request_handler.handle()
Пример #12
0
 def request_mobile_number_license(self, mobile_number):
     request_handler = MobilyApiJsonRequestHandler(self.auth)
     request_handler.set_api_method('addSender')
     request_handler.add_parameter('sender', mobile_number)
     response = request_handler.handle()
     return response.get('senderId')
Пример #13
0
class MobilySMS(object):
    def __init__(self, auth, numbers=None, sender='', msg='', delete_key=None, msg_id=None,
                 domain_name=None, application_type='24'):
        self.numbers = numbers
        if numbers is None:
            self.numbers = []
        self.auth = auth
        self.sender = sender
        self.msg = msg
        self.date_send = 0
        self.time_send = 0
        self.delete_key = delete_key
        self.msg_id = msg_id
        self.domain_name = domain_name
        self.application_type = application_type
        self.api_method_name = 'msgSend'
        self.request_handler = MobilyApiJsonRequestHandler(self.auth)

    def add_number(self, number):
        self.numbers.append(number)

    def get_numbers_as_csv(self):
        return ','.join(self.numbers)

    @staticmethod
    def can_send():
        # send status api method wrapper, doesn't need authentication
        request_handler = MobilyApiJsonRequestHandler()
        request_handler.set_api_method('sendStatus')
        try:
            response = request_handler.handle()
        except MobilyApiError:
            return False
        else:
            return response.get('result') == '1'

    def send(self):
        # send sms api method wrapper
        self._prepare_to_send()
        return self.request_handler.handle()

    def delete(self):
        # send sms api method wrapper
        if self.delete_key is None:
            return
        request_handler = MobilyApiJsonRequestHandler(self.auth)
        request_handler.set_api_method('deleteMsg')
        request_handler.add_parameter('deleteKey', self.delete_key)
        return request_handler.handle()

    def schedule_to_send_on(self, day, month, year, hour=0, minute=0, sec=0):
        self.time_send = '{:02d}:{:02d}:{:02d}'.format(hour, minute, sec)
        self.date_send = '{:02d}/{:02d}/{:04d}'.format(month, day, year)

    def _prepare_to_send(self):
        self.request_handler.set_api_method(self.api_method_name)
        self.request_handler.add_parameter('sender', self.sender)
        self.request_handler.add_parameter('msg', MobilyApiUnicodeConverter.convert(self.msg))
        self.request_handler.add_parameter('numbers', self.get_numbers_as_csv())
        self.request_handler.add_parameter('dateSend', self.date_send)
        self.request_handler.add_parameter('timeSend', self.time_send)
        self.request_handler.add_parameter('deleteKey', self.delete_key)
        self.request_handler.add_parameter('msgId', self.msg_id)
        self.request_handler.add_parameter('applicationType', self.application_type)
        self.request_handler.add_parameter('domainName', self.domain_name)
Пример #14
0
 def check_balance(self):
     # balance api method wrapper
     request_handler = MobilyApiJsonRequestHandler(self.auth)
     request_handler.set_api_method('balance')
     return request_handler.handle().get('balance')
Пример #15
0
 def change_password(self, new_password):
     # changePassword api method wrapper
     request_handler = MobilyApiJsonRequestHandler(self.auth)
     request_handler.set_api_method('changePassword')
     request_handler.add_parameter('newPassword', new_password)
     return request_handler.handle()