From 846dc63dbfc8824d82a42beb378615af471d30c7 Mon Sep 17 00:00:00 2001 From: Ilnur Zinnurov Date: Sun, 10 Nov 2019 23:35:26 +0300 Subject: [PATCH 1/3] =?UTF-8?q?=D0=94=D1=83=D0=BC=D0=B0=D0=BB,=20=D1=87?= =?UTF-8?q?=D1=82=D0=BE=20=D0=BE=D1=82=D0=BF=D1=80=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?,=20=D0=BD=D0=BE=20=D1=81=D0=B5=D0=B9=D1=87=D0=B0=D1=81=20?= =?UTF-8?q?=D0=B7=D0=B0=D0=BC=D0=B5=D1=82=D0=B8=D0=BB,=20=D1=87=D1=82?= =?UTF-8?q?=D0=BE=20=D0=BD=D0=B8=D1=87=D0=B5=D0=B3=D0=BE=20=D0=BD=D0=B5?= =?UTF-8?q?=D1=82=20=D0=BD=D0=B0=20=D1=81=D0=B5=D1=80=D0=B2=D0=B5=D1=80?= =?UTF-8?q?=D0=B5(?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- homeworks/homework_06_web/request_dumps.txt | 4 + homeworks/homework_06_web/rest_api.py | 113 ++++++++++++++++++++ homeworks/homework_06_web/tests.py | 28 +++++ 3 files changed, 145 insertions(+) create mode 100644 homeworks/homework_06_web/request_dumps.txt create mode 100644 homeworks/homework_06_web/rest_api.py create mode 100644 homeworks/homework_06_web/tests.py diff --git a/homeworks/homework_06_web/request_dumps.txt b/homeworks/homework_06_web/request_dumps.txt new file mode 100644 index 00000000..7b58730d --- /dev/null +++ b/homeworks/homework_06_web/request_dumps.txt @@ -0,0 +1,4 @@ +0 Urls(method='post', url='http://localhost:5000/api/flights', headers=None, json={'Departure(GMT)': 2130, 'Arrival(GMT)': 600, 'Travel time': 830, 'Destination': 'VNU', 'Aircraft type': 'IL-56'}, data=None) 201 True +1 Urls(method='get', url='http://localhost:5000/api/flights', headers=None, json=None, data=None) 200 True +2 Urls(method='put', url='http://localhost:5000/api/flights/1', headers=None, json={'Departure(GMT)': 2031, 'Arrival(GMT)': 1000, 'Travel time': 1031, 'Destination': 'Bashni blizneci', 'Aircraft type': 'airbus'}, data=None) 200 True +3 Urls(method='delete', url='http://localhost:5000/api/flights/2', headers=None, json=None, data=None) 204 True diff --git a/homeworks/homework_06_web/rest_api.py b/homeworks/homework_06_web/rest_api.py new file mode 100644 index 00000000..72574460 --- /dev/null +++ b/homeworks/homework_06_web/rest_api.py @@ -0,0 +1,113 @@ +#!flask/bin/python +import os +import logging +from logging.handlers import RotatingFileHandler +from flask import jsonify, request, abort, Flask +from flask import make_response + +""""Это просто гениальная статейка https://habr.com/ru/post/246699/ работал по ней""" + +app = Flask(__name__) +fields = ['Departure(GMT)', 'Arrival(GMT)', 'Travel time', 'Destination', 'Aircraft type'] +flights = { + 'flights': [ + { + 'id': 1, + 'Departure(GMT)': 1424, + 'Arrival(GMT)': 612, + 'Travel time': 812, + 'Destination': u'SVO', + 'Aircraft type': u'AA' + }, + { + 'id': 2, + 'Departure(GMT)': 2000, + 'Arrival(GMT)': 1000, + 'Travel time': 1000, + 'Destination': u'SPV', + 'Aircraft type': u'Boing' + } + ] +} # хранилище полётов + + +@app.route('/api/flights', methods=['GET']) +def get_flights(): + return jsonify({'flights': flights}), 200 + + +@app.route('/api/flights/', methods=['GET']) +def get_flight(flight_id: int): + marker = False + for elem in flights['flights']: + if elem['id'] == flight_id: + marker = True + return jsonify({'flight': elem}) + if marker is False: + abort(404) + + +@app.route('/api/flights/', methods=['PUT']) +def change_flight(flight_id): + flight_tmp = list(filter(lambda f: f['id'] == flight_id, flights['flights']))[0] + if len(flight_tmp) == 0: + abort(404) + if not request.json: + abort(400) + + for field in flight_tmp: + if field in request.json: + flight_tmp[field] = request.json[field] + return jsonify({'task': flight_tmp}, {'flights': flights}) + + +@app.errorhandler(404) +def not_found(error): + return make_response(jsonify({'error': 'Not found'}), 404) + + +@app.route('/api/flights', methods=['POST']) +def create_flight(): + if not request.json: + abort(400) + print(request.data) + print(request.json) + flight_tmp = {'id': flights['flights'][-1]['id'] + 1} if flights['flights'] else {'id': 1} + + for field in fields: + if field in request.json: + flight_tmp[field] = request.json[field] + else: + abort(400) + flights['flights'].append(flight_tmp) + return jsonify(flights['flights']), 201 + + +@app.route('/api/flights/', methods=['DELETE']) +def del_flight(flight_id): + try: + for index, elem in enumerate(flights['flights']): + if elem['id'] == flight_id: + flights['flights'].pop(index) + return jsonify(flights['flights']), 204 + + except IndexError: + print("IndexError") + abort(404) + + +if __name__ == '__main__': + if not app.debug: + if not os.path.exists('logs'): + os.mkdir('logs') + file_handler = RotatingFileHandler('logs/logs.log', maxBytes=10240, + backupCount=10) + file_handler.setFormatter(logging.Formatter( + '%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]')) + file_handler.setLevel(logging.INFO) + app.logger.addHandler(file_handler) + + app.logger.setLevel(logging.INFO) + app.logger.info('START') + app.debug = True + app.run(host='0.0.0.0') diff --git a/homeworks/homework_06_web/tests.py b/homeworks/homework_06_web/tests.py new file mode 100644 index 00000000..77757c79 --- /dev/null +++ b/homeworks/homework_06_web/tests.py @@ -0,0 +1,28 @@ +from collections import namedtuple +import requests +"""from requests_toolbelt.utils.dump import dump_all +тесты взял у кого-то!!!""" + +Urls = namedtuple('Urls', ['method', 'url', 'headers', 'json', 'data']) + +with open('request_dumps.txt', 'w') as f: + for index, url in enumerate([ + Urls('post', 'http://localhost:5000/api/flights', None, { + 'Departure(GMT)': 2130, + 'Arrival(GMT)': 600, + 'Travel time': 830, + 'Destination': u'VNU', + 'Aircraft type': u'IL-56' + }, None), + Urls('get', 'http://localhost:5000/api/flights', None, None, None), + Urls('put', 'http://localhost:5000/api/flights/1', None, { + 'Departure(GMT)': 2031, + 'Arrival(GMT)': 1000, + 'Travel time': 1031, + 'Destination': u'Bashni blizneci', + 'Aircraft type': u'airbus' + }, None), + Urls('delete', 'http://localhost:5000/api/flights/2', None, None, None) + ]): + resp = requests.request(method=url.method, url=url.url, json=url.json, data=url.data) + print(index, url, resp.status_code, resp.ok, file=f) \ No newline at end of file From 02093e29afce29369ed269fd744ca656792b80d8 Mon Sep 17 00:00:00 2001 From: Ilnur Zinnurov Date: Mon, 11 Nov 2019 18:19:27 +0300 Subject: [PATCH 2/3] pep check --- homeworks/homework_06_web/tests.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/homeworks/homework_06_web/tests.py b/homeworks/homework_06_web/tests.py index 77757c79..4009f57d 100644 --- a/homeworks/homework_06_web/tests.py +++ b/homeworks/homework_06_web/tests.py @@ -25,4 +25,5 @@ Urls('delete', 'http://localhost:5000/api/flights/2', None, None, None) ]): resp = requests.request(method=url.method, url=url.url, json=url.json, data=url.data) - print(index, url, resp.status_code, resp.ok, file=f) \ No newline at end of file + print(index, url, resp.status_code, resp.ok, file=f) + \ No newline at end of file From b7c3d1eb9f05c1e1d1b9bc8ae9e0297e3291b26e Mon Sep 17 00:00:00 2001 From: Ilnur Zinnurov Date: Mon, 11 Nov 2019 18:23:01 +0300 Subject: [PATCH 3/3] pep --- homeworks/homework_06_web/tests.py | 1 - 1 file changed, 1 deletion(-) diff --git a/homeworks/homework_06_web/tests.py b/homeworks/homework_06_web/tests.py index 4009f57d..843cffab 100644 --- a/homeworks/homework_06_web/tests.py +++ b/homeworks/homework_06_web/tests.py @@ -26,4 +26,3 @@ ]): resp = requests.request(method=url.method, url=url.url, json=url.json, data=url.data) print(index, url, resp.status_code, resp.ok, file=f) - \ No newline at end of file