2024-11-19 21:22:01 +08:00
|
|
|
from flask import jsonify, Response
|
|
|
|
|
2024-11-25 21:51:50 +08:00
|
|
|
from protobuf_files import niming_pb2
|
|
|
|
|
2024-11-19 21:22:01 +08:00
|
|
|
def error(message:str) -> Response:
|
2024-11-25 21:51:50 +08:00
|
|
|
return jsonify({"error":message})
|
|
|
|
|
|
|
|
|
2024-12-09 03:24:22 +08:00
|
|
|
def error_proto(message:str) -> bytes:
|
|
|
|
return niming_pb2.PostResponse(
|
|
|
|
status = niming_pb2.Status.Failed,
|
|
|
|
failed_message = message
|
|
|
|
).SerializeToString()
|
|
|
|
|
|
|
|
|
2024-11-25 21:51:50 +08:00
|
|
|
def internal_json2protobuf(original:list|dict) -> bytes:
|
|
|
|
if isinstance(original, dict):
|
|
|
|
original = [original]
|
|
|
|
|
2024-12-09 03:24:22 +08:00
|
|
|
res = niming_pb2.FetchResponse()
|
2024-11-25 21:51:50 +08:00
|
|
|
for o in original:
|
2024-12-09 03:24:22 +08:00
|
|
|
# drop null object
|
|
|
|
newo = {}
|
|
|
|
for oc in o:
|
|
|
|
if o[oc] is not None:
|
|
|
|
newo[oc] = o[oc]
|
|
|
|
o = newo
|
|
|
|
|
|
|
|
ob = niming_pb2.FetchResponse.Message()
|
|
|
|
|
|
|
|
if "id" in o: ob.id = o["id"]
|
|
|
|
if "content" in o: ob.content = o["content"]
|
|
|
|
if "igid" in o: ob.igid = o["igid"]
|
|
|
|
if "files_hash" in o: ob.files_hash.extend(o["files_hash"])
|
|
|
|
if "comments_hash" in o: ob.comments_hash.extend(o["comments_hash"])
|
2024-11-25 21:51:50 +08:00
|
|
|
res.posts.append(ob)
|
|
|
|
return res.SerializeToString()
|