Skip to main content

Helper proxy to sit between netmetr web client and backend. Adds a token/key and calls backend.

Project description

NetMetr netmetr-proxy

Helper proxy to sit between netmetr web client and backend. Adds a token/key and calls backend for:

  • Monthly stats approval
  • Uncleaned data download
browser       netmetr-proxy          backend
   |                 |                   |
   |     GET         |     POST          |
   | --- month ----> | --- month-------> |
   |   + year        |   + year          |
   |                 |   + token         |
   |                 |                   |
   | <-- response -- | <-- response ---- |
   |   (zip or json) |                   |
   |                 |                   |

It also adds some input validation, since the backend will happily return empty CSVs for future dates or even a .zip file with plaintext "illegal parameters" content.

Dependencies

  • Python >= 3.6
  • pyaml >= 17
  • flask >= 0.12

Running the server

Before the first run:

$ virtualenv -p `which python3.6` .venv
$ source .venv/bin/activate
$ pip install -r requirements.txt
$ cp config.example.yml config.yml
$ $EDITOR config.yml # set backend url & access token

Trying to run in console to check everything is OK:

$ python server.py
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
# in another window:
$ curl -I "http://127.0.0.1:5000/opendata?year=2018&month=3"
HTTP/1.0 200 OK
Content-disposition: attachment; filename=NetMetr-opendata-dirty-2018-03.zip
Content-Length: 11304559
…
# looks good :)

Running in production

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

Usage

Downloading uncleaned open data

GET /opendata
month <int>
year <int>

Returns opendata ZIP for download (NetMetr-opendata-dirty-{year}-{month}.zip).

Monthly stats approval

GET /approve
month <int>
year <int>

Returns:

GET /approve?year=2018&month=3
-> 200
   {"success": true, "message": "Results for 2018-03 were successfully approved."}

if called again:

GET /approve?year=2018&month=3
-> 200
   {"success": true, "message": "Results for 2018-03 were already approved before."}

Error responses

Returns errors from backend wrapped in 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!'}

Returns HTTP 400 and an error message for missing invalid params (bad format or future date):

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).'}

Returns 404 for any non-existing endpoint:

GET /
-> 404
GET /foo
-> 404

Development

Starting server with auto reload on file changes:

$ FLASK_APP=server.py FLASK_DEBUG=1 flask run

Linting Python code:

$ flake8 --config=.flake8rc *py

License

GPLv3

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

netmetr-proxy-0.1.tar.gz (19.5 kB view details)

Uploaded Source

File details

Details for the file netmetr-proxy-0.1.tar.gz.

File metadata

  • Download URL: netmetr-proxy-0.1.tar.gz
  • Upload date:
  • Size: 19.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.10

File hashes

Hashes for netmetr-proxy-0.1.tar.gz
Algorithm Hash digest
SHA256 8c7546f8d4990b39a3c145f0d21d7c6e4559c7e7d4733ded7d7bcd62c0ead683
MD5 cadf764f82688f39cc25fd7f799a5a50
BLAKE2b-256 ac5b7f362c7996ce643e7d5c2fd8cc9054433daa9a5f4543a8e1ad4d227700dd

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page