Skip to content

Commit

Permalink
refactor: separate type models from User
Browse files Browse the repository at this point in the history
  • Loading branch information
spencerwooo committed Feb 4, 2023
1 parent 219f112 commit f7c9223
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 32 deletions.
32 changes: 32 additions & 0 deletions bitsrun/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from enum import Enum
from typing import Literal, Optional, TypedDict, Union


class Action(Enum):
LOGIN = "login"
LOGOUT = "logout"


class UserResponseType(TypedDict):
client_ip: str
online_ip: str
# Field `error` is also `login_error` when logout action fails
error: Union[Literal["login_error"], Literal["ok"]]
error_msg: str
res: Union[Literal["login_error"], Literal["ok"]]
# Field `username` is not present on login fails and all logout scenarios
username: Optional[str]


class LoginStatusRespType(TypedDict):
# Field `error` is `not_online_error` when device is not logged in
error: str
client_ip: Optional[str]
# Field `online_ip` is always present regardless of login status
online_ip: str
# Below are fields only present when device is logged in
sum_bytes: Optional[int]
sum_seconds: Optional[int]
user_balance: Optional[int]
user_name: Optional[str]
wallet_balance: Optional[int]
34 changes: 2 additions & 32 deletions bitsrun/user.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,18 @@
import hmac
import json
from enum import Enum
from hashlib import sha1
from typing import Dict, Literal, Optional, TypedDict, Union
from typing import Dict, Optional, Union

import httpx

from bitsrun.models import Action, LoginStatusRespType, UserResponseType
from bitsrun.utils import fkbase64, xencode

_API_BASE = "http://10.0.0.55"
_TYPE_CONST = 1
_N_CONST = 200


class Action(Enum):
LOGIN = "login"
LOGOUT = "logout"


class UserResponseType(TypedDict):
client_ip: str
online_ip: str
# Field `error` is also `login_error` when logout action fails
error: Union[Literal["login_error"], Literal["ok"]]
error_msg: str
res: Union[Literal["login_error"], Literal["ok"]]
# Field `username` is not present on login fails and all logout scenarios
username: Optional[str]


class LoginStatusRespType(TypedDict):
# Field `error` is `not_online_error` when device is not logged in
error: str
client_ip: Optional[str]
# Field `online_ip` is always present regardless of login status
online_ip: str
# Below are fields only present when device is logged in
sum_bytes: Optional[int]
sum_seconds: Optional[int]
user_balance: Optional[int]
user_name: Optional[str]
wallet_balance: Optional[int]


def get_login_status(client: Optional[httpx.Client] = None) -> LoginStatusRespType:
"""Get current login status of the device.
Expand Down

0 comments on commit f7c9223

Please sign in to comment.