Exemplo n.º 1
0
def route():
    if request.method == 'POST':
        if 'request' in request.args and str(request.args['request']).strip() == 'route':
            if 'application/json' in request.headers['Content-Type'] and request.is_json:

                all_args = request.get_json(silent=True)

                if all_args is None:
                    raise api_exceptions.InvalidUsage(status_code=500, error_code=4000)

                try:
                    schema(all_args)
                except MultipleInvalid as error:
                    raise api_exceptions.InvalidUsage(status_code=500, error_code=4000, message=str(error))
                # query stats
                if all_args['request'] == 'list':
                    r = Response(mimetype='application/json; charset=utf-8')
                    return r
                if 'geometry' not in all_args:
                    raise api_exceptions.InvalidUsage(status_code=500, error_code=4002)

                if 'filters' not in all_args['geometry']:
                    raise api_exceptions.InvalidUsage(status_code=500, error_code=4001)

                if 'data_source' in all_args['geometry']['filters']:
                    if 'cfd' in all_args['geometry']['filters']['data_source']:
                        pass
                    elif 'ec' in all_args['geometry']['filters']['data_source']:
                        pass
                    else:
                        raise api_exceptions.InvalidUsage(status_code=500, error_code=4009,
                                                          message=str("Please provide a valid data source"))
                else:
                    raise api_exceptions.InvalidUsage(status_code=500, error_code=4009)

                if 'fuel_type' not in all_args['geometry']['filters']:
                    raise api_exceptions.InvalidUsage(status_code=500, error_code=4003)

                are_required_geom_present(all_args['geometry'])
                all_args['geometry'] = parse_geometries(all_args['geometry'])
                route: dict = request_route_calculation(all_args)
                r = Response(json.dumps(route, sort_keys=True), mimetype='application/json; charset=utf-8')
                return r
            else:
                raise api_exceptions.InvalidUsage(status_code=500, error_code=4006)
        else:
            raise api_exceptions.InvalidUsage(status_code=500, error_code=5001)
    else:
        raise api_exceptions.InvalidUsage(status_code=500, error_code=5002)
Exemplo n.º 2
0
def are_required_geom_present(geometry):
    """
    Checks if enough geometry options are are present in request.
    :param geometry: Geometry parameters from  post request
    """
    if 'geojson' not in geometry:
        raise api_exceptions.InvalidUsage(status_code=500, error_code=4005)
Exemplo n.º 3
0
def check_validity(geojson):
    """
    Checks if geojson is valid, throws exception otherwise.
    :param geojson: geojson object
    :return: will return the geo
    """
    if geojson.is_valid:
        return geojson
    else:
        raise api_exceptions.InvalidUsage(status_code=500, error_code=4007, message='{} {}'.format(
            "geojson", geojson.is_valid))
Exemplo n.º 4
0
def parse_geometries(geometry):
    """
    Parses the geometries to geojson objects and checks them for validity.
    :param geometry: Request parameters from get or post request
    :return: returns processed request parameters
    """

    # parse geojson
    if 'geojson' in geometry:

        s = json.dumps(geometry['geojson'])
        # Convert to geojson.geometry
        try:
            g1 = geojson.loads(s)
        except ValueError as e:
            raise api_exceptions.InvalidUsage(status_code=500, error_code=4007, message=str(e))

        # Feed to shape() to convert to shapely.geometry.linestring.LineString
        # This will invoke its __geo_interface__ (https://gist.github.com/sgillies/2217756)
        try:
            g2 = shape(g1)
        except ValueError as e:
            raise api_exceptions.InvalidUsage(status_code=500, error_code=4007, message=str(e))

        # parse geom if valid
        geojson_obj = check_validity(g2)

        if geojson_obj.geom_type == 'LineString':
            pass
        else:
            # type not supported
            raise api_exceptions.InvalidUsage(error_code=4007,
                                              message='GeoJSON type {} not supported'.format(geojson_obj.geom_type),
                                              status_code=500)

        geometry['geom'] = geojson_obj

    return geometry
Exemplo n.º 5
0
def basic_data():
    if request.method == 'GET':
        if 'request' in request.args and str(request.args['request']).strip() == 'brands':
            if 'source' not in request.args or str(request.args['source']).strip() == 'all':
                brands: dict = request_brands(wikicar_brands=True, envirocar_brands=True, carfueldata_brands=True)
            elif 'source' in request.args and str(request.args['source']).strip() == 'cfd':
                brands: dict = request_brands(wikicar_brands=False, envirocar_brands=False, carfueldata_brands=True)
            elif 'source' in request.args and str(request.args['source']).strip() == 'ec':
                brands: dict = request_brands(wikicar_brands=False, envirocar_brands=True, carfueldata_brands=False)
            elif 'source' in request.args and str(request.args['source']).strip() == 'wikicar':
                brands: dict = request_brands(wikicar_brands=True, envirocar_brands=False, carfueldata_brands=False)
            else:
                raise api_exceptions.InvalidUsage(status_code=500, error_code=5001,
                                                  message='Wrong or missing filter argument for brands')
            r = Response(json.dumps(brands, sort_keys=True), mimetype='application/json; charset=utf-8')
            return r
        elif 'request' in request.args and str(request.args['request']).strip() == 'cars' and 'source' in request.args:
            source = str(request.args['source']).strip()
            brand = str(request.args['brand']).strip()
            if source == 'cfd':
                cars = request_car_ids(brand=brand, source=source)
                r = Response(json.dumps(cars, sort_keys=True), mimetype='application/json; charset=utf-8')
                return r
            else:
                raise api_exceptions.InvalidUsage(status_code=500, error_code=5001,
                                                  message=("Source {} not supported.".format(source)))
        elif 'request' in request.args and str(request.args['request']).strip() == 'cars':
            if 'brand' in request.args and str(request.args['brand']).strip() is not '':
                brand = request.args['brand']
                real_manufacturer = check_manufacturer(brand)
                if real_manufacturer is not '':
                    cars: dict = request_cars(brand=real_manufacturer)
                    r = Response(json.dumps(cars, sort_keys=True), mimetype='application/json; charset=utf-8')
                    return r
                else:
                    raise api_exceptions.InvalidUsage(status_code=500, error_code=5001,
                                                      message=("Couldn't find any brand matching: {}".format(brand)))
            else:
                raise api_exceptions.InvalidUsage(status_code=500, error_code=5001,
                                                  message=("Couldn't find any cars for the request"))

        elif 'request' in request.args and str(request.args['request']).strip() == 'categories':
            categories = request_categories()
            r = Response(json.dumps(categories, sort_keys=True), mimetype='application/json; charset=utf-8')
            return r
        else:
            raise api_exceptions.InvalidUsage(status_code=500, error_code=5001)

    else:
        raise api_exceptions.InvalidUsage(status_code=500, error_code=5002)