def fire_audio_tasks(): from GatewayApp.requesters.audio_requester import AudioRequester if Queue._audio_status == 1: return else: Queue._audio_status = 1 print('Firing audio tasks...') atasks = list(filter(lambda x: x['type'] == Queue.AUDIO, Queue.queue)) ar = AudioRequester() tasks_to_delete = [] for task in atasks: if task['request'].method == 'POST': _, code = ar.post_audio(task['request'], task['data']) elif task['request'].method == 'PATCH': _, code = ar.patch_audio(task['request'], task['uuid'], task['data']) elif task['request'].method == 'DELETE': _, code = ar.delete_audio(task['request'], task['uuid']) else: code = 501 if code < 500: tasks_to_delete.append(task) for task in tasks_to_delete: Queue.queue.remove(task) print(f'{len(tasks_to_delete)} audio tasks fired with success')
def _delete_audio_from_message(self, request, message_json: dict) -> Tuple[dict, int]: from GatewayApp.requesters.audio_requester import AudioRequester if message_json['audio_uuid']: del_json, code = AudioRequester().delete_audio( request, message_json['audio_uuid']) if code not in (204, 404): return del_json, code return {}, 204
def post_message(self, request, data: dict) -> Tuple[dict, int]: from GatewayApp.requesters.audio_requester import AudioRequester from GatewayApp.requesters.images_requester import ImagesRequester # check_json, code = self._check_if_attachments_exist(request, data) # Прикрепление юзера и проверка что он вообще есть try: if not self._check_if_user_exists(request, data['to_user_id']): return {'error': 'No user found with given id'}, 404 except KeyError: return {'error': 'No to_user_id key was given'}, 400 data, code = self._add_from_user_id_to_data(request, data) if code != 200: return data, code # ПОСТ аудио try: upload_json, code = AudioRequester().post_audio( request, data['audio']) if code != 201: return upload_json, code data['audio_uuid'] = upload_json['uuid'] except KeyError: pass # ПОСТ картинки try: upload_json, code = ImagesRequester().post_image( request, data['image']) if code != 201: self.__audio_rollback( request, data) # Rollback если в посте картинки ошибка return upload_json, code data['image_uuid'] = upload_json['uuid'] except KeyError: pass # Пост самого сообщения response = self.perform_post_request(self.HOST, data=data) if response is None: self.__attachments_rollback(request, data) return Requester.ERROR_RETURN resp_json = self.get_valid_json_from_response(response) if response.status_code != 201: self.__attachments_rollback(request, data) return resp_json, response.status_code try: resp_json = self.__get_and_set_message_users(request, resp_json) except KeyError: return { 'error': 'Key error was raised while getting user json!' }, 500 except UserGetError as e: return e.err_msg, e.code return resp_json, response.status_code
def __get_and_set_message_audio(self, message: dict) -> dict: from GatewayApp.requesters.audio_requester import AudioRequester audio_uuid = message[ 'audio_uuid'] # Можеть быть кей еррор, поймаем в гет_мессаджес if audio_uuid is not None: audio_json, audio_status = AudioRequester().get_concrete_audio( request=None, uuid=audio_uuid) if audio_status != 200: from Gateway.settings import DEBUG if DEBUG: print(audio_json) raise AudioGetError(code=audio_status, err_json=audio_json) message['audio'] = audio_json return message
class BaseAudioView(APIView): REQUESTER = AudioRequester()
def __audio_rollback(self, request, data: dict): from GatewayApp.requesters.audio_requester import AudioRequester try: AudioRequester().delete_audio(request, data['audio_uuid']) except KeyError: pass
def _check_if_audio_exists(self, audio_uuid) -> bool: from GatewayApp.requesters.audio_requester import AudioRequester _, code = AudioRequester().get_concrete_audio(request=None, uuid=audio_uuid) return code == 200