How to use the webserver.views.api.exceptions.APIBadRequest function in webserver

To help you get started, we’ve selected a few webserver examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github metabrainz / acousticbrainz-server / webserver / views / api / v1 / path.py View on Github external
list of index specifications that we will use.
    :query n_neighbours *Optional.* Integer determines the number of
        similar recordings that should be returned.
        Default is 200 recordings.
    :query metric: *Required.* String specifying the metric name to be
        used when finding the most similar recordings.
        The metrics available are shown here :py:const:`~similarity.metrics.BASE_METRICS`.

    :resheader Content-Type: *application/json*
    """
    offset = validate_offset(request.args.get("n"))
    metric, distance_type, n_trees, n_neighbours = _check_index_params(metric)
    try:
        index = AnnoyModel(metric, n_trees=n_trees, distance_type=distance_type, load_existing=True)
    except IndexNotFoundException:
        raise webserver.views.api.exceptions.APIBadRequest("Index does not exist with specified parameters.")

    try:
        ids, similar_recordings, distances = index.get_nns_by_mbid(str(mbid), offset, n_neighbours)
        return jsonify(similar_recordings)
    except NoDataFoundException:
        raise webserver.views.api.exceptions.APINotFound("No submission exists for the given (MBID, offset) combination.")
    except ItemNotFoundException:
        raise webserver.views.api.exceptions.APINotFound("The submission of interest is not indexed.")
github metabrainz / acousticbrainz-server / webserver / views / api / v1 / datasets.py View on Github external
"class_name": "Happy",
            "recordings": ["770cc467-8dde-4d22-bc4c-a42f91e"]
        }

    :reqheader Content-Type: *application/json*
    :
github metabrainz / acousticbrainz-server / webserver / views / api / v1 / path.py View on Github external
def _check_index_params(metric):
    if metric not in BASE_INDICES:
        raise webserver.views.api.exceptions.APIBadRequest("An index with the specified metric does not exist.")

    distance_type = request.args.get("distance_type")
    if not distance_type or distance_type not in BASE_INDICES[metric]:
        distance_type = "angular"

    n_trees = request.args.get("n_trees")
    if not n_trees or n_trees not in BASE_INDICES[metric][distance_type]:
        n_trees = 10

    n_neighbours = request.args.get("n_neighbours")
    try:
        n_neighbours = int(n_neighbours)
        if n_neighbours > 1000:
            raise ValueError
    except (ValueError, TypeError):
        n_neighbours = 200
github metabrainz / acousticbrainz-server / webserver / views / api / v1 / datasets.py View on Github external
{
            "name": "Sad"
        }

    :reqheader Content-Type: *application/json*
    :
github metabrainz / acousticbrainz-server / webserver / views / api / v1 / path.py View on Github external
def generate_similarity_path(mbid_from, mbid_to):
    """Generate a path from one mbid to another taking the next most 
    similar track on the way to the final target

    :query metric: *Required* Name of the metric for which the similarity path should
    be found. Defines the acoustic properties which are looked at when determining
    similar recordings.

    The metrics available are shown here :py:const:`~similarity.metrics.BASE_METRICS`.

    :resheader Content-Type: *application/json*
    """
    steps = request.args.get("steps", "10")
    metric = request.args.get("metric")
    if not metric in similarity.metrics.BASE_METRICS:
        raise webserver.views.api.exceptions.APIBadRequest("The metric parameter provided is not supported.")

    try:
        steps = int(steps)
    except ValueError:
        steps = 10
    try:
        path = similarity.path.get_path((mbid_from, 0), (mbid_to, 0), steps, metric)
    except NoDataFoundException:
        abort(404)
    except IndexError:
        abort(404)
    except SimilarityException:
        abort(404)

    return jsonify(path)
github metabrainz / acousticbrainz-server / webserver / views / api / v1 / path.py View on Github external
where mbid is a recording MBID and n is an optional integer offset. If the offset is
    missing or non-integer, it is replaced with 0
    Returns:
        a two-list of (mbid, offset) tuples representing the parsed query string.
    Raises:
        APIBadRequest if there is no recording_ids parameter, there are more than 2 MBIDs in the parameter,
        or the format of the mbids or offsets are invalid
    """
    recording_ids = request.args.get("recording_ids")

    if not recording_ids:
        raise webserver.views.api.exceptions.APIBadRequest("Missing `recording_ids` parameter")

    recordings = _parse_bulk_params(recording_ids)
    if not len(recordings) == 2:
        raise webserver.views.api.exceptions.APIBadRequest("Does not contain 2 recordings in the request")

    return recordings
github metabrainz / acousticbrainz-server / webserver / views / api / v1 / core.py View on Github external
the format
        mbid:n;mbid:n
    where mbid is a recording MBID and n is an optional integer offset. If the offset is
    missing or non-integer, it is replaced with 0

    Returns:
        a list of (mbid, offset) tuples representing the parsed query string.

    Raises:
        APIBadRequest if there is no recording_ids parameter, there are more than 25 MBIDs in the parameter,
        or the format of the mbids or offsets are invalid
    """
    recording_ids = request.args.get("recording_ids")

    if not recording_ids:
        raise webserver.views.api.exceptions.APIBadRequest("Missing `recording_ids` parameter")

    recordings = _parse_bulk_params(recording_ids)
    if len(recordings) > MAX_ITEMS_PER_BULK_REQUEST:
        raise webserver.views.api.exceptions.APIBadRequest("More than %s recordings not allowed per request" % MAX_ITEMS_PER_BULK_REQUEST)

    return recordings