Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Connexion 3.0.0 config] Migration from flask.current_app.config #1804

Closed
vladislavkoz opened this issue Nov 10, 2023 · 8 comments
Closed

[Connexion 3.0.0 config] Migration from flask.current_app.config #1804

vladislavkoz opened this issue Nov 10, 2023 · 8 comments

Comments

@vladislavkoz
Copy link

With connexion 2 it was pretty easy to configure some variables that can be accessed from any place in the app.
We were able to pass these variables to the flask config and then do:

from flask import current_app
config = current_app.config

"config contains everything what we need"

How we can achieve the same using the connexion 3?

@Parthib
Copy link

Parthib commented Jan 5, 2024

Did you get a resolution on this question? I was wondering the same thing

@vladislavkoz
Copy link
Author

@Parthib We used "from connexion.context import scope" and implemented our own interface for managing things inside this virtual context so it would be easier to switch in the future if any breaking updates like this one as we had to refactor a lot of things.

@uniqueg
Copy link

uniqueg commented May 23, 2024

Facing the exact same problem. Would you mind expanding on the solution you came up with @vladislavkoz? Or, ideally, would you share it? Maybe it's something that could be worth including in the Connexion 3 cookbook (documentation is really a bit lacking at this point). I guess using current_app for that purpose is not an uncommon pattern...

@vladislavkoz
Copy link
Author

@uniqueg See my last comment

@uniqueg
Copy link

uniqueg commented May 24, 2024

Well, I guess I was hoping that you would share your Middleware class (or a reduced version focusing just on the use case that this issue is about, i.e., making config parameters available in application/runtime context), because l, given the current sparsity of the Connexion documentation in terms of concrete examples and the huge conceptual and technical differences between v2 and v3, it's not exactly trivial.

Fine if you can't or don't want to, of course, whatever the reason. But if you can, I think it would be useful for a lot of Connexion users, and it could form a basis for a potential new default middleware or an addition to the cookbook.

@vladislavkoz
Copy link
Author

@uniqueg
It's just super simple, nothing special so I thought that it's clear what todo.

from connexion.context import scope

SERVICE_CONTEXT_KEY = "service_context"

class ServiceContext:
    @staticmethod
    def _get_request_context_value(key):
        if scope:
            return scope.get(SERVICE_CONTEXT_KEY, {}).get(key)

    @staticmethod
    def _set_request_context_value(key, value):
        service_context = scope.get(SERVICE_CONTEXT_KEY)
        if not service_context:
            scope[SERVICE_CONTEXT_KEY] = {}
        scope[SERVICE_CONTEXT_KEY][key] = value

@vladislavkoz
Copy link
Author

BTW, there is also available

from connexion import request as connexion_request

@uniqueg
Copy link

uniqueg commented May 24, 2024

Thanks a lot @vladislavkoz, I find this very useful. Will probably try to make this into a middleware and add it before exception handling. If so, I'll post here what I did.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants