11
22from galaxy .http import HttpClient
3-
3+ from galaxy . api . errors import InvalidCredentials
44import aiohttp
55import logging as log
66from yarl import URL
@@ -42,7 +42,6 @@ def update_cookies(self, cookies):
4242 def set_auth_lost_callback (self , callback ):
4343 self ._auth_lost_callback = callback
4444
45-
4645 def get_credentials (self ):
4746 creds = {}
4847 creds ['cookie_jar' ] = pickle .dumps ([c for c in self ._cookie_jar ]).hex ()
@@ -53,8 +52,13 @@ async def do_request(self, method, *args, **kwargs):
5352 return await self .request (method , * args , ** kwargs )
5453 except Exception as e :
5554 log .warning (f"Request failed with { repr (e )} , attempting to refresh credentials" )
56- #await self.refresh_credentials()
55+ try :
56+ await self .refresh_credentials ()
5757 return await self .request (method , * args , ** kwargs )
58+ except Exception as e :
59+ log .error (f"Refresh workflow failed with { repr (e )} " )
60+ self ._auth_lost_callback ()
61+ raise InvalidCredentials ()
5862
5963 def authenticate_with_cookies (self , cookies ):
6064 cookiez = {}
@@ -67,6 +71,15 @@ def authenticate_with_cookies(self, cookies):
6771 self .token = cookiez ['SESSION_TOKEN' ]
6872 self ._store_credentials (self .get_credentials ())
6973
74+ async def refresh_credentials (self ):
75+ data = {'Authorization' : f'{{"session":{{"token":"{ self .token } "}}}}' ,
76+ 'content-type' : 'application/json' }
77+ payload = {}
78+ await self .request ('PUT' , 'https://api.paradox-interactive.com/accounts/sessions/accounts' , headers = data , json = payload )
79+ for cookie in self ._cookie_jar :
80+ if cookie ['key' ] == 'SESSION_TOKEN' :
81+ self .token = cookie ['value' ]
82+ self ._store_credentials (self .get_credentials ())
7083
7184
7285
0 commit comments