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

Handling multiple http methods for a single function from dictionary #183

Open
yb-yu opened this issue Mar 20, 2018 · 3 comments
Open

Handling multiple http methods for a single function from dictionary #183

yb-yu opened this issue Mar 20, 2018 · 3 comments

Comments

@yb-yu
Copy link

yb-yu commented Mar 20, 2018

I have read README about Handling multiple http methods and routes for a single function and its implementation.

But it is only when using external YAML files, right?

Are there any plans to support dictionary too?

@rochacbruno
Copy link
Member

I guess you can use https://github.com/rochacbruno/flasgger#using-dictionaries-as-raw-specs and pass method and endpoint

@yb-yu
Copy link
Author

yb-yu commented Mar 20, 2018

   def set_from_filepath(function):
        final_filepath = resolve_path(function, specs)
        function.swag_type = filetype or specs.split('.')[-1]

        if endpoint or methods:
            if not hasattr(function, 'swag_paths'):
                function.swag_paths = {}

        if not endpoint and not methods:
            function.swag_path = final_filepath
        elif endpoint and methods:
            for verb in methods:
                key = "{}_{}".format(endpoint, verb.lower())
                function.swag_paths[key] = final_filepath
        elif endpoint and not methods:
            function.swag_paths[endpoint] = final_filepath
        elif methods and not endpoint:
            for verb in methods:
                function.swag_paths[verb.lower()] = final_filepath

    def set_from_specs_dict(function):
        function.specs_dict = specs

    def decorator(function):

        if isinstance(specs, string_types):
            set_from_filepath(function)
            # function must have or a single swag_path or a list of them
            swag_path = getattr(function, 'swag_path', None)
            swag_paths = getattr(function, 'swag_paths', None)
            validate_args = {
                'filepath': swag_path or swag_paths,
                'root': getattr(function, 'root_path', None)
            }
        "if isinstance(specs, dict):
            set_from_specs_dict(function)
            validate_args = {'specs': specs}

        @wraps(function)
        def wrapper(*args, **kwargs):
            if validation is True:
                validate(
                    data,
                    schema_id or definition,
                    validation_function=validation_function,
                    validation_error_handler=validation_error_handler,
                    **validate_args
                )
            return function(*args, **kwargs)
        return wrapper

    return decorator

but in source code, when spec is dictionary, endpoint and methods aren't passed to function.

@applio
Copy link

applio commented Jun 7, 2018

FWIW, not respecting endpoint when supplying spec as a dict just bit me.

If it is possible to support the endpoint/methods arguments when supplying the spec as a dict, I think it would be helpful.

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

No branches or pull requests

3 participants