#43 - fix the issues reported by flake8
This commit is contained in:
+35
-47
@@ -1,8 +1,8 @@
|
||||
import base64
|
||||
import json
|
||||
from enum import Enum
|
||||
from typing import List, Dict
|
||||
from dataclasses import dataclass, field
|
||||
from enum import Enum
|
||||
from typing import Dict, List
|
||||
|
||||
from amqp.model.service_message_type import ServiceMessageType
|
||||
from amqp.model.snowflake_id import SnowflakeId
|
||||
@@ -16,9 +16,24 @@ Define the structure of messages used in Clever Micro
|
||||
"""
|
||||
|
||||
|
||||
@dataclass
|
||||
class CleverMicroMessage:
|
||||
DEFAULT_CONTENT_TYPE = ["application/octet-stream"]
|
||||
|
||||
"""
|
||||
The common structure of a message that transfers the user request to the Service and back. All data related messages
|
||||
conform to this base structure:
|
||||
|
||||
magic - identifies the message type (REST, RPC, DataResponse)
|
||||
id - unique message ID
|
||||
m_field - method name (for REST style calls) or service name (for RPC style calls) or response code for DataResponse
|
||||
d_field - domain name (for REST style calls) or module name (for RPC style calls) or error code for DataResponse
|
||||
p_field - path (for REST style calls) or method name (for RPC style calls) or error cause for DataResponse
|
||||
headers - headers of the message (e.g. Content-Type, Accept, etc.)
|
||||
trace_info - trace information (e.g. trace ID, span ID, etc. for Jaeger trace monitor)
|
||||
base64body - the body of the message encoded in base64
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
magic: str,
|
||||
@@ -66,11 +81,19 @@ class CleverMicroMessage:
|
||||
def body(self) -> bytes:
|
||||
return base64.b64decode(self.base64body())
|
||||
|
||||
def contentType(self) -> str:
|
||||
def content_type(self) -> str:
|
||||
return self.headers().get("Content-Type", self.DEFAULT_CONTENT_TYPE)[0]
|
||||
|
||||
|
||||
class DataMessage(CleverMicroMessage):
|
||||
"""
|
||||
The class represents a structure of a message that transfers the user request to the Service.
|
||||
It may also be used to make a call/request between services.
|
||||
The response to this call is in format of DataResponse class.
|
||||
|
||||
See DataMessageFactory for code to instantiate, serialize and deserialize the class
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
magic: str,
|
||||
@@ -95,8 +118,17 @@ class DataMessage(CleverMicroMessage):
|
||||
def path(self) -> str:
|
||||
return self.p_field()
|
||||
|
||||
def routing_key(self) -> str:
|
||||
return sanitize_as_rabbitmq_name(f"{self.d_field()}.{self.p_field()}")
|
||||
|
||||
|
||||
class DataResponse(CleverMicroMessage):
|
||||
"""
|
||||
The response message to REST & RPC style calls (note: the request is made with DataMessage)
|
||||
|
||||
See DataResponseFactory for code to instantiate, serialize and deserialize the class
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
magic: str,
|
||||
@@ -179,31 +211,6 @@ class AMQRoute:
|
||||
return self.__str__()
|
||||
|
||||
|
||||
@dataclass
|
||||
class DataMessage:
|
||||
"""
|
||||
The class represents a structure of a message that transfers the user request to the Service.
|
||||
It may also be used to make a call/request between services.
|
||||
The response to this call is in format of DataResponse class.
|
||||
Please see DataMessageFactory for code to instantiate, serialize and deserialize the class
|
||||
"""
|
||||
|
||||
magic: str
|
||||
id: SnowflakeId
|
||||
method: str
|
||||
domain: str
|
||||
path: str
|
||||
headers: dict[str, List[str]]
|
||||
trace_info: dict[str, str]
|
||||
base64_body: bytes
|
||||
|
||||
def routing_key(self) -> str:
|
||||
return sanitize_as_rabbitmq_name(f"{self.domain}.{self.path}")
|
||||
|
||||
def body(self) -> bytes:
|
||||
return base64.b64decode(self.base64_body)
|
||||
|
||||
|
||||
class MultipartDataMessage(DataMessage):
|
||||
def __init__(self, message: DataMessage, extra_data: dict):
|
||||
super().__init__(
|
||||
@@ -219,25 +226,6 @@ class MultipartDataMessage(DataMessage):
|
||||
self.extra_data = extra_data
|
||||
|
||||
|
||||
@dataclass
|
||||
class DataResponse:
|
||||
"""
|
||||
The response message to RPC style call (note: the request is made with DataMessage)
|
||||
Please see DataResponseFactory for code to instantiate, serialize and deserialize the class
|
||||
"""
|
||||
|
||||
id: str
|
||||
response_code: int
|
||||
content_type: str
|
||||
response: bytes
|
||||
error: str | None
|
||||
error_cause: str | None
|
||||
trace_info: dict[str, str]
|
||||
|
||||
def body(self) -> bytes:
|
||||
return base64.b64decode(self.response)
|
||||
|
||||
|
||||
# Ensure backward compatibility
|
||||
AMQResponse = DataResponse
|
||||
|
||||
|
||||
Reference in New Issue
Block a user