yusufgundogdu commited on
Commit
77970b1
·
verified ·
1 Parent(s): 4710299

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -71
app.py CHANGED
@@ -1,91 +1,55 @@
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
13
  def generate_new_id():
14
  if not fake_db["accounts"]:
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('/users', methods=['GET'])
49
  def get_all_accounts():
50
- return create_response(
51
- data={"users": fake_db["accounts"]},
52
- message="Users 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'])
58
  def get_or_create_account(platform, udid):
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()),
@@ -95,19 +59,17 @@ def get_or_create_account(platform, udid):
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)
 
1
+ from flask import Flask, request, jsonify
2
  import time
 
3
 
4
  app = Flask(__name__)
5
 
6
+ # Fake database
7
  fake_db = {
8
  "accounts": []
9
  }
10
 
 
11
  def generate_new_id():
12
  if not fake_db["accounts"]:
13
  return 1
14
  return max(account["id"] for account in fake_db["accounts"]) + 1
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  @app.route('/')
17
  def home():
18
+ return "stablediffusionapi test"
 
 
 
 
19
 
 
20
  @app.route('/users', methods=['GET'])
21
  def get_all_accounts():
22
+ return jsonify({
23
+ "success": True,
24
+ "users": fake_db["accounts"],
25
+ "error": None
26
+ })
27
 
 
28
  @app.route('/account/<platform>/<udid>', methods=['GET'])
29
  def get_or_create_account(platform, udid):
 
30
  platform = platform.upper()
31
  if platform not in ["IOS", "AND"]:
32
+ return jsonify({
33
+ "success": False,
34
+ "account": None,
35
+ "error": "Platform must be either IOS or AND"
36
+ }), 400
 
37
 
 
38
  account = next((acc for acc in fake_db["accounts"] if acc["udid"] == udid), None)
39
 
40
  if account:
41
+ return jsonify({
42
+ "success": True,
43
+ "account": {
44
+ "id": account["id"],
45
+ "timestamp": account["timestamp"],
46
+ "udid": account["udid"],
47
+ "platform": account["platform"],
48
+ "credit": account["credit"]
 
 
49
  },
50
+ "error": None
51
+ })
 
52
  else:
 
53
  new_account = {
54
  "id": generate_new_id(),
55
  "timestamp": int(time.time()),
 
59
  }
60
  fake_db["accounts"].append(new_account)
61
 
62
+ return jsonify({
63
+ "success": True,
64
+ "account": {
65
+ "id": new_account["id"],
66
+ "timestamp": new_account["timestamp"],
67
+ "udid": new_account["udid"],
68
+ "platform": new_account["platform"],
69
+ "credit": new_account["credit"]
 
70
  },
71
+ "error": None
72
+ }), 201
 
73
 
74
  if __name__ == '__main__':
75
  app.run(host='0.0.0.0', port=7860)