Skip to content

Commit 3efa29b

Browse files
Handle Access Token Refresh (#11)
* Document how to use context library * Refresh access token on a 401
1 parent 10ae46c commit 3efa29b

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# wayscript-python
22

3+
## Context
4+
5+
```
6+
from wayscript import context
7+
8+
event = context.get_event()
9+
```
10+
311
## Triggers
412

513
### HTTP Triggers

src/wayscript/settings.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44
import os
55

6-
6+
AUTH_ROUTE = "auth"
77
PROCESSES_ROUTE = "processes"
88
LAIRS_ROUTE = "lairs"
99
WEBHOOKS = "webhooks"
@@ -13,6 +13,7 @@
1313

1414

1515
ROUTES = {
16+
"auth": {"refresh": f"{AUTH_ROUTE}/refresh"},
1617
"lairs": {"detail": f"{LAIRS_ROUTE}/$id"},
1718
"processes": { "detail_expanded": f"{PROCESSES_ROUTE}/$id/detail"},
1819
"webhooks": {"http_trigger_response": f"{WEBHOOKS}/http-trigger/response/$id"},

src/wayscript/utils.py

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import functools
12
import os
23
import 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

1519
def 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+
2145
class 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

Comments
 (0)