55import asyncio
66import base64
77import binascii
8- import datetime
98import json
109import ssl
1110from collections .abc import Mapping
12- from typing import Any , cast
11+ from typing import TYPE_CHECKING , Any , cast
12+
13+ if TYPE_CHECKING :
14+ from botocore .client import BaseClient
1315
14- import boto3
1516from aiohttp import ClientSession , FormData
16- from botocore .client import BaseClient
17- from botocore .config import Config
18- from botocore .exceptions import ClientError
19- from warrant_lite import WarrantLite
2017
2118from pyoverkiz .auth .base import AuthContext , AuthStrategy
2219from pyoverkiz .auth .credentials import (
@@ -98,7 +95,7 @@ def __init__(
9895 ssl_context : ssl .SSLContext | bool ,
9996 api_type : APIType ,
10097 ) -> None :
101- """Initialize SessionLoginStrategy with given parameters ."""
98+ """Create a session-login strategy bound to the given credentials ."""
10299 super ().__init__ (session , server , ssl_context , api_type )
103100 self .credentials = credentials
104101
@@ -142,7 +139,7 @@ def __init__(
142139 ssl_context : ssl .SSLContext | bool ,
143140 api_type : APIType ,
144141 ) -> None :
145- """Initialize SomfyAuthStrategy with given parameters ."""
142+ """Create a Somfy OAuth2 strategy with a fresh auth context ."""
146143 super ().__init__ (session , server , ssl_context , api_type )
147144 self .credentials = credentials
148145 self .context = AuthContext ()
@@ -201,13 +198,7 @@ async def _request_access_token(
201198 if not access_token :
202199 raise SomfyServiceError ("No Somfy access token provided." )
203200
204- self .context .access_token = cast (str , access_token )
205- self .context .refresh_token = token .get ("refresh_token" )
206- expires_in = token .get ("expires_in" )
207- if expires_in :
208- self .context .expires_at = datetime .datetime .now (
209- datetime .UTC
210- ) + datetime .timedelta (seconds = cast (int , expires_in ) - 5 )
201+ self .context .update_from_token (token )
211202
212203
213204class CozytouchAuthStrategy (SessionLoginStrategy ):
@@ -257,6 +248,11 @@ class NexityAuthStrategy(SessionLoginStrategy):
257248
258249 async def login (self ) -> None :
259250 """Perform login using Nexity username and password."""
251+ import boto3
252+ from botocore .config import Config
253+ from botocore .exceptions import ClientError
254+ from warrant_lite import WarrantLite
255+
260256 loop = asyncio .get_running_loop ()
261257
262258 def _client () -> BaseClient :
@@ -307,7 +303,7 @@ def __init__(
307303 ssl_context : ssl .SSLContext | bool ,
308304 api_type : APIType ,
309305 ) -> None :
310- """Initialize LocalTokenAuthStrategy with given parameters ."""
306+ """Create a local-token strategy bound to the given credentials ."""
311307 super ().__init__ (session , server , ssl_context , api_type )
312308 self .credentials = credentials
313309
@@ -332,7 +328,7 @@ def __init__(
332328 ssl_context : ssl .SSLContext | bool ,
333329 api_type : APIType ,
334330 ) -> None :
335- """Initialize RexelAuthStrategy with given parameters ."""
331+ """Create a Rexel OAuth2 strategy with a fresh auth context ."""
336332 super ().__init__ (session , server , ssl_context , api_type )
337333 self .credentials = credentials
338334 self .context = AuthContext ()
@@ -395,13 +391,7 @@ async def _exchange_token(self, payload: Mapping[str, str]) -> None:
395391 raise InvalidTokenError ("No Rexel access token provided." )
396392
397393 self ._ensure_consent (access_token )
398- self .context .access_token = cast (str , access_token )
399- self .context .refresh_token = token .get ("refresh_token" )
400- expires_in = token .get ("expires_in" )
401- if expires_in :
402- self .context .expires_at = datetime .datetime .now (
403- datetime .UTC
404- ) + datetime .timedelta (seconds = cast (int , expires_in ) - 5 )
394+ self .context .update_from_token (token )
405395
406396 @staticmethod
407397 def _ensure_consent (access_token : str ) -> None :
@@ -423,7 +413,7 @@ def __init__(
423413 ssl_context : ssl .SSLContext | bool ,
424414 api_type : APIType ,
425415 ) -> None :
426- """Initialize BearerTokenAuthStrategy with given parameters ."""
416+ """Create a bearer-token strategy bound to the given credentials ."""
427417 super ().__init__ (session , server , ssl_context , api_type )
428418 self .credentials = credentials
429419
0 commit comments