介于netmetr网页客户端和后端之间的辅助代理。添加令牌/密钥并调用后端。
项目描述
netmetr-proxy
介于netmetr网页客户端和后端之间的辅助代理。添加令牌/密钥并调用后端,用于
- 月度统计数据审核
- 未清洗的数据下载
browser netmetr-proxy backend
| | |
| GET | POST |
| --- month ----> | --- month-------> |
| + year | + year |
| | + token |
| | |
| <-- response -- | <-- response ---- |
| (zip or json) | |
| | |
它还添加了一些输入验证,因为后端会愉快地返回未来日期的空CSV文件,甚至是一个包含“非法参数”内容的.zip
文件。
依赖项
- Python >= 3.6
- pyaml >= 17
- flask >= 0.12
安装
$ virtualenv -p `which python3.6` .venv
$ source .venv/bin/activate
$ pip install netmetr_proxy
$ cp config.example.yml config.yml
$ $EDITOR config.yml # set backend url & access token
在生产环境中运行
uWSGI
$ uwsgi --master --single-interpreter --threads 2 --http :5000 -H .venv -w netmetr_proxy.server
GUnicorn
$ gunicorn -w 2 -k gevent --timeout 160 -n netmetr-proxy netmetr_proxy:server:app
使用方法
下载未清洗的公开数据
GET /opendata
month <int>
year <int>
返回可下载的opendata ZIP文件(NetMetr-opendata-dirty-{year}-{month}.zip
)。
月度统计数据审核
GET /approve
month <int>
year <int>
返回
GET /approve?year=2018&month=3
-> 200
{"success": true, "message": "Results for 2018-03 were successfully approved."}
如果再次调用
GET /approve?year=2018&month=3
-> 200
{"success": true, "message": "Results for 2018-03 were already approved before."}
错误响应
返回后端包装在JSON中的错误
GET /opendata?year=2018&month=1
# invalid key in config results in "ERROR: invalid key!" message
# from backend (with HTTP 200 for some reason…)
-> 403
{'error': 'invalid key!'}
返回HTTP 400和错误信息,对于缺失或无效的参数(格式错误或未来日期)
GET /opendata?year=2018&month=0
-> 400
{'error': 'Invalid date.'}
GET /opendata?year=0&month=march
-> 400
{'error': 'Invalid date.'}
GET /approve?year=2018
-> 400
{'error': 'Missing or invalid parameter (year, month).'}
对于任何不存在的端点返回404
GET /
-> 404
GET /foo
-> 404
开发
使用自动重新加载文件更改来启动服务器
$ FLASK_APP=netmetr_proxy/server.py FLASK_DEBUG=1 flask run
检查Python代码
$ flake8 --config=.flake8rc *py
许可证
GPLv3
项目详情
关闭
netmetr-proxy-0.1.3.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 091b4810565324ecd00ddaad3d006087c0e6436456326a34d0bfb83364041324 |
|
MD5 | 3b35c8edb26f7a3bcc1a68d563ef78e5 |
|
BLAKE2b-256 | 1c630251071bbafe8b03d34f6a263edf7e9560a381267971712ac9db903050aa |