Beispiel #1
0
    def serialize(self, obj: object) -> Union[dict, list]:
        res = Stream(self.strategies) \
            .firstMatch(lambda s: s.can_handle_serialization(obj)) \
            .map(lambda s: s.serialize(obj))

        if res.isPresent():
            return res.get()

        if obj is None:
            return None

        if issubclass(type(obj), BUILTIN_TYPES):
            return obj

        if isinstance(obj, list) or isinstance(obj, tuple):
            return [self.serialize(x) for x in obj]

        if isinstance(obj, dict):
            dictionary = obj
            for key, value in dictionary.items():
                dictionary[key] = self.serialize(value)
            return dictionary

        if hasattr(obj, '__dict__'):
            return self.serialize({**obj.__dict__})

        raise SerializationException(
            f"Cannot serialize type {obj.__class__}. {obj}")
Beispiel #2
0
 def doFilter(self, request: Request, response: Response,
              chain: FilterChain):
     try:
         chain.doFilter(request, response)
     except Exception as e:
         exception_mapper = Stream(self.exception_mappers).firstMatch(
             lambda mapper: mapper.handles(e))
         if exception_mapper.isPresent():
             response.copy(exception_mapper.get().create_response(e))
         else:
             raise e
    def __init__(self, registry: Registry, resources: List[Registration]):
        super().__init__()
        for resource in resources:
            for primitive in http_methods:
                routable_functions = registry.get_annotated_in_package(primitive, resource.registered.__module__)
                sub_paths = registry.get_annotated_in_package(Path, resource.registered.__module__)

                for route_function in routable_functions:
                    route_sub_path = Stream(sub_paths).firstMatch(lambda r: r.registered == route_function.registered)
                    resource_path = resource.arguments['value']

                    if not route_sub_path.isPresent():
                        self.register_route(primitive, resource_path, resource.registered,
                                            route_function.registered)
                    else:
                        sub_path = route_sub_path.get().arguments['value']
                        path = resource_path + sub_path if \
                            resource_path.endswith('/') or sub_path.startswith('/') \
                            else resource_path + '/' + sub_path

                        self.register_route(primitive, path, resource.registered, route_function.registered)
Beispiel #4
0
 def __get_scope(self, component: type) -> Optional[ScopeCache]:
     found_scope = Stream(self.scopeCaches).firstMatch(
         lambda scope: scope.handles_component(component))
     return found_scope.get() if found_scope.isPresent() else None
Beispiel #5
0
    def test_givenEmptyStream_whenGettingFirst_thenReturnEmptyNullable(self):
        first = Stream([]).first()

        self.assertFalse(first.isPresent())