본문 바로가기

프로그래머스 데브 코스/TIL

[6기] 프로그래머스 인공지능 데브코스 15일차 TIL

0915

4주차-Day5) Mission 1. My New Assistant

Flask 실습 미션

from flask import Flask, jsonify, request

app = Flask(__name__)

corini = {
    "name" : "super-corini"
}

weapon = [
    {"id": 1, "name": "Gun", "stock":120},
    {"id": 2, "name": "Knife", "stock":190},
    {"id": 3, "name": "Bomb", "stock":60}
]

@app.route('/')
def hello_flask():
    return "Hello Strak!"

@app.route('/whoami')
def get_gitid():
    return corini.get("name")

@app.route('/echo')
def get_string():
    return request.args.get('string', '')

@app.route('/weapon')
def get_weapon():
    return jsonify({"weapon" : weapon})


@app.route('/weapon', methods=['POST'])
def create_weapon(): # Request가 JSON이라고 가정
    request_date = request.get_json() 
    new_weapon = {   
        "id" : len(weapon)+1,
        "name" : request_date['name'],
        "stock" : request_date['stock']
    }
    weapon.append(new_weapon)
    return jsonify(weapon)


@app.route('/weapon/<int:id>', methods=['PUT'])
def update_weapon(id):
    request_date = request.get_json() 
    new_weapon = {   
        "id" : id,
        "name" : request_date['name'],
        "stock" : request_date['stock']
    }
    weapon.insert(id-1, new_weapon)
    weapon.pop(id)
    return jsonify(weapon)


@app.route('/weapon/<int:id>', methods=['DELETE'])
def delete_weapon(id):
    weapon.pop(id-1)
    return jsonify(weapon)


if __name__ == '__main__':
    app.run()

 

 

아래는 미션 코드의 API Docs


Flask App Guide

INFO

ID 조회

  • URL : /whoami
  • Method : GET
  • Response :
    super-corini

String 인자 echo

  • URL : /echo?string="string"
  • Method : GET
  • Required : /echo?string=echo-string
  • Response :
    echo-string

 

 

Data

데이터 타입

  • id|int : 인덱스
  • name|string : weapon 종류
  • stock|int : weapon 수량

 

 

Service

데이터 조회

  • URL : /weapon
  • Method : GET
  • Response :
    {
      "weapon": [
          {
              "id": 1,
              "name": "Gun",
              "stock": 120
          },
          {
              "id": 2,
              "name": "Knife",
              "stock": 190
          },
          {
              "id": 3,
              "name": "Bomb",
              "stock": 60
          }
      ]
    }

 

데이터 추가

  • URL : /weapon
  • Method : POST
  • Required : { "name" : "IFV", "stock" : "5"}
  • Response :
    [
      {
          "id": 1,
          "name": "Gun",
          "stock": 120
      },
      {
          "id": 2,
          "name": "Knife",
          "stock": 190
      },
      {
          "id": 3,
          "name": "Bomb",
          "stock": 60
      },
      {
          "id": 4,
          "name": "IFV",
          "stock": 5
      }
    ]

 

데이터 수정

  • URL : /weapon/int:id
  • Method : PUT
  • Required : /weapon/1 { "name" : "Gun", "stock" : "110"}
  • Response :
    [
      {
          "id": 1,
          "name": "Gun",
          "stock": 110
      },
      {
          "id": 2,
          "name": "Knife",
          "stock": 190
      },
      {
          "id": 3,
          "name": "Bomb",
          "stock": 60
      }
    ]

 

데이터 삭제

  • URL : /weapon/int:id
  • Method : DELETE
  • Required : /weapon/1
  • Response :
    [
      {
          "id": 2,
          "name": "Knife",
          "stock": 190
      },
      {
          "id": 3,
          "name": "Bomb",
          "stock": 60
      }
    ]