コード例 #1
0
ファイル: api_module.py プロジェクト: Heknon/web-framework
    def __get_function_routes(self, function):
        if not has_meta_attribute(function):
            raise RuntimeError(
                f"Cannot find meta attribute on function {function}. Add RequestMapping."
            )
        if not inspect.isfunction(function) and not inspect.ismethod(function):
            raise RuntimeError(
                f"Argument passed must be of type function or method!")

        function_meta: RequestMappingMeta = get_meta_attribute(function)
        parent_class = self.get_parent()

        routes = set()
        if parent_class is not None:
            for class_route in self.meta.routes:
                for method_route in function_meta.routes:
                    route = self.normalize_route(class_route,
                                                 True) + self.normalize_route(
                                                     method_route, False)
                    routes.add(route)
        else:
            for method_route in function_meta.routes:
                route = self.normalize_route(method_route, False)
                routes.add(route)

        return routes
コード例 #2
0
ファイル: api_module.py プロジェクト: Heknon/web-framework
 def get_encapsulated_methods(self):
     applicable_methods = []
     for attribute in dir(self.callable_object):
         method = getattr(self.callable_object, attribute)
         if has_meta_attribute(method):
             setattr(get_meta_attribute(method), 'parent', self)
             applicable_methods.append(method)
     return applicable_methods
コード例 #3
0
ファイル: api_module.py プロジェクト: Heknon/web-framework
    def request_method_acceptable(self, method, request) -> bool:
        parent = self.get_parent()
        method_meta = get_meta_attribute(method)
        method_http_methods_contains_method = self.meta.acceptable_methods is not None and request.method in method_meta.acceptable_methods

        if parent is not None:
            class_http_methods_contains_method = self.meta.acceptable_methods is not None and request.method in self.meta.acceptable_methods
            return method_http_methods_contains_method or class_http_methods_contains_method
        else:
            return method_http_methods_contains_method
コード例 #4
0
 def __init__(self, method: Callable, api_registry, response, client):
     if not has_meta_attribute(method):
         raise RuntimeError(
             f"Method passed does not have method meta! {method}")
     self.method = method
     self.api_registry = api_registry
     self.response = response
     self.client = client
     self.meta = get_meta_attribute(method)
     self.content_type = self.meta.content_type
     self.parent = getattr(self.meta, 'parent')
コード例 #5
0
ファイル: api_module.py プロジェクト: Heknon/web-framework
    def __init__(self, callable_object):
        self.callable_object = callable_object
        self.is_class = '.' in callable_object.__qualname__

        if not has_meta_attribute(callable_object):
            raise RuntimeError(
                f"Cannot find meta attribute on {'class' if self.is_class else 'function'} {callable_object}. Add RequestMapping."
            )

        self.meta: RequestMappingMeta = get_meta_attribute(callable_object)

        self.encapsulated_methods = self.get_encapsulated_methods()
        self.conditional_routes = self.__get_conditional_routes()
        self.route_method_association = self.__get_routes_function_association(
        )
コード例 #6
0
ファイル: api_registry.py プロジェクト: Heknon/web-framework
    def execute_method(self, method, request, client) -> HttpResponse:
        """
        Executes an API method and builds an HttpResponse

        :param method: the method to execute
        :param request: the HttpRequest made that resulted in the method being called
        :param client: the received client socket
        :return:
        """

        method_meta = get_meta_attribute(method)
        clazz = getattr(method_meta, 'parent')

        if not clazz.request_method_acceptable(method, request):
            response = HttpResponse.build_empty_status_response(
                request, HttpStatus.BAD_REQUEST,
                f'Class {clazz} and method {method} do not accept {request.method}',
                client)
        else:
            response = HttpResponse.build_from_function(
                request, method, self, client)

        return response
コード例 #7
0
ファイル: api_module.py プロジェクト: Heknon/web-framework
 def get_encapsulated_methods(self):
     setattr(get_meta_attribute(self.callable_object), 'parent', self)
     return [self.callable_object]