- Python Microservices Development
- Tarek Ziadé
- 464字
- 2021-07-02 18:54:22
Response
In the previous examples, we've used the jsonify() function, which creates a Response object from the mapping returned by the view.
The Response object is, technically, a standard WSGI application you could use directly. It's wrapped by Flask, and called with the WSGI's environ, and the start_response function is received from the web server.
When Flask picks a view via its URL mapper, it expects it to return a callable object that can receive the environ and start_response arguments.
In case the returned value is not a callable, Flask will try to convert it into a Response object if it's one of the following cases:
- str: The data gets encoded as UTF-8 and used as the HTTP response body.
- bytes/bytesarray: Used as the body.
- A (response, status, headers) tuple: Where response can be a Response object or one of the previous types. status is an integer value that overwrites the response status, and headers is a mapping that extends the response headers.
- A (response, status) tuple: Like the previous one, but without specific headers
- A (response, headers) tuple: Like the preceding one, but with just extra headers.
Any other case will lead to an exception.
In most cases, when building microservices, we'll use the built-in jsonify() function, but in case you need your endpoints to produce another content type, creating a function that will convert the generated data into a Response class is easy enough.
Here's an example with YAML: the yamlify() function will return a (response, status, headers) tuple, which will be converted by Flask into a proper Response object.
from flask import Flask
import yaml # requires PyYAML
app = Flask(__name__)
def yamlify(data, status=200, headers=None):
_headers = {'Content-Type': 'application/x-yaml'}
if headers is not None:
_headers.update(headers)
return yaml.safe_dump(data), status, _headers
@app.route('/api')
def my_microservice():
return yamlify(['Hello', 'YAML', 'World!'])
if __name__ == '__main__':
app.run()
The way Flask handles requests can be summarized as follows:
- When the application starts, any function decorated with @app.route() is registered as a view, and stored into the app.url_map.
- A call is dispatched to the right view depending on its endpoint and method.
- A Request object is created in a thread-safe thread-local execution context.
- A Response object wraps the content to send back.
These four steps are roughly all you need to know to start building apps using Flask. The next section will summarize the most important built-in features that Flask offers alongside this request-response mechanism.
- Learning Microsoft Windows Server 2012 Dynamic Access Control
- Vue 3移動Web開發與性能調優實戰
- C語言程序設計實踐教程(第2版)
- Learning C# by Developing Games with Unity 2020
- ThinkPHP 5實戰
- 云原生Spring實戰
- The Complete Coding Interview Guide in Java
- Hands-On Full Stack Development with Go
- Unity Character Animation with Mecanim
- Learning TypeScript
- Python趣味創意編程
- Python程序設計教程
- Eclipse開發(學習筆記)
- 軟件測試項目實戰之功能測試篇
- Mastering MeteorJS Application Development