Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add method to get the user state #79

Merged
merged 2 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions alice/onboarding/onboarding.py
Original file line number Diff line number Diff line change
Expand Up @@ -1678,6 +1678,40 @@ def get_duplicates_searches(
)
)

def get_user_state(
self,
user_id: str,
verbose: bool = False,
) -> Result[UserState, OnboardingError]:
"""
Retrieves the state of a user
Parameters
----------
user_id
User identifier

verbose
Used for print service response as well as the time elapsed
Returns
-------
A Result where if the operation is successful it returns the user state.
Otherwise, it returns an OnboardingError.
"""
verbose = self.verbose or verbose
response = self.onboarding_client.get_user_state(
user_id=user_id,
verbose=verbose,
).unwrap_or_return()

if response.status_code == 200:
return Success(UserState(response.json()))
else:
return Failure(
OnboardingError.from_response(
operation="get_user_state", response=response
)
)

def update_user_state(
self,
user_id: str,
Expand Down
42 changes: 42 additions & 0 deletions alice/onboarding/onboarding_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1920,6 +1920,48 @@ def get_duplicates_searches(self, verbose: bool = False) -> Result[Response, Err

return Success(response)

@timeit
def get_user_state(
self,
user_id: str,
verbose: bool = False,
) -> Result[Response, Error]:
"""
Retrieves the state of a user

Parameters
----------
user_id
User identifier
verbose
Used for print service response as well as the time elapsed


Returns
-------
A Response object [requests library]
"""
print_intro("get_user_state", verbose=verbose)

backend_user_token = self.auth.create_backend_token(
user_id=user_id
).unwrap_or_return()
print_token("backend_token_with_user", backend_user_token, verbose=verbose)

headers = self._auth_headers(backend_user_token)
try:
response = requests.get(
f"{self.url}/user/state",
headers=headers,
timeout=self.timeout,
)
except requests.exceptions.Timeout:
return Failure(OnboardingError.timeout(operation="get_user_state"))

print_response(response=response, verbose=verbose)

return Success(response)

@timeit
def update_user_state(
self,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_integration_flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def do_complete_flow() -> Result[bool, Error]:

flow_id = onboarding.create_flow(
steps=[OnboardingSteps.SELFIE],
default=True,
default=False,
name="alice-flow-test-onboarding-python",
).unwrap_or_return()

Expand Down
13 changes: 8 additions & 5 deletions tests/test_integration_onboarding.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,25 @@ def do_complete_onboarding() -> Result[dict, Error]:
user_id=user_id
).unwrap_or_return()

_ = onboarding.retrieve_certificate(
onboarding.retrieve_certificate(
user_id=user_id, certificate_id=certificate_id
).unwrap_or_return()

_ = onboarding.retrieve_certificates(user_id=user_id).unwrap_or_return()
onboarding.retrieve_certificates(user_id=user_id).unwrap_or_return()

_ = onboarding.update_user_state(
onboarding.update_user_state(
user_id=user_id, user_state=UserState.TO_REVIEW
).unwrap_or_return()
_ = onboarding.update_user_state(
onboarding.update_user_state(
user_id=user_id, user_state=UserState.ACCEPTED
).unwrap_or_return()
_ = onboarding.update_user_state(
onboarding.update_user_state(
user_id=user_id, user_state=UserState.REJECTED
).unwrap_or_return()

user_state = onboarding.get_user_state(user_id=user_id).unwrap_or_return()
assert user_state == UserState.REJECTED

onboarding.delete_user(user_id).unwrap_or_return()

return Success(report)
Expand Down
Loading