Пример #1
0
    def call_api(self, api_client: API, session: Session) -> str:
        '''
        Override ApiJob.

        Star or Unstar an user on the server
        '''
        try:
            source_sdk_object = sdclientapi.Source(uuid=self.source_uuid)

            # TODO: Once https://github.com/freedomofpress/securedrop-client/issues/648, we will
            # want to pass the default request timeout to remove_star and add_star instead of
            # setting it on the api object directly.
            api_client.default_request_timeout = 5
            if self.is_starred:
                api_client.remove_star(source_sdk_object)
            else:
                api_client.add_star(source_sdk_object)

            return self.source_uuid
        except (RequestTimeoutError, ServerConnectionError) as e:
            error_message = f'Failed to update star on source {self.source_uuid} due to error: {e}'
            raise UpdateStarJobTimeoutError(error_message, self.source_uuid)
        except Exception as e:
            error_message = f'Failed to update star on source {self.source_uuid} due to {e}'
            raise UpdateStarJobError(error_message, self.source_uuid)
Пример #2
0
    def send_reply(self, source_uuid: str, msg_uuid: str,
                   message: str) -> None:
        sdk_source = sdclientapi.Source(uuid=source_uuid)

        try:
            encrypted_reply = self.gpg.encrypt_to_source(source_uuid, message)
        except Exception:
            tb = traceback.format_exc()
            logger.error('Failed to encrypt to source {}:\n'.format(
                source_uuid, tb))
            self.reply_failed.emit(msg_uuid)
        else:
            # Guard against calling the API if we're not logged in
            if self.api:
                self.call_api(
                    self.api.reply_source,
                    self.on_reply_success,
                    self.on_reply_failure,
                    sdk_source,
                    encrypted_reply,
                    msg_uuid,
                    current_object=(source_uuid, msg_uuid),
                )
            else:  # pragma: no cover
                logger.error('not logged in - not implemented!')
                self.reply_failed.emit(msg_uuid)
Пример #3
0
    def _make_call(self, encrypted_reply: str,
                   api_client: API) -> sdclientapi.Reply:
        sdk_source = sdclientapi.Source(uuid=self.source_uuid)

        # TODO: Once https://github.com/freedomofpress/securedrop-client/issues/648, we will want to
        # pass the default request timeout to reply_source instead of setting it on the api object
        # directly.
        api_client.default_request_timeout = 5
        return api_client.reply_source(sdk_source, encrypted_reply,
                                       self.reply_uuid)
Пример #4
0
    def call_api(self, api_client: API, session: Session) -> str:
        '''
        Override ApiJob.

        Delete a source on the server
        '''
        try:
            source_sdk_object = sdclientapi.Source(uuid=self.source_uuid)
            api_client.delete_source(source_sdk_object)

            return self.source_uuid
        except (RequestTimeoutError, ServerConnectionError):
            raise
        except Exception as e:
            error_message = "Failed to delete source {uuid} due to {exception}".format(
                uuid=self.source_uuid, exception=repr(e))
            raise DeleteSourceJobException(error_message, self.source_uuid)
Пример #5
0
    def call_api(self, api_client: API, session: Session) -> str:
        '''
        Override ApiJob.

        Star or Unstar an user on the server
        '''
        try:
            source_sdk_object = sdclientapi.Source(uuid=self.source_uuid)

            if self.star_status:
                api_client.remove_star(source_sdk_object)
            else:
                api_client.add_star(source_sdk_object)

            return self.source_uuid
        except Exception as e:
            error_message = "Failed to update star on source {uuid} due to {exception}".format(
                uuid=self.source_uuid, exception=repr(e))
            raise UpdateStarJobException(error_message, self.source_uuid)
Пример #6
0
    def update_star(self, source_db_object):
        """
        Star or unstar. The callback here is the API sync as we first make sure
        that we apply the change to the server, and then update locally.
        """
        if not self.api:  # Then we should tell the user they need to login.
            self.on_action_requiring_login()
            return
        else:  # Clear the error status bar
            self.gui.clear_error_status()

        source_sdk_object = sdclientapi.Source(uuid=source_db_object.uuid)

        if source_db_object.is_starred:
            self.call_api(self.api.remove_star, self.on_update_star_success,
                          self.on_update_star_failure, source_sdk_object)
        else:
            self.call_api(self.api.add_star, self.on_update_star_success,
                          self.on_update_star_failure, source_sdk_object)
Пример #7
0
    def call_api(self, api_client: API, session: Session) -> str:
        """
        Override ApiJob.

        Star or Unstar an user on the server
        """
        try:
            source_sdk_object = sdclientapi.Source(uuid=self.uuid)

            if self.is_starred:
                api_client.remove_star(source_sdk_object)
            else:
                api_client.add_star(source_sdk_object)

            return self.uuid
        except (RequestTimeoutError, ServerConnectionError) as e:
            error_message = f"Failed to update star on source {self.uuid} due to error: {e}"
            raise UpdateStarJobTimeoutError(error_message, self.uuid)
        except Exception as e:
            error_message = f"Failed to update star on source {self.uuid} due to {e}"
            raise UpdateStarJobError(error_message, self.uuid)
Пример #8
0
    def call_api(self, api_client: API, session: Session) -> str:
        '''
        Override ApiJob.

        Delete a source on the server
        '''
        try:
            source_sdk_object = sdclientapi.Source(uuid=self.source_uuid)

            # TODO: After https://github.com/freedomofpress/securedrop-client/issues/648 is
            # merged, we will want to pass the timeout to delete_source instead of setting
            # it on the api object
            api_client.default_request_timeout = 5
            api_client.delete_source(source_sdk_object)

            return self.source_uuid
        except (RequestTimeoutError, ServerConnectionError):
            raise
        except Exception as e:
            error_message = "Failed to delete source {uuid} due to {exception}".format(
                uuid=self.source_uuid, exception=repr(e))
            raise DeleteSourceJobException(error_message, self.source_uuid)
Пример #9
0
 def _make_call(self, encrypted_reply: str,
                api_client: API) -> sdclientapi.Reply:
     sdk_source = sdclientapi.Source(uuid=self.source_uuid)
     return api_client.reply_source(sdk_source, encrypted_reply,
                                    self.reply_uuid)