API Server 만들기
POST API 생성
./app/__init__.py
from flask import Flask
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
class CreateBot(Resource):
def post(self):
return {'status': 'success'}
def get(self):
return {'status': 'get_success'}
api.add_resource(CreateBot, '/bot')
./server.py
from burne import app
import optparse
if __name__ == '__main__':
parser = optparse.OptionParser()
parser.add_option( '--port', dest='port', default=8000, type='int')
parser.add_option( '--debug', dest='debug', action='store_true', default=False)
(options, args) = parser.parse_args()
app.run(debug=True, port=options.port)
Run Server & Call API
$ python server.py
curl -X POST http://localhost:5000/bot
Payload parsing 하기
reqparse
를 사용하여 payload 파싱하기
from flask_restful import Resource, Api
from flask_restful import reqparse
## (...)
class CreateBot(Resource):
def post(self):
try:
parser = reqparse.RequestParser()
parser.add_argument('id', type=str)
parser.add_argument('config', type=str)
args = parser.parse_args()
_id = args['id']
_config = args['config']
return {'botId': _id, 'config': _config}
except Exception as e:
return {'error': str(e)}
## (...)
payload 와 함께 API call 날려보기
curl -X POST \
http://localhost:5000/bot \
-H 'content-type: application/json' \
-d '{
"id": "bot000",
"config": "1"
}'
Last updated