Initial commit
This commit is contained in:
76
sources/com/ubtrobot/retrofit/adapter/urest/URestCall.java
Normal file
76
sources/com/ubtrobot/retrofit/adapter/urest/URestCall.java
Normal file
@@ -0,0 +1,76 @@
|
||||
package com.ubtrobot.retrofit.adapter.urest;
|
||||
|
||||
import com.ubt.jimu.unity.bluetooth.UnityActivity;
|
||||
import com.ubtrobot.http.rest.URestException;
|
||||
import java.io.IOException;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.net.ConnectException;
|
||||
import java.net.SocketException;
|
||||
import java.net.SocketTimeoutException;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Response;
|
||||
import retrofit2.Retrofit;
|
||||
|
||||
/* loaded from: classes2.dex */
|
||||
public class URestCall<T> {
|
||||
private final Retrofit a;
|
||||
private final Call<T> b;
|
||||
private volatile Response<T> c;
|
||||
|
||||
private static class ErrorMessage {
|
||||
public int a;
|
||||
public String b;
|
||||
|
||||
private ErrorMessage() {
|
||||
}
|
||||
}
|
||||
|
||||
URestCall(Retrofit retrofit, Call<T> call) {
|
||||
this.a = retrofit;
|
||||
this.b = call;
|
||||
}
|
||||
|
||||
private void b() {
|
||||
this.c = null;
|
||||
}
|
||||
|
||||
public T a() throws URestException {
|
||||
b();
|
||||
try {
|
||||
this.c = this.b.execute();
|
||||
if (this.c.isSuccessful()) {
|
||||
return this.c.body();
|
||||
}
|
||||
throw a(this.c);
|
||||
} catch (IOException e) {
|
||||
throw a(e);
|
||||
}
|
||||
}
|
||||
|
||||
private URestException a(Response<T> response) {
|
||||
try {
|
||||
ErrorMessage errorMessage = (ErrorMessage) this.a.responseBodyConverter(ErrorMessage.class, new Annotation[0]).convert(response.errorBody());
|
||||
if (errorMessage != null && errorMessage.a > 0) {
|
||||
return new URestException(errorMessage.a, errorMessage.b);
|
||||
}
|
||||
return new URestException(UnityActivity.CODE_BROWSE_PROGRAM, "Illegal error message response. Should be \"{\"code\": XXYY, \"message\": \"An error message\"}\"(XXYY is a positive integer.)");
|
||||
} catch (IOException e) {
|
||||
return new URestException(UnityActivity.CODE_BROWSE_PROGRAM, "Illegal error message response. Should be \"{\"code\": XXYY, \"message\": \"An error message\"}\"(XXYY is a positive integer.)", e);
|
||||
}
|
||||
}
|
||||
|
||||
private URestException a(Throwable th) {
|
||||
if (th instanceof SocketException) {
|
||||
if (th instanceof ConnectException) {
|
||||
return new URestException(-2, th.getMessage(), th);
|
||||
}
|
||||
if (th.getMessage() != null && th.getMessage().contains("Permission denied")) {
|
||||
return new URestException(-1, "Network permission denied. Add \"<uses-permission android:name=\"android.permission.INTERNET\" />\" in your AndroidManifest.xml", th);
|
||||
}
|
||||
}
|
||||
if (th instanceof SocketTimeoutException) {
|
||||
return new URestException(-3, "Socket timeout.", th);
|
||||
}
|
||||
return new URestException(-4, "Internal HTTP client error. Please contact the author to fix it.", th);
|
||||
}
|
||||
}
|
@@ -0,0 +1,54 @@
|
||||
package com.ubtrobot.retrofit.adapter.urest;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.CallAdapter;
|
||||
import retrofit2.Retrofit;
|
||||
|
||||
/* loaded from: classes2.dex */
|
||||
public class URestCallAdapterFactory extends CallAdapter.Factory {
|
||||
|
||||
private static final class ResponseCallAdapter<R> implements CallAdapter<R, URestCall<R>> {
|
||||
Retrofit a;
|
||||
Type b;
|
||||
|
||||
ResponseCallAdapter(Retrofit retrofit, Type type) {
|
||||
this.a = retrofit;
|
||||
this.b = type;
|
||||
}
|
||||
|
||||
@Override // retrofit2.CallAdapter
|
||||
public Type responseType() {
|
||||
return this.b;
|
||||
}
|
||||
|
||||
@Override // retrofit2.CallAdapter
|
||||
public URestCall<R> adapt(Call<R> call) {
|
||||
return new URestCall<>(this.a, call);
|
||||
}
|
||||
}
|
||||
|
||||
private URestCallAdapterFactory() {
|
||||
}
|
||||
|
||||
private Type a(Type type) {
|
||||
if (type instanceof ParameterizedType) {
|
||||
return CallAdapter.Factory.getParameterUpperBound(0, (ParameterizedType) type);
|
||||
}
|
||||
throw new IllegalArgumentException("Call return type must be parameterized as RestCall<Foo> or RestCall<? extends Foo>");
|
||||
}
|
||||
|
||||
public static URestCallAdapterFactory create() {
|
||||
return new URestCallAdapterFactory();
|
||||
}
|
||||
|
||||
@Override // retrofit2.CallAdapter.Factory
|
||||
public CallAdapter<?, ?> get(Type type, Annotation[] annotationArr, Retrofit retrofit) {
|
||||
if (CallAdapter.Factory.getRawType(type) != URestCall.class) {
|
||||
return null;
|
||||
}
|
||||
return new ResponseCallAdapter(retrofit, a(type));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user