From 5ca018b2ea01108950cec59d659e6105d2202c84 Mon Sep 17 00:00:00 2001 From: alemidev Date: Thu, 2 Dec 2021 20:16:29 +0100 Subject: [PATCH] helper methods to load token from file/dict --- aiocraft/mc/token.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/aiocraft/mc/token.py b/aiocraft/mc/token.py index 1cc9363..d29f90c 100644 --- a/aiocraft/mc/token.py +++ b/aiocraft/mc/token.py @@ -42,6 +42,28 @@ class Token: CONTENT_TYPE = "application/json" HEADERS = {"content-type": CONTENT_TYPE} + def dict(self): + return { + "username":self.username, + "access_token":self.access_token, + "client_token":self.client_token, + "profile": self.profile.dict(), + } + + @classmethod + def from_file(cls, fname:str): + with open(fname) as f: + return cls.from_json(json.load(f)) + + @classmethod + def from_dict(cls, data:dict): + return cls( + username=data["username"], + access_token=data["accessToken"], + client_token=data["clientToken"], + profile=Profile(**data["selectedProfile"]), + ) + @classmethod async def authenticate(cls, username, password, invalidate=False): payload = { @@ -62,7 +84,7 @@ class Token: username=username, access_token=res["accessToken"], client_token=res["clientToken"], - profile=Profile(res["selectedProfile"]["id"], res["selectedProfile"]["name"]) + profile=Profile(**res["selectedProfile"]) ) @classmethod @@ -80,7 +102,7 @@ class Token: self.access_token = res["accessToken"] self.client_token = res["clientToken"] - self.profile = Profile(res["selectedProfile"]["id"], res["selectedProfile"]["name"]) + self.profile = Profile(**res["selectedProfile"]) return res