flask.views http_method_funcs Example Code

http_method_funcs is an immutable Python set within the flask.views module of the Flask project. It contains strings of the HTTP methods "get", "post", "head", "options", "delete", "put", "trace", and "patch", which is useful for checking if an HTTP method is valid by comparing it against the items in this set.

MethodView and View are a couple of other callables within the flask.views package that also have code examples.

You should read up on these subjects along with these http_method_funcs examples:

Example 1 from flask-restx

Flask RESTX is an extension that makes it easier to build RESTful APIs into your applications. Flask RESTX aims for minimal configuration to get basic APIs running for existing applications and it exposes endpoint documentation using Swagger.

Flask RESTX is provided as open source under the BSD 3-Clause license.

flask-restx / flask_restx / namespace.py

# namespace.py
from __future__ import unicode_literals

import inspect
import warnings
import logging
from collections import namedtuple, OrderedDict

import six
from flask import request
from flask.views import http_method_funcs

from ._http import HTTPStatus
from .errors import abort
from .marshalling import marshal, marshal_with
from .model import Model, OrderedModel, SchemaModel
from .reqparse import RequestParser
from .utils import merge

ResourceRoute = namedtuple("ResourceRoute", "resource urls route_doc kwargs")

class Namespace(object):

    def __init__(

## ... source file abbreviated to get to http_method_funcs examples ...

        return (self._path or ("/" + self.name)).rstrip("/")

    def add_resource(self, resource, *urls, **kwargs):
        route_doc = kwargs.pop("route_doc", {})
        self.resources.append(ResourceRoute(resource, urls, route_doc, kwargs))
        for api in self.apis:
            ns_urls = api.ns_urls(self, urls)
            api.register_resource(self, resource, *ns_urls, **kwargs)

    def route(self, *urls, **kwargs):

        def wrapper(cls):
            doc = kwargs.pop("doc", None)
            if doc is not None:
                kwargs["route_doc"] = self._build_doc(cls, doc)
            self.add_resource(cls, *urls, **kwargs)
            return cls

        return wrapper

    def _build_doc(self, cls, doc):
        if doc is False:
            return False
        for http_method in http_method_funcs:
            if http_method in doc:
                if doc[http_method] is False:
                if "expect" in doc[http_method] and not isinstance(
                    doc[http_method]["expect"], (list, tuple)
                    doc[http_method]["expect"] = [doc[http_method]["expect"]]
        return merge(getattr(cls, "__apidoc__", {}), doc)

    def doc(self, shortcut=None, **kwargs):
        if isinstance(shortcut, six.text_type):
            kwargs["id"] = shortcut
        show = shortcut if isinstance(shortcut, bool) else True

        def wrapper(documented):
            documented.__apidoc__ = self._build_doc(
                documented, kwargs if show else False
            return documented

        return wrapper

## ... source file continues with no further http_method_funcs examples...

Sponsored By

Sentry logo

Software errors are inevitable. Chaos is not. Try Sentry for free.

AssemblyAI logo

The most accurate speech-to-text API. Built for Python developers.

Full Stack Python

Full Stack Python is an open book that explains concepts in plain language and provides helpful resources for those topics.
Updates via Twitter & Facebook.

Matt Makai 2012-2021