Update app.py
Browse files12345678910112 unmodified lines1415161718192021222324252627281 unmodified line30313233343536373839404142434445464748495051525354555657585960616263646566676869707172from flask import Flask, request, jsonifyimport timeapp = Flask(__name__)# Fake database simulation - more generic namefake_db = {"accounts": [] # Changed from "users" to "accounts" to better reflect the data}# Helper function to generate a new ID2 unmodified linesreturn 1return max(account["id"] for account in fake_db["accounts"]) + 1# Home page@app.route('/')def home():return "stablediffusionapi test"# GET all accounts@app.route('/accounts', methods=['GET'])def get_all_accounts():return jsonify({"accounts": fake_db["accounts"]})# GET or CREATE account by platform and UDID@app.route('/account/<platform>/<udid>', methods=['GET'])1 unmodified line# Validate platform (should be "IOS" or "AND")platform = platform.upper()if platform not in ["IOS", "AND"]:return jsonify({"error": "Invalid platform. Use 'IOS' or 'AND'."}), 400# Search for account in the databaseaccount = next((acc for acc in fake_db["accounts"] if acc["udid"] == udid), None)if account:# Account found, return the account datareturn jsonify({"id": account["id"],"timestamp": account["timestamp"],"udid": account["udid"],"platform": account["platform"],"credit": account["credit"]})else:# Account not found, create new account with 100 creditsnew_account = {"id": generate_new_id(),"timestamp": int(time.time()),"udid": udid,"platform": platform,"credit": 100 # Default credit}# Add to databasefake_db["accounts"].append(new_account)# Return the newly created accountreturn jsonify({"id": new_account["id"],"timestamp": new_account["timestamp"],"udid": new_account["udid"],"platform": new_account["platform"],"credit": new_account["credit"]}), 201 # 201 Created status codeif __name__ == '__main__':app.run(host='0.0.0.0', port=7860)1234567891011122 unmodified lines151617181920212223242526272829303132333435363738394041424344454647484950515253545556571 unmodified line5960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113from flask import Flask, request, jsonify, make_responseimport timefrom datetime import datetimeapp = Flask(__name__)# Fake database simulationfake_db = {"accounts": []}# Helper function to generate a new ID2 unmodified linesreturn 1return max(account["id"] for account in fake_db["accounts"]) + 1def create_response(data=None, message="", status_code=200, error=None):"""Standard response format for all API endpoints"""response = {"meta": {"timestamp": datetime.utcnow().isoformat(),"status": status_code,"message": message,"error": error},"data": data}headers = {"Content-Type": "application/json","X-API-Version": "1.0","X-Server": "StableDiffusionAPI"}return make_response(jsonify(response), status_code, headers)# Home page@app.route('/')def home():return create_response(data={"service": "stablediffusionapi test"},message="Service is running",status_code=200)# GET all accounts@app.route('/accounts', methods=['GET'])def get_all_accounts():return create_response(data={"accounts": fake_db["accounts"]},message="Accounts retrieved successfully",status_code=200)# GET or CREATE account by platform and UDID@app.route('/account/<platform>/<udid>', methods=['GET'])1 unmodified line# Validate platform (should be "IOS" or "AND")platform = platform.upper()if platform not in ["IOS", "AND"]:return create_response(data=None,message="Invalid platform",error="Platform must be either IOS or AND",status_code=400)# Search for account in the databaseaccount = next((acc for acc in fake_db["accounts"] if acc["udid"] == udid), None)if account:# Account foundreturn create_response(data={"account": {"id": account["id"],"timestamp": account["timestamp"],"udid": account["udid"],"platform": account["platform"],"credit": account["credit"]}},message="Account retrieved successfully",status_code=200)else:# Account not found, create new accountnew_account = {"id": generate_new_id(),"timestamp": int(time.time()),"udid": udid,"platform": platform,"credit": 100}fake_db["accounts"].append(new_account)return create_response(data={"account": {"id": new_account["id"],"timestamp": new_account["timestamp"],"udid": new_account["udid"],"platform": new_account["platform"],"credit": new_account["credit"]}},message="Account created successfully",status_code=201)if __name__ == '__main__':app.run(host='0.0.0.0', port=7860)
app.py+69-28