Skip to content

Commit 3823fc9

Browse files
committed
Add retry handling for API login stability
1 parent 3461026 commit 3823fc9

1 file changed

Lines changed: 24 additions & 19 deletions

File tree

api/auth_client.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,30 @@
66

77

88
class AuthClient:
9-
def __init__(self, playwright: Playwright, settings: Settings):
10-
self.request_context = playwright.request.new_context(base_url=settings.client_base_url)
11-
12-
def login(self, user_credentials: dict) -> dict:
13-
with allure.step("Authenticate user through Login API"):
14-
response = self.request_context.post(
15-
"/api/ecom/auth/login",
16-
data={
17-
"userEmail": user_credentials["userEmail"],
18-
"userPassword": user_credentials["userPassword"],
19-
},
20-
)
21-
22-
assert_status(response, 200, "Login API")
23-
response_body = response.json()
24-
assert_json_has_keys(response_body, ["token"], "Login API")
25-
return response_body
26-
27-
def get_token(self, user_credentials: dict) -> str:
9+
def __init__(self, request_context):
10+
self.request_context = request_context
11+
12+
def login(self, user_credentials):
13+
last_error = None
14+
15+
for attempt in range(3):
16+
try:
17+
response = self.request_context.post(
18+
"/api/ecom/auth/login",
19+
data=user_credentials,
20+
)
21+
assert_status(response, 200, "Login API")
22+
return response.json()
23+
24+
except PlaywrightError as error:
25+
last_error = error
26+
if attempt == 2:
27+
raise
28+
time.sleep(2)
29+
30+
raise last_error
31+
32+
def get_token(self, user_credentials):
2833
return self.login(user_credentials)["token"]
2934

3035
def login_with_payload(self, payload: dict):

0 commit comments

Comments
 (0)