Add sha256 checksum for api responses.

This commit is contained in:
kalzu 2022-12-28 20:36:44 +02:00
parent 1751a7f763
commit dd86cecec2
1 changed files with 10 additions and 5 deletions

View File

@ -4,6 +4,7 @@ import krakenex, math
import json, sqlite3
import requests, os, time
import threading
from hashlib import sha256
from flask import Flask, request
database = "btc_ohlc.db"
@ -172,7 +173,6 @@ def fetch_bybit():
last_minute = math.floor(current_unixtime / 60)
last_minute_unixtime = str(last_minute * 60 - 300)
query_url = ''.join([base_url, last_minute_unixtime])
print(query_url)
response = requests.get(query_url)
if response.status_code == 200: # check if the request was successful
@ -189,7 +189,6 @@ def fetch_bybit():
'trades': 0
}
bybit_json = json.dumps(candle_stick_data, indent=2)
print(bybit_json)
return bybit_json
else:
print(f"Error fetching data from Bybit API: {response.status_code}")
@ -236,17 +235,23 @@ def get_data():
database_lock.acquire()
db = sqlite3.connect(database)
if query_timestamp:
rows = db.execute("SELECT exchange, timestamp, open, high, low, close FROM ohlc WHERE timestamp > ?", (query_timestamp,)).fetchall()
rows = db.execute("SELECT exchange, timestamp, open, high, low, close FROM ohlc WHERE timestamp > ? ORDER BY timestamp", (query_timestamp,)).fetchall()
else:
rows = db.execute('SELECT exchange, timestamp, open, high, low, close FROM ohlc').fetchall()
rows = db.execute('SELECT exchange, timestamp, open, high, low, close FROM ohlc ORDER BY timestamp').fetchall()
query_timestamp = 0
database_lock.release()
data = {
"timestamp": time.time(),
"rows": rows
}
# make sha256 checksum and append it to the data object
data_shasum = sha256(json.dumps(data).encode('utf-8')).hexdigest()
updated_data = {"shasum": data_shasum}
updated_data.update(data)
data = updated_data
if query_pretty:
response = json.dumps(data, indent=2, separators=(';\n', ' :'))
else: