def request_dto_serialize(): dto = RequestDto[ComputerFlow]("some_method", ComputerFlow.get_random_flow()) json_dto = JsonFormatter.serialize(dto) dto_value = JsonFormatter.deserialize(json_dto, RequestDto[ComputerFlow]) print(dto_value) # repeat json_dto2 = JsonFormatter.serialize(dto_value) dto_value2 = JsonFormatter.deserialize(json_dto2, RequestDto[ComputerFlow]) print(dto_value2 ) # needed for breakpoint to check the result of formatting
def request_dto_computer_key(): dto0 = RequestDto[ComputerKey](data=ComputerKey("5", "40")) json0 = JsonFormatter.serialize(dto0) val0 = JsonFormatter.deserialize(json0, RequestDto[ComputerKey]) print(val0) print(RequestDto.__annotations__["data"]) dto1 = RequestDto[ComputerManager.GetKeysByAuditoriumDto]( data=ComputerManager.GetKeysByAuditoriumDto("37")) json1 = JsonFormatter.serialize(dto1) val1 = JsonFormatter.deserialize( json1, RequestDto[ComputerManager.GetKeysByAuditoriumDto]) print(val1) print(RequestDto.__annotations__["data"])
def __set_config_impl(cls, web_config: ClientConfig) -> bool: if not hasattr(web_config, "check_state_period") or not hasattr(web_config, "send_data_period"): raise ServerLogicException(401, "Received wrong data from client !") if ConfigManager.__validate_config(web_config): with open(cls.__file_name, "w") as config_file: config_file.write(JsonFormatter.serialize(web_config)) return True return False
def send_wrong_dto(): # wrong dto. Might be RequestDto[ComputerKey] dto = RequestDto[GetKeysByAuditoriumDto]( data=GetKeysByAuditoriumDto("39")) json = JsonFormatter.serialize(dto) result = NetworkManager.send("GetComputer", json) print(result) json_dto = '{"d": {"a": 3}}' # type value of "a" might be a string result2 = NetworkManager.send("GetComputer", json_dto) print(result2)
def use_unsupported_server_method(): # GET request result = NetworkManager.get("GetAuditor") print(result) # POST request dto = RequestDto[GetKeysByAuditoriumDto]( data=GetKeysByAuditoriumDto("39")) json = JsonFormatter.serialize(dto) result = NetworkManager.send("GetKeysByAuditori", json) print(result)
def do_GET(self): method_name = MethodHandler.get_server_method_name(self.path) try: response_dto = HttpRequestHandler.method_handler.do_get( method_name) except ServerLogicException as ex: self.send_error(ex.state_code, ex.message) return self.send_response(response_dto.state_code) self.__send_header() response_dto_json = JsonFormatter.serialize(response_dto) self.wfile.write(response_dto_json.encode())
def do_POST(self): length: int = int(self.headers.get('content-length', 0)) json_request_dto: str = self.rfile.read(length).decode("utf-8") method_name = MethodHandler.get_server_method_name(self.path) try: request_dto = HttpRequestHandler.method_handler.get_request_dto( json_request_dto, method_name, self.client_address) response_dto = HttpRequestHandler.method_handler.do_post( method_name, request_dto) except ServerLogicException as ex: self.send_error(ex.state_code, ex.message) return response_dto_json = JsonFormatter.serialize(response_dto) self.send_response(response_dto.state_code) self.__send_header() self.wfile.write(response_dto_json.encode())
def client_config(): c = ClientConfig(None, None) json = JsonFormatter.serialize(c) val = JsonFormatter.deserialize(json, ClientConfig) print(val.__dict__) print(val)
def computer_serialize(): computer = Computer.get_random_computer() json = JsonFormatter.serialize(computer) val = JsonFormatter.deserialize(json, Computer) print(val)
def dict_field_serialize(): some_type = SomeType.get_random_some_type() json = JsonFormatter.serialize(some_type) val = JsonFormatter.deserialize(json, SomeType) print(val)
def request_complex_dto_serialize(): dto = SomeEntity.get_temp_some_entity() json_dto = JsonFormatter.serialize(dto) dto_val = JsonFormatter.deserialize(json_dto, SomeEntity) print(dto_val)
def computer_flow_serialize(): computer_flow = ComputerFlow.get_random_flow() computer_flow_json = JsonFormatter.serialize(computer_flow) val = JsonFormatter.deserialize(computer_flow_json, ComputerFlow) print(val)
def get_keys_by_auditorium(): dto = RequestDto[GetKeysByAuditoriumDto]( data=GetKeysByAuditoriumDto("39")) json = JsonFormatter.serialize(dto) result = NetworkManager.send("GetKeysByAuditorium", json) print(result)
def serialize_test(): person: Person = Person(13, "Max") json = JsonFormatter.serialize(person) person_val = JsonFormatter.deserialize(json, Person) print(person_val)
def __store_temp_data(cls, data) -> None: with open(cls.__temp_data_file, "w") as temp_data_file: temp_data_file.write(JsonFormatter.serialize(data))
def __send_data_to_server_impl(computer_flow: ComputerFlow): dto = RequestDto[ComputerFlow](data=computer_flow) dto_json = JsonFormatter.serialize(dto) NetworkManager.send("SendComputerFlow", dto_json)
def update(self, key: ComputerKey, computer: Computer) -> None: self.__cache[key] = computer json_data = JsonFormatter.serialize(computer) super().update(key, json_data)