more apispec setup

This commit is contained in:
Gardient
2021-09-21 20:34:39 +03:00
parent 2cab5310c9
commit 8e67a58630
4 changed files with 17 additions and 11 deletions

View File

@@ -1,22 +1,25 @@
from flask import Blueprint, current_app
from flask_jwt_extended import jwt_required, create_access_token
from flask_apispec import use_kwargs, marshal_with
from flask_apispec import use_kwargs, marshal_with, doc
from marshmallow import fields
from api.exceptions import BadRequestException
import api.constants as constants
from .models import TokenResponse
from .serializers import login_schema, token_response_schema
from .serializers import token_response_schema
blueprint = Blueprint('login', __name__)
@doc(tags=['login'])
@blueprint.route('', methods=['POST'])
@jwt_required(optional=True)
@use_kwargs(login_schema)
@use_kwargs({
'username': fields.Str(required=True),
'password': fields.Str(required=True)
})
@marshal_with(token_response_schema)
def login_user(username, password, **kwargs):
if username == constants.API_USER and password == current_app.config[constants.API_PASS]:
return TokenResponse(create_access_token(identity=username, fresh=True))
return {'token': create_access_token(identity=username, fresh=True, expires_delta=False)}
else:
raise BadRequestException("Wrong combination of username and password")