File size: 2,583 Bytes
bf41ce7 1fac9b0 286137e 1fac9b0 bf41ce7 a8b45b8 bf41ce7 4cda30b bf41ce7 4cda30b bf41ce7 4cda30b bf41ce7 49d14d9 51b69ae 49d14d9 286137e 4e54695 286137e d05535d ef5e6f9 d05535d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | # assembler to mapping data into another data type.
from typing import Any, List
from Brain.src.common.http_response_codes import responses
from Brain.src.model.basic_model import BasicModel
from Brain.src.model.contact_model import ContactModel
from Brain.src.model.message_model import MessageModel
from Brain.src.model.req_model import ReqModel
from Brain.src.model.requests.request_model import (
ChatRising,
SendSMS,
TrainContacts,
BasicReq,
)
from Brain.src.model.sms_model import SMSModel
class Assembler:
"""mapping to BasicModel"""
def to_basic_model(self, data: Any) -> BasicModel:
model = BasicModel(data["image_name"], data["message"])
return model
"""mapping to http response"""
def to_response(self, code, message, result) -> Any:
response = {"message": responses[code], "result": result, "status_code": code}
return response
"""mapping data to a collection of MessageModel"""
def to_array_message_model(self, data: Any) -> List[MessageModel]:
result = []
for item in data:
result.append(self.to_message_model(item))
return result
"""mapping data to a MessageModel"""
def to_message_model(self, data: ChatRising.Format) -> MessageModel:
return MessageModel(data.role, data.content)
"""mapping data to a SMSModel"""
def to_sms_model(self, data: SendSMS.Body) -> SMSModel:
sms_model = SMSModel()
sms_model.get_sms_model(data)
return sms_model
"""mapping data to a ContactModel"""
def to_contact_model(self, data: TrainContacts.ContactReq) -> ContactModel:
contact_model = ContactModel()
contact_model.get_contact_model(data)
return contact_model
"""mapping result type into json
{
"program": sms | contacts | browser | selectitemdetailinfo,
"content": string
}
"""
def to_result_format(self, program: str, content: str) -> Any:
return {"program": program, "content": content}
"""mapping basic req"""
def to_req_model(self, basic_req: BasicReq.Confs) -> ReqModel:
return ReqModel(basic_req.to_json())
"""mapping data from a ContactModel into json
{
"contactId": int,
"displayName": string,
"phoneNumbers": [string]
}
"""
def to_contact_result_format(self, contact: ContactModel) -> Any:
return {
"contactId": contact.contact_id,
"displayName": contact.display_name,
"phoneNumbers": contact.phone_numbers,
}
|