1+ import functools
12import os
23import string
34
@@ -11,13 +12,36 @@ def get_process_execution_user_token():
1112 token = os .environ .get ("WAYSCRIPT_EXECUTION_USER_TOKEN" )
1213 return token
1314
15+ def set_process_execution_user_token (value : str ):
16+ os .environ ["WAYSCRIPT_EXECUTION_USER_TOKEN" ] = value
17+ return value
1418
1519def get_process_id ():
1620 """Return uuid of current container execution"""
1721 process_id = os .environ ["WS_PROCESS_ID" ]
1822 return process_id
1923
2024
25+ def get_refresh_token ():
26+ refresh_token = os .environ ["WAYSCRIPT_EXECUTION_USER_REFRESH_TOKEN" ]
27+ return refresh_token
28+
29+
30+ def retry_on_401_wrapper (f ):
31+
32+ @functools .wraps (f )
33+ def _retry_on_401 (client , * args , ** kwargs ):
34+
35+ response = f (client , * args , ** kwargs )
36+ # refresh credentials and retry on 401
37+ if response .status_code == 401 :
38+ client ._refresh_access_token ()
39+ response = f (client , * args , ** kwargs )
40+ return response
41+
42+ return _retry_on_401
43+
44+
2145class WayScriptClient :
2246
2347 def __init__ (self , * args , ** kwargs ):
@@ -29,36 +53,53 @@ def __init__(self, *args, **kwargs):
2953
3054 def _get_url (self , subpath : str , route : str , template_args : dict = None ):
3155 """Generate an url"""
32- subpath_template = string .Template (settings .ROUTES [subpath ][route ])
33- subpath = subpath_template .substitute (** template_args )
56+ subpath_template_str = settings .ROUTES [subpath ][route ]
57+ subpath_template = string .Template (subpath_template_str )
58+ full_subpath = subpath_template .substitute (** template_args ) if template_args else subpath_template_str
3459
35- url = f"{ settings .WAYSCRIPT_ORIGIN } /{ subpath } "
60+ url = f"{ settings .WAYSCRIPT_ORIGIN } /{ full_subpath } "
3661 return url
3762
63+ def _refresh_access_token (self ):
64+ """Refresh access token and update environment"""
65+ url = self ._get_url (subpath = "auth" , route = "refresh" )
66+ refresh_token = get_refresh_token ()
67+ payload = {"refresh_token" : refresh_token }
68+ response = self .session .post (url , json = payload )
69+ response .raise_for_status ()
70+ access_token = response .json ()["access_token" ]
71+ set_process_execution_user_token (access_token )
72+ self .session .headers ["authorization" ] = f"Bearer { access_token } "
73+
74+ @retry_on_401_wrapper
3875 def get_process_detail_expanded (self , _id : str ):
3976 """Request process expanded detail endpoint"""
4077 url = self ._get_url (subpath = "processes" , route = "detail_expanded" , template_args = {"id" : _id })
4178 response = self .session .get (url )
4279 return response
4380
81+ @retry_on_401_wrapper
4482 def get_workspace_integration_detail (self , _id : str ):
4583 """Request a workspace-integrations detail"""
4684 url = self ._get_url (subpath = "workspace-integrations" , route = "detail" , template_args = {"id" : _id })
4785 response = self .session .get (url )
4886 return response
4987
88+ @retry_on_401_wrapper
5089 def get_lair_detail (self , _id : str ):
5190 """Request lair detail"""
5291 url = self ._get_url (subpath = "lairs" , route = "detail" , template_args = {"id" : _id })
5392 response = self .session .get (url )
5493 return response
5594
95+ @retry_on_401_wrapper
5696 def get_workspace_detail (self , _id : str ):
5797 """Request workspace detail"""
5898 url = self ._get_url (subpath = "workspaces" , route = "detail" , template_args = {"id" : _id })
5999 response = self .session .get (url )
60100 return response
61101
102+ @retry_on_401_wrapper
62103 def post_webhook_http_trigger_response (self , _id : str , payload : dict = None ):
63104 """
64105 Post an http trigger response
0 commit comments