def validate_token(token): """parses the token if its a valid token return the User object otherwise raises an Invalid token exception """ with storage.context("users") as st: try: user, token_date = decode_token(token) except Exception: raise PyApiException(1004, "Invalid token") u = st.get(user) if not u or not u.is_token_valid(token): raise PyApiException(1004, "Invalid token") return u
def login(user, password): """ Login with the selected user and get the auth_token. Args: user (str): The username to login. password (str): The password of the user. Returns: str with the auth_token Raises: User doesn't exist - 1001 Wrong password - 1003 """ with storage.context("users") as st: u = st.get(user) if not u: raise PyApiException(1001, "User doesn't exist") if u.password != password: raise PyApiException(1003, "Wrong password") token = u.create_token() st.put(user, u) return token
def get_from(token, app_name, key): """ Gets the value of the specified key in the given user storage. Args: token (str): The auth token of the user. app_name (str): The name of the storage. key (str): The key of what to retrive from the storage. Returns: the retrived object. Raises: Invalid token - 1004 App storage doesn't exsit - 1201 Key doesn't exist - 1202 """ u = validate_token(token) with storage.context("app_storage") as st: us = st.get(u.username) if not app_name in us: raise PyApiException(1201, "App storage doesn't exist") if not key in us[app_name]: raise PyApiException(1202, "Key doesn't exist") return us[app_name][key]
def rem_from(token, app_name, key): """ Removes the value from the given user app storage. Args: token (str): The auth token of the user. app_name (str): The name of the storage. key (str): The key of what you want to remove. Returns: True if it was deleted. Raises: Invalid token - 1004 App storage doesn't exsit - 1201 Key doesn't exist - 1202 """ u = validate_token(token) with storage.context("app_storage") as st: us = st.get(u.username) if not app_name in us: raise PyApiException(1201, "App storage doesn't exist") if not key in us[app_name]: raise PyApiException(1202, "Key doesn't exist") del us[app_name][key] st.put(u.username, us) return True
def get_field(token, field): """ Returns the given field of the profile dict in json format. Args: token (str): The auth token of the user. field (str): The name of the field. Returns: str with the value. Raises: Field doesn't exist - 1101 Invalid token - 1004 """ u = validate_token(token) with storage.context("profiles") as st: p = st.get(u.username) if not field in p: raise PyApiException(1101, "Field doesn't exist") return str(p[field])
def get(token, app_name): """ Returns the json string of the given user app storage. Args: token (str): The auth token of the user. app_name (str): The name of the storage to return. Returns: json dict of all the storage. Raises: Invalid token - 1004 App storage doesn't exsit - 1201 """ u = validate_token(token) with storage.context("app_storage") as st: us = st.get(u.username) if not app_name in us: raise PyApiException(1201, "App storage doesn't exist") return us[app_name]
def register_user(user, password): """ Create a new user if doesn't already exist. Args: user (str): The username for the new user. password (str): The password for the user. Returns: str with the auth_token Raises: User already exists - 1002 """ with storage.context("users") as st: if st.get(user): raise PyApiException(1002, "User already exist") u = User(user, password) token = u.create_token() st.put(user, u) return token
def rem(token, app_name): """ Deletes the given app storage of the given user. Args: token (str): The auth token of the user. app_name (str): The name of the storage to delete. Returns: True if was deleted. Raises: Invalid token - 1004 App storage doesn't exsit - 1201 """ u = validate_token(token) with storage.context("app_storage") as st: us = st.get(u.username) if not app_name in us: raise PyApiException(1201, "App storage doesn't exist") del us[app_name] st.put(u.username, us) return True
def put_to(token, app_name, key, value): """ Sets the value of the given key in the given user storage. Args: token (str): The auth token of the user. app_name (str): The name of the storage. key (str): The key where you want to store tha value. value (str): The value to set. Returns: True if was stored. Raises: Invalid token - 1004 App storage doesn't exsit - 1201 """ u = validate_token(token) with storage.context("app_storage") as st: us = st.get(u.username) if not app_name in us: raise PyApiException(1201, "App storage doesn't exist") us[app_name][key] = value st.put(u.username, us) return True
def update(token, app_name, app_storage_dict): """ Updates the keys and values of app storage with the provided dict for the given user. Args: token (str): The auth token of the user. app_name (str): The name of the storage to update. app_storage_dict (str): A json string with a dict with the values to update Returns: str with the updated storage. Raises: Invalid token - 1004 App storage doesn't exsit - 1201 """ u = validate_token(token) with storage.context("app_storage") as st: us = st.get(u.username) if not app_name in us: raise PyApiException(1201, "App storage doesn't exist") us[app_name].update(json.loads(app_storage_dict)) st.put(u.username, us) return us[app_name]