from op_service_client import OPJSONEncoder
from starlette.datastructures import UploadFile


# Use this encoder for printing output as encoded by the OPJSONEncoder - it makes special accommodations for
# UploadFiles and handles any error by defaulting to str
class OPJSONEncoderFallback(OPJSONEncoder):

  def default(self, obj):
    if isinstance(obj, UploadFile):
      return 'File<%s, %s>' % (obj.filename, obj.content_type)
    try:
      return super().default(obj)
    except TypeError:
      return str(obj)
