def create_if_not_exist(token, app_name, app_storage_dict): """ Updates the keys and values of app storage with the provided dict for the given user without override. It only creates new keys if they doesn't exist. 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 """ u = validate_token(token) with storage.context("app_storage") as st: us = st.get(u.username) if not app_name in us: us[app_name] = {} d = json.loads(app_storage_dict) d.update(us[app_name]) us[app_name] = d st.put(u.username, us) return us[app_name]
def get_profile(token): """ Returns the profile json dict of the user Args: token (str): The auth token of the user. Returns: the json dict of the profile Raises: Invalid token - 1004 """ u = validate_token(token) with storage.context("profiles") as st: p = st.get(u.username) return p
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 change_password(token, new_password): """ Change the password of the user given by token. Args: token (str): The auth token of the user. new_password (str): The new password. Returns: True if successful Raises: Invalid token - 1004 """ with storage.context("users") as st: u = validate_token(token) st.put(u.username, u) return True
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 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 update_field(token, field, value): """ Updates the given field of the profile dict Args: token (str): The auth token of the user. field (str): The name of the field to update. value (str): The string with the value to update. Returns: True if was updated. Raises: Invalid token - 1004 """ u = validate_token(token) with storage.context("profiles") as st: p = st.get(u.username) p[field] = str(value) st.put(u.username, p) return True
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 update_profile(token, profile_dict): """ Receives a json dict with all fields and updates it into the actual profile Args: token (str): The auth token of the user. Returns: the json dict of the updated profile Raises: Invalid token - 1004 """ u = validate_token(token) with storage.context("profiles") as st: p = st.get(u.username) n_p = loads(profile_dict) p.update(n_p) st.put(u.username, p) return p
def put(token, app_name, app_storage_dict): """ Puts the app_storage_dict as the app_name storage of the given user. Args: token (str): The auth token of the user. app_name (str): The name with which save the storage. app_storage_dict (str): The json of the storage dict to save. Returns: True if ok. Raises: Invalid token - 1004 """ u = validate_token(token) with storage.context("app_storage") as st: us = st.get(u.username) us[app_name] = json.loads(app_storage_dict) st.put(u.username, us) return True
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 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 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 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 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]
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