Class PythonHttpConversion

java.lang.Object
io.micronaut.context.python.PythonHttpConversion

@Internal public final class PythonHttpConversion extends Object
Conversion of Python return values that are HTTP responses or reactive publishers.
Since:
5.2.0
  • Method Details

    • convertHttpResponse

      public static <T> @Nullable HttpResponse<T> convertHttpResponse(org.graalvm.polyglot.Value value, Class<T> bodyType)
      Convert a GraalPy-created HttpResponse and its response body to the declared Java body type.
      Type Parameters:
      T - The response body type
      Parameters:
      value - The source polyglot response
      bodyType - The declared response body type
      Returns:
      The converted response, or null when the value is not an HttpResponse
    • convertPublisher

      public static <T> Publisher<T> convertPublisher(Publisher<?> publisher, Class<T> itemType)
      Convert each item emitted by a Python-returned publisher to the declared Java item type.
      Type Parameters:
      T - The item type
      Parameters:
      publisher - The source publisher
      itemType - The declared publisher item type
      Returns:
      The converted publisher
    • convertPublisher

      public static <T> Publisher<T> convertPublisher(Publisher<?> publisher, PolyglotValueConverter<T> converter)
      Convert publisher items with a generated wrapper converter. A generated converter preserves the concrete Python wrapper type after the publisher crosses the reactive boundary.
      Type Parameters:
      T - The item type
      Parameters:
      publisher - The source publisher
      converter - The generated item converter
      Returns:
      The converted publisher
    • convertPublisher

      public static <T> @Nullable Publisher<T> convertPublisher(org.graalvm.polyglot.Value value, Class<T> itemType)
      Convert a GraalPy Value representing a publisher to a typed Java publisher.
      Type Parameters:
      T - The item type
      Parameters:
      value - The source polyglot publisher
      itemType - The declared publisher item type
      Returns:
      The converted publisher
    • convertPublisher

      public static <T> @Nullable Publisher<T> convertPublisher(org.graalvm.polyglot.Value value, PolyglotValueConverter<T> converter)
      Convert a GraalPy publisher with a generated item converter.
      Type Parameters:
      T - The item type
      Parameters:
      value - The source polyglot publisher
      converter - The generated item converter
      Returns:
      The converted publisher
    • convertPublisher

      public static <T,P> @Nullable P convertPublisher(org.graalvm.polyglot.Value value, Class<T> itemType, Class<P> targetType)
      Convert a GraalPy value representing a publisher to the reactive type a method declares: the items are converted to the declared item type and the publisher is then adapted to the declared type (Mono, Flux, a CompletionStage or any reactive type the conversion service knows), so a Python body may return a Flux where the Java signature says Mono, or a Mono where it says CompletableFuture.
      Type Parameters:
      T - The item type
      P - The reactive type
      Parameters:
      value - The source polyglot publisher
      itemType - The declared publisher item type
      targetType - The declared reactive return type
      Returns:
      The converted publisher, or null for None
    • convertPublisher

      public static <T,P> @Nullable P convertPublisher(org.graalvm.polyglot.Value value, PolyglotValueConverter<T> converter, Class<P> targetType)
      Convert a GraalPy publisher with a generated item converter to the reactive type a method declares.
      Type Parameters:
      T - The item type
      P - The reactive type
      Parameters:
      value - The source polyglot publisher
      converter - The generated item converter
      targetType - The declared reactive return type
      Returns:
      The converted publisher, or null for None
      See Also:
    • convertReactiveValue

      public static <P> @Nullable P convertReactiveValue(org.graalvm.polyglot.Value value, Class<P> targetType)
      Convert the value a Python method returned to the reactive type its signature declares: a host Publisher or CompletionStage of another type (a Reactor Mono returned by a method implementing a CompletableFuture declaration without a return annotation) is adapted as by convertReactive(Object, Class); any other value is converted as by PythonConversion.convertValue(Value, Class).
      Type Parameters:
      P - The reactive type
      Parameters:
      value - The polyglot value the Python method returned
      targetType - The declared reactive return type
      Returns:
      The converted value, or null for None
      Throws:
      IllegalArgumentException - When a reactive value cannot be adapted to the declared type
    • convertReactive

      public static <P> @Nullable P convertReactive(@Nullable Object source, Class<P> targetType)
      Adapt a reactive value (a Publisher or a CompletionStage) to the reactive type a method declares. A value that already is of the declared type is returned as is; a publisher is otherwise wrapped as Mono.from/Flux.from when Reactor is present, completed into a future when a CompletionStage is declared, and converted through the shared ConversionService for any other reactive type; a stage is adapted through the publisher it completes.
      Type Parameters:
      P - The reactive type
      Parameters:
      source - The publisher or completion stage
      targetType - The declared reactive return type
      Returns:
      The adapted value, or null for a null source
      Throws:
      IllegalArgumentException - When the value cannot be adapted to the declared type
    • convertHttpResponse

      public static <T> HttpResponse<T> convertHttpResponse(HttpResponse<?> response, Class<T> bodyType)
      Convert a response body to the declared Java body type.
      Type Parameters:
      T - The response body type
      Parameters:
      response - The source response
      bodyType - The declared response body type
      Returns:
      The converted response