Debugging Endpoint write data to DB

This commit is contained in:
ZKWolf 2023-06-26 19:00:43 +02:00
parent 5b3446e69d
commit 6a14ee9dcf
5 changed files with 64 additions and 23 deletions

View File

@ -6,4 +6,6 @@ steam:
mongodb:
host: mongodb://IP:27017/
db: db_name
collection: collection_name
collection: collection_name
api:
allowed_tokens: token1, token2, token3, token4

View File

@ -777,24 +777,4 @@ def catalog_get(game_version):
@app.errorhandler(404)
def debug_404(e):
print("##################################################################################")
print("DEBUG 404")
print("##################################################################################")
print('HTTP Headers:')
for header in request.headers:
print(f'{header[0]}: {header[1]}')
print('Cookies:')
for key, value in request.cookies.items():
print(f'{key}: {value}')
print('Endpoint: ', request.endpoint)
print('Path: ', request.path)
print('Method: ', request.method)
if request.json:
print('JSON Payload:')
print(request.json)
print("##################################################################################")
return jsonify({"status": "error", "code": 404})
return jsonify({"message": "Endpoint not found"}), 404

View File

@ -46,3 +46,39 @@ def debug_user(steamid):
finished_challanges=user_data.get('finished_challanges'),
open_challanges=user_data.get('open_challanges')
)
@app.route("/debug/mirrors", methods=["POST", "GET"])
def debug_mirrors_write():
get_remote_ip()
if request.method == "POST":
try:
api_token = request.cookies.get("api_token")
if api_token is None:
return jsonify({"status": "error", "message": "No api token found"}, 401)
else:
if api_token in allowed_tokens:
steam_user_id = request.json.get("steamid")
data_b = request.json.get("data")
if data_b or steam_user_id:
return_val = mongo.write_data_with_list(steamid=steam_user_id, items_dict=data_b, server=mongo_host, db=mongo_db,
collection=mongo_collection)
if return_val == None:
return jsonify({"status": "error", "message": "Error."}, 400)
else:
return jsonify({"status": "success", "message": "Data written."}, 200)
else:
return jsonify({"status": "error", "message": "No data or Steamid found."}, 400)
else:
return jsonify({"status": "error", "message": "Invalid api token"}, 401)
except TimeoutError:
print("Timeout error")
return jsonify({"status": "error"})
except Exception as e:
print(e)
logger.graylog_logger(level="error", handler="logging_debug_mirror_write", message=e)
if request.method == "GET":
return jsonify({"message": "Endpoint not found"}), 404

View File

@ -10,4 +10,5 @@ graylog_server = config['graylog']['host']
steam_api_key = config['steam']['api_key']
mongo_host = config['mongodb']['host']
mongo_db = config['mongodb']['db']
mongo_collection = config['mongodb']['collection']
mongo_collection = config['mongodb']['collection']
allowed_tokens = config['api']['allowed_tokens']

View File

@ -147,5 +147,27 @@ class Mongo:
logger.graylog_logger(level="error", handler="mongo_get_data_with_list", message=e)
return None
def write_data_with_list(self, steamid, items_dict, server, db, collection):
try:
steam_id = str(steamid)
self.dyn_db = db
self.dyn_collection = collection
client = pymongo.MongoClient(server)
self.dyn_db = client[self.dyn_db]
self.dyn_collection = self.dyn_db[self.dyn_collection]
existing_document = self.dyn_collection.find_one({'steamid': steamid})
if existing_document:
update_query = {'$set': items_dict}
self.dyn_collection.update_one({'steamid': steamid}, update_query)
return {"status": "success", "message": "Data updated"}
else:
print(f"No user found with steamid: {steam_id}")
return None
except Exception as e:
print(e)
logger.graylog_logger(level="error", handler="mongo_write_data_with_list", message=e)
return None
mongo = Mongo()