@harshthakur9030 1) use python-restx not restplus. 2) never done serverless, as I've always done much larger things. Reading this guide made sense: https://medium.com/@Twistacz/flask-serverless-api-in-aws-lambda-the-easy-way-a445a8805028
however I'd have serious doubts about the self-documenting OpenAPI part working properly on serverless. I haven't run across anyone doing it yet
api.inherit()
doesn't include the model in the schema spec unless I add the model to an ns.expect
ns.expect(child1, child2, validate=True)
is trying to validate the input payload against both children instead of either or. ns=api.namespace('todo')
parent = api.model('Parent', {
'name': fields.String,
'type': fields.String(discriminator=True)
})
child1 = api.inherit('Child1', parent, {
'extra': fields.String(required=True)
})
child2 = api.inherit('Child2', parent, {
'extra2': fields.String(required=True)
})
@ns.route('/')
class TodoList(Resource):
@ns.doc('create_todo')
@ns.expect(child1, child2, validate=True)
def post(self):
return api.payload
@harshthakur9030 I think some of it is not clearly defining serverless, and part of it is from a saving perspective. If you're doing serverless via something like AWS Lambda, they're meant to be fast running functions that spin up and down. long running tasks will end up costing far more than a traditional AWS VM. Having the continuous running service (the openAPI documentation on a continuous webserver) is probably far more expensive.
I haven't really looked at Amazon APIGateway, or other similar services, to see if there is some sort of API documentation service in them. When I hear serverless, I generally think of either Lambdas, which should be small and fast, or something that's more Backend as a Service oriented, with most logic in the client.
@api.route('/my-resource/')
class MyResource(Resource):
@api.response(400, 'Validation error')
@api.marshal_with(model, code=201, description='Object created')
def post(self):
pass
Hi @Justvuur, i'm facing some error on trying to create a Polymorph List in the response of a service (ex: fields.List(fields.Polymorph(mapping))).
parent_fields = {
'index': fields.Integer(description='index')
}
parent = ns.model('Parent', parent_fields)
child1_fields = {
'attr': fields.String(required=True, description='attr'),
}
child1 = parent.inherit('Child1', child1_fields)
child2_fields = {
'attr': fields.String(required=True, description='attr'),
}
child2 = parent.inherit('Child2', child2_fields)
mapping = {
child1: child1_fields,
child2: child2_fields,
}
fields.List(fields.Polymorph(mapping))
That gives a TypeError " TypeError: unhashable type: 'Model'".
Hey, guys, I need your help, I have a class which consists of all my error
class InternalServerError(Exception):
pass
class SchemaValidationError(Exception):
pass
class UpdatingUserError(Exception):
pass
class UserNotExistsError(Exception):
pass
class EmailAlreadyExistsError(Exception):
pass
class UnauthorizedError(Exception):
pass
errors = {
"InternalServerError": {
"message": "Something went wrong",
"status": 500
},
"SchemaValidationError": {
"message": "Request is missing required fields",
"status": 400
},
"UpdatingUserError": {
"message": "Updating user data is forbidden",
"status": 403
},
"UserNotExistsError": {
"message": "The user is not registered",
"status": 400
},
"EmailAlreadyExistsError": {
"message": "User with given email address already exists",
"status": 400
},
"UnauthorizedError": {
"message": "Invalid username or password",
"status": 401
}
}
How can I add this to my API
FROM python:3.8-slim-buster
EXPOSE 5001
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ADD requirements.txt .
RUN python -m pip install -r requirements.txt
WORKDIR /app
ADD . /app
RUN useradd appuser && chown -R appuser /app
USER appuser
CMD ["gunicorn", "--bind", "0.0.0.0:5001", "app:app"]
name
, age
, birthdate
, group
. I would like to send a request that has a params data for the fields that I only want to get from a Person model (e.g. name
and group
). It's easy to implement for unnested fields but for example, I further want to specify the fields that I want for the group
field (e.g. I only want the group ID). I'm not asking for a code, rather a stable structure/architecture on how to implement this. Thank you for the help!