Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:58:20

0001 #!/usr/bin/env python
0002 #
0003 # Licensed under the Apache License, Version 2.0 (the "License");
0004 # You may not use this file except in compliance with the License.
0005 # You may obtain a copy of the License at
0006 # http://www.apache.org/licenses/LICENSE-2.0OA
0007 #
0008 # Authors:
0009 # - Wen Guan, <wen.guan@cern.ch>, 2019 - 2024
0010 
0011 
0012 from flask import Response, request
0013 from flask.views import MethodView
0014 
0015 from idds.common.constants import HTTP_STATUS_CODE
0016 from idds.common.utils import json_dumps, get_logger
0017 
0018 
0019 class IDDSController(MethodView):
0020     """ Default ESS Controller class. """
0021 
0022     def get_class_name(self):
0023         return self.__class__.__name__
0024 
0025     def setup_logger(self, logger=None):
0026         """
0027         Setup logger
0028         """
0029         if logger:
0030             self.logger = logger
0031         else:
0032             self.logger = get_logger(name=self.get_class_name(), filename='idds_rest.log')
0033         return self.logger
0034 
0035     def get_logger(self):
0036         if hasattr(self, 'logger') and self.logger:
0037             return self.logger
0038         return self.setup_logger()
0039 
0040     def post(self):
0041         """ Not supported. """
0042         return Response(status=HTTP_STATUS_CODE.BadRequest, content_type='application/json')()
0043 
0044     def get(self):
0045         """ Not supported. """
0046         return Response(status=HTTP_STATUS_CODE.BadRequest, content_type='application/json')()
0047 
0048     def put(self):
0049         """ Not supported. """
0050         return Response(status=HTTP_STATUS_CODE.BadRequest, content_type='application/json')()
0051 
0052     def delete(self):
0053         """ Not supported. """
0054         return Response(status=HTTP_STATUS_CODE.BadRequest, content_type='application/json')()
0055 
0056     def get_request(self):
0057         return request
0058 
0059     def get_username(self):
0060         if 'username' in request.environ and request.environ['username']:
0061             return request.environ['username']
0062         return None
0063 
0064     def generate_message(self, exc_cls=None, exc_msg=None):
0065         if exc_cls is None and exc_msg is None:
0066             return None
0067         else:
0068             message = {}
0069             # if exc_cls is not None:
0070             #     message['ExceptionClass'] = exc_cls
0071             if exc_msg is not None:
0072                 message['msg'] = str(exc_msg)
0073             return json_dumps(message)
0074 
0075     def generate_http_response(self, status_code, data=None, exc_cls=None, exc_msg=None):
0076         enable_json_outputs = self.get_request().args.get('json_outputs', None)
0077         if enable_json_outputs and enable_json_outputs.upper() == 'TRUE':
0078             error = None
0079             if exc_cls:
0080                 error = {'ExceptionClass': exc_cls,
0081                          'ExceptionMessage': self.generate_message(exc_cls, exc_msg)}
0082             if status_code == HTTP_STATUS_CODE.OK:
0083                 status_code = 0
0084             response = {'status': status_code,
0085                         'data': data,
0086                         'error': error}
0087             resp = Response(response=json_dumps(response, sort_keys=True, indent=4), status=HTTP_STATUS_CODE.OK, content_type='application/json')
0088         else:
0089             resp = Response(response=json_dumps(data, sort_keys=True, indent=4) if data is not None else data, status=status_code, content_type='application/json')
0090             if exc_cls:
0091                 resp.headers['ExceptionClass'] = exc_cls
0092                 resp.headers['ExceptionMessage'] = self.generate_message(exc_cls, exc_msg)
0093         return resp