Last active
April 30, 2018 21:13
-
-
Save patrickhammond/6988682f226c0428f1fd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.androidfu.nowplaying.app.api; | |
import android.content.Context; | |
import com.androidfu.nowplaying.app.util.Log; | |
import com.androidfu.nowplaying.app.util.Preconditions; | |
import com.jakewharton.byteunits.DecimalByteUnit; | |
import com.squareup.okhttp.Cache; | |
import com.squareup.okhttp.OkHttpClient; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.concurrent.TimeUnit; | |
import hugo.weaving.DebugLog; | |
public final class OkHttpClientBuilder { | |
private static final String TAG = OkHttpClientBuilder.class.getSimpleName(); | |
private static final int DEFAULT_CONNECT_TIMEOUT = 10; | |
private static final int DEFAULT_WRITE_TIMEOUT = 10; | |
private static final int DEFAULT_READ_TIMEOUT = 10; | |
private static final long DEFAULT_DISK_CACHE_SIZE = DecimalByteUnit.MEGABYTES.toBytes(10); | |
private final Context context; | |
private int connectTimeout = DEFAULT_CONNECT_TIMEOUT; | |
private int writeTimeout = DEFAULT_WRITE_TIMEOUT; | |
private int readTimeout = DEFAULT_READ_TIMEOUT; | |
private long diskCacheSize = DEFAULT_DISK_CACHE_SIZE; | |
private File directory; | |
public OkHttpClientBuilder(Context context) { | |
Preconditions.checkNotNull(context, "context != null"); | |
this.context = context; | |
} | |
public Builder connectTimeout(int connectTimeout) { | |
Preconditions.checkArgument(connectTimeout > 0, "connectTimeout must be > 0"); | |
this.connectTimeout = connectTimeout; | |
return this; | |
} | |
public Builder writeTimeout(int writeTimeout) { | |
Preconditions.checkArgument(writeTimeout > 0, "writeTimeout must be > 0"); | |
this.writeTimeout = writeTimeout; | |
return this; | |
} | |
public Builder readTimeout(int readTimeout) { | |
Preconditions.checkArgument(readTimeout > 0, "readTimeout must be > 0"); | |
this.readTimeout = readTimeout; | |
return this; | |
} | |
public Builder diskCacheSize(long diskCacheSize) { | |
Preconditions.checkArgument(diskCacheSize > 0, "diskSize must be > 0"); | |
this.diskCacheSize = diskCacheSize; | |
return this; | |
} | |
public Builder file(File directory) { | |
Preconditions.checkNotNull(directory, "directory != null"); | |
this.directory = directory; | |
return this; | |
} | |
public OkHttpClient build() { | |
ensureSaneDefaults(); | |
OkHttpClient client = new OkHttpClient(); | |
client.setConnectTimeout(connectTimeout, TimeUnit.SECONDS); | |
client.setWriteTimeout(writeTimeout, TimeUnit.SECONDS); | |
client.setReadTimeout(readTimeout, TimeUnit.SECONDS); | |
if (directory != null) { // Or some other condition | |
try { | |
Cache cache = new Cache(directory, diskCacheSize); | |
client.setCache(cache); | |
} catch (IOException e) { | |
Log.e(TAG, String.format("Error creating response cache: %s", e.getMessage()), e); | |
} | |
} | |
return client; | |
} | |
private void ensureSaneDefaults() { | |
if (directory == null) { | |
directory = new File(context.getCacheDir(), "http"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment