Skip to content

Commit

Permalink
Merge pull request #346 from nielsvanvelzen/java-continuation
Browse files Browse the repository at this point in the history
Rewrite Java callback helpers to support error handling and non-api functions
  • Loading branch information
Maxr1998 authored Dec 31, 2021
2 parents 33bf6b4 + e14969e commit d7ac4f0
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 95 deletions.
12 changes: 3 additions & 9 deletions jellyfin-api/api/jellyfin-api.api
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,11 @@ public final class org/jellyfin/sdk/api/client/Response {
public final synthetic fun getValue (Ljava/lang/Object;Lkotlin/reflect/KProperty;)Ljava/lang/Object;
}

public abstract class org/jellyfin/sdk/api/client/compatibility/JavaCallback : kotlin/coroutines/Continuation {
public abstract class org/jellyfin/sdk/api/client/compatibility/JavaResponseContinuation : kotlin/coroutines/Continuation {
public fun <init> ()V
public fun getContext ()Lkotlin/coroutines/CoroutineContext;
public abstract fun onResponse (Lorg/jellyfin/sdk/api/client/Response;)V
public fun resumeWith (Ljava/lang/Object;)V
}

public abstract class org/jellyfin/sdk/api/client/compatibility/JavaDataCallback : kotlin/coroutines/Continuation {
public fun <init> ()V
public fun getContext ()Lkotlin/coroutines/CoroutineContext;
public abstract fun onData (Ljava/lang/Object;)V
public fun onError (Ljava/lang/Throwable;)V
public abstract fun onResponse (Ljava/lang/Object;)V
public fun resumeWith (Ljava/lang/Object;)V
}

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.jellyfin.sdk.api.client.compatibility

import org.jellyfin.sdk.api.client.Response
import kotlin.coroutines.Continuation
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext

public abstract class JavaResponseContinuation<T> : Continuation<Response<T>> {
override val context: CoroutineContext = EmptyCoroutineContext
override fun resumeWith(result: Result<Response<T>>): Unit = result.fold(
onSuccess = { onResponse(it.content) },
onFailure = ::onError,
)

public abstract fun onResponse(response: T)
public open fun onError(error: Throwable): Unit = throw error
}
60 changes: 34 additions & 26 deletions jellyfin-core/api/android/jellyfin-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,40 @@ public final class org/jellyfin/sdk/android/AndroidDeviceKt {
public static final fun androidDevice (Landroid/content/Context;)Lorg/jellyfin/sdk/model/DeviceInfo;
}

public abstract class org/jellyfin/sdk/compatibility/JavaContinuation : kotlin/coroutines/Continuation {
public fun <init> ()V
public fun getContext ()Lkotlin/coroutines/CoroutineContext;
public fun onError (Ljava/lang/Throwable;)V
public abstract fun onSuccess (Ljava/lang/Object;)V
public fun resumeWith (Ljava/lang/Object;)V
}

public final class org/jellyfin/sdk/compatibility/JavaFlow {
public static final field INSTANCE Lorg/jellyfin/sdk/compatibility/JavaFlow;
public static final fun collect (Lkotlinx/coroutines/flow/Flow;Lorg/jellyfin/sdk/compatibility/JavaFlow$ResultCallback;)Lorg/jellyfin/sdk/compatibility/JavaFlow$FlowJob;
public static final fun collect (Lkotlinx/coroutines/flow/Flow;Lorg/jellyfin/sdk/compatibility/JavaFlow$StartCallback;Lorg/jellyfin/sdk/compatibility/JavaFlow$ResultCallback;)Lorg/jellyfin/sdk/compatibility/JavaFlow$FlowJob;
public static final fun collect (Lkotlinx/coroutines/flow/Flow;Lorg/jellyfin/sdk/compatibility/JavaFlow$StartCallback;Lorg/jellyfin/sdk/compatibility/JavaFlow$ResultCallback;Lorg/jellyfin/sdk/compatibility/JavaFlow$CompletionCallback;)Lorg/jellyfin/sdk/compatibility/JavaFlow$FlowJob;
public static final fun collect (Lkotlinx/coroutines/flow/Flow;Lorg/jellyfin/sdk/compatibility/JavaFlow$StartCallback;Lorg/jellyfin/sdk/compatibility/JavaFlow$ResultCallback;Lorg/jellyfin/sdk/compatibility/JavaFlow$CompletionCallback;Lkotlinx/coroutines/CoroutineScope;)Lorg/jellyfin/sdk/compatibility/JavaFlow$FlowJob;
public static synthetic fun collect$default (Lkotlinx/coroutines/flow/Flow;Lorg/jellyfin/sdk/compatibility/JavaFlow$StartCallback;Lorg/jellyfin/sdk/compatibility/JavaFlow$ResultCallback;Lorg/jellyfin/sdk/compatibility/JavaFlow$CompletionCallback;Lkotlinx/coroutines/CoroutineScope;ILjava/lang/Object;)Lorg/jellyfin/sdk/compatibility/JavaFlow$FlowJob;
}

public abstract interface class org/jellyfin/sdk/compatibility/JavaFlow$CompletionCallback {
public abstract fun onCompletion (Ljava/lang/Throwable;)V
}

public final class org/jellyfin/sdk/compatibility/JavaFlow$FlowJob : java/io/Closeable {
public fun <init> (Lkotlinx/coroutines/Job;)V
public fun close ()V
}

public abstract interface class org/jellyfin/sdk/compatibility/JavaFlow$ResultCallback {
public abstract fun onResult (Ljava/lang/Object;)V
}

public abstract interface class org/jellyfin/sdk/compatibility/JavaFlow$StartCallback {
public abstract fun onStart ()V
}

public final class org/jellyfin/sdk/discovery/AddressCandidateHelper {
public static final field Companion Lorg/jellyfin/sdk/discovery/AddressCandidateHelper$Companion;
public static final field JF_HTTPS_PORT I
Expand Down Expand Up @@ -217,32 +251,6 @@ public final class org/jellyfin/sdk/discovery/RecommendedServerIssue$Unsupported
public fun toString ()Ljava/lang/String;
}

public final class org/jellyfin/sdk/discovery/compatibility/JavaFlow {
public static final field INSTANCE Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow;
public static final fun collect (Lkotlinx/coroutines/flow/Flow;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$ResultCallback;)Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$FlowJob;
public static final fun collect (Lkotlinx/coroutines/flow/Flow;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$StartCallback;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$ResultCallback;)Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$FlowJob;
public static final fun collect (Lkotlinx/coroutines/flow/Flow;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$StartCallback;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$ResultCallback;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$CompletionCallback;)Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$FlowJob;
public static final fun collect (Lkotlinx/coroutines/flow/Flow;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$StartCallback;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$ResultCallback;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$CompletionCallback;Lkotlinx/coroutines/CoroutineScope;)Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$FlowJob;
public static synthetic fun collect$default (Lkotlinx/coroutines/flow/Flow;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$StartCallback;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$ResultCallback;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$CompletionCallback;Lkotlinx/coroutines/CoroutineScope;ILjava/lang/Object;)Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$FlowJob;
}

public abstract interface class org/jellyfin/sdk/discovery/compatibility/JavaFlow$CompletionCallback {
public abstract fun onCompletion (Ljava/lang/Throwable;)V
}

public final class org/jellyfin/sdk/discovery/compatibility/JavaFlow$FlowJob : java/io/Closeable {
public fun <init> (Lkotlinx/coroutines/Job;)V
public fun close ()V
}

public abstract interface class org/jellyfin/sdk/discovery/compatibility/JavaFlow$ResultCallback {
public abstract fun onResult (Ljava/lang/Object;)V
}

public abstract interface class org/jellyfin/sdk/discovery/compatibility/JavaFlow$StartCallback {
public abstract fun onStart ()V
}

public abstract interface class org/jellyfin/sdk/util/ApiClientFactory {
public abstract fun create (Ljava/lang/String;Ljava/lang/String;Ljava/util/UUID;Lorg/jellyfin/sdk/model/ClientInfo;Lorg/jellyfin/sdk/model/DeviceInfo;Lorg/jellyfin/sdk/api/client/HttpClientOptions;)Lorg/jellyfin/sdk/api/client/ApiClient;
}
Expand Down
60 changes: 34 additions & 26 deletions jellyfin-core/api/jvm/jellyfin-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,40 @@ public final class org/jellyfin/sdk/JellyfinOptionsKt {
public static final fun createJellyfinOptions (Lkotlin/jvm/functions/Function1;)Lorg/jellyfin/sdk/JellyfinOptions;
}

public abstract class org/jellyfin/sdk/compatibility/JavaContinuation : kotlin/coroutines/Continuation {
public fun <init> ()V
public fun getContext ()Lkotlin/coroutines/CoroutineContext;
public fun onError (Ljava/lang/Throwable;)V
public abstract fun onSuccess (Ljava/lang/Object;)V
public fun resumeWith (Ljava/lang/Object;)V
}

public final class org/jellyfin/sdk/compatibility/JavaFlow {
public static final field INSTANCE Lorg/jellyfin/sdk/compatibility/JavaFlow;
public static final fun collect (Lkotlinx/coroutines/flow/Flow;Lorg/jellyfin/sdk/compatibility/JavaFlow$ResultCallback;)Lorg/jellyfin/sdk/compatibility/JavaFlow$FlowJob;
public static final fun collect (Lkotlinx/coroutines/flow/Flow;Lorg/jellyfin/sdk/compatibility/JavaFlow$StartCallback;Lorg/jellyfin/sdk/compatibility/JavaFlow$ResultCallback;)Lorg/jellyfin/sdk/compatibility/JavaFlow$FlowJob;
public static final fun collect (Lkotlinx/coroutines/flow/Flow;Lorg/jellyfin/sdk/compatibility/JavaFlow$StartCallback;Lorg/jellyfin/sdk/compatibility/JavaFlow$ResultCallback;Lorg/jellyfin/sdk/compatibility/JavaFlow$CompletionCallback;)Lorg/jellyfin/sdk/compatibility/JavaFlow$FlowJob;
public static final fun collect (Lkotlinx/coroutines/flow/Flow;Lorg/jellyfin/sdk/compatibility/JavaFlow$StartCallback;Lorg/jellyfin/sdk/compatibility/JavaFlow$ResultCallback;Lorg/jellyfin/sdk/compatibility/JavaFlow$CompletionCallback;Lkotlinx/coroutines/CoroutineScope;)Lorg/jellyfin/sdk/compatibility/JavaFlow$FlowJob;
public static synthetic fun collect$default (Lkotlinx/coroutines/flow/Flow;Lorg/jellyfin/sdk/compatibility/JavaFlow$StartCallback;Lorg/jellyfin/sdk/compatibility/JavaFlow$ResultCallback;Lorg/jellyfin/sdk/compatibility/JavaFlow$CompletionCallback;Lkotlinx/coroutines/CoroutineScope;ILjava/lang/Object;)Lorg/jellyfin/sdk/compatibility/JavaFlow$FlowJob;
}

public abstract interface class org/jellyfin/sdk/compatibility/JavaFlow$CompletionCallback {
public abstract fun onCompletion (Ljava/lang/Throwable;)V
}

public final class org/jellyfin/sdk/compatibility/JavaFlow$FlowJob : java/io/Closeable {
public fun <init> (Lkotlinx/coroutines/Job;)V
public fun close ()V
}

public abstract interface class org/jellyfin/sdk/compatibility/JavaFlow$ResultCallback {
public abstract fun onResult (Ljava/lang/Object;)V
}

public abstract interface class org/jellyfin/sdk/compatibility/JavaFlow$StartCallback {
public abstract fun onStart ()V
}

public final class org/jellyfin/sdk/discovery/AddressCandidateHelper {
public static final field Companion Lorg/jellyfin/sdk/discovery/AddressCandidateHelper$Companion;
public static final field JF_HTTPS_PORT I
Expand Down Expand Up @@ -204,32 +238,6 @@ public final class org/jellyfin/sdk/discovery/RecommendedServerIssue$Unsupported
public fun toString ()Ljava/lang/String;
}

public final class org/jellyfin/sdk/discovery/compatibility/JavaFlow {
public static final field INSTANCE Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow;
public static final fun collect (Lkotlinx/coroutines/flow/Flow;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$ResultCallback;)Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$FlowJob;
public static final fun collect (Lkotlinx/coroutines/flow/Flow;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$StartCallback;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$ResultCallback;)Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$FlowJob;
public static final fun collect (Lkotlinx/coroutines/flow/Flow;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$StartCallback;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$ResultCallback;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$CompletionCallback;)Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$FlowJob;
public static final fun collect (Lkotlinx/coroutines/flow/Flow;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$StartCallback;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$ResultCallback;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$CompletionCallback;Lkotlinx/coroutines/CoroutineScope;)Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$FlowJob;
public static synthetic fun collect$default (Lkotlinx/coroutines/flow/Flow;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$StartCallback;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$ResultCallback;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$CompletionCallback;Lkotlinx/coroutines/CoroutineScope;ILjava/lang/Object;)Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$FlowJob;
}

public abstract interface class org/jellyfin/sdk/discovery/compatibility/JavaFlow$CompletionCallback {
public abstract fun onCompletion (Ljava/lang/Throwable;)V
}

public final class org/jellyfin/sdk/discovery/compatibility/JavaFlow$FlowJob : java/io/Closeable {
public fun <init> (Lkotlinx/coroutines/Job;)V
public fun close ()V
}

public abstract interface class org/jellyfin/sdk/discovery/compatibility/JavaFlow$ResultCallback {
public abstract fun onResult (Ljava/lang/Object;)V
}

public abstract interface class org/jellyfin/sdk/discovery/compatibility/JavaFlow$StartCallback {
public abstract fun onStart ()V
}

public abstract interface class org/jellyfin/sdk/util/ApiClientFactory {
public abstract fun create (Ljava/lang/String;Ljava/lang/String;Ljava/util/UUID;Lorg/jellyfin/sdk/model/ClientInfo;Lorg/jellyfin/sdk/model/DeviceInfo;Lorg/jellyfin/sdk/api/client/HttpClientOptions;)Lorg/jellyfin/sdk/api/client/ApiClient;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.jellyfin.sdk.compatibility

import kotlin.coroutines.Continuation
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext

public abstract class JavaContinuation<T> : Continuation<T> {
override val context: CoroutineContext = EmptyCoroutineContext
override fun resumeWith(result: Result<T>): Unit = result.fold(
onSuccess = ::onSuccess,
onFailure = ::onError,
)

public abstract fun onSuccess(result: T)
public open fun onError(error: Throwable): Unit = throw error
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.jellyfin.sdk.discovery.compatibility
package org.jellyfin.sdk.compatibility

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand Down
18 changes: 11 additions & 7 deletions samples/java-cli/src/main/java/org/jellyfin/sample/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import org.jellyfin.sdk.Jellyfin;
import org.jellyfin.sdk.JellyfinOptions;
import org.jellyfin.sdk.api.client.ApiClient;
import org.jellyfin.sdk.api.client.compatibility.JavaDataCallback;
import org.jellyfin.sdk.api.client.compatibility.JavaResponseContinuation;
import org.jellyfin.sdk.api.operations.UserApi;
import org.jellyfin.sdk.model.ClientInfo;
import org.jellyfin.sdk.model.DeviceInfo;
import org.jellyfin.sdk.model.api.AuthenticateUserByName;
import org.jellyfin.sdk.model.api.AuthenticationResult;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -28,14 +28,18 @@ public static void main(String[] args) {

CountDownLatch latch = new CountDownLatch(1);
UserApi userApi = new UserApi(client);
userApi.authenticateUserByName(new AuthenticateUserByName("demo", null, ""), new JavaDataCallback<AuthenticationResult>() {
userApi.authenticateUserByName(new AuthenticateUserByName("demo", null, ""), new JavaResponseContinuation<AuthenticationResult>() {
@Override
public void onData(@Nullable AuthenticationResult data) {
assert data != null;
public void onResponse(@NotNull AuthenticationResult response) {
client.setAccessToken(response.getAccessToken());

client.setAccessToken(data.getAccessToken());
logger.info("Got access token: {}", response.getAccessToken());
latch.countDown();
}

logger.info("Got access token: {}", data.getAccessToken());
@Override
public void onError(@NotNull Throwable error) {
logger.error("Unable to get access token", error);
latch.countDown();
}
});
Expand Down

0 comments on commit d7ac4f0

Please sign in to comment.