Skip to content

Commit

Permalink
chore(sync): Minor code refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Niedermann <[email protected]>
  • Loading branch information
stefan-niedermann committed Jan 16, 2024
1 parent 420a62a commit d275548
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
3 changes: 0 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,6 @@ dependencies {
// Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.9.0'

// ReactiveX
implementation 'io.reactivex.rxjava2:rxjava:2.2.21'

// Zoom Layout
implementation("com.otaliastudios:zoomlayout:1.9.0")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public synchronized void initSsoApi(@NonNull final NextcloudAPI.ApiConnectedList
}
}

@NonNull
public Context getContext() {
return this.context;
}

public DeckAPI getDeckAPI() {
return deckAPI;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package it.niedermann.nextcloud.deck.remote.api;

import android.content.Context;

import androidx.annotation.NonNull;

import com.nextcloud.android.sso.exceptions.NextcloudHttpRequestFailedException;

import java.util.function.Supplier;

import it.niedermann.nextcloud.deck.DeckLog;
import it.niedermann.nextcloud.deck.util.ExecutorServiceProvider;
import okhttp3.Request;
Expand All @@ -14,64 +18,59 @@

public class RequestHelper {

// static {
// RxJavaPlugins.setErrorHandler(DeckLog::logError);
// }

public static <T> void request(@NonNull final ApiProvider provider, @NonNull final ObservableProvider<T> call, @NonNull final ResponseCallback<T> callback) {
public static <T> void request(@NonNull final ApiProvider provider,
@NonNull final Supplier<Call<T>> callProvider,
@NonNull final ResponseCallback<T> callback) {
if (provider.getDeckAPI() == null) {
provider.initSsoApi(callback::onError);
}

final ResponseConsumer<T> cb = new ResponseConsumer<>(callback);
ExecutorServiceProvider.getLinkedBlockingQueueExecutor().submit(() -> call.getObservableFromCall().enqueue(cb));
// .subscribeOn(Schedulers.from(ExecutorServiceProvider.getExecutorService()))
// .subscribe(cb, cb.getExceptionConsumer());
}

public interface ObservableProvider<T> {
Call<T> getObservableFromCall();
final ResponseConsumer<T> cb = new ResponseConsumer<>(provider.getContext(), callback);
ExecutorServiceProvider.getLinkedBlockingQueueExecutor().submit(() -> callProvider.get().enqueue(cb));
}

private static class ResponseConsumer<T> implements Callback<T> {
@NonNull
private final Context context;
@NonNull
private final ResponseCallback<T> callback;

private ResponseConsumer(@NonNull ResponseCallback<T> callback) {
private ResponseConsumer(@NonNull Context context, @NonNull ResponseCallback<T> callback) {
this.context = context;
this.callback = callback;
}

@Override
public void onResponse(@NonNull Call<T> call, Response<T> response) {
if(response.isSuccessful()) {
if (response.isSuccessful()) {
T responseObject = response.body();
callback.fillAccountIDs(responseObject);
callback.onResponseWithHeaders(responseObject, response.headers());
} else {
onFailure(call, new NextcloudHttpRequestFailedException(response.code(), buildCause(response)));
onFailure(call, new NextcloudHttpRequestFailedException(context, response.code(), buildCause(response)));
}
}

private RuntimeException buildCause(Response<T> response){
private RuntimeException buildCause(Response<T> response) {
Request request = response.raw().request();
String url = request.url().toString();
String method = request.method();
int code = response.code();
String responseBody = "<empty>";
try (ResponseBody body = response.errorBody()) {
if (body != null) {
responseBody = response.errorBody().string() ;
responseBody = response.errorBody().string();
}
} catch (Exception e) {
responseBody = "<unable to build response body: "+e.getMessage()+">";
responseBody = "<unable to build response body: " + e.getMessage() + ">";
}
return new RuntimeException("HTTP StatusCode wasn't 2xx:\n" +
"Got [HTTP " + code + "] for Call [" + method + " " + url + "] with Message:\n" +
"[" + responseBody + "]");
}

@Override
public void onFailure(@NonNull Call<T> call, Throwable t) {
public void onFailure(@NonNull Call<T> call, @NonNull Throwable t) {
DeckLog.logError(t);
callback.onError(ServerCommunicationErrorHandler.translateError(t));
}
Expand Down

0 comments on commit d275548

Please sign in to comment.