Files
py-ailbl-user-profile/apps/ailbl-users_profile-insert-get-update.py

76 lines
2.1 KiB
Python
Raw Normal View History

2025-12-08 07:50:09 +00:00
import crud
from flask import jsonify, request
2025-12-08 10:06:44 +00:00
from helpers import CORS_HEADERS
2025-12-08 07:50:09 +00:00
def main():
"""
```fission
{
"name": "profile-users-get-insert-put",
"http_triggers": {
"profile-users-get-insert-put-http": {
"url": "/ailbl/users/profiles",
"methods": ["POST", "PUT", "GET"]
}
}
}
```
"""
try:
if request.method == "PUT":
return make_update_request()
elif request.method == "POST":
return make_insert_request()
elif request.method == "GET":
return make_get_request()
else:
return {"error": "Method not allow"}, 405
except Exception as ex:
return jsonify({"error": str(ex)}), 500
def make_insert_request():
try:
2025-12-08 10:06:44 +00:00
user_id = request.headers.get("X-User")
2025-12-08 08:13:52 +00:00
if not user_id:
return jsonify({"errorCode": "USER_ID_REQUIRED"}), 400, CORS_HEADERS
2025-12-08 10:06:44 +00:00
2025-12-08 08:13:52 +00:00
# Lấy dữ liệu từ body của request (thông tin hồ sơ người dùng)
data = request.get_json()
2025-12-08 10:06:44 +00:00
response, status, header = crud.insert_profile(user_id, data)
return jsonify(response), status, header
2025-12-08 07:50:09 +00:00
except Exception as e:
return jsonify({"error": str(e)}), 500
def make_update_request():
try:
data = request.get_json() # Lay du lieu json tu request body
2025-12-08 10:06:44 +00:00
user_id = request.headers.get("X-User")
2025-12-08 07:50:09 +00:00
if not user_id:
return jsonify({"error": "user_id is required"}), 400
2025-12-08 10:06:44 +00:00
2025-12-08 07:50:09 +00:00
# Call CRUD function to update profile
2025-12-08 10:06:44 +00:00
response, status, header = crud.update_profile(user_id, data)
return jsonify(response), status, header
2025-12-08 07:50:09 +00:00
except Exception as e:
return jsonify({"error": str(e)}), 500
def make_get_request():
try:
user_id = request.headers.get("X-User")
if not user_id:
return jsonify({"error": "user_id is required"}), 400
2025-12-08 10:06:44 +00:00
response, status, header = crud.get_profile(user_id)
return jsonify(response), status, header
2025-12-08 07:50:09 +00:00
except Exception as e:
return jsonify({"error": str(e)}), 500