Skip to content

Commit

Permalink
Merge pull request #15 from novitae/master
Browse files Browse the repository at this point in the history
Typing literals + UserAgent __repr__
  • Loading branch information
iamdual authored Oct 20, 2024
2 parents c3e8bf7 + 2a54693 commit d8d05da
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
12 changes: 7 additions & 5 deletions src/ua_generator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
"""
import typing

from . import user_agent, options as _options
from . import user_agent, options as _options, data as _data


def generate(device: typing.Union[tuple, str, None] = None,
platform: typing.Union[tuple, str, None] = None,
browser: typing.Union[tuple, str, None] = None,
options: typing.Union[_options.Options, None] = None) -> user_agent.UserAgent:
def generate(
device: typing.Union[tuple[_data._DEVICES_TYPE], _data._DEVICES_TYPE, None] = None,
platform: typing.Union[tuple[_data._PLATFORMS_TYPE], _data._PLATFORMS_TYPE, None] = None,
browser: typing.Union[tuple[_data._BROWSERS_TYPE], _data._BROWSERS_TYPE, None] = None,
options: typing.Union[_options.Options, None] = None
):
return user_agent.UserAgent(device, platform, browser, options)
5 changes: 5 additions & 0 deletions src/ua_generator/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
Copyright: 2022-2024 Ekin Karadeniz (github.com/iamdual)
License: Apache License 2.0
"""
from typing import Literal

DEVICES = ('desktop', 'mobile')
_DEVICES_TYPE = Literal['desktop', 'mobile', None]

PLATFORMS = ('windows', 'macos', 'ios', 'linux', 'android')
_PLATFORMS_TYPE = Literal['windows', 'macos', 'ios', 'linux', 'android', None]
PLATFORMS_DESKTOP = ('windows', 'macos', 'linux') # Platforms on desktop devices
PLATFORMS_MOBILE = ('ios', 'android') # Platforms on mobile devices

BROWSERS = ('chrome', 'edge', 'firefox', 'safari')
_BROWSERS_TYPE = Literal['chrome', 'edge', 'firefox', 'safari', None]
BROWSERS_SUPPORT_CH = ('chrome', 'edge') # Browsers that support Client Hints
_BROWSERS_SUPPORT_CH_TYPE = Literal['chrome', 'edge', None]
19 changes: 17 additions & 2 deletions src/ua_generator/user_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,26 @@

from . import utils, exceptions
from .client_hints import ClientHints
from .data import DEVICES, BROWSERS, PLATFORMS, PLATFORMS_DESKTOP, PLATFORMS_MOBILE
from .data import (
DEVICES, _DEVICES_TYPE,
BROWSERS, _BROWSERS_TYPE,
PLATFORMS, _PLATFORMS_TYPE,
PLATFORMS_DESKTOP,
PLATFORMS_MOBILE,
)
from .data.generator import Generator
from .headers import Headers
from .options import Options


class UserAgent:
def __init__(self, device=None, platform=None, browser=None, options=None):
def __init__(
self,
device: _DEVICES_TYPE = None,
platform: _PLATFORMS_TYPE = None,
browser: _BROWSERS_TYPE = None,
options: typing.Union[Options, None] = None,
):
self.device: typing.Union[str, None] = utils.choice(device) if device else None
self.platform: typing.Union[str, None] = utils.choice(platform) if platform else None
self.browser: typing.Union[str, None] = utils.choice(browser) if browser else None
Expand Down Expand Up @@ -88,3 +100,6 @@ def __complete(self):

def __str__(self):
return self.text

def __repr__(self) -> str:
return f"UserAgent(\"{self.text}\", device={self.device}, platform={self.platform}, browser={self.browser})"

0 comments on commit d8d05da

Please sign in to comment.