Код: Выделить всё
`#%% ALL server logic
from flask import Flask,jsonify,request,make_response
app = Flask(__name__)
@app.route('/')
def nakedRoute():
print('inside the teapot')
return "I'm a teapot", 418
@app.route('/echo/', methods=['POST'])
def serverAPI_ejemplo():
try:
if not request.is_json:
return make_response(jsonify({"error": "I was expecting a JSON"}), 400)
data = request.json['key'] + 1
return jsonify({"key":data}), 200 # Código de estado 200 (OK)
except Exception as e:
return make_response(jsonify({"error": 33}), 500)
#%% Mount the server
if __name__ == "__main__":
app.run(port=5000, debug=True)
если захочу чтобы пройти первый маршрут
Код: Выделить всё
# %% TEST naked router
with app.app_context():
result= nakedRoute()
print(result)
Код: Выделить всё
# %% TEST /echo/ for error 500
with app.app_context():
result= serverAPI_ejemplo()
print(result)
кроме случаев, когда я собираюсь добавить Json в CALL
Код: Выделить всё
# %% TEST /echo/ for a JSON
with app.app_context():
my_JSON = {"key":33}
#...???
result= serverAPI_ejemplo()
print(result)`
EDIT:Мне удалось научиться (спасибо @Dmitri) тестировать звонки внутри ячейки:
Код: Выделить всё
# %% IT WORKS !!!!
if my_JSON == None:
my_JSON = {"key":33}
with app.test_client() as c:
headers = {"Content-Type": "application/json; charset=utf-8"}
response = c.post('/echo/', headers=headers, json=my_JSON)
print(response.get_json())
my_JSON = response.json
причина: попытка изучить и преподавать Python
Подробнее здесь: https://stackoverflow.com/questions/786 ... code-cells