ํ์๊ฐ์ API ์์ฑํ๊ธฐ
์ด๋ฉ์ผ ํ์์ด ์ฌ๋ฐ๋ฅธ์ง ํ์ธํ๊ธฐ ์ํด ์ฌ์ฉํ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์น
pip install email-validator
pip install passlib
pip install psycopg2-binary
DB์ postman์ ์ฐ๋ํ๋ ๋ฐฉ๋ฒ์ ์๋ ๊ธ์ ์ฐธ๊ณ ํ๋ค
https://codebunny99.tistory.com/87
MySQL DB๋ฅผ RestFul API๋ก GETํ๊ธฐ(DB์ ์๋ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๋ API ๋ง๋ค๊ธฐ)
Postman GET์ Request๋ Query String(Query Parameters)์ ์ฌ์ฉํ๋ค- offset : 0- limit : 25 DB ์ค๊ณํ๊ธฐ ์ฝ๋ ์ ๋ ฅํ๊ณ ์คํํ๊ธฐ (๋น๋ฐ๋ฒํธ ๋ถ๋ถ ์์ ํด์ ์ ๋ ฅํ๋ค)-- recipe_db ์๋ง ์ ์ฉ์ผ๋ก ์ ์ํ ์
codebunny99.tistory.com
app.py ์์ฑํ๊ธฐ
user.py ๊ธฐ๋ณธ ํ ๋ง๋ค๊ธฐ
from flask import request
from mysql.connector import Error
from flask_restful import Resource # ์์๋ฐ๊ธฐ ์ํด Resource ์ํฌํธํ๋ค
class UserRegisterResource(Resource) :
def post(self):
return
user.py ์์ฑ
from email_validator import EmailNotValidError, validate_email # 3. ์ถ๊ฐ
from flask import request
from mysql.connector import Error
from flask_restful import Resource
from mysql_connection import get_connection
from utils import hash_password
class UserRegisterResource(Resource) :
def post(self):
# 1. ํด๋ผ์ด์ธํธ๊ฐ ๋ณด๋ธ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ค๋ค
data = request.get_json()
print(data)
# 2. ๋ฐ์ดํฐ๊ฐ ๋ชจ๋ ์๋์ง ํ์ธ - ๋ฐ์ดํฐ๊ฐ ์๊ฑฐ๋ ๋๋ฝ๋ ๊ฒฝ์ฐ์๋
# {"result":"fail"} ๋ก 400 bad request๋ก ์๋ดํ๋๋ก ์ฝ๋๋ฅผ ์์ฑํ๋ค
# if (data.get('email') is None or data.get('email').strip() == '' or\
# data.get('username') is None or data.get('username').strip() == '' or\
# data.get('password' is None or data.get('password').strip() == '')):
# return {"result": "fail"}, 400
# 2-1 ์์ด์ผ ํ ์ปฌ๋ผ ์์ฒด๊ฐ ์๋ ๊ฒฝ์ฐ
if 'email' not in data or 'username' not in data or 'password' not in data :
print('error')
return {"result" : "fail"}, 400
# 2-2 ์ปฌ๋ผ์ ์์ง๋ง ๋ฐ์ดํฐ๊ฐ ๋น์ด์๋ ๊ฒฝ์ฐ strip์ ๊ณต๋ฐฑ ์ ๊ฑฐํ๋ ํจ์์ด๋ค
if data['email'].strip() == '' or data['username'].strip() == '' or data['password'].strip() == '' :
return {"result" : "fail"}, 400
# 3. ์ด๋ฉ์ผ์ฃผ์ ํ์์ด ์ฌ๋ฐ๋ฅธ์ง ํ์ธํ๋ค
try :
validate_email(data['email'])
except EmailNotValidError as e :
return {'result' : 'fail', 'error' : str(e)}, 400
# 4. ๋น๋ฐ๋ฒํธ ๊ธธ์ด๊ฐ ์ ํจํ์ง ์ฒดํฌํ๋ค ์) 4์๋ฆฌ ์ด์ 12์๋ฆฌ ์ดํ
if len(data['password']) < 4 or len(data['password']) > 12 :
return {'result' : 'fail'}, 400
# 5. ๋น๋ฐ๋ฒํธ๋ฅผ ์ํธํํ๋ค
password = hash_password(data['password'])
print(password)
# 6. DB์ ์ ์ฅํ๋ค
try :
### 1. DB์ ์ฐ๊ฒฐ
connection = get_connection()
### 2. ์ฟผ๋ฆฌ๋ฌธ ๋ง๋ค๊ธฐ
query = '''insert into user
(username, email, password)
values
( %s , %s , %s);'''
### 3. ์ฟผ๋ฆฌ์ ๋งค์นญ๋๋ ๋ณ์ ์ฒ๋ฆฌ \
record = (data['username'],data['email'],password)
### 4. ์ปค์๋ฅผ ๊ฐ์ ธ์จ๋ค
cursor = connection.cursor()
### 5. ์ฟผ๋ฆฌ๋ฌธ์ ์ปค์๋ก ์คํํ๋ค
cursor.execute(query, record)
### 6. DB์ ์์ ํ ๋ฐ์ํ๊ธฐ ์ํด์๋ commitํ๋ค ์ํ๋ฉด rollback๋จ
connection.commit()
### 7. DB์ ํ์๊ฐ์
ํ์ฌ, user ํ
์ด๋ธ์ insert๋ ํ userํ
์ด๋ธ์ id ๊ฐ์ ๊ฐ์ ธ์์ผ ํ๋ค
user_id = cursor.lastrowid
### 8. ์์ ํด์
cursor.close()
connection.close()
except Error as e :
if cursor is not None :
cursor.close()
if connection is not None :
connection.close()
return {'result':'fail'}, 500
# 7. ์๋ตํ ๋ฐ์ดํฐ๋ฅผ JSON์ผ๋ก ๋ง๋ค์ด์ ๋ฆฌํด.
return { 'result' : 'success' , 'user_id' : user_id } , 200 # 200์ ๋ํดํธ๊ฐ์ด๋ฏ๋ก ์์จ๋ ๋จ
ํ์ผ์ ์ ์ฅํ์ฌ vscode ํฐ๋ฏธ๋ cmd ์ฐฝ์ flask run ํ๋ค.
DB์ ๊ฐ์ ํ์ธํด๋ณด๊ธฐ
๋ ์ํผ ๋ฆฌ์คํธ๋ฅผ ์ถ๊ฐํ ๋ user_id๋ฅผ ๋ฐ์ํ๋๋ก ํ๊ธฐ
recipe.py์ query๋ฌธ ๋ณ๊ฒฝ (user_id ์ถ๊ฐ)
query = '''insert into recipe
(user_id, name, description, num_of_servings, cook_time, directions)
values
( %s , %s , %s , %s , %s , %s);'''
record = (data['user_id'],data['name'],data['description'],data['num_of_servings'],data['cook_time'],data['directions'])
DB์ ๊ฐ๋ณด๋ฉด recipe_db์ recipe ํ ์ด๋ธ์ user_id๊ฐ 3์ผ๋ก ์ถ๊ฐ๋์๋ค.