Skip to content

Commit

Permalink
chore: update lowercase id
Browse files Browse the repository at this point in the history
  • Loading branch information
jmoreira-valory committed Oct 16, 2024
1 parent 1171f3e commit f04ef72
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
31 changes: 23 additions & 8 deletions market_approval_server/market_approval_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@
Usage for server running in http mode (replace by https if applies).
- Service API:
curl -X POST -H "Authorization: YOUR_API_KEY" -H "Content-Type: application/json" -d '{"id": "MARKET_ID", ...}' -k http://127.0.0.1:5000/propose_market
curl -X POST -H "Authorization: YOUR_API_KEY" -H "Content-Type: application/json" -d '{"id": "MARKET_ID"}' -k http://127.0.0.1:5000/process_market
curl -X POST -H "Authorization: YOUR_API_KEY" -H "Content-Type: application/json" -d '{"id": "market_id", ...}' -k http://127.0.0.1:5000/propose_market
curl -X POST -H "Authorization: YOUR_API_KEY" -H "Content-Type: application/json" -d '{"id": "market_id"}' -k http://127.0.0.1:5000/process_market
curl -X POST -H "Authorization: YOUR_API_KEY" -H "Content-Type: application/json" -k http://127.0.0.1:5000/get_process_random_approved_market
curl -X PUT -H "Authorization: YOUR_API_KEY" -H "Content-Type: application/json" -d '{"id": "MARKET_ID", ...}' -k http://127.0.0.1:5000/update_market
curl -X PUT -H "Authorization: YOUR_API_KEY" -H "Content-Type: application/json" -d '{"id": "market_id", ...}' -k http://127.0.0.1:5000/update_market
curl -X PUT -H "Authorization: YOUR_API_KEY" -H "Content-Type: application/json" -d '{"id": "market_id", "new_id": "new_market_id"}' -k http://127.0.0.1:5000/update_market_id
- User API
curl -X POST -H "Authorization: YOUR_API_KEY" -H "Content-Type: application/json" -d '{"id": "MARKET_ID"}' -k http://127.0.0.1:5000/approve_market
curl -X POST -H "Authorization: YOUR_API_KEY" -H "Content-Type: application/json" -d '{"id": "MARKET_ID"}' -k http://127.0.0.1:5000/reject_market
curl -X POST -H "Authorization: YOUR_API_KEY" -H "Content-Type: application/json" -d '{"id": "market_id"}' -k http://127.0.0.1:5000/approve_market
curl -X POST -H "Authorization: YOUR_API_KEY" -H "Content-Type: application/json" -d '{"id": "market_id"}' -k http://127.0.0.1:5000/reject_market
curl -X DELETE -H "Authorization: YOUR_API_KEY" -k http://127.0.0.1:5000/clear_proposed_markets
curl -X DELETE -H "Authorization: YOUR_API_KEY" -k http://127.0.0.1:5000/clear_approved_markets
Expand Down Expand Up @@ -209,7 +210,10 @@ 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 in {endpoint}s."}), 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

Expand All @@ -222,7 +226,10 @@ def get_market_by_id_all_databases(market_id: str) -> Tuple[Response, int]:
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
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 @@ -294,6 +301,8 @@ def propose_market() -> Tuple[Response, int]:
market["id"] = str(uuid.uuid4())

market_id = str(market["id"])
market_id = market_id.lower()
market["id"] = market_id

if any(
market_id in db
Expand Down Expand Up @@ -358,6 +367,7 @@ def move_market() -> Tuple[Response, int]:
return jsonify({"error": "Invalid JSON format. Missing id."}), 400

market_id = data["id"]
market_id = market_id.lower()

if market_id not in move_from:
return jsonify({"error": f"Market ID {market_id} not found."}), 404
Expand Down Expand Up @@ -386,7 +396,7 @@ def get_random_approved_market() -> Tuple[Response, int]:
return (
jsonify({"info": "No approved markets available."}),
204,
) # No content, json will be ignored by the server
)

market_id = secrets.choice(list(approved_markets.keys()))
market = approved_markets[market_id]
Expand Down Expand Up @@ -414,6 +424,7 @@ def update_market() -> Tuple[Response, int]:
return jsonify({"error": "Invalid JSON format. Missing id."}), 400

market_id = market["id"]
market_id = market_id.lower()

# Check if the market exists in any of the databases
databases = [
Expand Down Expand Up @@ -468,6 +479,10 @@ 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

# The next line is intentionally commented to allow fixing uppercase market_ids externally.
# current_market_id = current_market_id.lower()
new_market_id = new_market_id.lower()

databases = get_databases()

if any(new_market_id in db for _, db in databases.items()):
Expand Down
29 changes: 16 additions & 13 deletions market_approval_server/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,44 +24,47 @@ <h2>View the server databases</h2>
<li><a href="/all_markets">All markets</a></li>
</ul>

<h2>View a single market by MARKET_ID endpoints</h2>
<p>Requires to know the MARKET_ID:</p>
<h2>View a single market by <code>market_id</code> endpoints</h2>
<p>Requires to know the <code>market_id</code>:</p>
<ul>
<li><code>/proposed_market/MARKET_ID</code>: Marked in "Proposed markets" database</a></li>
<li><code>/approved_market/MARKET_ID</code>: Marked in "Approved markets" database</a></li>
<li><code>/rejected_market/MARKET_ID</code>: Marked in "Rejected markets" database</a></li>
<li><code>/processed_market/MARKET_ID</code>: Marked in "Processed markets" database</a></li>
<li><code>/market/MARKET_ID</code>: Marked in any database</a></li>
<li><code>/proposed_market/market_id</code>: Marked in "Proposed markets" database</a></li>
<li><code>/approved_market/market_id</code>: Marked in "Approved markets" database</a></li>
<li><code>/rejected_market/market_id</code>: Marked in "Rejected markets" database</a></li>
<li><code>/processed_market/market_id</code>: Marked in "Processed markets" database</a></li>
<li><code>/market/market_id</code>: Marked in any database</a></li>
</ul>

<h2>Note on <code>market_id</code>s</h2>
<p>All <code>market_id</code>s must be lowercase. The server will always convert received market IDs to lowercase before storing or modifying the IDs of any market on the database.</p>

<h2>Main interaction methods</h2>

<ul>
<li>Approve a market:
<pre>curl -X POST -H "Authorization: YOUR_API_KEY" -H "Content-Type: application/json" -d '{"id": "MARKET_ID"}' -k {{ server_ip }}/approve_market</pre>
<pre>curl -X POST -H "Authorization: YOUR_API_KEY" -H "Content-Type: application/json" -d '{"id": "market_id"}' -k {{ server_ip }}/approve_market</pre>
</li>

<li>Reject a market:
<pre>curl -X POST -H "Authorization: YOUR_API_KEY" -H "Content-Type: application/json" -d '{"id": "MARKET_ID"}' -k {{ server_ip }}/reject_market</pre>
<pre>curl -X POST -H "Authorization: YOUR_API_KEY" -H "Content-Type: application/json" -d '{"id": "market_id"}' -k {{ server_ip }}/reject_market</pre>
</li>
</ul>

<h2>Other methods</h2>
<ul>
<li>Propose a market:
<pre>curl -X POST -H "Authorization: YOUR_API_KEY" -H "Content-Type: application/json" -d '{"id": "MARKET_ID", ...}' -k {{ server_ip }}/propose_market</pre>
<pre>curl -X POST -H "Authorization: YOUR_API_KEY" -H "Content-Type: application/json" -d '{"id": "market_id", ...}' -k {{ server_ip }}/propose_market</pre>
</li>

<li>Mark a market as processed:
<pre>curl -X POST -H "Authorization: YOUR_API_KEY" -H "Content-Type: application/json" -d '{"id": "MARKET_ID"}' -k {{ server_ip }}/process_market</pre>
<pre>curl -X POST -H "Authorization: YOUR_API_KEY" -H "Content-Type: application/json" -d '{"id": "market_id"}' -k {{ server_ip }}/process_market</pre>
</li>

<li>Update a market:
<pre>curl -X PUT -H "Authorization: YOUR_API_KEY" -H "Content-Type: application/json" -d '{"id": "MARKET_ID", ...}' -k {{ server_ip }}/update_market</pre>
<pre>curl -X PUT -H "Authorization: YOUR_API_KEY" -H "Content-Type: application/json" -d '{"id": "market_id", ...}' -k {{ server_ip }}/update_market</pre>
</li>

<li>Update a market id:
<pre>curl -X PUT -H "Authorization: YOUR_API_KEY" -H "Content-Type: application/json" -d '{"id": "MARKET_ID", "new_id": "NEW_MARKET_ID"}' -k {{ server_ip }}/update_market_id</pre>
<pre>curl -X PUT -H "Authorization: YOUR_API_KEY" -H "Content-Type: application/json" -d '{"id": "market_id", "new_id": "new_market_id"}' -k {{ server_ip }}/update_market_id</pre>
</li>

<li>Get a random accepted market and mark it as processed:
Expand Down

0 comments on commit f04ef72

Please sign in to comment.