From fae031044545b37e7a70c88e9552e06778fb2b87 Mon Sep 17 00:00:00 2001 From: MarkParker5 Date: Wed, 27 Apr 2022 00:16:04 +0200 Subject: [PATCH] save tokens endpoint --- Controls/API/endpoints/hub/HubManager.py | 6 ++++++ Controls/API/endpoints/hub/router.py | 6 +++++- Controls/API/endpoints/hub/schemas.py | 4 ++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Controls/API/endpoints/hub/HubManager.py b/Controls/API/endpoints/hub/HubManager.py index 4f17518..d6cae42 100644 --- a/Controls/API/endpoints/hub/HubManager.py +++ b/Controls/API/endpoints/hub/HubManager.py @@ -38,3 +38,9 @@ class HubManager: def wifi(self, ssid: str, password: str): WiFi.save_and_connect(ssid, password) + + def set_tokens(tokens_pair: schemas.TokensPair): + with open(f'{path}/{resources}/access_token.txt', 'w') as f: + f.write(tokens_pair.access_token) + with open(f'{path}/{resources}/refresh_token.txt', 'w') as f: + f.write(tokens_pair.refresh_token) diff --git a/Controls/API/endpoints/hub/router.py b/Controls/API/endpoints/hub/router.py index 6695e2e..d615235 100644 --- a/Controls/API/endpoints/hub/router.py +++ b/Controls/API/endpoints/hub/router.py @@ -2,7 +2,7 @@ from uuid import UUID from fastapi import APIRouter, Depends import Controls.API.exceptions from .HubManager import HubManager -from .schemas import Hub, PatchHub +from .schemas import Hub, PatchHub, TokensPair router = APIRouter( @@ -25,3 +25,7 @@ async def hub_patch(hub: PatchHub, manager: HubManager = Depends()): @router.post('/wifi') async def hub_wifi(ssid: str, password: str, manager: HubManager = Depends()): manager.wifi(ssid, password) + +@router.post('set_tokens') +async def set_tokens(tokens: TokensPair): + manager.save_tokens(tokens) diff --git a/Controls/API/endpoints/hub/schemas.py b/Controls/API/endpoints/hub/schemas.py index 8edc6f9..97f5d57 100644 --- a/Controls/API/endpoints/hub/schemas.py +++ b/Controls/API/endpoints/hub/schemas.py @@ -12,3 +12,7 @@ class Hub(BaseModel): class Config: orm_mode = True + +class TokensPair(BaseModel): + access_token: str + refresh_token: str