Skip to content

Commit

Permalink
feat: support setting a timeout to all requests from Config object (#57)
Browse files Browse the repository at this point in the history
* feat: support setting a timeout to all requests from Config object

* fix: accept/reject user methods were not returning a Result object

* fix: use Field constraints to avoid mypy errors

See: pydantic/pydantic#4162

---------

Co-authored-by: Fran García Salomón <[email protected]>
  • Loading branch information
miguel-lorenzo and fgsalomon authored Feb 16, 2023
1 parent 9a9c068 commit 16c42fb
Show file tree
Hide file tree
Showing 8 changed files with 475 additions and 186 deletions.
6 changes: 5 additions & 1 deletion alice/auth/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def from_config(config: Config) -> "Auth":
api_key=config.api_key, # type: ignore
session=session,
url=config.onboarding_url,
timeout=config.timeout,
verbose=config.verbose,
)

Expand All @@ -32,9 +33,12 @@ def __init__(
api_key: str,
session: Session,
url: str = DEFAULT_URL,
timeout: Union[float, None] = None,
verbose: Optional[bool] = False,
):
self._auth_client = AuthClient(url=url, api_key=api_key, session=session)
self._auth_client = AuthClient(
url=url, api_key=api_key, session=session, timeout=timeout
)
self.url = url
self.verbose = verbose

Expand Down
43 changes: 39 additions & 4 deletions alice/auth/auth_client.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
import json
import time
from typing import Optional, Union
from unittest.mock import Mock

import jwt
import requests
from requests import Response, Session

from alice.onboarding.tools import print_intro, print_response, timeit


class AuthClient:
def __init__(self, url: str, api_key: str, session: Session):
def __init__(
self,
url: str,
api_key: str,
session: Session,
timeout: Union[float, None] = None,
):
self.url = url
self._api_key = api_key
self._login_token: Union[str, None] = None
self.session = session
self.timeout = timeout

@timeit
def create_backend_token(
Expand All @@ -35,7 +44,16 @@ def create_backend_token(
final_url += f"/{user_id}"

headers = {"Authorization": f"Bearer {self._login_token}"}
response = self.session.get(final_url, headers=headers)
try:
response = self.session.get(
final_url, headers=headers, timeout=self.timeout
)
except requests.exceptions.Timeout:
response = Mock(spec=Response)
response.json.return_value = {"message": "Request timed out"}
response.text.return_value = "Request timed out"
response.status_code = 408

print_response(response=response, verbose=verbose)

return response
Expand All @@ -56,7 +74,15 @@ def create_user_token(

final_url = f"{self.url}/user_token/{user_id}"
headers = {"Authorization": f"Bearer {self._login_token}"}
response = self.session.get(final_url, headers=headers)
try:
response = self.session.get(
final_url, headers=headers, timeout=self.timeout
)
except requests.exceptions.Timeout:
response = Mock(spec=Response)
response.json.return_value = {"message": "Request timed out"}
response.text.return_value = "Request timed out"
response.status_code = 408

print_response(response=response, verbose=verbose)

Expand All @@ -65,7 +91,16 @@ def create_user_token(
def _create_login_token(self) -> Response:
final_url = f"{self.url}/login_token"
headers = {"apikey": self._api_key}
response = self.session.get(final_url, headers=headers)
try:
response = self.session.get(
final_url, headers=headers, timeout=self.timeout
)
except requests.exceptions.Timeout:
response = Mock(spec=Response)
response.json.return_value = {"message": "Request timed out"}
response.text.return_value = "Request timed out"
response.status_code = 408

return response

@staticmethod
Expand Down
3 changes: 3 additions & 0 deletions alice/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class Config:
)
api_key: Union[str, None] = Field(default=None)
sandbox_token: Union[str, None] = Field(default=None)
timeout: Union[float, None] = Field(
default=None, description="Timeout for every request in seconds", ge=1, le=100
)
send_agent: bool = Field(default=True)
verbose: bool = Field(default=False)
session: Union[Session, None] = None
8 changes: 5 additions & 3 deletions alice/onboarding/onboarding.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def from_config(config: Config) -> "Onboarding":
return Onboarding(
auth=Auth.from_config(config),
url=config.onboarding_url,
timeout=config.timeout,
send_agent=config.send_agent,
verbose=config.verbose,
session=session,
Expand All @@ -44,11 +45,12 @@ def __init__(
auth: Auth,
session: Session,
url: str = DEFAULT_URL,
timeout: Union[float, None] = None,
send_agent: bool = True,
verbose: bool = False,
):
self.onboarding_client = OnboardingClient(
auth=auth, url=url, send_agent=send_agent, session=session
auth=auth, url=url, timeout=timeout, send_agent=send_agent, session=session
)
self.url = url
self.verbose = verbose
Expand Down Expand Up @@ -1673,7 +1675,7 @@ def accept_user(
verbose = self.verbose or verbose
response = self.onboarding_client.accept_user(
user_id=user_id, operator=operator, verbose=verbose
)
).unwrap_or_return()

if response.status_code == 200:
return isSuccess
Expand Down Expand Up @@ -1714,7 +1716,7 @@ def reject_user(
rejection_reasons=rejection_reasons,
operator=operator,
verbose=verbose,
)
).unwrap_or_return()

if response.status_code == 200:
return isSuccess
Expand Down
Loading

0 comments on commit 16c42fb

Please sign in to comment.