yusufgundogdu commited on
Commit
778fae3
·
verified ·
1 Parent(s): 15a3e96

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -28
app.py CHANGED
@@ -1,11 +1,12 @@
1
- from flask import Flask, request, jsonify
2
  import time
 
3
 
4
  app = Flask(__name__)
5
 
6
- # Fake database simulation - more generic name
7
  fake_db = {
8
- "accounts": [] # Changed from "users" to "accounts" to better reflect the data
9
  }
10
 
11
  # Helper function to generate a new ID
@@ -14,15 +15,43 @@ def generate_new_id():
14
  return 1
15
  return max(account["id"] for account in fake_db["accounts"]) + 1
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  # Home page
18
  @app.route('/')
19
  def home():
20
- return "stablediffusionapi test"
 
 
 
 
21
 
22
  # GET all accounts
23
  @app.route('/accounts', methods=['GET'])
24
  def get_all_accounts():
25
- return jsonify({"accounts": fake_db["accounts"]})
 
 
 
 
26
 
27
  # GET or CREATE account by platform and UDID
28
  @app.route('/account/<platform>/<udid>', methods=['GET'])
@@ -30,43 +59,55 @@ def get_or_create_account(platform, udid):
30
  # Validate platform (should be "IOS" or "AND")
31
  platform = platform.upper()
32
  if platform not in ["IOS", "AND"]:
33
- return jsonify({
34
- "error": "Invalid platform. Use 'IOS' or 'AND'."
35
- }), 400
 
 
 
36
 
37
  # Search for account in the database
38
  account = next((acc for acc in fake_db["accounts"] if acc["udid"] == udid), None)
39
 
40
  if account:
41
- # Account found, return the account data
42
- return jsonify({
43
- "id": account["id"],
44
- "timestamp": account["timestamp"],
45
- "udid": account["udid"],
46
- "platform": account["platform"],
47
- "credit": account["credit"]
48
- })
 
 
 
 
 
 
49
  else:
50
- # Account not found, create new account with 100 credits
51
  new_account = {
52
  "id": generate_new_id(),
53
  "timestamp": int(time.time()),
54
  "udid": udid,
55
  "platform": platform,
56
- "credit": 100 # Default credit
57
  }
58
-
59
- # Add to database
60
  fake_db["accounts"].append(new_account)
61
 
62
- # Return the newly created account
63
- return jsonify({
64
- "id": new_account["id"],
65
- "timestamp": new_account["timestamp"],
66
- "udid": new_account["udid"],
67
- "platform": new_account["platform"],
68
- "credit": new_account["credit"]
69
- }), 201 # 201 Created status code
 
 
 
 
 
70
 
71
  if __name__ == '__main__':
72
  app.run(host='0.0.0.0', port=7860)
 
1
+ from flask import Flask, request, jsonify, make_response
2
  import time
3
+ from datetime import datetime
4
 
5
  app = Flask(__name__)
6
 
7
+ # Fake database simulation
8
  fake_db = {
9
+ "accounts": []
10
  }
11
 
12
  # Helper function to generate a new ID
 
15
  return 1
16
  return max(account["id"] for account in fake_db["accounts"]) + 1
17
 
18
+ def create_response(data=None, message="", status_code=200, error=None):
19
+ """Standard response format for all API endpoints"""
20
+ response = {
21
+ "meta": {
22
+ "timestamp": datetime.utcnow().isoformat(),
23
+ "status": status_code,
24
+ "message": message,
25
+ "error": error
26
+ },
27
+ "data": data
28
+ }
29
+
30
+ headers = {
31
+ "Content-Type": "application/json",
32
+ "X-API-Version": "1.0",
33
+ "X-Server": "StableDiffusionAPI"
34
+ }
35
+
36
+ return make_response(jsonify(response), status_code, headers)
37
+
38
  # Home page
39
  @app.route('/')
40
  def home():
41
+ return create_response(
42
+ data={"service": "stablediffusionapi test"},
43
+ message="Service is running",
44
+ status_code=200
45
+ )
46
 
47
  # GET all accounts
48
  @app.route('/accounts', methods=['GET'])
49
  def get_all_accounts():
50
+ return create_response(
51
+ data={"accounts": fake_db["accounts"]},
52
+ message="Accounts retrieved successfully",
53
+ status_code=200
54
+ )
55
 
56
  # GET or CREATE account by platform and UDID
57
  @app.route('/account/<platform>/<udid>', methods=['GET'])
 
59
  # Validate platform (should be "IOS" or "AND")
60
  platform = platform.upper()
61
  if platform not in ["IOS", "AND"]:
62
+ return create_response(
63
+ data=None,
64
+ message="Invalid platform",
65
+ error="Platform must be either IOS or AND",
66
+ status_code=400
67
+ )
68
 
69
  # Search for account in the database
70
  account = next((acc for acc in fake_db["accounts"] if acc["udid"] == udid), None)
71
 
72
  if account:
73
+ # Account found
74
+ return create_response(
75
+ data={
76
+ "account": {
77
+ "id": account["id"],
78
+ "timestamp": account["timestamp"],
79
+ "udid": account["udid"],
80
+ "platform": account["platform"],
81
+ "credit": account["credit"]
82
+ }
83
+ },
84
+ message="Account retrieved successfully",
85
+ status_code=200
86
+ )
87
  else:
88
+ # Account not found, create new account
89
  new_account = {
90
  "id": generate_new_id(),
91
  "timestamp": int(time.time()),
92
  "udid": udid,
93
  "platform": platform,
94
+ "credit": 100
95
  }
 
 
96
  fake_db["accounts"].append(new_account)
97
 
98
+ return create_response(
99
+ data={
100
+ "account": {
101
+ "id": new_account["id"],
102
+ "timestamp": new_account["timestamp"],
103
+ "udid": new_account["udid"],
104
+ "platform": new_account["platform"],
105
+ "credit": new_account["credit"]
106
+ }
107
+ },
108
+ message="Account created successfully",
109
+ status_code=201
110
+ )
111
 
112
  if __name__ == '__main__':
113
  app.run(host='0.0.0.0', port=7860)