|
1 | | -import traceback |
2 | | - |
3 | | -from flask import Blueprint, jsonify, request |
| 1 | +from flask import Blueprint |
4 | 2 | from werkzeug.exceptions import NotFound, InternalServerError, TooManyRequests |
5 | 3 |
|
6 | | -from mldictionary_api.models import RedisRequests |
7 | | -from mldictionary_api.const import ( |
8 | | - API_PREFIX, |
9 | | - DICTIONARIES, |
10 | | - LOCAL_ADDR, |
11 | | - TOTAL_REQUESTS_ALLOW, |
12 | | - TTL_REQUEST, |
| 4 | +from mldictionary_api.const import API_PREFIX |
| 5 | +from mldictionary_api.resources.response import ResponseAPI |
| 6 | +from mldictionary_api.resources.const import ( |
| 7 | + ENGLISH_REPR, |
| 8 | + ENGLISH_TO_PORTUGUESE_REPR, |
| 9 | + PORTUGUESE_REPR, |
| 10 | + PORTUGUESE_TO_ENGLISH, |
| 11 | + SPANISH_REPR, |
13 | 12 | ) |
14 | 13 |
|
15 | | - |
16 | 14 | api = Blueprint('mldictionary_api', __name__, url_prefix=API_PREFIX) |
17 | 15 |
|
18 | 16 |
|
19 | 17 | @api.route('/dictionary/en/<word>/') |
| 18 | +def english(word: str): |
| 19 | + return ResponseAPI().get_meanings(ENGLISH_REPR, word) |
| 20 | + |
| 21 | + |
20 | 22 | @api.route('/dictionary/pt/<word>/') |
| 23 | +def portuguese(word: str): |
| 24 | + return ResponseAPI().get_meanings(PORTUGUESE_REPR, word) |
| 25 | + |
| 26 | + |
21 | 27 | @api.route('/dictionary/es/<word>/') |
22 | | -@api.route('/translator/en-pt/<word>/') |
23 | | -@api.route('/translator/pt-en/<word>/') |
24 | | -def dictionary(word: str): |
| 28 | +def spanish(word: str): |
| 29 | + return ResponseAPI().get_meanings(SPANISH_REPR, word) |
25 | 30 |
|
26 | | - requests_db = RedisRequests() |
27 | 31 |
|
28 | | - choice = request.url.split('/')[5] |
29 | | - dictionary = DICTIONARIES[choice] |
30 | | - request_ip = request.remote_addr if not request.headers.getlist("X-Forwarded-For") else request.headers.getlist("X-Forwarded-For")[0] |
31 | | - total_requests = requests_db.get(f'requests:{request_ip}') |
32 | | - if not (meanings := dictionary.get_meanings(word)): |
33 | | - raise NotFound(f'"{word}" not found, check the spelling and try again') |
34 | | - if request_ip != LOCAL_ADDR: |
35 | | - if total_requests > TOTAL_REQUESTS_ALLOW: |
36 | | - raise TooManyRequests( |
37 | | - f'The address {request_ip} is allow to make only "{TOTAL_REQUESTS_ALLOW}" requests ' |
38 | | - f'wait until {int(TTL_REQUEST / 60)} minutes and try again' |
39 | | - ) |
| 32 | +@api.route('/translator/en-pt/<word>/') |
| 33 | +def english_to_portuguese(word: str): |
| 34 | + return ResponseAPI().get_meanings(ENGLISH_TO_PORTUGUESE_REPR, word) |
40 | 35 |
|
41 | | - requests_db.set(f'requests:{request_ip}', str(total_requests + 1), TTL_REQUEST) |
42 | 36 |
|
43 | | - return jsonify({'source': dictionary.URL.format(word), 'meanings': meanings}), 200 |
| 37 | +@api.route('/translator/pt-en/<word>/') |
| 38 | +def portuguese_to_english(word: str): |
| 39 | + return ResponseAPI().get_meanings(PORTUGUESE_TO_ENGLISH, word) |
44 | 40 |
|
45 | 41 |
|
46 | 42 | @api.app_errorhandler(NotFound) |
47 | 43 | def not_found(err): |
48 | | - traceback.print_tb(err.__traceback__) |
49 | | - return jsonify({'message': err.description}), err.code |
| 44 | + return ResponseAPI().handle_error(err) |
50 | 45 |
|
51 | 46 |
|
52 | 47 | @api.app_errorhandler(TooManyRequests) |
53 | 48 | def too_many_requests(err): |
54 | | - traceback.print_tb(err.__traceback__) |
55 | | - return jsonify({'message': err.description}), err.code |
| 49 | + return ResponseAPI().handle_error(err) |
56 | 50 |
|
57 | 51 |
|
58 | 52 | @api.app_errorhandler(InternalServerError) |
59 | 53 | def internal_error(err): |
60 | | - traceback.print_tb(err.__traceback__) |
61 | | - return jsonify({'message': err.description}), err.code |
| 54 | + return ResponseAPI().handle_error(err) |
62 | 55 |
|
63 | 56 |
|
64 | 57 | @api.app_errorhandler(Exception) |
65 | 58 | def general_exception(err): |
66 | | - traceback.print_tb(err.__traceback__) |
67 | | - return jsonify({'message': 'Don\'t recognize erro'}), 500 |
| 59 | + err.description = 'Don\'t recognize erro' |
| 60 | + err.code = 500 |
| 61 | + return ResponseAPI().handle_error(err) |
0 commit comments