#!/usr/bin/env python3 # -*- coding: utf-8 -*- from typing import Any, Self, Callable, Sequence, Optional from json import dumps as json_encode from Abstracts.RouteAbstract import RouteAbstract from Utils.Common import Common class RequestModel: def __init__(self:Self) -> None: self.post_variables:dict[str, Any|None] = {} self.get_variables:dict[str, Any|None] = {} self.url_variables:dict[str, Any|None] = {} self.variables:dict[str, Any|None] = {} self.request_headers:dict[str, Any|None] = {} self.method:str|None = None self.route:RouteAbstract|None = None self.response:str|bytes|None = None self.response_mime:str|None = None self.response_code:int = 0 self.response_headers:dict[str, Any|None] = {} self.callback:Callable[[RequestModel, str|bytes|None], None]|None = None def get(self:Self, key:str|Sequence[str], default:Optional[Any] = None) -> Any|None: return Common.get_value(key, ( self.url_variables, self.get_variables, self.post_variables, self.variables ), default) def set_variables(self:Self, inputs:dict[str, Any|None], on:Optional[str] = None) -> None: ( self.url_variables if on == "url" else self.get_variables if on == "get" else self.post_variables if on == "post" else self.variables).update(Common.get_dictionary(Common.load_json(inputs))) def set_response(self:Self, data:Any|None) -> None: if isinstance(data, dict): self.response_code = Common.get_value("code", data, 200) if data is None or isinstance(data, (str, bytes)): if not self.response_code: self.response_code = 200 self.response = data elif isinstance(data, (dict, list, tuple, set)): self.response_mime = "application/json" self.response = json_encode(data) elif isinstance(data, bool): self.response = "true" if data else "false" else: self.response = str(data) self.callback and self.callback(self, self.response) def set_request_headers(self:Self, inputs:Any|None) -> None: self.request_headers.update(Common.get_dictionary(inputs)) def set_response_headers(self:Self, inputs:Any|None) -> None: self.response_headers.update(Common.get_dictionary(inputs))