Skip to content

Commit 7370263

Browse files
add signup module
1 parent a290f54 commit 7370263

File tree

5 files changed

+93
-5
lines changed

5 files changed

+93
-5
lines changed

sonicbit/base.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
from requests import Session
22

3+
from sonicbit.constants import Constants
4+
35

46
class SonicBitBase:
57
"""Base class for all SonicBit modules."""
68

79
session = Session
10+
11+
12+
@staticmethod
13+
def url(path: str) -> str:
14+
return f"{Constants.API_BASE_URL}{path}"

sonicbit/modules/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
from .remote_download import RemoteDownload
44
from .torrent import Torrent
55
from .user import User
6+
from .signup import Signup
67

78
__all__ = [
89
"Auth",
910
"File",
1011
"RemoteDownload",
1112
"Torrent",
1213
"User",
14+
"Signup",
1315
]

sonicbit/modules/auth.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,3 @@ def login(email: str, password: str) -> AuthResponse:
4949
)
5050

5151
return AuthResponse.from_response(response)
52-
53-
@staticmethod
54-
def url(path: str) -> str:
55-
return f"{Constants.API_BASE_URL}{path}"

sonicbit/modules/signup.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
from sonicbit.base import SonicBitBase
2+
from sonicbit.error.error import SonicbitError
3+
import requests
4+
from sonicbit.constants import Constants
5+
import logging
6+
7+
8+
logger = logging.getLogger(__name__)
9+
10+
11+
class Signup(SonicBitBase):
12+
@staticmethod
13+
def signup(name: str, email: str, password: str, otp_callback: callable = None):
14+
"""Signup to SonicBit."""
15+
16+
data = {
17+
"name": name,
18+
"email": email,
19+
"password": password,
20+
}
21+
22+
logger.debug(f"Signing up as {email}")
23+
response = requests.post(
24+
SonicBitBase.url("/user/register"), json=data, headers=Constants.API_HEADERS
25+
).json()
26+
27+
28+
if response["success"] == True:
29+
if otp_callback:
30+
otp = otp_callback(email)
31+
Signup.submit_otp(otp)
32+
return True
33+
else:
34+
raise SonicbitError(f"Failed to signup: {response.get('msg', response)}")
35+
36+
@staticmethod
37+
def submit_otp(otp: str):
38+
"""Submit OTP to SonicBit."""
39+
40+
otp = otp.strip()
41+
42+
if not otp.isdigit() and len(otp) == 6:
43+
raise SonicbitError("OTP must be a 6 digit number")
44+
45+
data = {"code": otp.strip(), "type": "registration", "platform": "Web_Dash_V4"}
46+
47+
logger.debug(f"Submitting OTP {otp}")
48+
response = requests.post(
49+
SonicBitBase.url("/verification/code"),
50+
json=data,
51+
headers=Constants.API_HEADERS,
52+
).json()
53+
54+
55+
if response["success"] == True:
56+
token = response['data']['token']
57+
Signup._complete_tutorial(token)
58+
return True
59+
else:
60+
raise SonicbitError(f"Failed to submit OTP: {response.get('msg', response)}")
61+
62+
@staticmethod
63+
def _complete_tutorial(token: str):
64+
"""Complete signup."""
65+
66+
data = {"delete": True}
67+
68+
headers = Constants.API_HEADERS
69+
headers['Authorization'] = f"Bearer {token}"
70+
71+
logger.debug(f"Marking tutorial as completed")
72+
response = requests.post(
73+
SonicBitBase.url("/user/account/welcome_completed"),
74+
json=data,
75+
headers=headers
76+
).json()
77+
78+
79+
if response.get('success') == True:
80+
return True
81+
else:
82+
raise SonicbitError(f"Failed to complete signup: {response.get('message', response.get('msg', response))}")

sonicbit/sonicbit.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
from sonicbit.modules.remote_download import RemoteDownload
66
from sonicbit.modules.torrent import Torrent
77
from sonicbit.modules.user import User
8+
from sonicbit.modules.signup import Signup
89

910

10-
class SonicBit(Auth, User, File, Torrent, RemoteDownload):
11+
class SonicBit(Auth, Signup, User, File, Torrent, RemoteDownload):
1112
def __init__(
1213
self,
1314
email: str,

0 commit comments

Comments
 (0)