Improve JSON documents generation

This commit is contained in:
poka 2020-11-22 04:39:27 +01:00
parent 4aea990144
commit 6bb37dbb47
1 changed files with 40 additions and 29 deletions

View File

@ -41,29 +41,19 @@ class ReadFromCesium:
else: else:
boxType = "recipient" boxType = "recipient"
return { data = {}
"sort": { "time": "desc" }, data['sort'] = { "time": "desc" }
"from": 0, data['from'] = 0
"size": nbrMsg, data['size'] = nbrMsg
"_source":[ data['_source'] = ['issuer','recipient','title','content','time','nonce','read_signature']
"issuer", data['query'] = {}
"recipient", data['query']['bool'] = {}
"title", data['query']['bool']['filter'] = {}
"content", data['query']['bool']['filter']['term'] = {}
"time", data['query']['bool']['filter']['term'][boxType] = self.recipient
"nonce",
"read_signature"
],"query":{
"bool":{
"filter":{
"term":{
boxType: self.recipient
}
}
}
}
}
document = json.dumps(data)
return document
def sendDocument(self, nbrMsg, outbox): def sendDocument(self, nbrMsg, outbox):
if outbox: if outbox:
@ -71,7 +61,7 @@ class ReadFromCesium:
else: else:
boxType = "inbox" boxType = "inbox"
document = json.dumps(self.configDoc(nbrMsg, outbox)) document = self.configDoc(nbrMsg, outbox)
headers = { headers = {
'Content-type': 'application/json', 'Content-type': 'application/json',
} }
@ -179,8 +169,16 @@ class SendToCesium:
# Get current timestamp # Get current timestamp
timeSent = int(time.time()) timeSent = int(time.time())
# Generate document to customize # Generate custom JSON
document = str({"issuer":self.issuer,"recipient":self.recipient,"title":title,"content":msg,"time":timeSent,"nonce":b58nonce,"version":2}).replace("'",'"') data = {}
data['issuer'] = self.issuer
data['recipient'] = self.recipient
data['title'] = title
data['content'] = msg
data['time'] = timeSent
data['nonce'] = b58nonce
data['version'] = 2
document = json.dumps(data)
# Generate hash of document # Generate hash of document
hashDoc = sha256(document.encode()).hexdigest().upper() hashDoc = sha256(document.encode()).hexdigest().upper()
@ -216,7 +214,8 @@ class SendToCesium:
print("ID: " + result.text) print("ID: " + result.text)
return result return result
else: else:
sys.stderr.write("Erreur inconnue.") sys.stderr.write("Erreur inconnue:" + '\n')
print(str(pp_json(result.text)) + '\n')
def send(self, title, msg): def send(self, title, msg):
finalDoc = self.configDoc(self.encryptMsg(title), self.encryptMsg(msg)) # Configure JSON document to send finalDoc = self.configDoc(self.encryptMsg(title), self.encryptMsg(msg)) # Configure JSON document to send
@ -257,8 +256,15 @@ class DeleteFromCesium:
else: else:
boxType = "inbox" boxType = "inbox"
document = str({"version":2,"index":"message","type":boxType,"id":idMsg,"issuer":self.issuer,"time":timeSent}).replace("'",'"') #document = str({"version":2,"index":"message","type":boxType,"id":idMsg,"issuer":self.issuer,"time":timeSent}).replace("'",'"')
# "{\"version\":2,\"index\":\"message\",\"type\":\"$type\",\"id\":\"$id\",\"issuer\":\"$issuer\",\"time\":$times}" data = {}
data['version'] = 2
data['index'] = "message"
data['type'] = boxType
data['id'] = idMsg
data['issuer'] = self.issuer
data['time'] = timeSent
document = json.dumps(data)
# Generate hash of document # Generate hash of document
hashDoc = sha256(document.encode()).hexdigest().upper() hashDoc = sha256(document.encode()).hexdigest().upper()
@ -267,7 +273,12 @@ class DeleteFromCesium:
signature = fmt["64"](sign(hashDoc.encode(), get_privkey(self.dunikey, "pubsec"))[:-len(hashDoc.encode())]).decode() signature = fmt["64"](sign(hashDoc.encode(), get_privkey(self.dunikey, "pubsec"))[:-len(hashDoc.encode())]).decode()
# Build final document # Build final document
finalDoc = '{' + '"hash":"{0}","signature":"{1}",'.format(hashDoc, signature) + document[1:] data = {}
data['hash'] = hashDoc
data['signature'] = signature
signJSON = json.dumps(data)
finalJSON = {**json.loads(signJSON), **json.loads(document)}
finalDoc = json.dumps(finalJSON)
return finalDoc return finalDoc