diff --git a/main.py b/main.py index e4a9875..10d0bcd 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_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/swagger.yaml b/swagger.yaml new file mode 100644 index 0000000..d972921 --- /dev/null +++ b/swagger.yaml @@ -0,0 +1,122 @@ + + +--- +swagger: "2.0" +info: + title: REST API + description: description in repository on github https://github.com/Raskolnikofff/StudyWebServerRestFullAPI + version: "1.0" +tags: +- name: "student on course" + description: "Everything about your student" +paths: + /students: + get: + summary: "find all students on course" + tags: + - "student on course" + responses: + '200': + description: 'OK' + schema: + $ref: "#/definitions/student" + /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' + schema: + $ref: "#/definitions/student" + post: + consumes: + - "application/x-www-form-urlencoded" + 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' + schema: + $ref: "#/definitions/student" + '409': + description: 'user with this id exists' + put: + consumes: + - "application/x-www-form-urlencoded" + 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' + schema: + $ref: "#/definitions/student" + 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" +definitions: + student: + type: "object" + properties: + id: + type: "integer" + format: "int64" + name: + type: "string" + surname: + type: "string" \ No newline at end of file diff --git a/views.py b/views.py index d8afb20..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() @@ -53,6 +50,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