Exemple #1
0
def list_paginated_items(api_key, items_per_page=25, collection=None,
                         hostname=None):
    "List all items, paginated by @items_per_page"

    if hostname:
        craftar.settings.MANAGEMENT_HOSTNAME = hostname

    offset = 0

    while True:
        print("- Retrieving items %s to %s..." % (offset + 1,
                                                  offset + items_per_page))

        item_list = craftar.get_item_list(
            api_key=api_key,
            limit=items_per_page,
            offset=offset,
            collection=collection,
        )

        for i in item_list:
            print("%s: %s" % (i["uuid"], i["name"]))
        offset += items_per_page
        if len(item_list) < items_per_page:
            print("No more items!")
            return
def list_paginated_items(api_key, items_per_page=25, collection=None, hostname=None):
    "List all items, paginated by @items_per_page"

    if hostname:
        craftar.settings.MANAGEMENT_HOSTNAME = hostname

    offset = 0

    while True:
        print("- Retrieving items %s to %s..." % (offset + 1, offset + items_per_page))

        item_list = craftar.get_item_list(api_key=api_key, limit=items_per_page, offset=offset, collection=collection)

        for i in item_list:
            print("%s: %s" % (i["uuid"], i["name"]))
        offset += items_per_page
        if len(item_list) < items_per_page:
            print("No more items!")
            return
def run_management(api_key, filename, items_per_page=5, hostname=None):
    """
    Basic example of listing, creating, updating and finally deleting
    each type of object, using CraftAR's Management API.
    """

    if hostname:
        craftar.settings.MANAGEMENT_HOSTNAME = hostname

    print("\n- Retrieving first %s collections..." % items_per_page)
    collection_list = craftar.get_collection_list(
        api_key,
        limit=items_per_page,
        offset=0,
    )
    for collection in collection_list:
        print("%s: %s" % (collection["uuid"], collection["name"]))

    print("\n- Creating collection...")
    collection = craftar.create_collection(
        api_key,
        name="My API collection",
    )
    collection_uuid = collection["uuid"]
    pprint(collection)

    print("\n- Retrieving collection...")
    collection = craftar.get_collection(
        api_key,
        uuid=collection_uuid,
    )
    pprint(collection)

    print("\n- Updating collection...")
    new_name = "My edited API collection"
    success = craftar.update_collection(
        api_key,
        uuid=collection_uuid,
        name=new_name,
    )
    print("Updated: %s, name: '%s'" % (success, new_name))

    print("\n- Retrieving collection...")
    collection = craftar.get_collection(
        api_key,
        uuid=collection_uuid,
    )
    pprint(collection)

    print("\n- Retrieving first %s tokens..." % items_per_page)
    token_list = craftar.get_token_list(
        api_key,
        limit=items_per_page,
        offset=0,
        collection=collection_uuid,
    )
    for t in token_list:
        print("Collection: %s Token: %s" % (t["collection"], t["token"]))

    print("\n- Creating token...")
    token = craftar.create_token(
        api_key,
        collection=collection_uuid,
    )
    token_id = token["token"]
    pprint(token)

    print("\n- Deleting token...")
    success = craftar.delete_token(
        api_key,
        token=token_id,
    )
    print("Deleted: %s" % success)

    print("\n- Retrieving first %s items..." % items_per_page)
    item_list = craftar.get_item_list(
        api_key,
        limit=items_per_page,
        offset=0,
        collection=collection_uuid,
    )
    for i in item_list:
        print("%s: %s" % (i["uuid"], i["name"]))

    print("\n- Creating item...")
    item = craftar.create_item(
        api_key,
        name="My API item",
        collection=collection_uuid,
        url="http://example.com",
        custom="Lorem Ipsum",
    )
    item_uuid = item["uuid"]
    pprint(item)

    print("\n- Retrieving first %s items..." % items_per_page)
    item_list = craftar.get_item_list(
        api_key,
        limit=items_per_page,
        offset=0,
        collection=collection_uuid,
    )
    for i in item_list:
        print("%s: %s" % (i["uuid"], i["name"]))

    print("\n- Retrieving item...")
    item = craftar.get_item(
        api_key,
        uuid=item_uuid,
    )
    pprint(item)

    print("\n- Updating item...")
    new_name = "My edited API item"
    success = craftar.update_item(
        api_key,
        uuid=item_uuid,
        name=new_name,
        custom="New Lorem Ipsum",
    )
    print("Updated: %s, name: '%s'" % (success, new_name))

    print("\n- Retrieving item...")
    item = craftar.get_item(
        api_key,
        uuid=item_uuid,
    )
    pprint(item)

    print("\n- Retrieving first %s images..." % items_per_page)
    image_list = craftar.get_image_list(
        api_key,
        limit=items_per_page,
        offset=0,
        item=item_uuid,
    )
    for i in image_list:
        print("%s: %s" % (i["uuid"], i["name"]))

    print("\n- Uploading image...")
    image = craftar.create_image(
        api_key,
        item=item_uuid,
        filename=filename,
    )
    image_uuid = image["uuid"]
    pprint(image)

    print("\n- Retrieving image...")
    image = craftar.get_image(
        api_key,
        uuid=image_uuid,
    )
    print("uuid: %s" % image["uuid"])
    pprint(image)

    print("\n- Deleting image...")
    success = craftar.delete_image(
        api_key,
        uuid=image_uuid,
    )
    print("Deleted: %s" % success)

    print("\n- Deleting item...")
    success = craftar.delete_item(
        api_key,
        uuid=item_uuid,
    )
    print("Deleted: %s" % success)

    print("\n- Deleting collection...")
    success = craftar.delete_collection(
        api_key,
        uuid=collection_uuid,
    )
    print("Deleted: %s" % success)