def get_asset_metadata_operator()

in source/dataplaneapi/app.py [0:0]


def get_asset_metadata_operator(asset_id, operator_name):
    """
    Retrieves specified operator metadata for a given asset.

    If the results are paginated, the first call to this method will include a cursor in the response.

    To receive the remaining paginated data, make additional calls to this endpoint and pass in the value of the cursor
    response key as the value for the cursor query parameter.

    Once all results have been retrieved, no cursor key will be present in the response.

    Returns:

        Metadata that a specific operator created

        .. code-block:: python

            {
                "asset_id": asset_id,
                "operator": operator_name,
                "cursor": encoded_cursor_value,
                "results": first_page_data
            }

    Raises:
        ChaliceViewError - 500
    """
    logging.info(
        "Returning {operator} metadata for asset: {asset_id}".format(asset_id=asset_id, operator=operator_name))
    table_name = dataplane_table_name

    # Check if cursor is present, if not this is the first request

    if app.current_request.query_params is None:
        first_call = True
    else:
        # TODO: Do I want to add another check here?
        cursor = app.current_request.query_params['cursor']
        first_call = False

    if first_call is True:
        projection_expression = "#attr"
        expression_attribute_names = {"#attr": operator_name}
        try:
            table = dynamo_resource.Table(table_name)
            asset_item = table.get_item(
                Key={
                    'AssetId': asset_id
                },
                ProjectionExpression=projection_expression,
                ExpressionAttributeNames=expression_attribute_names
            )
        except ClientError as e:
            error = e.response['Error']['Message']
            logger.error(
                "Exception occurred while retreiving metadata for {asset}: {e}".format(asset=asset_id, e=error))
            raise ChaliceViewError("Unable to retrieve metadata: {e}".format(e=error))
        except Exception as e:
            logger.error(
                "Exception occurred while retreiving metadata for {asset}: {e}".format(asset=asset_id, e=e))
            raise ChaliceViewError("Unable to retrieve metadata: {e}".format(e=e))
        else:
            if "Item" in asset_item:
                pointer = asset_item["Item"][operator_name][0]["pointer"]
                s3_object = read_metadata_from_s3(dataplane_s3_bucket, pointer)
                # TODO: Add error handling for s3 call
                operator_metadata = json.loads(s3_object["Object"])
                if is_metadata_list(operator_metadata) is True:
                    first_page_num = 0
                    next_page_num = first_page_num + 1

                    first_page_data = operator_metadata[first_page_num]

                    if next_page_valid(operator_metadata, next_page_num) is True:
                        next = {operator_name: pointer, "page": next_page_num}
                        # TODO: Do I really need this for getting results of a specific operator?
                        remaining = [operator_name]
                        new_cursor = build_cursor_object(next, remaining)
                        response = {"asset_id": asset_id, "operator": operator_name,
                                    "cursor": encode_cursor(new_cursor),
                                    "results": first_page_data}
                    else:
                        response = {"asset_id": asset_id, "operator": operator_name, "results": first_page_data}
                    return response
                else:
                    page = operator_metadata
                    response = {"asset_id": asset_id, "operator": operator_name, "results": page}
                    return response
            # TODO: Add else block to handle not finding an item
    else:
        decoded_cursor = decode_cursor(cursor)

        pointer = decoded_cursor["next"][operator_name]
        page_num = decoded_cursor["next"]["page"]

        next_page_num = page_num + 1

        # TODO: Add error handling for s3 call
        s3_object = read_metadata_from_s3(dataplane_s3_bucket, pointer)

        operator_metadata = json.loads(s3_object["Object"])
        page_data = operator_metadata[page_num]

        if next_page_valid(operator_metadata, next_page_num) is True:
            next = {operator_name: pointer, "page": next_page_num}
            remaining = [operator_name]
            new_cursor = build_cursor_object(next, remaining)
            response = {"asset_id": asset_id, "operator": operator_name,
                        "cursor": encode_cursor(new_cursor), "results": page_data}
        else:
            response = {"asset_id": asset_id, "operator": operator_name, "results": page_data}

        return response