RestFul API

MySQL DB์™€ Postman์„ ์—ฐ๋™ํ•˜์—ฌ ํšŒ์›๊ฐ€์ž… API ๋งŒ๋“ค๊ธฐ

567Rabbit 2024. 5. 22. 11:29

ํšŒ์›๊ฐ€์ž… 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์œผ๋กœ ์ถ”๊ฐ€๋˜์—ˆ๋‹ค.