handles wrong api format

This commit is contained in:
Mariotaku Lee 2015-06-30 08:45:42 +08:00
parent 4cbd0ac353
commit 6b02d12ea3
2 changed files with 41 additions and 14 deletions

View File

@ -11,7 +11,6 @@ import android.text.TextUtils;
import android.util.Pair; import android.util.Pair;
import android.webkit.URLUtil; import android.webkit.URLUtil;
import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.internal.Internal; import com.squareup.okhttp.internal.Internal;
@ -176,7 +175,7 @@ public class TwitterAPIFactory implements TwidereConstants {
return TwitterAPIFactory.getInstance(context, getEndpoint(credentials, cls), credentials, cls); return TwitterAPIFactory.getInstance(context, getEndpoint(credentials, cls), credentials, cls);
} }
public static Endpoint getEndpoint(ParcelableCredentials credentials, Class<?> cls) throws APIFormatException { public static Endpoint getEndpoint(ParcelableCredentials credentials, Class<?> cls) {
final String apiUrlFormat; final String apiUrlFormat;
final boolean sameOAuthSigningUrl = credentials.same_oauth_signing_url; final boolean sameOAuthSigningUrl = credentials.same_oauth_signing_url;
final boolean noVersionSuffix = credentials.no_version_suffix; final boolean noVersionSuffix = credentials.no_version_suffix;
@ -203,8 +202,6 @@ public class TwitterAPIFactory implements TwidereConstants {
} }
final String endpointUrl; final String endpointUrl;
endpointUrl = getApiUrl(apiUrlFormat, domain, versionSuffix); endpointUrl = getApiUrl(apiUrlFormat, domain, versionSuffix);
if (endpointUrl == null || HttpUrl.parse(endpointUrl) == null)
throw new APIFormatException(apiUrlFormat);
if (credentials.auth_type == ParcelableCredentials.AUTH_TYPE_XAUTH || credentials.auth_type == ParcelableCredentials.AUTH_TYPE_OAUTH) { if (credentials.auth_type == ParcelableCredentials.AUTH_TYPE_XAUTH || credentials.auth_type == ParcelableCredentials.AUTH_TYPE_OAUTH) {
final String signEndpointUrl; final String signEndpointUrl;
if (!sameOAuthSigningUrl) { if (!sameOAuthSigningUrl) {
@ -212,8 +209,6 @@ public class TwitterAPIFactory implements TwidereConstants {
} else { } else {
signEndpointUrl = endpointUrl; signEndpointUrl = endpointUrl;
} }
if (signEndpointUrl == null || HttpUrl.parse(signEndpointUrl) == null)
throw new APIFormatException(apiUrlFormat);
return new OAuthEndpoint(endpointUrl, signEndpointUrl); return new OAuthEndpoint(endpointUrl, signEndpointUrl);
} }
return new Endpoint(endpointUrl); return new Endpoint(endpointUrl);
@ -408,9 +403,4 @@ public class TwitterAPIFactory implements TwidereConstants {
} }
} }
public static class APIFormatException extends RuntimeException {
public APIFormatException(String format) {
super("Wrong api format " + format);
}
}
} }

View File

@ -28,6 +28,7 @@ import android.util.Pair;
import com.squareup.okhttp.Call; import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback; import com.squareup.okhttp.Callback;
import com.squareup.okhttp.Headers; import com.squareup.okhttp.Headers;
import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.MediaType; import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request; import com.squareup.okhttp.Request;
@ -48,6 +49,7 @@ import org.mariotaku.twidere.util.DebugModeUtils;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.MalformedURLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -73,10 +75,14 @@ public class OkHttpRestClient implements RestHttpClient {
return new OkRestHttpResponse(call.execute()); return new OkRestHttpResponse(call.execute());
} }
private Call newCall(final RestHttpRequest restHttpRequest) { private Call newCall(final RestHttpRequest restHttpRequest) throws MalformedURLException {
final Request.Builder builder = new Request.Builder(); final Request.Builder builder = new Request.Builder();
builder.method(restHttpRequest.getMethod(), RestToOkBody.wrap(restHttpRequest.getBody())); builder.method(restHttpRequest.getMethod(), RestToOkBody.wrap(restHttpRequest.getBody()));
builder.url(restHttpRequest.getUrl()); final HttpUrl httpUrl = HttpUrl.parse(restHttpRequest.getUrl());
if (httpUrl == null) {
throw new MalformedURLException();
}
builder.url(httpUrl);
final List<Pair<String, String>> headers = restHttpRequest.getHeaders(); final List<Pair<String, String>> headers = restHttpRequest.getHeaders();
if (headers != null) { if (headers != null) {
for (Pair<String, String> header : headers) { for (Pair<String, String> header : headers) {
@ -89,7 +95,23 @@ public class OkHttpRestClient implements RestHttpClient {
@Override @Override
public RestQueuedRequest enqueue(final RestHttpRequest request, final RestHttpCallback callback) { public RestQueuedRequest enqueue(final RestHttpRequest request, final RestHttpCallback callback) {
final Call call = newCall(request); final Call call;
try {
call = newCall(request);
} catch (final MalformedURLException e) {
final DummyRequest dummyCall = new DummyRequest();
client.getDispatcher().getExecutorService().execute(new Runnable() {
@Override
public void run() {
if (dummyCall.isCancelled()) {
callback.cancelled();
return;
}
callback.exception(e);
}
});
return dummyCall;
}
call.enqueue(new Callback() { call.enqueue(new Callback() {
@Override @Override
public void onFailure(final Request request, final IOException e) { public void onFailure(final Request request, final IOException e) {
@ -234,6 +256,21 @@ public class OkHttpRestClient implements RestHttpClient {
} }
} }
private static class DummyRequest implements RestQueuedRequest {
private boolean cancelled;
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void cancel() {
cancelled = true;
}
}
private static class OkHttpQueuedRequest implements RestQueuedRequest { private static class OkHttpQueuedRequest implements RestQueuedRequest {
private final OkHttpClient client; private final OkHttpClient client;
private final Call call; private final Call call;