Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • Jan 17 17:48
    a-salimkhanov commented #243
  • Jan 17 05:30
    jingapore commented #403
  • Jan 15 19:44
    a-salimkhanov opened #406
  • Jan 11 16:11
    anandtripathi5 edited #405
  • Jan 11 16:09
    anandtripathi5 edited #405
  • Jan 11 16:09
    anandtripathi5 labeled #405
  • Jan 11 16:09
    anandtripathi5 opened #405
  • Jan 10 10:07
    heja1600 commented #27
  • Jan 10 10:05
    StephaneConq commented #27
  • Jan 10 10:05
    StephaneConq commented #27
  • Jan 10 10:05
    StephaneConq commented #27
  • Jan 09 23:02
    BullyBear commented #176
  • Jan 07 05:43
    rakeshk121 labeled #404
  • Jan 07 05:43
    rakeshk121 opened #404
  • Jan 06 10:09
    heja1600 commented #27
  • Jan 02 16:56
    jingapore opened #403
  • Jan 01 23:56
    mahoyen synchronize #402
  • Jan 01 23:49
    mahoyen opened #402
  • Jan 01 23:30
    mahoyen commented #373
  • Jan 01 19:33
    mahoyen commented #373
John Chittum
@j5awry

@E-Kalla thanks for really digging into the issue. I'd need to read a bit more to understand everything happening.

If you have an approach in mind, could you clearly state it in the issue? If it's to remove @cached_property i'm not sure the entire range of effects there, in particular with regards to benchmarking

serenanycvb
@serenanycvb:matrix.org
[m]

Hi, hope everyone is having a great Monday. I am currently setting up a POST method redirection using flask.redirect . For some reason, the redirection does not work and I was not able to get any useful logging even when I set FLASK_ENV=development. Here is the log I got:

127.0.0.1 - - [16/Aug/2021 16:49:57] "POST /runs HTTP/1.1" 200 -
[I 210816 16:49:57 _internal:122] 127.0.0.1 - - [16/Aug/2021 16:49:57] "POST /runs HTTP/1.1" 200 -

It seems this endpoint never got redirected.

Anyone has any insight on debugging this issue ?

Unfortunately, I am not able to provide any code here because the redirection works well when I tried to set up some dummy code, but it did not work with a big project that I am currently working on.

here is the log I got for dummy code that I set up:

127.0.0.1 - - [16/Aug/2021 17:04:51] "POST /todos2/1 HTTP/1.1" 307 -
127.0.0.1 - - [16/Aug/2021 17:04:51] "POST /todos/ HTTP/1.1" 200 -

From this log, the endpoint actually got redirected.

John Chittum
@j5awry
without code, it's hard to help debug.
serenanycvb
@serenanycvb:matrix.org
[m]
I am able to reproduce the error now, it was due to marshal_with which seems changed returned redirection:
Here is the code:
import flask
from flask import Flask
from flask_restx import Model, Resource, fields, Namespace, abort, Api
from werkzeug.middleware.proxy_fix import ProxyFix
app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app)
api = Api(app, version='1.0', title='TodoMVC API',
    description='A simple TodoMVC API',
)

ns = Namespace('todos', description='TODO operations')
ns2 = Namespace('todos2', description='TODO operations')

todo = Model('todo', {
    'id': fields.Integer(required=True, description='The task unique identifier'),
    'task': fields.String(required=True, description='The task details'),
    'description': fields.String(required=True, description='Put some description here')
})

response = Model('response', {
    'id': fields.Integer,
    'task': fields.String
})

ns.models[todo.name] = todo
ns.models[response.name] = response

todo2 = Model('todo2', {
    'task': fields.String(required=True, description='The task details'),
    'todo_description': fields.String(required=True, description='Put some description here')
})

response2 = Model('response2', {
    'id': fields.Integer,
    'task': fields.String
})

ns2.models[todo2.name] = todo2
ns2.models[response2.name] = response

api.add_namespace(ns)
api.add_namespace(ns2)

@ns2.route('/<int:id>')
@ns2.response(404, 'Todo not found')
class Todo(Resource):
    '''Show a single todo item and lets you delete them'''
    @ns2.doc('get_todo')
    def get(self, id):
        '''Fetch a given resource'''
        return "get todo 2"

    @ns2.expect(todo2)
    @ns2.marshal_with(response)
    def post(self, id):
        '''Update a task given its identifier'''
        if id == 1:
            redirection = flask.redirect(flask.url_for('todos_todo_list', _method='POST'), code=307)
            return redirection
        return {"id": "dummy id", "task": "some dummy task here"}


@ns.route('/')
class TodoList(Resource):
    '''Shows a list of all todos, and lets you POST to add new tasks'''
    @ns.doc('list_todos')
    def get(self):
        '''List all tasks'''
        return "get todo list"

    @ns.doc('create_todo')
    @ns2.expect(todo)
    @ns.marshal_with(response)
    def post(self):
        '''Create a new task'''
        payload = ns.payload
        return {"id": payload.get("id"), "task": payload.get("task")}


if __name__ == '__main__':
    app.run(debug=True)
John Chittum
@j5awry

so the requirement is:

Given an endpoint with a variable (var == $CONDITION), have a redirect only for $CONDITION, otherwise, return properly

My gut would say, if you want a single endpoint, split by condition, to do 2 redirects. One for $CONDITION, one for everything else. and the resultant endpoints handling the marshalling. Because, in the end, if their responses are going to be different, they should probably be considered separate endpoints

and that's going to be more properly rendered in the Swagger schema as well.
serenanycvb
@serenanycvb:matrix.org
[m]
Yes. The reason that I set up redirection is because, the endpoint already exists to do 2 things where separated by if-else condition statement.
For right now, I want to separate the 2 things into 2 different endpoints.
In order to not introduce breaking change for our users, so I decided to make the redirection from old endpoint to the new endpoint before fully separate them.
It would be nice to not remove marshaling because the old endpoint still needed to be kept.
John Chittum
@j5awry

Considering you're changing core functionality based upon a condition, i'd say keeping the original endpoint with 2 redirects, split by condition. then if you want to deprecate the v1 style endpoint later, you can

the other option is to split v1 and v2 endpoints, and just tell folks to use v2 for the new setup. Versioning and moving some endpoints is normal, and versioning is supported in flask-restx. you can provide version= to the API generator.

serenanycvb
@serenanycvb:matrix.org
[m]
Hi, John. Thank you for your suggestion. I think both are valid approaches. I think I am going to implement the first approach because we as a team had agree on not introducing breaking change for this new deployment and having this redirection will give users more time to adapt to the new endpoint.
E-Kalla
@E-Kalla
Hello ,
I have a question about marshalling.
I want to use marshall_with by passing a polymorph field map in argument , but it doesn't work , can someone help me ?
https://prnt.sc/1qfkppk
I want my function to be able to marshal list of this polymorph field
Carlos PiƱa
@cpinamtz_twitter

Hi, I have a question about how can I create a field which is a fixed list of different models:

model_1 = api.model(...)
model_2 = api.model(...)
model_3 = api.model(...)

model = api.model('model', {'field': fields.List([model_1, model_2, model_3])})

I know passing the list of previous models is not expected as the first param of the fields.List class but I don't know how to achieve it. Any ideas?

Matt
@mtcarr_gitlab
Hey folks, is there any way to define an arbitrary field in an api model?
Besides going the "stringly typed" route
My first thought would have been using fields.Raw but it seems to enforce an object structure when using validate=True
e.g. it won't allow "abc" or 123, data needs to be inside a json object {}
Brock Palen
@brockpalen
I'm trying to use values that are set in the current apps .config[] list as static values in my api.models but I'm getting errors about app_context() Rather than try to push an context in my include that has all my models are there any tips you would recomend for including configuration values when marshaling results?
Brock Palen
@brockpalen
Ok looks like using current_app.confg[] in my models only throw the context error for setting validation values like min= max= but not when used in attribute= lambda
Abdur-Rahmaan Janhangeer
@Abdur-rahmaanJ
I just did this talk: Advanced Flask: Recipes For An All-weather Craft | https://www.youtube.com/watch?v=veMSbl2fbXE and mentionned Flask-restx as an example to integrate. At the end there was a question on upgrading the OpenApi spec (around 27:37). I said that we need some more helping hands
Abdur-Rahmaan Janhangeer
@Abdur-rahmaanJ
Btw
If anybody is up for a talk about restx would be awesome: https://flaskcon.com/y/2021/
People need to discover the framework more!
Tony
@axc450
Hey all :) I have a quick question I couldn't find the answer to. I'm looking for a way to use api.doc() to document a POST request which should be able to take any JSON body (that is to say a valid JSON body is required, doesn't matter what is actually inside that body). At the moment, I am using a reqparse that is passed to the api.expect() which I dont actually use in my code but is just there to force the Swagger docs to give me a JSON body when clicking "Try it out". I understand there isn't really a way to pass custom Swagger JSON (as you would be able to pass invalid JSON) so what would be the best way to do this?
failo
@arduino__test_twitter
Hi, how do I change the default response content type in swagger ui. I tried ns.produces and api.representation. I woul dlike to replace application/json to application/xml
Brock Palen
@brockpalen
Is there a way when marsharling a response to not display a field if it's null / None? I have a single code base that has nested models, and some systems don't have a given feature so rather than returning null the front end people prefer we not pass along something that doesn't exist at all on that system.
Harshal Chaudhari
@harshalchaudhari35
@brockpalen I remember vaguely there is an option to skip none fields during marshalling. You will have to check the fields.raw and other classes used for marshalling. Alternatively, you could override the output function of any fields class and change the output to suit your needs
Brock Palen
@brockpalen
@harshalchaudhari35 Looks like there is a mask= option that is a callable, I'll try that and report.
Harshal Chaudhari
@harshalchaudhari35
image.png
@brockpalen here this is what I have referring to earlier
Brock Palen
@brockpalen
@harshalchaudhari35 skip_none= is what I wanted actually, and it works perfectly becaus depending on configuration different values in the nested group are populated. In our case does our campus HPC cluster have single precision GPUs or not,
Turns out mask= does something I hacked around by having a nasty lambda function in my model so that will clean that up significantly,
Really appreciate it, I need to get in the habbit of looking at the prototypes and not just the normal example documentation,
Harshal Chaudhari
@harshalchaudhari35
@brockpalen glad that helped and worked out for you. Yeah my experience has been the same and daunting with restx. Many times I find myself overriding many functions from the library.
Brock Palen
@brockpalen
NP, I really like the library, just wish it had more community behind it, it front ends out 14,000 CPU HPC cluster, and will soon front end our 20PB research archive, our univeristy really appreciates such an easy to understand library is available,