jasminb / jsonapi-converter

JSONAPI-Converter is a Java/Android library that provides support for working with JSONAPI spec
Apache License 2.0
272 stars 81 forks source link

Does the library support Javas webclient and the reactor implementation? #250

Open moizalidv opened 3 years ago

moizalidv commented 3 years ago

I see there is a a plugin for Okhttp3. Was wondering if there will be support for the the reactive webclient

moizalidv commented 3 years ago

I have been able to make it work:

fun <T> WebClient.fetchJsonApiSingleObject(
    resourceConverter: ResourceConverter,
    modelImplementation: Class<T>,
    errorHandler: ((ClientResponse) -> Mono<Throwable>)? = null,
    uriBuilderFunction: (UriBuilder) -> URI
): Mono<T> =
    get()
        .uri(uriBuilderFunction)
        .retrieve()
        .onStatus(HttpStatus::isError) { clientResponse: ClientResponse ->
            errorHandler?.invoke(clientResponse) ?: handleErrors(clientResponse)
        }
        .bodyToMono(ByteArray::class.java)
        .map {
            resourceConverter.readDocument(it, modelImplementation).get()  as T
        }