def create_application() -> Flask: logging.config.dictConfig(settings.create_logging_setting()) app = Flask(__name__) init_sentry() init_db(app, db.meta) init_jaeger(app) RequestID(app) @app.route('/books', methods=['GET']) def book(): logger.info('got_request', extra={'endpoint': '/books'}) book_ids = request.args.getlist('book_ids', int) books = model.Book.query.filter(model.Book.id.in_(book_ids)).all() result = {} for b in books: result[b.id] = { 'title': b.title, 'author_name': b.author.name, } return jsonify({'result': result}) @app.route('/ping', methods=['GET']) def ping(): return 'pong' return app
def create_app() -> FastAPI: default_responses = { status.HTTP_401_UNAUTHORIZED: { "description": "Incorrect auth credentials" } } init_sentry() _app = FastAPI( title="Keeper", description= ("Project to keep information about read/watched books/comics/cinema/tv-shows/etc, " "and other different stuff (like personal bookkeeping)."), responses=default_responses, # type: ignore redoc_url=None, version=settings.version, ) # there is no need to include these middlewares to the unit tests if not settings.is_test_env: _app.add_middleware( CORSMiddleware, **cors_settings, ) if settings.sentry_dsn: _app.add_middleware(SentryAsgiMiddleware) _app.include_router(auth.router) _app.include_router(users.router) _app.include_router(employers.router) _app.include_router(accounts.router) _app.include_router(currency_exchange_rates.router) _app.include_router(transactions.router) _app.include_router(finance_categories.router) if settings.is_test_env: _app.include_router(tests_helpers.router) return _app
def create_application() -> Flask: logging.config.dictConfig(settings.create_logging_setting()) app = Flask(__name__) init_sentry() init_db(app, db.meta) init_jaeger(app) RequestID(app) @app.route('/ping', methods=['GET']) def ping(): return 'pong' @app.route('/user/<int:user_id>/orders', methods=['GET']) def get_order(user_id): logger.info('got_request', extra={'endpoint': '/user/<int:user_id>/orders'}) orders = model.Order.query.filter_by(user_id=user_id).all() orders = marshall_order(orders) return jsonify({'result': {'orders': orders}}) return app
def create_application() -> Flask: logging.config.dictConfig(settings.create_logging_setting()) app = Flask(__name__) init_sentry() init_jaeger(app) RequestID(app) @app.route('/user/<int:user_id>/orders', methods=['GET']) def get_orders(user_id): logger.info('got_request', extra={'endpoint': '/user/<user_id>/orders'}) headers = {'X-Request-ID': current_request_id()} response = requests.get(ORDERS_URL.format(user_id=user_id), headers=headers) orders = response.json()['result']['orders'] book_ids = get_book_ids_from_orders(orders) response = requests.get(BOOKS_URL, params={'book_ids': book_ids}, headers=headers) books = response.json()['result'] return jsonify({'result': { 'orders': orders, 'books': books, }}) @app.route('/ping', methods=['GET']) def ping(): logger.info('got_request', extra={'endpoint': '/ping'}) return 'pong' return app
import argparse from lib.logger import set_up_logging from lib.sentry import init_sentry from lib.users.crud import create_user init_sentry() logger = set_up_logging() def main(): parser = argparse.ArgumentParser(description="Creates new user") parser.add_argument("username", type=str) parser.add_argument("email", type=str) parser.add_argument("password", type=str) args = parser.parse_args() user = create_user(args.username, args.email, args.password) logger.info(f"{user.id} created!") if __name__ == "__main__": main()