018/Flask-RESTful

/flask_restful/test/test_flask_restful.py
from flask import Flask, request
from flask_restful import Resource, Api, reqparse

import json

class Test(Resource):
def get(self):
parser = reqparse.RequestParser()
parser.add_argument("test", type=str, required=False, help="Must provide the test")
args = parser.parse_args()
print(args)

return {"test": args["test"]}

app = Flask(__name__)
api = Api(app)
api.add_resource(Test, "/test")

if __name__ == '__main__':
app.run(debug=True)

/README.md
# Flask-RESTful
A Flask wrapper for Flask-RESTful

Installation

```bash pip install flask-restful pip install flask-restful-extensions ```

Usage

```python from flask import Flask, request from flask_restful import Resource, Api, reqparse

class Test(Resource):
def get(self):
parser = reqparse.RequestParser()
parser.add_argument("test", type=str, required=False, help="Must provide the test")
args = parser.parse_args()
print(args)

return {"test": args["test"]}

app = Flask(__name__)
api = Api(app)
api.add_resource(Test, "/test")

if __name__ == '__main__':
app.run(debug=True)
```

Custom request parser

You can create a custom request parser by subclassing `RequestParser` and adding your own arguments.

```python
from flask_restful import RequestParser
from flask_restful import Resource, Api

class CustomParser(RequestParser):
def __init__(self, *args, **kwargs):
super(CustomParser, self).__init__(*args, **kwargs)

self.add_argument("test", type=str, required=False, help="Must provide the test")
self.add_argument("test2", type=str, required=False, help="Must provide the test2")

def parse_args(self, json=None, headers=None, environ=None, skip_none=True):
args = super(CustomParser, self).parse_args(json, headers, environ, skip_none)
print(args)
return args

app = Flask(__name__)
api = Api(app)

api.add_resource(Test, "/test", parser=CustomParser)

class Test(Resource):
def get(self):
print(request.args)
return {"test": request.args["test"], "test2": request.args["test2"]}

if __name__ == '__main__':
app.run(debug=True)
```

Custom response class

You can create a custom response class by subclassing `Response`.

```python
from flask import Flask, request
from flask_restful import Resource, Api, reqparse
from flask_restful import Response as FlaskResponse

class Response(FlaskResponse):
def __init__(self, data=None, status=None, headers=None, status_code=200):
super(Response, self).__init__(data, status, headers, status_code)
self.headers['X-Header'] = 'test'

app = Flask(__name__)
api = Api(app)
api.add_resource(Test, "/test")

class Test(Resource):
def get(self):
return {"test": "test"}, 200, {"X-Header": "test"}

if __name__ == '__main__':
app.run(debug=True)
```

Custom error handler

You can create a custom error handler by subclassing `Resource`.

```python
from flask import Flask, request
from flask_restful import Resource, Api, reqparse

class Test(Resource):
def get(self):
print(request.args)
return {"test": request.args["test"]}

def post(self):
parser = reqparse.RequestParser()
parser.add_argument("test", type=str, required=True, help="Must provide the test")
args = parser.parse_args()

if args["test"] == "test":
return {"test": args["test"]}, 200
else:
raise BadRequest("Test is not 'test'")

app = Flask(__name__)
api = Api(app)
api.add_resource(Test, "/test")

class BadRequest(Exception):
def __init__(self, message):
super(BadRequest, self).__init__(message)

if __name__ == '__main__':
app.run(debug=True)
```

Custom error response class

You can create a custom error response class by subclassing `Resource`.

```python
from flask import Flask, request
from flask_restful import Resource, Api, reqparse

class BadRequest(Resource):
def __init__(self, message):
self.message = message

def get(self):
return {"error": self.message}, 400

app = Flask(__name__)
api = Api(app)
api.add_resource(BadRequest, "/test", endpoint="error")

if __name__ == '__main__':
app.run(debug=True)
```

Custom resource response

You can create a custom resource response by subclassing `Resource`.

```python
from flask import Flask, request
from flask_restful import Resource, Api, reqparse

class Test(Resource):
def get(self):
return self._custom_response()

def _custom_response(self):
return {"test": "test"}, 200

app = Flask(__name__)
api = Api(app)
api.add_resource(Test, "/test")

if __name__ == '__main__':
app.run(debug=True)
```

Custom resource methods

You can create a custom resource method by subclassing `Resource`.

```python
from flask import Flask, request
from flask_restful import Resource, Api, reqparse

class Test(Resource):
def get(self):
print(request.args)