Skip to content

Commit

Permalink
update docs language to english
Browse files Browse the repository at this point in the history
  • Loading branch information
Eastwu5788 committed Jun 6, 2020
1 parent 170dacb commit 95274aa
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 85 deletions.
2 changes: 1 addition & 1 deletion docs/source/customize.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
自定义
Customize
===========

自定义响应
Expand Down
19 changes: 6 additions & 13 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
.. rst-class:: hide-header

.. rst-class:: hide-header

欢迎使用 pre-request 框架
=========================

.. image:: ../static/logo.jpg
:alt: pre-request: 请求预处理框架
:alt: pre-request: Validate flask request params
:align: center
:target: https://github.com/Eastwu5788/pre-request

欢迎您使用pre-request框架,pre-request致力于简化请求参数验证工作。为Flask的
网络请求参数验证提供了解决方案。
Welcome to pre-request's document. This framework is designed to validate params for Restful api request,
this framework can validate complex struct and field, including Cross Field, Cross Struct.

pre-request提供了非常方便的使用的方法,也提供了灵活的扩展接口方便您实现自定义的
业务逻辑。

User's Guide
==============

本文档将带您一步步使用pre-request框架
This part of the documentation will instruct how to use pre-request in your flask project

.. toctree::
:maxdepth: 2
Expand Down
39 changes: 11 additions & 28 deletions docs/source/installation.rst
Original file line number Diff line number Diff line change
@@ -1,40 +1,23 @@
pre-request 安装
Installation
====================

本部分文档将向您介绍如何安装pre-request框架到您的环境中
Python Version
-----------------

PIP安装
--------
We recommend using the latest version of python 3. pre-request supports Python 3.5 and newer and PyPy

pre-request 目前支持PyPi安装方式,您仅需使用pip命令即可安装成功

.. code-block:: text
Dependencies
---------------

pip install pre-request
This framework is designed for Flask, therefore Flask will be installed automatically when installing pre-request


源码安装
--------
Install pre-request with pip
------------------------------

如果您希望体验最新版本特性,您可以从github下载最新develop分支代码到您的本地直接安装
You can use the following command to install pre-request:

.. code-block:: text
python setup.py install
如果您希望在本地以开发模式安装则可以使用以下命令

.. code-block:: text
python setup.py develop
本地whl包
----------

当然您也可以在本地构建 whl 进行分发

.. code-block:: text
python setup.py sdist
pip wheel --wheel-dir=./dist ./
pip install pre-request
11 changes: 1 addition & 10 deletions docs/source/intro.rst
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
pre-request 规则介绍
Validate Rules
=====================

欢迎您使用pre-request框架,pre-request致力于简化请求参数验证工作。为Flask的
网络请求参数验证提供了解决方案。

pre-request提供了非常方便的使用的方法,也提供了灵活的扩展接口方便您实现自定义的
业务逻辑。

下面我们将挨个介绍pre-request支持的所有规则


location
-------------

Expand Down
67 changes: 34 additions & 33 deletions docs/source/quickstart.rst
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
快速开始
Quickstart
===============

本篇文档将向您介绍pre-request的详细使用方式,帮助您尽快将pre-request应用于您的项目。
Eager to get started? This page gives a good example to use pre-request. It assumes you already have pre-request installed
If you do not, head over to the Installation section.

Minimal Usage
Minimal Example
----------------

pre-request最简单的使用方式
A minimal example looks something like this:

.. code-block:: python
Expand All @@ -27,28 +28,28 @@ pre-request最简单的使用方式
return str(params)
上面的代码中发生了什么呢?
what happened in this code ?

1. 首先我们从 `pre-request` 库中引入全局 `pre` 对象,使用该对象来过滤用户参数
2. 我们定义了一个请求参数 `userId` 并规定该参数的目标类型为 `int` ,并且不允许为空
3. 使用 `@pre.catch(req_params)` 将参数规则赋值给装饰器,并装饰处理函数
4. 格式化后的参数置于 :class:`~flask.g` 中,同时尝试将格式化后的参数置于原函数的 `params` 参数中
1. Use `pre-request` library to import a global object `pre`
2. Define request params rule, `userId` must be type of `int` and required
3. Use `@pre.catch(req_params)` to filter input value
4. Use `~flask.g` or `def hello_world(params)` to get formatted input value


`pre` 单例介绍
---------------
`pre` singleton
----------------

pre-request 提供了一个全局单例 `pre` 对象,我们可以通过操作 `pre` 来修改pre-request的运行参数。
pre-request support global singleton object, we can use this object to update runtime params

- `pre.fuzzy` 指示pre-request是否对参数验证错误原因进行模糊化处理。防止暴露过多信息
- `pre.sore_key` 自定义pre-request解析完成的参数的存储key
- `pre.content_type` 设置pre-request的错误响应格式,目前支持 `application/json` `text/html`
- `pre.fuzzy` pre-request will fuzzy error message to avoid expose sensitive information
- `pre.sore_key` use another params to store formatted request info
- `pre.content_type` pre-request will response html or json error message, use `application/json` or `text/html`


`Flask` 扩展支持
`Flask` Extension
------------------

pre-request 提供了基于Flask扩展方式配置参数的能力。
pre-request support flask extension configuration to load params.


.. code-block:: python
Expand All @@ -62,12 +63,11 @@ pre-request 提供了基于Flask扩展方式配置参数的能力。
pre.init_app(app=app)
`@pre.catch` 装饰器介绍
-------------------------
use decorator
--------------

pre-request的过滤能力主要是通过`@pre.catch`装饰器来实现的
pre-request use decorator `pre.catch` to validate request params with special kind of method

我们可以使用 `@pre.catch` 过滤指定method的请求参数, 也可以指定针对特定请求 `method` 进行过滤

.. code-block:: python
Expand All @@ -81,32 +81,33 @@ pre-request的过滤能力主要是通过`@pre.catch`装饰器来实现的
def get_handler(params):
return str(params)
当然我们也为使用不同请求方式解析不同参数的情况提供了支持
we can also support validate different rule for different request params

.. code-block:: python
# 同时设置get和post的过滤参数
@app.route("/all", methods=['get', 'post'])
@pre.catch(get=get_field, post=post_field)
def all_handler(params):
return str(params)
`pre.parse` 解析函数介绍
---------------------------
Use parse
-------------

如果您觉得使用 `@pre.cache` 装饰器模式对您的代码侵入性太高,我们也提供了 `pre.parse()` 函数来解析用户入参
We can use function `pre.parse` instead of decorator `@pre.catch()`

.. code-block:: python
def get_handler():
params = pre.parse(get=req_params)
return str(params)
如果用户入参不符合规则要求,我们会抛出 `ParamsValueError` 异常,您可以在Flask框架中对所有此类型异常进行捕获并格式化返回

.. code-block:: python
args = {
"params": Rule(email=True)
}
@app.errorhandler(ParamsValueError)
def params_value_error(e):
return pre.fmt_resp(e)
@app.route("/index")
def example_handler():
rst = pre.parse(args)
return str(rst)

0 comments on commit 95274aa

Please sign in to comment.