api_based_extension_fields.py 716 B

1234567891011121314151617181920212223
  1. from flask_restful import fields
  2. from libs.helper import TimestampField
  3. class HiddenAPIKey(fields.Raw):
  4. def output(self, key, obj):
  5. api_key = obj.api_key
  6. # If the length of the api_key is less than 8 characters, show the first and last characters
  7. if len(api_key) <= 8:
  8. return api_key[0] + '******' + api_key[-1]
  9. # If the api_key is greater than 8 characters, show the first three and the last three characters
  10. else:
  11. return api_key[:3] + '******' + api_key[-3:]
  12. api_based_extension_fields = {
  13. 'id': fields.String,
  14. 'name': fields.String,
  15. 'api_endpoint': fields.String,
  16. 'api_key': HiddenAPIKey,
  17. 'created_at': TimestampField
  18. }