diff --git a/fishy/version.txt b/fishy/version.txt index d2d81b7..aa49d36 100644 --- a/fishy/version.txt +++ b/fishy/version.txt @@ -1 +1 @@ -0.5.18 \ No newline at end of file +0.5.19 \ No newline at end of file diff --git a/fishy/web/__init__.py b/fishy/web/__init__.py index 1ab3672..2a894d1 100644 --- a/fishy/web/__init__.py +++ b/fishy/web/__init__.py @@ -1,3 +1,3 @@ from .urls import get_notification_page, get_terms_page -from .web import (get_session, is_subbed, register_user, send_fish_caught, +from .web import (get_session, is_subbed, _register_user, send_fish_caught, send_notification, sub, unsub) diff --git a/fishy/web/decorators.py b/fishy/web/decorators.py index 9713d16..678d0ab 100644 --- a/fishy/web/decorators.py +++ b/fishy/web/decorators.py @@ -2,8 +2,11 @@ import logging import traceback from functools import wraps +from fishy.web import web + def uses_session(f): + """directly returns none if it couldn't get session, instead of running the function""" @wraps(f) def wrapper(*args, **kwargs): from .web import get_session @@ -21,6 +24,9 @@ def fallback(default): # noinspection PyBroadException @wraps(f) def wrapper(*args, **kwargs): + if not web.is_online(): + return default + try: return f(*args, **kwargs) except Exception: diff --git a/fishy/web/web.py b/fishy/web/web.py index de3cd78..59de536 100644 --- a/fishy/web/web.py +++ b/fishy/web/web.py @@ -10,6 +10,11 @@ from . import urls from .decorators import fallback, uses_session _session_id = None +_online = True + + +def is_online(): + return _online @fallback(-1) @@ -44,7 +49,7 @@ def logout(): @fallback(None) -def register_user(): +def _register_user(): ip = get_ip(GoogleDnsProvider) body = {"ip": ip, "apiversion": apiversion} response = requests.post(urls.user, json=body) @@ -111,7 +116,12 @@ def unsub(): def get_session(lazy=True): - global _session_id + """ + this doesn't have @fallback as this doesn't actually make any web calls directly + this web call needs to be the first thing to be called, as it sets the online status + todo maybe shift this to web.init() or something to signify that + """ + global _session_id, _online # lazy loading logic if lazy and _session_id is not None: @@ -122,22 +132,22 @@ def get_session(lazy=True): # then create session if uid: - _session_id, online = _create_new_session(uid) + _session_id, _online = _create_new_session(uid) # if not, create new id then try creating session again else: - uid = register_user() + uid = _register_user() config.set("uid", uid, True) logging.debug(f"New User, generated new uid: {uid}") if uid: - _session_id, online = _create_new_session(uid) + _session_id, _online = _create_new_session(uid) else: - online = False + _online = False - # when the user is already registered but session is not created as uid is not found - if online and not _session_id: + # when the user is already registered but session is not created as uid is not found by the server + if _online and not _session_id: logging.error("user not found, generating new uid.. contact dev if you don't want to loose data") - new_uid = register_user() - _session_id, online = _create_new_session(new_uid) + new_uid = _register_user() + _session_id, _online = _create_new_session(new_uid) config.set("uid", new_uid, True) config.set("old_uid", uid, True) @@ -147,7 +157,7 @@ def get_session(lazy=True): @fallback((None, False)) def _create_new_session(uid): body = {"uid": uid, "apiversion": apiversion} - response = requests.post(urls.session, json=body) + response = requests.post(urls.session, json=body, timeout=10) if response.status_code == 405: return None, True