Exemplo n.º 1
0
    def compile(
        self,
        obj: object,
        arguments: Optional[Arguments] = None,
    ) -> RequestBodyCompiled:
        """
        Compiles an object into an intermediate request body.

        Args:
            obj: A compiled object, which should be a mapping.
            arguments: Arguments to inject.
        Returns:
            The result of compilation.
        Raises:
            CompilationError: when compilation fails.
        """

        obj = inject_arguments(obj, arguments)
        obj = ensure_mapping(obj)
        compiled = self._default

        type_obj = obj.get(_KEY_TYPE)
        if type_obj is not None:
            with on_key(_KEY_TYPE):
                key = ensure_str(type_obj)
                factory = _TYPE_FACTORY_MAP.get(key)
                if not factory:
                    raise CompilationError(
                        f"Must be in {list(_TYPE_FACTORY_MAP)}"
                        f", but given: {key}")
            compiled = self._default.replace(factory())

        return compiled.compile_and_replace(obj)
Exemplo n.º 2
0
def _compile_method(obj: object) -> Method:
    key = ensure_str(obj).upper()
    method = _METHOD_MAP.get(key)
    if not method:
        message = f"Must be in {list(_METHOD_MAP)}, but given: {obj}"
        raise CompilationError(message)
    return method
Exemplo n.º 3
0
    def compile(
        self,
        obj: object,
        arguments: Optional[Arguments] = None,
    ) -> RequestCompiled:
        """
        Compiles an object into an intermediate request.

        Args:
            obj: A compiled object, which should be a mapping or a string.
            arguments: Arguments to inject.
        Returns:
            The result of compilation.
        Raises:
            CompilationError: when compilation fails.
        """
        if isinstance(obj, Argument):
            obj = inject_arguments(obj, arguments)

        if isinstance(obj, str):
            return self.compile({_KEY_PATH: obj}, arguments)

        obj = ensure_mapping(obj)
        compiled = self._default

        method_obj = obj.get(_KEY_METHOD)
        if method_obj is not None:
            with on_key(_KEY_METHOD):
                method_obj = inject_arguments(method_obj, arguments)
                method = _compile_method(method_obj)
            compiled = replace(compiled, method=method)

        path_obj = obj.get(_KEY_PATH)
        if path_obj is not None:
            with on_key(_KEY_PATH):
                path_obj = inject_arguments(path_obj, arguments)
                path = ensure_str(path_obj)
            compiled = replace(compiled, path=path)

        headers_obj = obj.get(_KEY_HEADERS)
        if headers_obj is not None:
            with on_key(_KEY_HEADERS):
                headers_obj = inject_arguments(headers_obj, arguments)
                headers = _compile_headers(headers_obj)
            compiled = replace(compiled, headers=headers)

        params_obj = obj.get(_KEY_PARAMS)
        if params_obj is not None:
            with on_key(_KEY_PARAMS):
                params = compile_url_params(params_obj, arguments)
            compiled = replace(compiled, params=params)

        body_obj = obj.get(_KEY_BODY)
        if body_obj is not None:
            with on_key(_KEY_BODY):
                body = self._body.compile(body_obj, arguments)
            compiled = replace(compiled, body=body)

        return compiled
Exemplo n.º 4
0
    def _compile_cast(obj: object) -> Callable[[object], Any]:
        """`obj` should be a string."""

        key = ensure_str(obj)
        cast = _CAST_FUNC_MAP.get(key)
        if not cast:
            raise CompilationError(f"Invalid value: {key}")

        return cast
Exemplo n.º 5
0
def _compile_header_item(key: object,
                         value: object) -> Optional[Tuple[str, str]]:
    if value is None:
        return None
    return ensure_str(key), ensure_str(value)