def send_logs_to_fb(zipped_logs_file_handle, success_callback=None, failure_callback=None): try: run_id = Analytics.send_logs_to_fb(zipped_logs_file_handle) if success_callback: return success_callback(run_id) except Exception as e: if failure_callback: return failure_callback(e)
def send_results_to_fb(result, success_callback=None, failure_callback=None): try: event = Events.RUN_ACTIONS_AND_SEND_RESULTS data = json.dumps(result) run_id = Analytics.send_event(event, data) result['run_id'] = run_id if success_callback: success_callback(result) except Exception as e: if failure_callback: failure_callback(e)
def test_send_event(mocker): mocked_call = mocker.patch('requests.post', side_effect=mocked_requests_post) expected_post_data = { 'access_token': Analytics.CLIENT_TOKEN, 'event_type': Events.RUN_ACTIONS_AND_SEND_RESULTS, 'event_data': "test_payload", 'phone_number': None, 'version': Analytics.VERSION, } run_id = Analytics.send_event(Events.RUN_ACTIONS_AND_SEND_RESULTS, "test_payload") assert mocked_call.call_count == 1 mocked_call.assert_called_with(url=Analytics.API_ENDPOINT, data=expected_post_data, timeout=Analytics.TIMEOUT, files=None) assert run_id is not None
def test_send_event(self, mock_request_post): expected_post_data = { "access_token": Analytics.CLIENT_TOKEN, "event_type": Events.RUN_ACTIONS_AND_SEND_RESULTS, "event_data": "test_payload", "phone_number": None, "version": Analytics.VERSION, } run_id = Analytics.send_event(Events.RUN_ACTIONS_AND_SEND_RESULTS, "test_payload") assert mock_request_post.call_count == 1 mock_request_post.assert_called_with( url=Analytics.API_ENDPOINT, data=expected_post_data, timeout=Analytics.TIMEOUT, files=None, ) assert run_id is not None
def send_logs_to_fb( zipped_logs_file_handle, success_callback=None, failure_callback=None ): phone_number = None try: config = Config().values if config: api = WABizAPI(**config.get("webapp")) phone_number = api.get_phone_number() except Exception: pass try: run_id = Analytics.send_logs_to_fb(zipped_logs_file_handle, phone_number) if success_callback: return success_callback(run_id) except Exception as e: if failure_callback: return failure_callback(e)
def test_send_logs_to_fb(mocker): mocked_call = mocker.patch('requests.post', side_effect=mocked_requests_post) dummy_log_file = BytesIO(b'not important') expected_post_data = { 'access_token': Analytics.CLIENT_TOKEN, 'event_type': Events.SEND_LOGS, 'event_data': "none", 'phone_number': None, 'version': Analytics.VERSION, } expected_files_param = { 'logs_archive': ('wadebug_logs.zip', dummy_log_file, 'application/zip') } run_id = Analytics.send_logs_to_fb(dummy_log_file) assert mocked_call.call_count == 1 mocked_call.assert_called_with(url=Analytics.API_ENDPOINT, data=expected_post_data, timeout=Analytics.TIMEOUT, files=expected_files_param) assert run_id is not None
def send_results_to_fb(result, success_callback=None, failure_callback=None): phone_number = None try: config = Config().values if config: api = WABizAPI(**config.get("webapp")) phone_number = api.get_phone_number() except Exception: pass try: event = Events.RUN_ACTIONS_AND_SEND_RESULTS data = json.dumps(result) run_id = Analytics.send_event(event, data, phone_number) result["run_id"] = run_id if success_callback: success_callback(result) except Exception as e: if failure_callback: failure_callback(e)
def test_send_logs_to_fb(self, mock_request_post): dummy_log_file = BytesIO(b"not important") expected_post_data = { "access_token": Analytics.CLIENT_TOKEN, "event_type": Events.SEND_LOGS, "event_data": "none", "phone_number": None, "version": Analytics.VERSION, } expected_files_param = { "logs_archive": ("wadebug_logs.zip", dummy_log_file, "application/zip") } run_id = Analytics.send_logs_to_fb(dummy_log_file) assert mock_request_post.call_count == 1 mock_request_post.assert_called_with( url=Analytics.API_ENDPOINT, data=expected_post_data, timeout=Analytics.TIMEOUT, files=expected_files_param, ) assert run_id is not None