-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
182 lines (141 loc) · 5.8 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# Import the necessary modules
from flask import Flask, render_template, jsonify, request
import requests
app = Flask(__name__)
# Define a default route
@app.route('/')
def index():
return render_template('update.html')
# Define your REST API routes
@app.route('/update')
def update():
return render_template('update.html')
@app.route('/add')
def create():
return render_template('add.html')
@app.route('/update-address')
def update_address():
return render_template('update-address.html')
@app.route('/add-address')
def create_address():
return render_template('add-address.html')
@app.route('/delete')
def delete():
return render_template('delete.html')
@app.route('/users', methods=['GET'])
def get_data():
# URL of the RESTful API endpoint
api_url = 'https://api.demojoyto.win/users'
# Make a GET request to the RESTful API
response = requests.get(api_url)
# Check if the request was successful
if response.status_code == 200:
return response.json()
else:
# Handle errors or unsuccessful responses
return jsonify({'error': 'Failed to fetch data from API'}), response.status_code
@app.route('/users/<int:data_id>/address', methods=['GET'])
def get_address(data_id):
api_url = f'https://api.demojoyto.win/users/{data_id}/address'
response = requests.get(api_url)
if response.status_code == 200:
return response.json()
else:
# Handle errors or unsuccessful responses
return jsonify({'error': 'Failed to fetch data from API'}), response.status_code
# Define the route for updating data
@app.route('/users/<int:data_id>', methods=['PUT'])
def update_data(data_id):
# URL of the RESTful API endpoint
api_url = f'https://api.demojoyto.win/users/{data_id}'
# Get data from the request
data = request.json
# Make a PUT request to the RESTful API
response = requests.put(api_url, json=data)
# Check if the request was successful
if response.status_code == 200:
# Return the updated user data
return response.text
else:
# Handle errors or unsuccessful responses
return jsonify({'error': 'Failed to update user data'}), response.status_code
# Define another route for updating data
@app.route('/users', methods=['PUT'])
def update_data2():
# URL of the RESTful API endpoint for creating a new user
api_url = 'https://api.demojoyto.win/users'
# Extract the data to be created from the incoming request
user_data = request.json
# Make a PUT request to the RESTful API
response = requests.put(api_url, json=user_data)
# Check if the request was successful
if response.status_code == 201:
# Return the created user data response
return response.text
else:
# Handle errors or unsuccessful responses
return jsonify({'error': 'Failed to update user'}), response.status_code
# Define the route for creating data
@app.route('/users', methods=['POST'])
def create_data():
# URL of the RESTful API endpoint for creating a new user
api_url = 'https://api.demojoyto.win/users'
# Extract the data to be created from the incoming request
user_data = request.json
# Make a POST request to the RESTful API
response = requests.post(api_url, json=user_data)
# Check if the request was successful
if response.status_code == 201:
# Return the created user data response
return response.text
else:
# Handle errors or unsuccessful responses
return jsonify({'error': 'Failed to create new user'}), response.status_code
# Define the route for creating address data
@app.route('/users/<int:data_id>/address', methods=['POST'])
def create_address_data(data_id):
# URL of the RESTful API endpoint for creating a new user
api_url = f'https://api.demojoyto.win/users/{data_id}/address'
# Extract the data to be created from the incoming request
address_data = request.json
# Make a POST request to the RESTful API
response = requests.post(api_url, json=address_data)
# Check if the request was successful
if response.status_code == 201:
# Return the created address data response
return response.text
else:
# Handle errors or unsuccessful responses
return jsonify({'error': 'Failed to create new address'}), response.status_code
# Define the route for updating address data
@app.route('/users/<int:data_id>/address', methods=['PUT'])
def update_address_data(data_id):
# URL of the RESTful API endpoint for creating a new user
api_url = f'https://api.demojoyto.win/users/{data_id}/address'
# Extract the data to be created from the incoming request
address_data = request.json
# Make a PUT request to the RESTful API
response = requests.put(api_url, json=address_data)
# Check if the request was successful
if response.status_code == 204:
# Return the created address data response
return response.text
else:
# Handle errors or unsuccessful responses
return jsonify({'error': 'Failed to update address'}), response.status_code
# Define the route for deleting data
@app.route('/users/<int:data_id>', methods=['DELETE'])
def delete_data(data_id):
# URL of the RESTful API endpoint for deleting a user
api_url = f'https://api.demojoyto.win/users/{data_id}'
# Make a DELETE request to the RESTful API
response = requests.delete(api_url)
# Check if the request was successful
if response.status_code == 200 or response.status_code == 204:
# Return a success message
return jsonify({'message': f'User with ID {data_id} deleted successfully'}), 200
else:
# Handle errors or unsuccessful responses
return jsonify({'error': 'Failed to delete user'}), response.status_code
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=8080)