from__future__importannotationsimportjsonfromtypingimportTYPE_CHECKING,Any,Generic,TypeVar,castfrom..utilsimportMISSINGfrom.base_objectimportToMessageBasefrom.enumsimportErrorCodeifTYPE_CHECKING:from.._types.jsonrpc.responsesimport(ErrorPayload,)from.._types.jsonrpc.responsesimport(RawErrorResponseasErrorResponsePayload,)from.._types.jsonrpc.responsesimport(RawExecuteResponseasExecuteResponsePayload,)from.._types.jsonrpc.responsesimport(RawQueryResponseasQueryResponsePayload,)from.resultsimportResultT=TypeVar("T")__all__=("ErrorResponse","ExecuteResponse","QueryResponse",)classBaseResponse(ToMessageBase[T],Generic[T]):r"""This represents a response to flow. .. WARNING:: This class is NOT to be used as is. Use one of it's subclasses instead. """defto_message(self,id:int)->bytes:return(json.dumps({"jsonrpc":"2.0","id":id,}|cast("dict[Any, Any]",self.to_dict()))+"\r\n").encode()
[docs]classErrorResponse(BaseResponse["ErrorResponsePayload"]):r"""This represents an error sent to or from flow. Attributes -------- code: :class:`int` The error code for the error message: :class:`str` The error's message data: Optional[Any] Any extra data """__slots__="code","data","message"def__init__(self,code:int,message:str,data:Any|None=None)->None:self.code=codeself.message=messageself.data=datadefto_dict(self)->ErrorResponsePayload:data=self.dataifisinstance(data,Exception):data=f"{data}"return{"error":{"code":self.code,"message":self.message,"data":data}}@classmethoddeffrom_dict(cls:type[ErrorResponse],data:ErrorPayload|ErrorResponsePayload)->ErrorResponse:if"error"indata:data=data["error"]returncls(code=data["code"],message=data["message"],data=data.get("data"))@classmethoddefinternal_error(cls:type[ErrorResponse],data:Any=None)->ErrorResponse:returncls(code=ErrorCode.server_error_start.value,message="Internal error",data=data)
[docs]classQueryResponse(BaseResponse["QueryResponsePayload"]):r"""This response represents the response from search handler's callbacks and context menus. See the :ref:`search handler section <search_handlers>` for more information about using search handlers. Attributes -------- results: list[:class:`~flogin.jsonrpc.results.Result`] The results to be sent as the result of the query settings_changes: dict[:class:`str`, Any] Any changes to be made to the plugin's settings. debug_message: :class:`str` A debug message if you want """__slots__="debug_message","results","settings_changes"def__init__(self,results:list[Result],settings_changes:dict[str,Any]|None=None,debug_message:str=MISSING,)->None:self.results=resultsself.settings_changes=settings_changesor{}self.debug_message=debug_messageor""defto_dict(self)->QueryResponsePayload:return{"result":{"settingsChange":self.settings_changes,"debugMessage":self.debug_messageor"","result":[res.to_dict()forresinself.results],}}
[docs]classExecuteResponse(BaseResponse["ExecuteResponsePayload"]):r"""This response is a generic response for jsonrpc requests, most notably result callbacks. Attributes -------- hide: :class:`bool` Whether to hide the flow menu after execution or not """__slots__=("hide",)def__init__(self,hide:bool=True)->None:self.hide=hidedefto_dict(self)->ExecuteResponsePayload:return{"result":{"hide":self.hide}}