跳转到主要内容

介于netmetr网页客户端和后端之间的辅助代理。添加令牌/密钥并调用后端。

项目描述

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 (19.1 kB 查看哈希值)

上传时间

由以下提供支持