def register_handler(clss, endpoint_name, url, resource_id_name, resource_id_type): view_func = clss.as_view(endpoint_name) # Register endpoint name # Plain URL with optional resource ID after it (i.e. "/url" or "/url/resource_id"); GET, PUT, DELETE, HEAD, and OPTIONS allowed app.add_url_rule( url, defaults={"example_resource_id": None}, view_func=view_func, methods=["GET", "PUT", "DELETE", "HEAD", "OPTIONS"], ) # Plain URL (i.e. "/url"); POST uses this app.add_url_rule(url, view_func=view_func, methods=["GET", "POST", "HEAD", "OPTIONS"]) # URL with int resource ID following it (i.e. "/url/4") app.add_url_rule( "%s<%s:%s>" % (url, "int", "example_resource_id"), view_func=view_func, methods=["GET", "PUT", "DELETE", "HEAD", "OPTIONS"], ) # URL with range following it (i.e. "/url/1;3"), for retrieving a range of resources. Starts at 1 app.add_url_rule( "%s<%s:%s>;<%s:%s>" % (url, "int", "lower_cut", "int", "upper_cut"), view_func=view_func, methods=["GET", "HEAD", "OPTIONS"], )
def register_handler(clss, endpoint_name, url, resource_id_name, resource_id_type): ''' Default URL routing configuration, called by the API's router to register the resource to the API. Override to provide resource-specific URL routing schemes. Parameters passed in: endpoint_name: name of the API endpoint, str url: the relative URL path to the resource, str resource_id_name: the name of the ID for this REST resource, str resource_id_type: the resource ID data type (e.g. int, str...) By default, a reasonable set of routing schemes are supported for the appropriate HTTP methods. Ranged queries on GET are also supported by default, through "/url/1;5" format. ''' view_func = clss.as_view(endpoint_name) app.add_url_rule(url, defaults={resource_id_name: None}, view_func=view_func, methods=['GET', 'HEAD', 'OPTIONS']) app.add_url_rule('%s<%s:%s>;<%s:%s>' % (url, 'int', 'lower_cut', 'int', 'upper_cut'), view_func=view_func, methods=['GET', 'HEAD', 'OPTIONS']) app.add_url_rule(url, view_func=view_func, methods=['GET', 'POST', 'HEAD', 'OPTIONS']) app.add_url_rule('%s<%s:%s>' % (url, resource_id_type, resource_id_name), view_func=view_func, methods=['GET', 'PUT', 'DELETE', 'HEAD', 'OPTIONS'])
if not shop: raise APIException('Shop not found with {_id: %s}' % shop_id, 404) update = request.get_json() mongo.db.osaka.update_one({"_id": shop_id}, {"$set": update}) return jsonify({ 'code': 200, 'message': 'Update successful', 'data': update }) def delete(self, shop_id): result = mongo.db.osaka.delete_one({"_id": shop_id}) if result.deleted_count != 1: raise APIException('Deletion failed', 400, payload={'_id': str(shop_id)}) return jsonify({'code': 200, 'message': 'Deletion successful'}) one_coin_view = OneCoinAPI.as_view('one_coin_api') app.add_url_rule('/api/', defaults={'shop_id': None}, view_func=one_coin_view, methods=[ 'GET', 'POST', ]) app.add_url_rule('/api/<ObjectId:shop_id>', view_func=one_coin_view, methods=['GET', 'PATCH', 'DELETE'])
def register_handler(clss, endpoint_name, url, resource_id_name, resource_id_type): view_func = clss.as_view(endpoint_name) app.add_url_rule(url, view_func=view_func, methods=['GET', 'HEAD', 'OPTIONS'])