-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: disable FeatureFlagMiddleware and update user-agent parser
- Loading branch information
Showing
6 changed files
with
100 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,41 @@ | ||
from typing import Optional | ||
from typing import Optional, Dict, Any | ||
|
||
from ua_parser import user_agent_parser | ||
|
||
from .models import Device | ||
from .models import UserAgentInfo, UserAgent, CPU, Device, OS | ||
|
||
|
||
def safe_get(dictionary: Dict[str, any], keys: str, default: Optional[any] = None) -> any: | ||
""" | ||
Safely get a nested value from a dictionary. | ||
Args: | ||
dictionary (Dict[str, any]): The dictionary to extract the value from. | ||
keys (str): A string representing the keys separated by dots. | ||
default (Optional[any], optional): Default value to return if the keys are not found. Defaults to None. | ||
Returns: | ||
any: The value found at the specified keys, or the default value if not found. | ||
""" | ||
keys_list = keys.split('.') | ||
for key in keys_list: | ||
if isinstance(dictionary, dict) and key in dictionary: | ||
dictionary = dictionary[key] | ||
else: | ||
return default if default is not None else {} | ||
return dictionary | ||
|
||
|
||
class UAParser: | ||
def __init__(self, user_agent: Optional[str] = None): | ||
self.ua = user_agent_parser.Parse(user_agent or '') | ||
|
||
def get_result(self) -> Device: | ||
return Device( | ||
browser=self.ua.get('user_agent', {}).get('family'), | ||
cpu=self.ua('cpu', {}).get('architecture'), | ||
device=self.ua('device', {}).get('family'), | ||
engine=self.ua('user_agent', {}).get('family'), | ||
os=self.ua('os', {}).get('family'), | ||
def get_result(self) -> UserAgentInfo: | ||
return UserAgentInfo( | ||
raw=safe_get(self.ua, 'string'), | ||
user_agent=UserAgent(**safe_get(self.ua, 'user_agent')), | ||
cpu=CPU(**safe_get(self.ua, 'cpu')), | ||
device=Device(**safe_get(self.ua, 'device')), | ||
engine=UserAgent(**safe_get(self.ua, 'engine')), | ||
os=OS(**safe_get(self.ua, 'os')) | ||
) |