Skip to content

Commit

Permalink
Split OkHttpNetworkFetcher's fetch method to make part of it reusable
Browse files Browse the repository at this point in the history
Subclasses overriding the fetch method might want to reuse some of its functionality. This change splits the fetch method to make this reusability possible.
  • Loading branch information
Adam Comella committed Oct 7, 2016
1 parent cc2479a commit 9aa1e75
Showing 1 changed file with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,8 @@ public OkHttpNetworkFetchState createFetchState(
return new OkHttpNetworkFetchState(consumer, context);
}

@Override
public void fetch(final OkHttpNetworkFetchState fetchState, final Callback callback) {
fetchState.submitTime = SystemClock.elapsedRealtime();
final Uri uri = fetchState.getUri();
final Request request = new Request.Builder()
.cacheControl(new CacheControl.Builder().noStore().build())
.url(uri.toString())
.get()
.build();
protected void fetchWithRequest(final OkHttpNetworkFetchState fetchState, final Callback callback,
final Request request) {
final Call call = mOkHttpClient.newCall(request);

fetchState.getContext().addCallbacks(
Expand Down Expand Up @@ -109,9 +102,9 @@ public void onResponse(Call call, Response response) throws IOException {
try {
if (!response.isSuccessful()) {
handleException(
call,
new IOException("Unexpected HTTP code " + response),
callback);
call,
new IOException("Unexpected HTTP code " + response),
callback);
return;
}

Expand All @@ -138,6 +131,18 @@ public void onFailure(Call call, IOException e) {
});
}

@Override
public void fetch(final OkHttpNetworkFetchState fetchState, final Callback callback) {
fetchState.submitTime = SystemClock.elapsedRealtime();
final Uri uri = fetchState.getUri();
final Request request = new Request.Builder()
.cacheControl(new CacheControl.Builder().noStore().build())
.url(uri.toString())
.get()
.build();
fetchWithRequest(fetchState, callback, request);
}

@Override
public void onFetchCompletion(OkHttpNetworkFetchState fetchState, int byteSize) {
fetchState.fetchCompleteTime = SystemClock.elapsedRealtime();
Expand Down

0 comments on commit 9aa1e75

Please sign in to comment.