feat(batch): add --batch-limit

This commit is contained in:
Claas Augner 2021-01-19 18:53:53 +01:00
parent 2c5eba14b4
commit 8761af718d
3 changed files with 11 additions and 2 deletions

View file

@ -92,6 +92,7 @@ docker-compose up -d --build
| --port | Set port to bind the server to | `5000` | | --port | Set port to bind the server to | `5000` |
| --char-limit | Set character limit | `No limit` | | --char-limit | Set character limit | `No limit` |
| --req-limit | Set maximum number of requests per minute per client | `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` | | --ga-id | Enable Google Analytics on the API client page by providing an ID | `No tracking` |
| --debug | Enable debug environment | `False` | | --debug | Enable debug environment | `False` |
| --ssl | Whether to enable SSL | `False` | | --ssl | Whether to enable SSL | `False` |

View file

@ -13,7 +13,7 @@ def get_remote_address():
return ip 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 from app.init import boot
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) 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 char_limit != -1:
if batch: if batch:
chars = sum([len(text) for text in q]) chars = sum([len(text) for text in q])

View file

@ -10,6 +10,8 @@ parser.add_argument('--char-limit', default=-1, type=int, metavar="<number of ch
help='Set character limit (%(default)s)') help='Set character limit (%(default)s)')
parser.add_argument('--req-limit', default=-1, type=int, metavar="<number>", parser.add_argument('--req-limit', default=-1, type=int, metavar="<number>",
help='Set maximum number of requests per minute per client (%(default)s)') help='Set maximum number of requests per minute per client (%(default)s)')
parser.add_argument('--batch-limit', default=-1, type=int, metavar="<number of texts>",
help='Set maximum number of texts to translate in a batch request (%(default)s)')
parser.add_argument('--ga-id', type=str, default=None, metavar="<GA ID>", parser.add_argument('--ga-id', type=str, default=None, metavar="<GA ID>",
help='Enable Google Analytics on the API client page by providing an ID (%(default)s)') help='Enable Google Analytics on the API client page by providing an ID (%(default)s)')
parser.add_argument('--debug', default=False, action="store_true", parser.add_argument('--debug', default=False, action="store_true",
@ -27,6 +29,7 @@ args = parser.parse_args()
if __name__ == "__main__": if __name__ == "__main__":
app = create_app(char_limit=args.char_limit, app = create_app(char_limit=args.char_limit,
req_limit=args.req_limit, req_limit=args.req_limit,
batch_limit=args.batch_limit,
ga_id=args.ga_id, ga_id=args.ga_id,
debug=args.debug, debug=args.debug,
frontend_language_source=args.frontend_language_source, frontend_language_source=args.frontend_language_source,