From 0b2220e4f50c0cf4084eda6ffe94fdc95883841c Mon Sep 17 00:00:00 2001 From: Raskolnikofff Date: Fri, 25 Dec 2020 13:53:34 +0000 Subject: [PATCH 1/3] Add openAPI (swagger) --- main.py | 4 +- swagger.yaml | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++ views.py | 1 + 3 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 swagger.yaml diff --git a/main.py b/main.py index e4a9875..cb44272 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,9 @@ from aiohttp import web from routes import setup_routes +from aiohttp_swagger import * + app = web.Application() setup_routes(app) +setup_swagger(app,swagger_from_file="swagger.yaml") web.run_app(app,host='127.0.0.1') - diff --git a/swagger.yaml b/swagger.yaml new file mode 100644 index 0000000..0226fd4 --- /dev/null +++ b/swagger.yaml @@ -0,0 +1,107 @@ + + +--- +swagger: "2.0" +info: + title: REST API + description: description in repository on github https://github.com/Raskolnikofff/StudyWebServerRestFullAPI + version: 0.1.9 +tags: +- name: "student on course" + description: "Everything about your student" +paths: + /: + get: + summary: "open homepage" + tags: + - "student on course" + responses: + '200': + description: 'OK' + /students: + get: + summary: "find all students on course" + tags: + - "student on course" + responses: + '200': + description: 'OK' + /students/{id}: + get: + parameters: + - name: "id" + in: "path" + description: "ID of student to return" + required: true + type: "integer" + format: "int64" + summary: "Find students with ID" + tags: + - "student on course" + responses: + '200': + description: 'OK' + post: + parameters: + - name: "id" + in: "path" + description: "ID of student to return" + required: true + type: "integer" + format: "int64" + - name: 'name' + in: 'formData' + description: "students name" + required: true + type: "string" + - name: 'surname' + in: 'formData' + description: "students surname" + required: true + type: "string" + summary: "new student with ID" + tags: + - "student on course" + responses: + '200': + description: 'OK' + '409': + description: 'user with this id exists' + put: + parameters: + - name: "id" + in: "path" + description: "ID of student to change" + required: true + type: "integer" + format: "int64" + - name: 'name' + in: 'formData' + description: "students name" + required: true + type: "string" + - name: 'surname' + in: 'formData' + description: "students surname" + required: true + type: "string" + summary: "update an existing student with id" + tags: + - "student on course" + responses: + '200': + description: 'OK' + delete: + parameters: + - name: "id" + in: "path" + description: "ID of student to delete in DataBase" + required: true + type: "integer" + format: "int64" + summary: "delete student with id" + tags: + - "student on course" + responses: + '204': + description: "No Content" diff --git a/views.py b/views.py index d8afb20..214c486 100644 --- a/views.py +++ b/views.py @@ -53,6 +53,7 @@ def deleteidhandler(request): with open('DataBase.txt', 'w') as dbfilewr: b=dbfile.read() print((a+b)[:-1],file=dbfilewr) + return web.Response(status=204) a+=user return From 079cac03a11b2096c08066429a7072b87253a01a Mon Sep 17 00:00:00 2001 From: Raskolnikofff Date: Sun, 3 Jan 2021 12:37:18 +0000 Subject: [PATCH 2/3] Add swagger on /swagger. Delete homepage --- main.py | 2 +- readme.md | 8 +------- routes.py | 3 +-- views.py | 3 --- 4 files changed, 3 insertions(+), 13 deletions(-) diff --git a/main.py b/main.py index cb44272..10d0bcd 100644 --- a/main.py +++ b/main.py @@ -5,5 +5,5 @@ app = web.Application() setup_routes(app) -setup_swagger(app,swagger_from_file="swagger.yaml") +setup_swagger(app,swagger_url='/swagger',swagger_from_file="swagger.yaml") web.run_app(app,host='127.0.0.1') diff --git a/readme.md b/readme.md index da8ea3c..a5b3916 100644 --- a/readme.md +++ b/readme.md @@ -28,11 +28,5 @@ python3 main.py ``` * Requests -Your data is in quotation marks " ". -``` -curl -X GET -d http://0.0.0.0:8080/students -curl -X DELETE http://0.0.0.0:8080/students/id -curl -X POST -d 'name="your name"&surname="your surname"' http://0.0.0.0:8080/students/"id" -curl -X PUT -d 'surname="your name"&surname="your surname"' http://0.0.0.0:8080/students/"id" -``` +All requests can be seen and tested in http://localhost:8080/swagger You can also display the GET request in the browser by entering the appropriate address. diff --git a/routes.py b/routes.py index 57addcd..3a09cca 100644 --- a/routes.py +++ b/routes.py @@ -1,9 +1,8 @@ from aiohttp import web -from views import homepagehandler,gethandler,getidhandler,putidhandler,postidhandler,deleteidhandler +from views import gethandler,getidhandler,putidhandler,postidhandler,deleteidhandler def setup_routes(app): app.add_routes([ - web.get('/',homepagehandler), web.get('/students',gethandler), web.get('/students/{id}',getidhandler), web.put('/students/{id}',putidhandler), diff --git a/views.py b/views.py index 214c486..01a2cbb 100644 --- a/views.py +++ b/views.py @@ -14,9 +14,6 @@ async def postinfo(request): surname=data['surname'] return name,surname -def homepagehandler(request): - return web.FileResponse('website/index.html') - def gethandler(request): with open('DataBase.txt') as dbfile: data = list() From 5f3b7ab240d8bf259c06d3498a4ff62b0bf96ab0 Mon Sep 17 00:00:00 2001 From: Raskolnikofff Date: Mon, 8 Feb 2021 16:12:16 +0300 Subject: [PATCH 3/3] Add definition student --- swagger.yaml | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/swagger.yaml b/swagger.yaml index 0226fd4..d972921 100644 --- a/swagger.yaml +++ b/swagger.yaml @@ -5,19 +5,11 @@ swagger: "2.0" info: title: REST API description: description in repository on github https://github.com/Raskolnikofff/StudyWebServerRestFullAPI - version: 0.1.9 + version: "1.0" tags: - name: "student on course" description: "Everything about your student" paths: - /: - get: - summary: "open homepage" - tags: - - "student on course" - responses: - '200': - description: 'OK' /students: get: summary: "find all students on course" @@ -26,6 +18,8 @@ paths: responses: '200': description: 'OK' + schema: + $ref: "#/definitions/student" /students/{id}: get: parameters: @@ -41,7 +35,11 @@ paths: responses: '200': description: 'OK' + schema: + $ref: "#/definitions/student" post: + consumes: + - "application/x-www-form-urlencoded" parameters: - name: "id" in: "path" @@ -65,9 +63,13 @@ paths: responses: '200': description: 'OK' + schema: + $ref: "#/definitions/student" '409': description: 'user with this id exists' put: + consumes: + - "application/x-www-form-urlencoded" parameters: - name: "id" in: "path" @@ -91,6 +93,8 @@ paths: responses: '200': description: 'OK' + schema: + $ref: "#/definitions/student" delete: parameters: - name: "id" @@ -105,3 +109,14 @@ paths: responses: '204': description: "No Content" +definitions: + student: + type: "object" + properties: + id: + type: "integer" + format: "int64" + name: + type: "string" + surname: + type: "string" \ No newline at end of file