updated
This commit is contained in:
parent
a3a4618769
commit
7a42723b9e
103 changed files with 1879 additions and 1195 deletions
|
@ -15,12 +15,16 @@
|
|||
package code.name.monkey.retromusic.rest;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import code.name.monkey.retromusic.rest.service.LastFMService;
|
||||
import java.io.File;
|
||||
import okhttp3.Cache;
|
||||
import okhttp3.Call;
|
||||
import okhttp3.ConnectionPool;
|
||||
import okhttp3.Interceptor;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
|
@ -31,50 +35,55 @@ import retrofit2.converter.gson.GsonConverterFactory;
|
|||
|
||||
public class LastFMRestClient {
|
||||
|
||||
private static final String BASE_URL = "https://ws.audioscrobbler.com/2.0/";
|
||||
private static final String BASE_URL = "https://ws.audioscrobbler.com/2.0/";
|
||||
|
||||
private LastFMService apiService;
|
||||
private LastFMService apiService;
|
||||
|
||||
public LastFMRestClient(@NonNull Context context) {
|
||||
this(createDefaultOkHttpClientBuilder(context).build());
|
||||
}
|
||||
|
||||
public LastFMRestClient(@NonNull Call.Factory client) {
|
||||
Retrofit restAdapter = new Retrofit.Builder()
|
||||
.baseUrl(BASE_URL)
|
||||
.callFactory(client)
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
|
||||
.build();
|
||||
|
||||
apiService = restAdapter.create(LastFMService.class);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static Cache createDefaultCache(Context context) {
|
||||
File cacheDir = new File(context.getCacheDir().getAbsolutePath(), "/okhttp-lastfm/");
|
||||
if (cacheDir.mkdirs() || cacheDir.isDirectory()) {
|
||||
return new Cache(cacheDir, 1024 * 1024 * 10);
|
||||
public LastFMRestClient(@NonNull Context context) {
|
||||
this(createDefaultOkHttpClientBuilder(context).build());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static Interceptor createCacheControlInterceptor() {
|
||||
return chain -> {
|
||||
Request modifiedRequest = chain.request().newBuilder()
|
||||
.addHeader("Cache-Control", String.format("max-age=%d, max-stale=%d", 31536000, 31536000))
|
||||
.build();
|
||||
return chain.proceed(modifiedRequest);
|
||||
};
|
||||
}
|
||||
public LastFMRestClient(@NonNull Call.Factory client) {
|
||||
Retrofit restAdapter = new Retrofit.Builder()
|
||||
.baseUrl(BASE_URL)
|
||||
.callFactory(client)
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
|
||||
.build();
|
||||
|
||||
public static OkHttpClient.Builder createDefaultOkHttpClientBuilder(Context context) {
|
||||
return new OkHttpClient.Builder()
|
||||
.cache(createDefaultCache(context))
|
||||
.addInterceptor(createCacheControlInterceptor());
|
||||
}
|
||||
apiService = restAdapter.create(LastFMService.class);
|
||||
}
|
||||
|
||||
public LastFMService getApiService() {
|
||||
return apiService;
|
||||
}
|
||||
@Nullable
|
||||
private static Cache createDefaultCache(Context context) {
|
||||
File cacheDir = new File(context.getCacheDir().getAbsolutePath(), "/okhttp-lastfm/");
|
||||
if (cacheDir.mkdirs() || cacheDir.isDirectory()) {
|
||||
return new Cache(cacheDir, 1024 * 1024 * 10);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static Interceptor createCacheControlInterceptor() {
|
||||
return chain -> {
|
||||
Request modifiedRequest = chain.request().newBuilder()
|
||||
.addHeader("Cache-Control", String.format("max-age=%d, max-stale=%d", 31536000, 31536000))
|
||||
.build();
|
||||
return chain.proceed(modifiedRequest);
|
||||
};
|
||||
}
|
||||
|
||||
public static OkHttpClient.Builder createDefaultOkHttpClientBuilder(Context context) {
|
||||
return new OkHttpClient.Builder()
|
||||
.connectionPool(new ConnectionPool(0, 1, TimeUnit.NANOSECONDS))
|
||||
.retryOnConnectionFailure(true)
|
||||
.connectTimeout(1, TimeUnit.MINUTES) // connect timeout
|
||||
.writeTimeout(1, TimeUnit.MINUTES) // write timeout
|
||||
.readTimeout(1, TimeUnit.MINUTES) // read timeout
|
||||
.cache(createDefaultCache(context))
|
||||
.addInterceptor(createCacheControlInterceptor());
|
||||
}
|
||||
|
||||
public LastFMService getApiService() {
|
||||
return apiService;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue