def send_data(text, full_info, subject="", network_send=True): ''' Sends the data to our servers through a post request ''' files = {} # packs all the information into 'files' if full_info: files['report'] = get_metadata_archive() # This is the actual info: subject, text, email, username payload = { "text": text, "email": get_email(), "username": get_mixed_username(), "category": "os", "subject": subject } if not network_send: return True, None # send the bug report and remove all the created files success, error, data = request_wrapper('post', '/feedback', data=payload, files=files) delete_tmp_dir() if not success: return False, error if full_info: # kano-profile stat collection increment_app_state_variable_with_dialog('kano-feedback', 'bugs_submitted', 1) # logs were sent, clean up logging.cleanup() return True, None
def _report(self): success, value = login_using_token() if success: endpoint = '/apps/{}/installed'.format(self._app['id']) gs = get_glob_session() if gs: success, text, data = request_wrapper('post', endpoint, session=gs.session)
def _report_share_opened(item_id): success, value = login_using_token() if success: endpoint = '/share/{}/installed'.format(item_id) gs = get_glob_session() if gs: success, text, data = request_wrapper('post', endpoint, session=gs.session)
def send_data(text, full_info, subject='', network_send=True, logs_path=''): """Sends the data to our servers through a post request. It uses :func:`~get_metadata_archive` to gather all the logs on the system. Args: text (str): The description of the email when sending the logs full_info (bool): Whether to attach all logs to the payload subject (str): The title of the email when sending the logs network_send (bool): Whether to send the data to our servers logs_path (str): Path to an existing logs archive to use instead Returns: bool, error: Whether the operation was successful or there was an error as returned by :func:`kano_world.functions.request_wrapper` """ from kano_world.functions import get_email, get_mixed_username files = {} # packs all the information into 'files' if full_info: if logs_path and os.path.exists(logs_path): files['report'] = open(logs_path, 'rb') else: files['report'] = get_metadata_archive(title=subject, desc=text) # This is the actual info: subject, text, email, username payload = { "text": text, "email": get_email(), "username": get_mixed_username(), "category": "os", "subject": subject } if not network_send: return True, None # send the bug report and remove all the created files success, error, data = request_wrapper('post', '/feedback', data=payload, files=files) delete_tmp_dir() if not success: return False, error if full_info: # kano-profile stat collection from kano_profile.badges import increment_app_state_variable_with_dialog increment_app_state_variable_with_dialog('kano-feedback', 'bugs_submitted', 1) # logs were sent, clean up logging.cleanup() return True, None
def query_for_app(app_id_or_slug): endpoint = '/apps/{}'.format(app_id_or_slug) success, text, data = request_wrapper( 'get', endpoint, headers=content_type_json ) if not success: endpoint = '/apps/slug/{}'.format(app_id_or_slug) success, text, data = request_wrapper( 'get', endpoint, headers=content_type_json ) if not success: raise AppDownloadError(text) return data['app']
def _ping(): """ The ping event is unauthenticated and is dispatched right away, without passing through the caching layer in the `events` file. """ event = json.dumps(get_action_event('ping')) status, err, _ = request_wrapper('post', '/tracking/ping', data=event, headers=content_type_json) if not status: msg = "Failed to send the ping event ({})".format(err) raise RuntimeError(msg)
def _ping(): """ The ping event is unauthenticated and is dispatched right away, without passing through the caching layer in the `events` file. """ event = json.dumps(get_action_event('ping')) status, err, _ = request_wrapper('post', '/tracking/ping', data=event, headers=content_type_json) if not status: msg = _("Failed to send the ping event ({})").format(err) raise RuntimeError(msg)
def _load_remote_prompts(self, num_retries=10): ''' Get the prompts/questions through a request, retrying <num_retries> if network is not up. ''' for attempt in xrange(0, num_retries): if attempt != 0: time.sleep(2) if not is_internet(): continue try: # Contact Kano questions API success, error, res = request_wrapper('get', '/questions') if not success: logger.warn( 'Error loading prompts from API (try {}): {}'.format( attempt, error)) continue prompts = sorted(res['questions'], key=lambda k: k['date_created']) if not len(prompts): return False self.prompts = prompts return True except Exception as exception: logger.error('Error loading prompts (try {}): {}'.format( attempt, exception)) return False
def _load_remote_prompts(self, num_retries=10): ''' Get the prompts/questions through a request, retrying <num_retries> if network is not up. ''' for attempt in xrange(0, num_retries): if attempt != 0: time.sleep(2) if not is_internet(): continue try: # Contact Kano questions API success, error, res = request_wrapper('get', '/questions') if not success: logger.warn('Error loading prompts from API (try {}): {}' .format(attempt, error)) continue prompts = sorted(res['questions'], key=lambda k: k['date_created']) if not len(prompts): return False self.prompts = prompts return True except Exception as exception: logger.error('Error loading prompts (try {}): {}' .format(attempt, exception)) return False
def send_question_response(answers, interactive=True, tags=['os', 'feedback-widget'], debug=False, dry_run=False): ''' This function is used by the Feedback widget to network-send the responses. The information (question_id, answer, username and email) is sent to a Kano API endpoint. answers is a list of tuples each having a Question ID and an Answer literal. The answers will be all packed into a payload object and sent in one single network transaction. ''' from kano_world.functions import get_email, get_mixed_username ok_msg_title = _('Thank you') # noqa: F821 ok_msg_body = _( # noqa: F821 'We will use your feedback to improve your experience' ) if interactive and not try_connect() or not try_login(): # The answer will be saved as offline, act as if it was sent correctly thank_you = KanoDialog(ok_msg_title, ok_msg_body) thank_you.dialog.set_position(Gtk.WindowPosition.CENTER_ALWAYS) thank_you.run() return False payload = { 'email': get_email(), 'username': get_mixed_username(), 'answers': [ { 'question_id': answer[0], 'text': answer[1], 'tags': tags } for answer in answers ] } if debug: print 'PAYLOAD construction:' print json.dumps(payload, sort_keys=True, indent=4, separators=(',', ': ')) # Send the answers unless we are testing the API if dry_run: return True success, error, dummy = request_wrapper('post', '/questions/responses', data=json.dumps(payload), headers=content_type_json) # Retry on error only if in GUI mode if not success: logger.error('Error while sending feedback: {}'.format(error)) if not interactive: return False retry = KanoDialog( title_text=_('Unable to send'), # noqa: F821 description_text=_( # noqa: F821 'Error while sending your feedback. Do you want to retry?' ), button_dict={ _('Close feedback').upper(): { # noqa: F821 'return_value': False, 'color': 'red' }, _('Retry').upper(): { # noqa: F821 'return_value': True, 'color': 'green' } } ) if retry.run(): # Try again until they say no return send_question_response([(answer[0], answer)], interactive=interactive) return False if interactive: thank_you = KanoDialog(ok_msg_title, ok_msg_body) thank_you.dialog.set_position(Gtk.WindowPosition.CENTER_ALWAYS) thank_you.run() return True
def send_question_response(answers, interactive=True, tags=['os', 'feedback-widget'], debug=False, dry_run=False): ''' This function is used by the Feedback widget to network-send the responses. The information (question_id, answer, username and email) is sent to a Kano API endpoint. answers is a list of tuples each having a Question ID and an Answer literal. The answers will be all packed into a payload object and sent in one single network transaction. ''' ok_msg_title = _('Thank you') ok_msg_body = _('We will use your feedback to improve your experience') if interactive and not try_connect() or not try_login(): # The answer will be saved as offline, act as if it was sent correctly thank_you = KanoDialog(ok_msg_title, ok_msg_body) thank_you.dialog.set_position(Gtk.WindowPosition.CENTER_ALWAYS) thank_you.run() return False payload = { 'email': get_email(), 'username': get_mixed_username(), 'answers': [ { 'question_id': answer[0], 'text': answer[1], 'tags': tags } for answer in answers ] } if debug: print 'PAYLOAD construction:' print json.dumps(payload, sort_keys=True, indent=4, separators=(',', ': ')) # Send the answers unless we are testing the API if dry_run: return True success, error, dummy = request_wrapper('post', '/questions/responses', data=json.dumps(payload), headers=content_type_json) # Retry on error only if in GUI mode if not success: logger.error('Error while sending feedback: {}'.format(error)) if not interactive: return False retry = KanoDialog( title_text=_('Unable to send'), description_text=_('Error while sending your feedback. Do you want to retry?'), button_dict={ _('Close feedback').upper(): { 'return_value': False, 'color': 'red' }, _('Retry').upper(): { 'return_value': True, 'color': 'green' } } ) if retry.run(): # Try again until they say no return send_question_response([(answer[0], answer)], interactive=interactive) return False if interactive: thank_you = KanoDialog(ok_msg_title, ok_msg_body) thank_you.dialog.set_position(Gtk.WindowPosition.CENTER_ALWAYS) thank_you.run() return True
def send_question_response(question_id, answer, interactive=True): ''' This function is used by the Feedback widget. The information (question_id, answer, username and email) is sent to an API endpoint. ''' ok_msg_title = 'Thank you' ok_msg_body = 'We will use your feedback to improve your experience' if interactive and not try_connect() or not try_login(): # The answer will be saved as offline, act as if it was sent correctly thank_you = KanoDialog(ok_msg_title, ok_msg_body) thank_you.dialog.set_position(Gtk.WindowPosition.CENTER_ALWAYS) thank_you.run() return False payload = { 'email': get_email(), 'username': get_mixed_username(), 'answers': [ { 'question_id': question_id, 'text': answer, 'tags': ['os', 'feedback-widget'] } ] } # Send data success, error, dummy = request_wrapper('post', '/questions/responses', data=json.dumps(payload), headers=content_type_json) if not success: logger.error('Error while sending feedback: {}'.format(error)) retry = KanoDialog( title_text='Unable to send', description_text=('Error while sending your feedback. ' \ 'Do you want to retry?'), button_dict={ 'Close feedback'.upper(): { 'return_value': False, 'color': 'red' }, 'Retry'.upper(): { 'return_value': True, 'color': 'green' } } ) if retry.run(): # Try again until they say no return send_question_response(question_id=question_id, answer=answer, interactive=interactive) return False thank_you = KanoDialog(ok_msg_title, ok_msg_body) thank_you.dialog.set_position(Gtk.WindowPosition.CENTER_ALWAYS) thank_you.run() return True