Skip to content

Commit

Permalink
chore: update market approval server
Browse files Browse the repository at this point in the history
  • Loading branch information
jmoreira-valory committed Oct 16, 2024
1 parent 299b062 commit 9c5dd3b
Showing 1 changed file with 51 additions and 12 deletions.
63 changes: 51 additions & 12 deletions market_approval_server/market_approval_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ class MarketState(str, Enum):
api_keys: Dict[str, str] = {}


def get_databases() -> Dict[str, Dict[str, Any]]:
"""Returns all databases into a single object."""

return {
"proposed_markets": proposed_markets,
"approved_markets": approved_markets,
"rejected_markets": rejected_markets,
"processed_markets": processed_markets,
}


def load_config() -> None:
"""Loads the configuration from a JSON file."""
global proposed_markets, approved_markets, rejected_markets, processed_markets, api_keys # pylint: disable=global-statement
Expand Down Expand Up @@ -198,7 +209,21 @@ def get_market_by_id(market_id: str) -> Tuple[Response, int]:
market = markets[market_id]
return jsonify(market), 200
else:
return jsonify({"error": f"Market ID {market_id} not found."}), 404
return jsonify({"error": f"Market ID {market_id} not found in {endpoint}s."}), 404
except Exception as e: # pylint: disable=broad-except
return jsonify({"error": str(e)}), 500


@app.route("/market/<market_id>", methods=["GET"])
def get_market_by_id_all_databases(market_id: str) -> Tuple[Response, int]:
"""Retrieve a market by its ID from any database."""
try:
for _, db in get_databases().items():
if market_id in db:
return jsonify(db[market_id]), 200

return jsonify({"error": f"Market ID '{market_id}' not found in any database."}), 404

except Exception as e: # pylint: disable=broad-except
return jsonify({"error": str(e)}), 500

Expand Down Expand Up @@ -443,27 +468,41 @@ def update_market_id() -> Tuple[Response, int]:
if current_market_id == new_market_id:
return jsonify({"error": "'id' is equal to 'new_id' in the request."}), 409

databases = [
("proposed_markets", proposed_markets),
("approved_markets", approved_markets),
("rejected_markets", rejected_markets),
("processed_markets", processed_markets),
]
databases = get_databases()

if any(new_market_id in db for _, db in databases):
return jsonify({"error": f"Market ID {new_market_id} already exists in database. Try using a different ID."}), 409
if any(new_market_id in db for _, db in databases.items()):
return (
jsonify(
{
"error": f"Market ID {new_market_id} already exists in database. Try using a different ID."
}
),
409,
)

for db_name, db in databases:
for db_name, db in databases.items():
if current_market_id in db:
db[new_market_id] = db.pop(current_market_id)
db[new_market_id]["id"] = new_market_id
db[new_market_id]["utc_timestamp_updated"] = int(
datetime.utcnow().timestamp()
)
save_config()
return jsonify({"message": f"Market ID '{current_market_id}' successfully changed to '{new_market_id}' in {db_name}."}), 200
return (
jsonify(
{
"message": f"Market ID '{current_market_id}' successfully changed to '{new_market_id}' in {db_name}."
}
),
200,
)

return jsonify({"error": f"Market ID '{current_market_id}' not found in the database."}), 404
return (
jsonify(
{"error": f"Market ID '{current_market_id}' not found in the database."}
),
404,
)

except Exception as e: # pylint: disable=broad-except
return jsonify({"error": str(e)}), 500
Expand Down

0 comments on commit 9c5dd3b

Please sign in to comment.