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 user-defined converters to routing #231

Closed
em92 opened this issue Nov 22, 2018 · 1 comment · Fixed by #1437
Closed

Add user-defined converters to routing #231

em92 opened this issue Nov 22, 2018 · 1 comment · Fixed by #1437
Labels
clean up Refinement to existing functionality

Comments

@em92
Copy link
Contributor

em92 commented Nov 22, 2018

To solve application task in #230 I use my own defined convertor:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import starlette.convertors as conv

class UnderscoreConvertor(conv.Convertor):
    regex = ".*"

    def convert(self, value: str) -> str:
        return "_" + value

    def to_string(self, value: str) -> str:
        return value[1:]

conv.CONVERTOR_TYPES['underscore'] = UnderscoreConvertor()



from starlette.applications import Starlette

from starlette.responses import PlainTextResponse

import uvicorn

app = Starlette()

@app.route("/test/{username:underscore}")
def http_test(request):
    username = request.path_params['username']
    return PlainTextResponse(username + "\n")

if __name__ == '__main__':
    uvicorn.run(app, host='0.0.0.0', port=8080)

So feature request is allowing user-defined converters in more acceptable way. Something like:
starlette.convertors.add("underscore", UnderscoreConverter())

@tomchristie
Copy link
Member

Yup we'd certainly like something along these lines.

@tomchristie tomchristie added the clean up Refinement to existing functionality label Dec 18, 2018
@tomchristie tomchristie changed the title Feature request: user-defined converters Add user-defined converters to routing Dec 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clean up Refinement to existing functionality
Projects
None yet
2 participants