projectal.profile

Projectal profiles serve as an all-purpose key-value store for clients to store any kind of data that interests them, targeting a specific entity uuId. Writes always override the old dictionary entirely.

While the profile API supports multiple entries under the same key combination, this library abstracts away that concept and ensures you only have a single dictionary with get/set functions for that key combination. This assumption may not hold if that key combination is modified externally.

View Source
"""
Projectal profiles serve as an all-purpose key-value store for clients
to store any kind of data that interests them, targeting a specific
entity uuId. Writes always override the old dictionary entirely.

While the profile API supports multiple entries under the same key
combination, this library abstracts away that concept and ensures
you only have a single dictionary with get/set functions for that
key combination. This assumption may not hold if that key combination
is modified externally.
"""

# TODO: maybe we can splice changes instead of overwrite?

from projectal import api


def get(category_key, folder_key, uuId):
    profiles = __profile_list(category_key, folder_key, uuId)
    if len(profiles) > 0:
        return profiles[0]
    else:
        # Current state is no key/value, so this is equivalent.
        # Will be created on next write.
        return {}


def set(category_key, folder_key, uuId, payload):
    # Get the first profile and save to it. If one doesn't exist, create it.
    profiles = __profile_list(category_key, folder_key, uuId)
    if len(profiles) > 0:
        payload['uuId'] = profiles[0]['uuId']
        return __profile_update(category_key, folder_key, uuId, payload)

    # Create and add in one step
    profile = __profile_add(category_key, folder_key, uuId, payload)
    payload['uuId'] = profile[0]['uuId']
    return payload


def __profile_add(category_key, folder_key, uuId, payload):
    endpoint = '/api/profile/{}/{}/{}/add'.format(category_key, folder_key, uuId)
    return api.__request('post', endpoint, [payload])


def __profile_delete(category_key, folder_key, uuId):
    endpoint = '/api/profile/{}/{}/{}/delete'.format(category_key, folder_key, uuId)
    return api.__request('post', endpoint)


def __profile_update(category_key, folder_key, uuId, payload):
    endpoint = '/api/profile/{}/{}/{}/update'.format(category_key, folder_key, uuId)
    return api.__request('put', endpoint, [payload])


def __profile_list(category_key, folder_key, uuId):
    endpoint = '/api/profile/{}/{}/{}/list'.format(category_key, folder_key, uuId)
    return api.__request('get', endpoint)
#   def get(category_key, folder_key, uuId):
View Source
def get(category_key, folder_key, uuId):
    profiles = __profile_list(category_key, folder_key, uuId)
    if len(profiles) > 0:
        return profiles[0]
    else:
        # Current state is no key/value, so this is equivalent.
        # Will be created on next write.
        return {}
#   def set(category_key, folder_key, uuId, payload):
View Source
def set(category_key, folder_key, uuId, payload):
    # Get the first profile and save to it. If one doesn't exist, create it.
    profiles = __profile_list(category_key, folder_key, uuId)
    if len(profiles) > 0:
        payload['uuId'] = profiles[0]['uuId']
        return __profile_update(category_key, folder_key, uuId, payload)

    # Create and add in one step
    profile = __profile_add(category_key, folder_key, uuId, payload)
    payload['uuId'] = profile[0]['uuId']
    return payload