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

add urlencode support to Config #695

Closed
taybin opened this issue Oct 28, 2019 · 1 comment
Closed

add urlencode support to Config #695

taybin opened this issue Oct 28, 2019 · 1 comment

Comments

@taybin
Copy link

taybin commented Oct 28, 2019

I found this helpful for working around encode/databases#145

class SafeConfig(Config):
    """Subclass of starlette.config.Config, but it will urlencode
       values if requested."""
    def __call__(self,
                 key: str,
                 cast: type = None,
                 default: typing.Any = undefined,
                 encode: bool = False) -> typing.Any:
        return self.get(key, cast=cast, default=default, encode=encode)

    def get(self,
            key: str,
            cast: type = None,
            default: typing.Any = undefined,
            encode: bool = False) -> typing.Any:
        value = None
        if key in self.environ:
            value = self.environ[key]
        elif key in self.file_values:
            value = self.file_values[key]
        elif default is not undefined:
            value = default
        if value:
            if encode:
                value = quote(value)
            return self._perform_cast(key, value, cast)
        raise KeyError(f"Config '{key}' is missing, and has no default.")

I'd prefer to have the function arguments be def __call__(self, key, *, cast, default, encode), but that breaks from the superclass arguments. It seems safer though as all of them except key are optional.

@tomchristie
Copy link
Member

Not a bad idea, but going to close this off as stale.
If we get other user feedback along these lines in the future perhaps we could reconsider if it's worth prioritising or not.

(Passing thought: we might consider if Starlette really needs it's own config parsing code or not.)

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

2 participants