From 8761af718d110a63a2a716e7ff94949717f315b4 Mon Sep 17 00:00:00 2001 From: Claas Augner Date: Tue, 19 Jan 2021 18:53:53 +0100 Subject: [PATCH] feat(batch): add --batch-limit --- README.md | 1 + app/app.py | 7 ++++++- main.py | 5 ++++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ec6564b..7c46c73 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,7 @@ docker-compose up -d --build | --port | Set port to bind the server to | `5000` | | --char-limit | Set character limit | `No limit` | | --req-limit | Set maximum number of requests per minute per client | `No limit` | +| --batch-limit | Set maximum number of texts to translate in a batch request | `No limit` | | --ga-id | Enable Google Analytics on the API client page by providing an ID | `No tracking` | | --debug | Enable debug environment | `False` | | --ssl | Whether to enable SSL | `False` | diff --git a/app/app.py b/app/app.py index f398b8b..79634f8 100644 --- a/app/app.py +++ b/app/app.py @@ -13,7 +13,7 @@ def get_remote_address(): return ip -def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False, frontend_language_source="en", frontend_language_target="en"): +def create_app(char_limit=-1, req_limit=-1, batch_limit=-1, ga_id=None, debug=False, frontend_language_source="en", frontend_language_target="en"): from app.init import boot boot() @@ -207,6 +207,11 @@ def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False, frontend_la batch = isinstance(q, list) + if batch and batch_limit != -1: + batch_size = len(q) + if batch_limit < batch_size: + abort(400, description="Invalid request: Request (%d) exceeds text limit (%d)" % (batch_size, batch_limit)) + if char_limit != -1: if batch: chars = sum([len(text) for text in q]) diff --git a/main.py b/main.py index 36e4ef6..47e7c43 100644 --- a/main.py +++ b/main.py @@ -10,6 +10,8 @@ parser.add_argument('--char-limit', default=-1, type=int, metavar="