def __getitem__(self, item: str) -> str: found_header = Stream(self.content.items()).firstMatch( lambda key, value: key == item or _format_camel_case( key) == _format_camel_case(item)) if found_header: return found_header.get()[1] return None
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}")
class SingletonScopeCache(ScopeCache): def __init__(self, name: str, scoped_components: List[type]): """ :param name: human-readable name :param scoped_components: components managed by this scope """ super().__init__(name) self.scoped_components = Stream(scoped_components).map( lambda clazz: (clazz, None)).toDict() def handles_component(self, component: type) -> bool: return component in self.scoped_components.keys() def is_stored(self, component: type) -> bool: return self.scoped_components[component] is not None def get(self, component: type) -> object: stored_component = self.scoped_components.get(component) if stored_component is None: raise UninstantiatedObjectException(component) return stored_component def store(self, component: type, instance: object): if component not in self.scoped_components.keys(): raise ComponentNotHandledByScopeException(component) self.scoped_components[component] = instance
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 build_request(self, env: dict) -> Request: """Builds the Jivago request object from the WSGI environment dictionary.""" raw_headers = Stream(env.items()).filter( lambda key, value: key.startswith("HTTP_")).map( lambda key, value: (re.sub("^HTTP_", "", key).title().replace( "_", "-"), value)).toDict() request_size = int( env.get('CONTENT_LENGTH')) if 'CONTENT_LENGTH' in env else 0 if 'CONTENT_TYPE' in env: raw_headers['Content-Type'] = env['CONTENT_TYPE'] if raw_headers.get("Transfer-Encoding") == "chunked": body = StreamingRequestBody(env['wsgi.input']) else: body = env['wsgi.input'].read(request_size) return Request(env['REQUEST_METHOD'], env['PATH_INFO'], Headers(raw_headers), env['QUERY_STRING'], body)
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)
class ScopeCache(object): def __init__(self, scope: Annotation, scoped_components: List[type]): self.scope = scope self.scoped_components = Stream(scoped_components).map( lambda clazz: (clazz, None)).toDict() def handles_component(self, component: type) -> bool: return component in self.scoped_components.keys() def is_stored(self, component: type) -> bool: return self.scoped_components[component] is not None def get(self, component: type) -> object: stored_component = self.scoped_components.get(component) if stored_component is None: raise UninstantiatedObjectException(component) return stored_component def store(self, component: type, instance: object): if component not in self.scoped_components.keys(): raise ComponentNotHandledByScopeException(component) self.scoped_components[component] = instance
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