niming_backend/utils/misc.py
2024-11-25 13:51:50 +00:00

32 lines
851 B
Python

from flask import jsonify, Response
from protobuf_files import niming_pb2
def error(message:str) -> Response:
return jsonify({"error":message})
def error_proto(message:str) -> Response:
proto = niming_pb2.PostResponse()
proto.status = niming_pb2.PostStatus.Failed
proto.hash = ""
proto.id = 0
proto.failed_message = message
return proto.SerializeToString()
def internal_json2protobuf(original:list|dict) -> bytes:
if isinstance(original, dict):
original = [original]
res = niming_pb2.FetchResponse()
for o in original:
ob = niming_pb2.FetchResponse.Message()
ob.id = o["id"]
ob.content = o["content"]
if o["reference"]:
ob.ref = o["reference"]
ob.files_id.extend(o["files"])
res.posts.append(ob)
return res.SerializeToString()