123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- # -*- coding: utf-8 -*-
- import json
- from http import HTTPStatus
-
- from pyramid.config import Configurator
- from wsgiref.simple_server import make_server
- import time
- from datetime import datetime
- import hapic
- from example.fake_api.schema import *
- from hapic.data import HapicData
-
-
- class NoContentException(Exception):
- pass
-
-
- class Controllers(object):
- @hapic.with_api_doc()
- @hapic.output_body(AboutResponseSchema())
- def about(self, context, request):
- """
- General information about this API.
- """
- return {
- 'version': '1.2.3',
- 'datetime': datetime.now(),
- }
-
- @hapic.with_api_doc()
- @hapic.output_body(ListsUserSchema())
- def get_users(self, context, request):
- """
- Obtain users list.
- """
- return {
- 'item_nb': 1,
- 'items': [
- {
- 'id': 4,
- 'username': 'some_user',
- 'display_name': 'Damien Accorsi',
- 'company': 'Algoo',
- },
- ],
- 'pagination': {
- 'first_id': 0,
- 'last_id': 5,
- 'current_id': 0,
- }
- }
-
- @hapic.with_api_doc()
- @hapic.input_path(UserPathSchema())
- @hapic.output_body(UserSchema())
- def get_user(self, context, request, hapic_data: HapicData):
- """
- Obtain one user
- """
- return {
- 'id': 4,
- 'username': 'some_user',
- 'email_address': 'some.user@hapic.com',
- 'first_name': 'Damien',
- 'last_name': 'Accorsi',
- 'display_name': 'Damien Accorsi',
- 'company': 'Algoo',
- }
-
- @hapic.with_api_doc()
- # TODO - G.M - 2017-12-5 - Support input_forms ?
- # TODO - G.M - 2017-12-5 - Support exclude, only ?
- @hapic.input_body(UserSchema(exclude=('id',)))
- @hapic.output_body(UserSchema())
- def add_user(self, context, request, hapic_data: HapicData):
- """
- Add new user
- """
- return {
- 'id': 4,
- 'username': 'some_user',
- 'email_address': 'some.user@hapic.com',
- 'first_name': 'Damien',
- 'last_name': 'Accorsi',
- 'display_name': 'Damien Accorsi',
- 'company': 'Algoo',
- }
-
- @hapic.with_api_doc()
- @hapic.handle_exception(NoContentException, http_code=HTTPStatus.NO_CONTENT)
- @hapic.input_path(UserPathSchema())
- def del_user(self, context, request, hapic_data: HapicData):
- """
- delete user
- """
-
- # TODO - G.M - 2017-12-05 - Add better
- # way to doc response of this function, using response object ?
- # return bottle.Response(
- # status=204,
- # )
- raise NoContentException
-
- def bind(self, configurator: Configurator):
- configurator.add_route('about', '/about', request_method='GET')
- configurator.add_view(self.about, route_name='about', renderer='json')
-
- configurator.add_route('get_users', '/users', request_method='GET') # nopep8
- configurator.add_view(self.get_users, route_name='get_users', renderer='json') # nopep8
-
- configurator.add_route('get_user', '/users/{id}', request_method='GET') # nopep8
- configurator.add_view(self.get_user, route_name='get_user', renderer='json') # nopep8
-
- configurator.add_route('add_user', '/users/', request_method='POST') # nopep8
- configurator.add_view(self.add_user, route_name='add_user', renderer='json') # nopep8
-
- configurator.add_route('del_user', '/users/{id}', request_method='DELETE') # nopep8
- configurator.add_view(self.del_user, route_name='del_user', renderer='json') # nopep8
-
- configurator = Configurator(autocommit=True)
- controllers = Controllers()
- controllers.bind(configurator)
- hapic.set_context(hapic.ext.pyramid.PyramidContext(configurator))
- time.sleep(1)
- s = json.dumps(hapic.generate_doc())
- time.sleep(1)
- # print swagger doc
- print(s)
- # Run app
- server = make_server('0.0.0.0', 8081, configurator.make_wsgi_app())
- server.serve_forever()
|