diff --git a/homeworks/homework_06_web/data.json b/homeworks/homework_06_web/data.json new file mode 100644 index 00000000..f78ebe01 --- /dev/null +++ b/homeworks/homework_06_web/data.json @@ -0,0 +1,23 @@ +{ + "2": { + "departure_time": "11:00", + "arrival_time": "12:00", + "travel_time": "01:00", + "destination_airport": "Sheremetievo", + "type_aircraft": "TU" + }, + "3": { + "departure_time": "06:00", + "arrival_time": "08:00", + "travel_time": "02:00", + "destination_airport": "Sheremetievo", + "type_aircraft": "TU" + }, + "4": { + "departure_time": "10:00", + "arrival_time": "11:00", + "travel_time": "01:00", + "destination_airport": "Vnukovo", + "type_aircraft": "TU" + } +} \ No newline at end of file diff --git a/homeworks/homework_06_web/data_processing.py b/homeworks/homework_06_web/data_processing.py new file mode 100644 index 00000000..46f83110 --- /dev/null +++ b/homeworks/homework_06_web/data_processing.py @@ -0,0 +1,105 @@ +import json +import sys +from os import path +from marshmallow import Schema, fields, ValidationError + + +class Cnt: + def __init__(self): + self.cnt = 0 + if path.isfile("data.json"): + with open("data.json", 'r', encoding='utf-8')as file: + data = json.loads(file.read()) + if len(data) > 0: + self.cnt = max(data.keys()) + + def new_id(self): + self.cnt += 1 + return str(self.cnt) + + +c = Cnt() + + +class Flight(Schema): + departure_time = fields.Time(required=True) + arrival_time = fields.Time(required=True) + travel_time = fields.Time(required=True) + destination_airport = fields.Str(required=True) + type_aircraft = fields.Str(required=True) + + +def is_valid(entry): + schema = Flight() + try: + schema.load(entry) + return True + except ValidationError: + return False + + +def select_all(sort_by, filter_field, filter_value): + with open("data.json", 'r', encoding='utf-8')as file: + res = json.loads(file.read()) + if len(res) == 0: + res = [] + else: + res = [{"id_flight": i, "flight": res[i]} for i in res] + if filter_field is not None and filter_value is not None and len(res) > 0: + try: + res = [i for i in res if i["flight"][filter_field] == filter_value] + except BaseException: + return None + if sort_by is not None and len(res) > 0: + try: + res.sort(key=lambda a: a["flight"][sort_by]) + except BaseException: + return None + return res + + +def select(id_flight): + with open("data.json", 'r', encoding='utf-8')as file: + data = json.loads(file.read()) + try: + return True, data[str(id_flight)] + except BaseException: + return False, "" + + +def insert(entry): + with open("data.json", 'r', encoding='utf-8')as file: + data = json.loads(file.read()) + if is_valid(entry): + key = c.new_id() + data[key] = entry + with open("data.json", 'w', encoding='utf-8')as file: + json.dump(data, file, ensure_ascii=False, indent=4) + return key + return None + + +def update(id_flight, flight): + with open("data.json", 'r', encoding='utf-8')as file: + data = json.loads(file.read()) + if is_valid(flight): + try: + data[str(id_flight)] = flight + with open("data.json", 'w', encoding='utf-8')as file: + json.dump(data, file, ensure_ascii=False, indent=4) + return True, "" + except BaseException: + pass + return False, '''id_flight not exist or flight format not valid''' + + +def delete(id_flight): + with open("data.json", 'r', encoding='utf-8')as file: + data = json.loads(file.read()) + try: + del data[str(id_flight)] + with open("data.json", 'w', encoding='utf-8')as file: + json.dump(data, file, ensure_ascii=False, indent=4) + return True, "" + except BaseException: + return False, "id_flight not exist" diff --git a/homeworks/homework_06_web/flight_service.logging b/homeworks/homework_06_web/flight_service.logging new file mode 100644 index 00000000..71b136f4 --- /dev/null +++ b/homeworks/homework_06_web/flight_service.logging @@ -0,0 +1,31 @@ +2019-11-02 19:40:13,980 - werkzeug - INFO - * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) +2019-11-02 19:40:15,725 - flask.app - INFO - {'url': 'http://localhost:5000/show_flights', 'args': {}, 'body': None, 'req_method': 'GET', 'duration': 0.0004, 'response': , 'http_status': 200} +2019-11-02 19:40:15,725 - werkzeug - INFO - 127.0.0.1 - - [02/Nov/2019 19:40:15] "GET /show_flights HTTP/1.1" 200 - +2019-11-02 19:40:15,734 - flask.app - INFO - {'url': 'http://localhost:5000/new_flight', 'args': {}, 'body': {'departure_time': '10:00', 'arrival_time': '13:00', 'travel_time': '03:00', 'destination_airport': 'Vnukovo', 'type_aircraft': 'TU'}, 'req_method': 'POST', 'duration': 0.0016, 'response': , 'http_status': 200} +2019-11-02 19:40:15,735 - werkzeug - INFO - 127.0.0.1 - - [02/Nov/2019 19:40:15] "POST /new_flight HTTP/1.1" 200 - +2019-11-02 19:40:15,742 - flask.app - INFO - {'url': 'http://localhost:5000/new_flight', 'args': {}, 'body': {'departure_time': '11:00', 'arrival_time': '12:00', 'travel_time': '01:00', 'destination_airport': 'Sheremetievo', 'type_aircraft': 'TU'}, 'req_method': 'POST', 'duration': 0.0009, 'response': , 'http_status': 200} +2019-11-02 19:40:15,742 - werkzeug - INFO - 127.0.0.1 - - [02/Nov/2019 19:40:15] "POST /new_flight HTTP/1.1" 200 - +2019-11-02 19:40:15,749 - flask.app - INFO - {'url': 'http://localhost:5000/new_flight', 'args': {}, 'body': {'departure_time': '12:00', 'arrival_time': '25:00', 'travel_time': '02:00', 'destination_airport': 'Vnukovo', 'type_aircraft': 'TU'}, 'req_method': 'POST', 'duration': 0.0015, 'response': , 'http_status': 400} +2019-11-02 19:40:15,750 - werkzeug - INFO - 127.0.0.1 - - [02/Nov/2019 19:40:15] "POST /new_flight HTTP/1.1" 400 - +2019-11-02 19:40:15,762 - flask.app - INFO - {'url': 'http://localhost:5000/show_flights', 'args': {}, 'body': None, 'req_method': 'GET', 'duration': 0.0006, 'response': , 'http_status': 200} +2019-11-02 19:40:15,765 - werkzeug - INFO - 127.0.0.1 - - [02/Nov/2019 19:40:15] "GET /show_flights HTTP/1.1" 200 - +2019-11-02 19:40:15,774 - flask.app - INFO - {'url': 'http://localhost:5000/show_flight/1', 'args': {}, 'body': None, 'req_method': 'GET', 'duration': 0.0004, 'response': , 'http_status': 200} +2019-11-02 19:40:15,774 - werkzeug - INFO - 127.0.0.1 - - [02/Nov/2019 19:40:15] "GET /show_flight/1 HTTP/1.1" 200 - +2019-11-02 19:40:15,780 - flask.app - INFO - {'url': 'http://localhost:5000/show_flight/33', 'args': {}, 'body': None, 'req_method': 'GET', 'duration': 0.0018, 'response': , 'http_status': 400} +2019-11-02 19:40:15,781 - werkzeug - INFO - 127.0.0.1 - - [02/Nov/2019 19:40:15] "GET /show_flight/33 HTTP/1.1" 400 - +2019-11-02 19:40:15,790 - flask.app - INFO - {'url': 'http://localhost:5000/update_flight', 'args': {}, 'body': {'id_flight': '1', 'flight': {'departure_time': '16:00', 'arrival_time': '17:00', 'travel_time': '01:00', 'destination_airport': 'Vnukovo', 'type_aircraft': 'TU'}}, 'req_method': 'PUT', 'duration': 0.0011, 'response': , 'http_status': 200} +2019-11-02 19:40:15,790 - werkzeug - INFO - 127.0.0.1 - - [02/Nov/2019 19:40:15] "PUT /update_flight HTTP/1.1" 200 - +2019-11-02 19:40:15,797 - flask.app - INFO - {'url': 'http://localhost:5000/update_flight', 'args': {}, 'body': {'id_flight': '3', 'flight': {'departure_time': 1600, 'arrival_time': '17:00', 'travel_time': '01:00', 'destination_airport': 'Vnukovo', 'type_aircraft': 'TU'}}, 'req_method': 'PUT', 'duration': 0.003, 'response': , 'http_status': 400} +2019-11-02 19:40:15,797 - werkzeug - INFO - 127.0.0.1 - - [02/Nov/2019 19:40:15] "PUT /update_flight HTTP/1.1" 400 - +2019-11-02 19:40:15,807 - flask.app - INFO - {'url': 'http://localhost:5000/delete_flight', 'args': {}, 'body': {'id_flight': '1'}, 'req_method': 'DELETE', 'duration': 0.001, 'response': , 'http_status': 200} +2019-11-02 19:40:15,808 - werkzeug - INFO - 127.0.0.1 - - [02/Nov/2019 19:40:15] "DELETE /delete_flight HTTP/1.1" 200 - +2019-11-02 19:40:15,821 - flask.app - INFO - {'url': 'http://localhost:5000/delete_flight', 'args': {}, 'body': {'id_flight': '4'}, 'req_method': 'DELETE', 'duration': 0.0027, 'response': , 'http_status': 400} +2019-11-02 19:40:15,822 - werkzeug - INFO - 127.0.0.1 - - [02/Nov/2019 19:40:15] "DELETE /delete_flight HTTP/1.1" 400 - +2019-11-02 19:40:15,827 - flask.app - INFO - {'url': 'http://localhost:5000/show_flights', 'args': {}, 'body': None, 'req_method': 'GET', 'duration': 0.0006, 'response': , 'http_status': 200} +2019-11-02 19:40:15,830 - werkzeug - INFO - 127.0.0.1 - - [02/Nov/2019 19:40:15] "GET /show_flights HTTP/1.1" 200 - +2019-11-02 19:40:15,842 - flask.app - INFO - {'url': 'http://localhost:5000/new_flight', 'args': {}, 'body': {'departure_time': '06:00', 'arrival_time': '08:00', 'travel_time': '02:00', 'destination_airport': 'Sheremetievo', 'type_aircraft': 'TU'}, 'req_method': 'POST', 'duration': 0.0013, 'response': , 'http_status': 200} +2019-11-02 19:40:15,842 - werkzeug - INFO - 127.0.0.1 - - [02/Nov/2019 19:40:15] "POST /new_flight HTTP/1.1" 200 - +2019-11-02 19:40:15,853 - flask.app - INFO - {'url': 'http://localhost:5000/new_flight', 'args': {}, 'body': {'departure_time': '10:00', 'arrival_time': '11:00', 'travel_time': '01:00', 'destination_airport': 'Vnukovo', 'type_aircraft': 'TU'}, 'req_method': 'POST', 'duration': 0.0023, 'response': , 'http_status': 200} +2019-11-02 19:40:15,855 - werkzeug - INFO - 127.0.0.1 - - [02/Nov/2019 19:40:15] "POST /new_flight HTTP/1.1" 200 - +2019-11-02 19:40:15,862 - flask.app - INFO - {'url': 'http://localhost:5000/show_flights?sort_by=arrival_time&filter_field=destination_airport&filter_value=Sheremetievo', 'args': {'sort_by': 'arrival_time', 'filter_field': 'destination_airport', 'filter_value': 'Sheremetievo'}, 'body': None, 'req_method': 'GET', 'duration': 0.0017, 'response': , 'http_status': 200} +2019-11-02 19:40:15,863 - werkzeug - INFO - 127.0.0.1 - - [02/Nov/2019 19:40:15] "GET /show_flights?sort_by=arrival_time&filter_field=destination_airport&filter_value=Sheremetievo HTTP/1.1" 200 - diff --git a/homeworks/homework_06_web/manage.py b/homeworks/homework_06_web/manage.py new file mode 100644 index 00000000..ce65af27 --- /dev/null +++ b/homeworks/homework_06_web/manage.py @@ -0,0 +1,106 @@ +from flask import Flask, jsonify, request, g +import data_processing as dp +from os import path +import json +import logging +import time + +app = Flask(__name__) + + +@app.route("/show_flights", methods=["GET"]) +def show_flights(): + sort_by = request.args.get("sort_by") + filter_field = request.args.get("filter_field", default=None) + filter_value = request.args.get("filter_value", default=None) + resp = jsonify( + success=True, + result=dp.select_all( + sort_by, + filter_field, + filter_value)) + return resp + + +@app.route("/show_flight/", methods=["GET"]) +def show_flight(id_flight): + exist, flight = dp.select(id_flight) + if exist: + resp = jsonify(success=True, flight=flight) + resp.status_code = 200 + else: + resp = jsonify(success=False) + resp.status_code = 400 + return resp + + +@app.route("/new_flight", methods=["POST"]) +def new_flight(): + res = dp.insert(request.json) + if res is not None: + resp = jsonify(success=True, id_flight=res) + resp.status_code = 200 + else: + resp = jsonify(success=False) + resp.status_code = 400 + return resp + + +@app.route("/update_flight", methods=["PUT"]) +def update_flight(): + body = request.json + if "id_flight" not in body or "flight" not in body: + return False, '''"id_flight" of "flight not scecified in request''' + result, message = dp.update(body["id_flight"], body["flight"]) + if result: + resp = jsonify(success=True) + resp.status_code = 200 + else: + resp = jsonify(success=False, message=message) + resp.status_code = 400 + return resp + + +@app.route("/delete_flight", methods=["DELETE"]) +def delete_flights(): + body = request.json + if "id_flight" not in body: + return False, '''"id_flight" not scecified in request''' + res, message = dp.delete(body["id_flight"]) + if res: + resp = jsonify(success=True) + resp.status_code = 200 + else: + resp = jsonify(success=False, message=message) + resp.status_code = 400 + return resp + + +def start_server(): + if not path.isfile("data.json"): + with open("data.json", "w", encoding='utf-8') as file: + json.dump(dict(), file, ensure_ascii=False, indent=4) + + logging.basicConfig( + filename="flight_service.logging", + level=logging.INFO, + format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') + + @app.before_request + def timer_on(): + g.start_handlefunc = time.time() + + @app.after_request + def log_request(resp): + dur = round(time.time() - g.start_handlefunc, 4) + app.logger.info({ + "url": request.url, + "args": dict(request.args), + "body": request.json, + "req_method": request.method, + "duration": dur, + "response": resp, + "http_status": resp.status_code + }) + return resp + app.run() diff --git a/homeworks/homework_06_web/request_dumps.txt b/homeworks/homework_06_web/request_dumps.txt new file mode 100644 index 00000000..6d824d29 --- /dev/null +++ b/homeworks/homework_06_web/request_dumps.txt @@ -0,0 +1,273 @@ +0 Urls(method='get', url='http://localhost:5000/show_flights', headers=None, json=None, params=None) 200 True +< GET /show_flights HTTP/1.1 +< Host: localhost:5000 +< User-Agent: python-requests/2.18.4 +< Accept-Encoding: gzip, deflate +< Accept: */* +< Connection: keep-alive +< + +> HTTP/1.0 200 OK +> Content-Type: application/json +> Content-Length: 29 +> Server: Werkzeug/0.16.0 Python/3.6.8 +> Date: Sat, 02 Nov 2019 16:40:15 GMT +> +{"result":[],"success":true} + +1 Urls(method='post', url='http://localhost:5000/new_flight', headers=None, json={'departure_time': '10:00', 'arrival_time': '13:00', 'travel_time': '03:00', 'destination_airport': 'Vnukovo', 'type_aircraft': 'TU'}, params=None) 200 True +< POST /new_flight HTTP/1.1 +< Host: localhost:5000 +< User-Agent: python-requests/2.18.4 +< Accept-Encoding: gzip, deflate +< Accept: */* +< Connection: keep-alive +< Content-Length: 133 +< Content-Type: application/json +< +< {"departure_time": "10:00", "arrival_time": "13:00", "travel_time": "03:00", "destination_airport": "Vnukovo", "type_aircraft": "TU"} +> HTTP/1.0 200 OK +> Content-Type: application/json +> Content-Length: 33 +> Server: Werkzeug/0.16.0 Python/3.6.8 +> Date: Sat, 02 Nov 2019 16:40:15 GMT +> +{"id_flight":"1","success":true} + +2 Urls(method='post', url='http://localhost:5000/new_flight', headers=None, json={'departure_time': '11:00', 'arrival_time': '12:00', 'travel_time': '01:00', 'destination_airport': 'Sheremetievo', 'type_aircraft': 'TU'}, params=None) 200 True +< POST /new_flight HTTP/1.1 +< Host: localhost:5000 +< User-Agent: python-requests/2.18.4 +< Accept-Encoding: gzip, deflate +< Accept: */* +< Connection: keep-alive +< Content-Length: 138 +< Content-Type: application/json +< +< {"departure_time": "11:00", "arrival_time": "12:00", "travel_time": "01:00", "destination_airport": "Sheremetievo", "type_aircraft": "TU"} +> HTTP/1.0 200 OK +> Content-Type: application/json +> Content-Length: 33 +> Server: Werkzeug/0.16.0 Python/3.6.8 +> Date: Sat, 02 Nov 2019 16:40:15 GMT +> +{"id_flight":"2","success":true} + +3 Urls(method='post', url='http://localhost:5000/new_flight', headers=None, json={'departure_time': '12:00', 'arrival_time': '25:00', 'travel_time': '02:00', 'destination_airport': 'Vnukovo', 'type_aircraft': 'TU'}, params=None) 400 False +< POST /new_flight HTTP/1.1 +< Host: localhost:5000 +< User-Agent: python-requests/2.18.4 +< Accept-Encoding: gzip, deflate +< Accept: */* +< Connection: keep-alive +< Content-Length: 133 +< Content-Type: application/json +< +< {"departure_time": "12:00", "arrival_time": "25:00", "travel_time": "02:00", "destination_airport": "Vnukovo", "type_aircraft": "TU"} +> HTTP/1.0 400 BAD REQUEST +> Content-Type: application/json +> Content-Length: 18 +> Server: Werkzeug/0.16.0 Python/3.6.8 +> Date: Sat, 02 Nov 2019 16:40:15 GMT +> +{"success":false} + +4 Urls(method='get', url='http://localhost:5000/show_flights', headers=None, json=None, params=None) 200 True +< GET /show_flights HTTP/1.1 +< Host: localhost:5000 +< User-Agent: python-requests/2.18.4 +< Accept-Encoding: gzip, deflate +< Accept: */* +< Connection: keep-alive +< + +> HTTP/1.0 200 OK +> Content-Type: application/json +> Content-Length: 337 +> Server: Werkzeug/0.16.0 Python/3.6.8 +> Date: Sat, 02 Nov 2019 16:40:15 GMT +> +{"result":[{"flight":{"arrival_time":"13:00","departure_time":"10:00","destination_airport":"Vnukovo","travel_time":"03:00","type_aircraft":"TU"},"id_flight":"1"},{"flight":{"arrival_time":"12:00","departure_time":"11:00","destination_airport":"Sheremetievo","travel_time":"01:00","type_aircraft":"TU"},"id_flight":"2"}],"success":true} + +5 Urls(method='get', url='http://localhost:5000/show_flight/1', headers=None, json=None, params=None) 200 True +< GET /show_flight/1 HTTP/1.1 +< Host: localhost:5000 +< User-Agent: python-requests/2.18.4 +< Accept-Encoding: gzip, deflate +< Accept: */* +< Connection: keep-alive +< + +> HTTP/1.0 200 OK +> Content-Type: application/json +> Content-Length: 151 +> Server: Werkzeug/0.16.0 Python/3.6.8 +> Date: Sat, 02 Nov 2019 16:40:15 GMT +> +{"flight":{"arrival_time":"13:00","departure_time":"10:00","destination_airport":"Vnukovo","travel_time":"03:00","type_aircraft":"TU"},"success":true} + +6 Urls(method='get', url='http://localhost:5000/show_flight/33', headers=None, json=None, params=None) 400 False +< GET /show_flight/33 HTTP/1.1 +< Host: localhost:5000 +< User-Agent: python-requests/2.18.4 +< Accept-Encoding: gzip, deflate +< Accept: */* +< Connection: keep-alive +< + +> HTTP/1.0 400 BAD REQUEST +> Content-Type: application/json +> Content-Length: 18 +> Server: Werkzeug/0.16.0 Python/3.6.8 +> Date: Sat, 02 Nov 2019 16:40:15 GMT +> +{"success":false} + +7 Urls(method='put', url='http://localhost:5000/update_flight', headers=None, json={'id_flight': '1', 'flight': {'departure_time': '16:00', 'arrival_time': '17:00', 'travel_time': '01:00', 'destination_airport': 'Vnukovo', 'type_aircraft': 'TU'}}, params=None) 200 True +< PUT /update_flight HTTP/1.1 +< Host: localhost:5000 +< User-Agent: python-requests/2.18.4 +< Accept-Encoding: gzip, deflate +< Accept: */* +< Connection: keep-alive +< Content-Length: 163 +< Content-Type: application/json +< +< {"id_flight": "1", "flight": {"departure_time": "16:00", "arrival_time": "17:00", "travel_time": "01:00", "destination_airport": "Vnukovo", "type_aircraft": "TU"}} +> HTTP/1.0 200 OK +> Content-Type: application/json +> Content-Length: 17 +> Server: Werkzeug/0.16.0 Python/3.6.8 +> Date: Sat, 02 Nov 2019 16:40:15 GMT +> +{"success":true} + +8 Urls(method='put', url='http://localhost:5000/update_flight', headers=None, json={'id_flight': '3', 'flight': {'departure_time': 1600, 'arrival_time': '17:00', 'travel_time': '01:00', 'destination_airport': 'Vnukovo', 'type_aircraft': 'TU'}}, params=None) 400 False +< PUT /update_flight HTTP/1.1 +< Host: localhost:5000 +< User-Agent: python-requests/2.18.4 +< Accept-Encoding: gzip, deflate +< Accept: */* +< Connection: keep-alive +< Content-Length: 160 +< Content-Type: application/json +< +< {"id_flight": "3", "flight": {"departure_time": 1600, "arrival_time": "17:00", "travel_time": "01:00", "destination_airport": "Vnukovo", "type_aircraft": "TU"}} +> HTTP/1.0 400 BAD REQUEST +> Content-Type: application/json +> Content-Length: 77 +> Server: Werkzeug/0.16.0 Python/3.6.8 +> Date: Sat, 02 Nov 2019 16:40:15 GMT +> +{"message":"id_flight not exist or flight format not valid","success":false} + +9 Urls(method='delete', url='http://localhost:5000/delete_flight', headers=None, json={'id_flight': '1'}, params=None) 200 True +< DELETE /delete_flight HTTP/1.1 +< Host: localhost:5000 +< User-Agent: python-requests/2.18.4 +< Accept-Encoding: gzip, deflate +< Accept: */* +< Connection: keep-alive +< Content-Length: 18 +< Content-Type: application/json +< +< {"id_flight": "1"} +> HTTP/1.0 200 OK +> Content-Type: application/json +> Content-Length: 17 +> Server: Werkzeug/0.16.0 Python/3.6.8 +> Date: Sat, 02 Nov 2019 16:40:15 GMT +> +{"success":true} + +10 Urls(method='delete', url='http://localhost:5000/delete_flight', headers=None, json={'id_flight': '4'}, params=None) 400 False +< DELETE /delete_flight HTTP/1.1 +< Host: localhost:5000 +< User-Agent: python-requests/2.18.4 +< Accept-Encoding: gzip, deflate +< Accept: */* +< Connection: keep-alive +< Content-Length: 18 +< Content-Type: application/json +< +< {"id_flight": "4"} +> HTTP/1.0 400 BAD REQUEST +> Content-Type: application/json +> Content-Length: 50 +> Server: Werkzeug/0.16.0 Python/3.6.8 +> Date: Sat, 02 Nov 2019 16:40:15 GMT +> +{"message":"id_flight not exist","success":false} + +11 Urls(method='get', url='http://localhost:5000/show_flights', headers=None, json=None, params=None) 200 True +< GET /show_flights HTTP/1.1 +< Host: localhost:5000 +< User-Agent: python-requests/2.18.4 +< Accept-Encoding: gzip, deflate +< Accept: */* +< Connection: keep-alive +< + +> HTTP/1.0 200 OK +> Content-Type: application/json +> Content-Length: 185 +> Server: Werkzeug/0.16.0 Python/3.6.8 +> Date: Sat, 02 Nov 2019 16:40:15 GMT +> +{"result":[{"flight":{"arrival_time":"12:00","departure_time":"11:00","destination_airport":"Sheremetievo","travel_time":"01:00","type_aircraft":"TU"},"id_flight":"2"}],"success":true} + +12 Urls(method='post', url='http://localhost:5000/new_flight', headers=None, json={'departure_time': '06:00', 'arrival_time': '08:00', 'travel_time': '02:00', 'destination_airport': 'Sheremetievo', 'type_aircraft': 'TU'}, params=None) 200 True +< POST /new_flight HTTP/1.1 +< Host: localhost:5000 +< User-Agent: python-requests/2.18.4 +< Accept-Encoding: gzip, deflate +< Accept: */* +< Connection: keep-alive +< Content-Length: 138 +< Content-Type: application/json +< +< {"departure_time": "06:00", "arrival_time": "08:00", "travel_time": "02:00", "destination_airport": "Sheremetievo", "type_aircraft": "TU"} +> HTTP/1.0 200 OK +> Content-Type: application/json +> Content-Length: 33 +> Server: Werkzeug/0.16.0 Python/3.6.8 +> Date: Sat, 02 Nov 2019 16:40:15 GMT +> +{"id_flight":"3","success":true} + +13 Urls(method='post', url='http://localhost:5000/new_flight', headers=None, json={'departure_time': '10:00', 'arrival_time': '11:00', 'travel_time': '01:00', 'destination_airport': 'Vnukovo', 'type_aircraft': 'TU'}, params=None) 200 True +< POST /new_flight HTTP/1.1 +< Host: localhost:5000 +< User-Agent: python-requests/2.18.4 +< Accept-Encoding: gzip, deflate +< Accept: */* +< Connection: keep-alive +< Content-Length: 133 +< Content-Type: application/json +< +< {"departure_time": "10:00", "arrival_time": "11:00", "travel_time": "01:00", "destination_airport": "Vnukovo", "type_aircraft": "TU"} +> HTTP/1.0 200 OK +> Content-Type: application/json +> Content-Length: 33 +> Server: Werkzeug/0.16.0 Python/3.6.8 +> Date: Sat, 02 Nov 2019 16:40:15 GMT +> +{"id_flight":"4","success":true} + +14 Urls(method='get', url='http://localhost:5000/show_flights', headers=None, json=None, params={'sort_by': 'arrival_time', 'filter_field': 'destination_airport', 'filter_value': 'Sheremetievo'}) 200 True +< GET /show_flights?sort_by=arrival_time&filter_field=destination_airport&filter_value=Sheremetievo HTTP/1.1 +< Host: localhost:5000 +< User-Agent: python-requests/2.18.4 +< Accept-Encoding: gzip, deflate +< Accept: */* +< Connection: keep-alive +< + +> HTTP/1.0 200 OK +> Content-Type: application/json +> Content-Length: 342 +> Server: Werkzeug/0.16.0 Python/3.6.8 +> Date: Sat, 02 Nov 2019 16:40:15 GMT +> +{"result":[{"flight":{"arrival_time":"08:00","departure_time":"06:00","destination_airport":"Sheremetievo","travel_time":"02:00","type_aircraft":"TU"},"id_flight":"3"},{"flight":{"arrival_time":"12:00","departure_time":"11:00","destination_airport":"Sheremetievo","travel_time":"01:00","type_aircraft":"TU"},"id_flight":"2"}],"success":true} + diff --git a/homeworks/homework_06_web/script.py b/homeworks/homework_06_web/script.py new file mode 100644 index 00000000..1007e644 --- /dev/null +++ b/homeworks/homework_06_web/script.py @@ -0,0 +1,111 @@ +from collections import namedtuple +import requests +from requests_toolbelt.utils.dump import dump_all + +Urls = namedtuple('Urls', ['method', 'url', 'headers', 'json', 'params']) + +jsons = { + "first insert": + { + "departure_time": "10:00", + "arrival_time": "13:00", + "travel_time": "03:00", + "destination_airport": "Vnukovo", + "type_aircraft": "TU" + }, + "second insert": + { + "departure_time": "11:00", + "arrival_time": "12:00", + "travel_time": "01:00", + "destination_airport": "Sheremetievo", + "type_aircraft": "TU" + }, + "bad insert": + { + "departure_time": "12:00", + "arrival_time": "25:00", + "travel_time": "02:00", + "destination_airport": "Vnukovo", + "type_aircraft": "TU" + }, + "update": + { + "id_flight": "1", + "flight": + { + "departure_time": "16:00", + "arrival_time": "17:00", + "travel_time": "01:00", + "destination_airport": "Vnukovo", + "type_aircraft": "TU" + } + }, + "bad update": + { + "id_flight": "3", + "flight": + { + "departure_time": 1600, + "arrival_time": "17:00", + "travel_time": "01:00", + "destination_airport": "Vnukovo", + "type_aircraft": "TU" + } + }, + "delete": + { + "id_flight": "1", + }, + "bad delete": + { + "id_flight": "4", + }, + "third insert": + { + "departure_time": "06:00", + "arrival_time": "08:00", + "travel_time": "02:00", + "destination_airport": "Sheremetievo", + "type_aircraft": "TU" + }, + "forth insert": + { + "departure_time": "10:00", + "arrival_time": "11:00", + "travel_time": "01:00", + "destination_airport": "Vnukovo", + "type_aircraft": "TU" + }, +} +params = { + "sort_by": "arrival_time", + "filter_field": "destination_airport", + "filter_value": "Sheremetievo"} + +with open('request_dumps.txt', 'w') as f: + for index, url in enumerate([ + Urls('get', 'http://localhost:5000/show_flights', None, None, None), + Urls('post', 'http://localhost:5000/new_flight', None, jsons["first insert"], None), + Urls('post', 'http://localhost:5000/new_flight', None, jsons["second insert"], None), + Urls('post', 'http://localhost:5000/new_flight', None, jsons["bad insert"], None), + Urls('get', 'http://localhost:5000/show_flights', None, None, None), + Urls('get', 'http://localhost:5000/show_flight/1', None, None, None), + Urls('get', 'http://localhost:5000/show_flight/33', None, None, None), + Urls('put', 'http://localhost:5000/update_flight', None, jsons["update"], None), + Urls('put', 'http://localhost:5000/update_flight', None, jsons["bad update"], None), + Urls('delete', 'http://localhost:5000/delete_flight', None, jsons["delete"], None), + Urls('delete', 'http://localhost:5000/delete_flight', None, jsons["bad delete"], None), + Urls('get', 'http://localhost:5000/show_flights', None, None, None), + Urls('post', 'http://localhost:5000/new_flight', None, jsons["third insert"], None), + Urls('post', 'http://localhost:5000/new_flight', None, jsons["forth insert"], None), + Urls('get', 'http://localhost:5000/show_flights', None, None, params), + ]): + resp = requests.request( + method=url.method, + url=url.url, + headers=url.headers, + json=url.json, + params=url.params) + print(index, url, resp.status_code, resp.ok, file=f) + print(dump_all(resp).decode('utf-8'), file=f) diff --git a/homeworks/homework_06_web/start_server.py b/homeworks/homework_06_web/start_server.py new file mode 100644 index 00000000..33c7bedb --- /dev/null +++ b/homeworks/homework_06_web/start_server.py @@ -0,0 +1,3 @@ +from manage import start_server + +start_server()