Skip to content

Instantly share code, notes, and snippets.

@swankjesse
Created April 2, 2017 14:29
Show Gist options
  • Save swankjesse/d461f9020b9051afb7b5cf1323e93abb to your computer and use it in GitHub Desktop.
Save swankjesse/d461f9020b9051afb7b5cf1323e93abb to your computer and use it in GitHub Desktop.
/*
* Copyright (C) 2017 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okhttp3.recipes;
import java.io.File;
import java.io.IOException;
import okhttp3.Cache;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public final class Cheat {
private final CheatingInterceptor cheatingInterceptor;
private final OkHttpClient client;
public Cheat(File cacheDirectory) {
cheatingInterceptor = new CheatingInterceptor();
int cacheSize = 10 * 1024 * 1024; // 10 MiB
client = new OkHttpClient.Builder()
.addInterceptor(cheatingInterceptor)
.cache(new Cache(cacheDirectory, cacheSize))
.build();
}
public void run() throws Exception {
Request request = new Request.Builder()
.url("https://publicobject.com/helloworld.txt")
.build();
// Request 1 seeds the cache.
try (Response response = client.newCall(request).execute()) {
System.out.println("Response 1 response: " + response);
System.out.println("Response 1 cache response: " + response.cacheResponse());
}
// Request 2 exercises the cache.
try (Response response = client.newCall(request).execute()) {
System.out.println("Response 2 response: " + response);
System.out.println("Response 2 cache response: " + response.cacheResponse());
}
// Request 3 uses an interceptor to disable the cache. Note that the cheating interceptor breaks
// call canceling.
cheatingInterceptor.clientOverride = new OkHttpClient.Builder()
.cache(null)
.build();
try (Response response = client.newCall(request).execute()) {
System.out.println("Response 3 response: " + response);
System.out.println("Response 3 cache response: " + response.cacheResponse());
}
}
public static void main(String... args) throws Exception {
new Cheat(new File("Cheat.tmp")).run();
}
/** Note that this breaks call canceling. */
public static class CheatingInterceptor implements Interceptor {
volatile OkHttpClient clientOverride;
@Override public Response intercept(Chain chain) throws IOException {
OkHttpClient override = clientOverride;
if (override != null) {
return override.newCall(chain.request()).execute();
}
return chain.proceed(chain.request());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment