A small rest framework
Project description
restfy
A small rest framework.
Instalation
pip install restfy
Usage
Minimal usage
from restfy import Application, Server
from restfy.http import Response, Request
async def handler(request: Request) -> Response:
data = 'restfy'
return Response(data)
app = Application()
app.add_route('/', handler, method='GET')
server = Server(app)
server.run()
Adding route by router decorator
A route can be added by decorating handler function with .get, .post, .put, .delete or .path methods.
from restfy import Application, Router, Request, Response
# By using router object
router = Router()
@router.get('')
async def handler(request: Request) -> Response:
ret = {}
return Response(ret)
app = Application()
app.register_router('', router=router)
# Or by app router decorator
@app.post('')
async def other_handler(request: Request) -> Response:
ret = request.data
return Response(ret)
...
Receiving JSON data and args from request object
By default, Restfy will try to deserialize body data into request object data property by content type header information. You can prefer deserialize body value manually or using dict request method. For this case, it's recommended to disable the process of deserialize by parsing False to prepare_request_data in Application.
The querystring values are deserialized using args() request method. The raw querystring is on query request attribute.
...
from restfy.http import Response, Request
...
async def handler(request: Request) -> Response:
data = request.data # pre-deserialized body data before execute handler.
args = request.args() # A dict with querystring values.
data = request.dict() # Try deserialize body data in a dictionary. Recommended to use request.data instead.
query = request.query
...
Parsing value in url path.
If a path item is inside {}, its a variable. The handler function should have a parameter with the same name.
from restfy import Application, Server
from restfy.http import Response, Request
async def handler(request: Request, pk: int) -> Response:
data = f'restfy: pk {pk}'
return Response(data)
app = Application()
app.add_route('/{pk}', handler, method='GET')
...
Returning a response with custom
By default, the Response class set 200 as status code. The content type is identified dynamically by data type. These parameters may be changed instancing the response passing status, headers and content_type parameters.
from restfy.http import Response, Request
...
async def handler(request: Request, pk: int) -> Response:
data = f'<b>restfy: pk {pk}</b>'
headers = {
'Content-Type': 'text/html'
}
return Response(data, status=400, headers=headers)
...
async def handler_other(request: Request, pk: int) -> Response:
data = f'<b>restfy: pk {pk}</b>'
return Response(data, status=400, content_type='text/html')
...
Middlewares
Restfy uses middleware creating a class with .exec() method. The parameter request must be passed into exec method.
The Application has the method .register_middleware() to register middlewares. The register order is the same order of execution.
from restfy import Application, Middleware
class DefaultMiddleware(Middleware):
async def exec(self, request):
# Do something with request object
...
response = await self.forward(request)
...
# Do something with response object
return response
app = Application()
app.register_middleware(DefaultMiddleware)
HTTP client requests
With http module, you can realize asynchronous requests to other services. The most simple request can be seen below.
from restfy import http
...
res = await http.get('https://someserver.api/endpoint')
print(res.status)
# 200
In addition to the get function, others functions are available like post(), put(), delete() and patch(). If other request method are necessary, we can use request() function passing method param. The next example shows a more complex example.
from restfy import http
...
url = 'https://someserver.api/endpoint'
data = {
'name': 'Nick',
'surname': 'Lauda'
}
headers = {
'Content-Type': 'application/json'
}
...
res = await http.post(url=url, data=data, headers=headers)
print(res.status)
# 200
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.