| 1 |  |  | from functools import wraps | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | import json | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | import time | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | from flask import Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | from flask_jwt_extended import jwt_required, get_jwt_identity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | from flask_restful import Resource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | from werkzeug.exceptions import HTTPException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | def after_request(response): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     response.headers['X-Content-Type-Options'] = 'nosniff' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     response.headers['X-Frame-Options'] = 'deny' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     return response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | def auth_required(model): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     def decorator(fn): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         @wraps(fn) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         @jwt_required | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         def wrapper(*args, **kwargs): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |             raise NotImplementedError() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             # return fn(*args, **kwargs) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         return wrapper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     return decorator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | class BaseResource(Resource): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     def __init__(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         self.now = time.strftime('%Y-%m-%d %H:%M:%S') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     @classmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     def unicode_safe_json_dumps(cls, data, status_code=200, **kwargs) -> Response: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         return Response( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             json.dumps(data, ensure_ascii=False), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             status_code, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             content_type='application/json; charset=utf8', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             **kwargs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     class ValidationError(Exception): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         def __init__(self, description='', *args): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             self.description = description | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             super(BaseResource.ValidationError, self).__init__(*args) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | def _register_error_handlers(blueprint): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     from app import error_handlers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     blueprint.register_error_handler(HTTPException, error_handlers.http_exception_handler) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     blueprint.register_error_handler(BaseResource.ValidationError, error_handlers.validation_error_handler) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     blueprint.register_error_handler(Exception, error_handlers.broad_exception_error_handler) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | def load_api(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     from app.views import sample | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 62 |  |  | def route(app): | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |     from app import api_v1_blueprint | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |     _register_error_handlers(api_v1_blueprint) | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |     api_v1_blueprint.after_request(after_request) | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |     load_api() | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |     handle_exception_func = app.handle_exception | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |     handle_user_exception_func = app.handle_user_exception | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |     # register_blueprint 시 defer되었던 함수들이 호출되며, flask-restful.Api._init_app()이 호출되는데 | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |     # 해당 메소드가 app 객체의 에러 핸들러를 오버라이딩해서, 별도로 적용한 handler의 HTTPException 관련 로직이 동작하지 않음 | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |     # 따라서 두 함수를 임시 저장해 두고, register_blueprint 이후 함수를 재할당하도록 함 | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |     app.register_blueprint(api_v1_blueprint) | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |     app.handle_user_exception = handle_exception_func | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |     app.handle_user_exception = handle_user_exception_func | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |  |