def _receive_ask_sectional_thumbnail(self, message: W24TechreadMessage) -> None: """Handle incoming Sectional Thumbnails Args: message (W24TechreadMessage): Message with the Sectional Thumbnail """ self.api_feed.add_headline("Sectional Thumbnail") self.api_feed.add_image(message.payload_bytes) # store the sectional locally so that we can # reuse the image when drawing the measures # and gd&ts if message.payload_dict is None: return # write it into the local store sectional_id = message.payload_dict.get('sectional_id') if sectional_id is not None: self.sectional_thumbnails[sectional_id] = message.payload_bytes # after we stored it..., censor and print message.payload_bytes = b"--- binary --" self.api_feed.add_json(message.json()) self.api_feed.add_line()
def _receive_ask_page_thumbnail(self, message: W24TechreadMessage) -> None: """Handle incoming Page Thumbnails Args: message (W24TechreadMessage): Message with the Page Thumbnail """ self.api_feed.add_headline("Page Thumbnail") self.api_feed.add_image(message.payload_bytes) message.payload_bytes = b"--- binary --" self.api_feed.add_json(message.json()) self.api_feed.add_line()
def _receive_progress_started(self, message: W24TechreadMessage) -> None: """ Handle Incomeing errors Args: message (W24TechreadMessage): Message with the Progress Message """ self.api_feed.add_headline("Progress") self.api_feed.add_json(message.json()) self.api_feed.add_line()
def _receive_error(self, message: W24TechreadMessage) -> None: """ Handle Incomeing errors Args: message (W24TechreadMessage): Message with the Error message """ self.api_feed.add_headline("ERROR") self.api_feed.add_json(message.json()) self.api_feed.add_line()
async def _process_message(message_raw: str) -> W24TechreadMessage: """ Interpret the raw websocket message and turn it inot a W24TechreadMessage Arguments: message_raw {str} -- Raw message Raises: UnauthorizedException: Exception is raised when you requested an action that you have no priviledges for (or that does not exist) ServerException: Exception is raised when the server did not respond as expected Returns: W24TeachreadMessage -- interpreted message """ # interpret and return try: message = W24TechreadMessage.parse_raw(message_raw) # if that failes, we are probably receiving a # message from the gateway directly except ValidationError: # The Gateway responds with the format # {"message": str, "connectionId":str, "requestId":str} # Obtain the message response = json.loads(message_raw) message = response.get('message') # raise a specific exception if the # requested action was forbidden if message == 'Forbidden': raise UnauthorizedException("Requested Action forbidden") # otherwise fail with an UnknownException raise ServerException( f"Unexpected server response '{message_raw}'.") return message
async def _trigger_asks_exception( asks: List[W24Ask], exception_raw: Union[BadRequestException, RequestTooLargeException] ) -> AsyncGenerator[W24TechreadMessage, None]: """ Trigger exceptions for all the submitted asks. This helps us to mock consistent exception handling behavior even when the files are rejected before they reach the API. Args: asks (List[W24Ask]): List of all submitted asks exception (RequestTooLargeException): Exception that shall be pushed Yields: W24TechreadMessage: Exception message """ # get the exception type from the MAP try: exception_type = EXCEPTION_MAP[type(exception_raw)] # if we see an exception that we were not supposed # to handle, there must have been a developer passing # a new exception type. Let's tell her by rasing # a runtime error except KeyError: raise RuntimeError( f"Unknown exception type passed: {type(exception_raw)}") # translate the exception into an official exception exception = W24TechreadException( exception_level=W24TechreadExceptionLevel.ERROR, exception_type=exception_type) # then yield one message for each of the requested asks for cur_ask in asks: yield W24TechreadMessage(request_id=uuid.uuid4(), message_type=W24TechreadMessageType.ASK, message_subtype=cur_ask.ask_type, exceptions=[exception])
def _receive_ask_variant_measures(self, message: W24TechreadMessage) -> None: """Handle incoming Variant Measures Args: message (W24TechreadMessage): Message with the Variant Measures """ self.api_feed.add_headline("Variant Measures") self.api_feed.add_json(message.json()) # check whether we have an stored version of the sectional if message.payload_dict is None: return sectional_id = message.payload_dict.get('sectional_id') if sectional_id is None: return # now get the measure list from the payload and stop # the execution if the payload is None measure_list_raw = message.payload_dict.get('measures') if measure_list_raw is None or not measure_list_raw: return # if we have a payload, translate it into usable objects measure_list = [W24Measure.parse_obj(m) for m in measure_list_raw] # if we really have sectional_bytes and measures, illustrate # the results sectional_bytes = self.sectional_thumbnails.get(sectional_id) if sectional_bytes is not None: sectional_bytes_w_measures = event_illustrator\ .illustrate_sectional_measures(sectional_bytes, measure_list) self.api_feed.add_image(sectional_bytes_w_measures) # add the table for the measures measure_table = W24GuiMeasureTable.from_measure_list(measure_list) self.api_feed.add_table(measure_table) self.api_feed.add_line()