Centralized Retrofit client policies

This commit is contained in:
antonio 2023-03-11 19:37:34 +01:00
parent d07855a48d
commit ea151e8438
10 changed files with 77 additions and 336 deletions

View file

@ -0,0 +1,59 @@
package com.cappielloantonio.play.subsonic
import com.cappielloantonio.play.App
import com.cappielloantonio.play.subsonic.utils.CacheUtil
import com.google.gson.GsonBuilder
import okhttp3.Cache
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit
class RetrofitClient(subsonic: Subsonic) {
var retrofit: Retrofit
init {
retrofit = Retrofit.Builder()
.baseUrl(subsonic.url)
.addConverterFactory(GsonConverterFactory.create(GsonBuilder().setLenient().create()))
.client(getOkHttpClient())
.build()
}
private fun getOkHttpClient(): OkHttpClient {
val cacheUtil = CacheUtil(60, 60 * 60 * 24 * 30)
// BrowsingClient 60
// MediaAnnotationClient 0
// MediaLibraryScanningClient 0
// MediaRetrievalClient 0
// PlaylistClient 0
// PodcastClient 60
// SearchClient 60
// SystemClient 60
// AlbumSongListClient 60
return OkHttpClient.Builder()
.callTimeout(2, TimeUnit.MINUTES)
.connectTimeout(20, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.addInterceptor(getHttpLoggingInterceptor())
.addInterceptor(cacheUtil.offlineInterceptor)
.addNetworkInterceptor(cacheUtil.onlineInterceptor)
.cache(getCache())
.build()
}
private fun getHttpLoggingInterceptor(): HttpLoggingInterceptor {
val loggingInterceptor = HttpLoggingInterceptor()
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
return loggingInterceptor
}
private fun getCache(): Cache {
val cacheSize = 10 * 1024 * 1024
return Cache(App.getContext().cacheDir, cacheSize.toLong())
}
}

View file

@ -3,6 +3,7 @@ package com.cappielloantonio.play.subsonic.api.albumsonglist;
import android.util.Log;
import com.cappielloantonio.play.App;
import com.cappielloantonio.play.subsonic.RetrofitClient;
import com.cappielloantonio.play.subsonic.Subsonic;
import com.cappielloantonio.play.subsonic.base.ApiResponse;
import com.cappielloantonio.play.subsonic.utils.CacheUtil;
@ -25,14 +26,7 @@ public class AlbumSongListClient {
public AlbumSongListClient(Subsonic subsonic) {
this.subsonic = subsonic;
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(subsonic.getUrl())
.addConverterFactory(GsonConverterFactory.create(new GsonBuilder().setLenient().create()))
.client(getOkHttpClient())
.build();
this.albumSongListService = retrofit.create(AlbumSongListService.class);
this.albumSongListService = new RetrofitClient(subsonic).getRetrofit().create(AlbumSongListService.class);
}
public Call<ApiResponse> getAlbumList(String type, int size, int offset) {
@ -69,35 +63,4 @@ public class AlbumSongListClient {
Log.d(TAG, "getStarred2()");
return albumSongListService.getStarred2(subsonic.getParams());
}
private OkHttpClient getOkHttpClient() {
CacheUtil cacheUtil = new CacheUtil(60, 60 * 60 * 24 * 30);
return new OkHttpClient.Builder()
.callTimeout(2, TimeUnit.MINUTES)
.connectTimeout(20, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.callTimeout(2, TimeUnit.MINUTES)
.connectTimeout(20, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.addInterceptor(getHttpLoggingInterceptor())
.addInterceptor(cacheUtil.offlineInterceptor)
.addNetworkInterceptor(cacheUtil.onlineInterceptor)
.cache(getCache())
.build();
}
private HttpLoggingInterceptor getHttpLoggingInterceptor() {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
return loggingInterceptor;
}
private Cache getCache() {
int cacheSize = 10 * 1024 * 1024;
return new Cache(App.getContext().getCacheDir(), cacheSize);
}
}

View file

@ -2,20 +2,11 @@ package com.cappielloantonio.play.subsonic.api.browsing;
import android.util.Log;
import com.cappielloantonio.play.App;
import com.cappielloantonio.play.subsonic.RetrofitClient;
import com.cappielloantonio.play.subsonic.Subsonic;
import com.cappielloantonio.play.subsonic.base.ApiResponse;
import com.cappielloantonio.play.subsonic.utils.CacheUtil;
import com.google.gson.GsonBuilder;
import java.util.concurrent.TimeUnit;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class BrowsingClient {
private static final String TAG = "BrowsingClient";
@ -25,14 +16,7 @@ public class BrowsingClient {
public BrowsingClient(Subsonic subsonic) {
this.subsonic = subsonic;
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(subsonic.getUrl())
.addConverterFactory(GsonConverterFactory.create(new GsonBuilder().setLenient().create()))
.client(getOkHttpClient())
.build();
this.browsingService = retrofit.create(BrowsingService.class);
this.browsingService = new RetrofitClient(subsonic).getRetrofit().create(BrowsingService.class);
}
public Call<ApiResponse> getMusicFolders() {
@ -119,31 +103,4 @@ public class BrowsingClient {
Log.d(TAG, "getTopSongs()");
return browsingService.getTopSongs(subsonic.getParams(), artist, count);
}
private OkHttpClient getOkHttpClient() {
CacheUtil cacheUtil = new CacheUtil(60, 60 * 60 * 24 * 30);
return new OkHttpClient.Builder()
.callTimeout(2, TimeUnit.MINUTES)
.connectTimeout(20, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.addInterceptor(getHttpLoggingInterceptor())
.addInterceptor(cacheUtil.offlineInterceptor)
.addNetworkInterceptor(cacheUtil.onlineInterceptor)
.cache(getCache())
.build();
}
private HttpLoggingInterceptor getHttpLoggingInterceptor() {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
return loggingInterceptor;
}
private Cache getCache() {
int cacheSize = 10 * 1024 * 1024;
return new Cache(App.getContext().getCacheDir(), cacheSize);
}
}

View file

@ -2,20 +2,11 @@ package com.cappielloantonio.play.subsonic.api.mediaannotation;
import android.util.Log;
import com.cappielloantonio.play.App;
import com.cappielloantonio.play.subsonic.RetrofitClient;
import com.cappielloantonio.play.subsonic.Subsonic;
import com.cappielloantonio.play.subsonic.base.ApiResponse;
import com.cappielloantonio.play.subsonic.utils.CacheUtil;
import com.google.gson.GsonBuilder;
import java.util.concurrent.TimeUnit;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MediaAnnotationClient {
private static final String TAG = "BrowsingClient";
@ -25,14 +16,7 @@ public class MediaAnnotationClient {
public MediaAnnotationClient(Subsonic subsonic) {
this.subsonic = subsonic;
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(subsonic.getUrl())
.addConverterFactory(GsonConverterFactory.create(new GsonBuilder().setLenient().create()))
.client(getOkHttpClient())
.build();
this.mediaAnnotationService = retrofit.create(MediaAnnotationService.class);
this.mediaAnnotationService = new RetrofitClient(subsonic).getRetrofit().create(MediaAnnotationService.class);
}
public Call<ApiResponse> star(String id, String albumId, String artistId) {
@ -54,30 +38,4 @@ public class MediaAnnotationClient {
Log.d(TAG, "scrobble()");
return mediaAnnotationService.scrobble(subsonic.getParams(), id);
}
private OkHttpClient getOkHttpClient() {
CacheUtil cacheUtil = new CacheUtil(0, 60 * 60 * 24 * 30);
return new OkHttpClient.Builder()
.callTimeout(2, TimeUnit.MINUTES)
.connectTimeout(20, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.addInterceptor(getHttpLoggingInterceptor())
.addInterceptor(cacheUtil.offlineInterceptor)
.cache(getCache())
.build();
}
private HttpLoggingInterceptor getHttpLoggingInterceptor() {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
return loggingInterceptor;
}
private Cache getCache() {
int cacheSize = 10 * 1024 * 1024;
return new Cache(App.getContext().getCacheDir(), cacheSize);
}
}

View file

@ -3,6 +3,7 @@ package com.cappielloantonio.play.subsonic.api.medialibraryscanning;
import android.util.Log;
import com.cappielloantonio.play.App;
import com.cappielloantonio.play.subsonic.RetrofitClient;
import com.cappielloantonio.play.subsonic.Subsonic;
import com.cappielloantonio.play.subsonic.base.ApiResponse;
import com.cappielloantonio.play.subsonic.utils.CacheUtil;
@ -25,14 +26,7 @@ public class MediaLibraryScanningClient {
public MediaLibraryScanningClient(Subsonic subsonic) {
this.subsonic = subsonic;
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(subsonic.getUrl())
.addConverterFactory(GsonConverterFactory.create(new GsonBuilder().setLenient().create()))
.client(getOkHttpClient())
.build();
this.mediaLibraryScanningService = retrofit.create(MediaLibraryScanningService.class);
this.mediaLibraryScanningService = new RetrofitClient(subsonic).getRetrofit().create(MediaLibraryScanningService.class);
}
public Call<ApiResponse> startScan() {
@ -44,30 +38,4 @@ public class MediaLibraryScanningClient {
Log.d(TAG, "getScanStatus()");
return mediaLibraryScanningService.getScanStatus(subsonic.getParams());
}
private OkHttpClient getOkHttpClient() {
CacheUtil cacheUtil = new CacheUtil(0, 60 * 60 * 24 * 30);
return new OkHttpClient.Builder()
.callTimeout(2, TimeUnit.MINUTES)
.connectTimeout(20, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.addInterceptor(getHttpLoggingInterceptor())
.addInterceptor(cacheUtil.offlineInterceptor)
.cache(getCache())
.build();
}
private HttpLoggingInterceptor getHttpLoggingInterceptor() {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
return loggingInterceptor;
}
private Cache getCache() {
int cacheSize = 10 * 1024 * 1024;
return new Cache(App.getContext().getCacheDir(), cacheSize);
}
}

View file

@ -3,6 +3,7 @@ package com.cappielloantonio.play.subsonic.api.mediaretrieval;
import android.util.Log;
import com.cappielloantonio.play.App;
import com.cappielloantonio.play.subsonic.RetrofitClient;
import com.cappielloantonio.play.subsonic.Subsonic;
import com.cappielloantonio.play.subsonic.base.ApiResponse;
import com.cappielloantonio.play.subsonic.utils.CacheUtil;
@ -25,14 +26,7 @@ public class MediaRetrievalClient {
public MediaRetrievalClient(Subsonic subsonic) {
this.subsonic = subsonic;
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(subsonic.getUrl())
.addConverterFactory(GsonConverterFactory.create(new GsonBuilder().setLenient().create()))
.client(getOkHttpClient())
.build();
this.mediaRetrievalService = retrofit.create(MediaRetrievalService.class);
this.mediaRetrievalService = new RetrofitClient(subsonic).getRetrofit().create(MediaRetrievalService.class);
}
public Call<ApiResponse> stream(String id, Integer maxBitRate, String format) {
@ -49,31 +43,4 @@ public class MediaRetrievalClient {
Log.d(TAG, "getLyrics()");
return mediaRetrievalService.getLyrics(subsonic.getParams(), artist, title);
}
private OkHttpClient getOkHttpClient() {
CacheUtil cacheUtil = new CacheUtil(0, 60 * 60 * 24 * 30);
return new OkHttpClient.Builder()
.callTimeout(2, TimeUnit.MINUTES)
.connectTimeout(20, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.addInterceptor(getHttpLoggingInterceptor())
.addInterceptor(cacheUtil.offlineInterceptor)
.addNetworkInterceptor(cacheUtil.onlineInterceptor)
.cache(getCache())
.build();
}
private HttpLoggingInterceptor getHttpLoggingInterceptor() {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
return loggingInterceptor;
}
private Cache getCache() {
int cacheSize = 10 * 1024 * 1024;
return new Cache(App.getContext().getCacheDir(), cacheSize);
}
}

View file

@ -3,6 +3,7 @@ package com.cappielloantonio.play.subsonic.api.playlist;
import android.util.Log;
import com.cappielloantonio.play.App;
import com.cappielloantonio.play.subsonic.RetrofitClient;
import com.cappielloantonio.play.subsonic.Subsonic;
import com.cappielloantonio.play.subsonic.base.ApiResponse;
import com.cappielloantonio.play.subsonic.utils.CacheUtil;
@ -26,14 +27,7 @@ public class PlaylistClient {
public PlaylistClient(Subsonic subsonic) {
this.subsonic = subsonic;
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(subsonic.getUrl())
.addConverterFactory(GsonConverterFactory.create(new GsonBuilder().setLenient().create()))
.client(getOkHttpClient())
.build();
this.playlistService = retrofit.create(PlaylistService.class);
this.playlistService = new RetrofitClient(subsonic).getRetrofit().create(PlaylistService.class);
}
public Call<ApiResponse> getPlaylists() {
@ -60,30 +54,4 @@ public class PlaylistClient {
Log.d(TAG, "deletePlaylist()");
return playlistService.deletePlaylist(subsonic.getParams(), id);
}
private OkHttpClient getOkHttpClient() {
CacheUtil cacheUtil = new CacheUtil(0, 60 * 60 * 24 * 30);
return new OkHttpClient.Builder()
.callTimeout(2, TimeUnit.MINUTES)
.connectTimeout(20, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.addInterceptor(getHttpLoggingInterceptor())
.addInterceptor(cacheUtil.offlineInterceptor)
.cache(getCache())
.build();
}
private HttpLoggingInterceptor getHttpLoggingInterceptor() {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
return loggingInterceptor;
}
private Cache getCache() {
int cacheSize = 10 * 1024 * 1024;
return new Cache(App.getContext().getCacheDir(), cacheSize);
}
}

View file

@ -3,6 +3,7 @@ package com.cappielloantonio.play.subsonic.api.podcast;
import android.util.Log;
import com.cappielloantonio.play.App;
import com.cappielloantonio.play.subsonic.RetrofitClient;
import com.cappielloantonio.play.subsonic.Subsonic;
import com.cappielloantonio.play.subsonic.base.ApiResponse;
import com.cappielloantonio.play.subsonic.utils.CacheUtil;
@ -25,14 +26,7 @@ public class PodcastClient {
public PodcastClient(Subsonic subsonic) {
this.subsonic = subsonic;
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(subsonic.getUrl())
.addConverterFactory(GsonConverterFactory.create(new GsonBuilder().setLenient().create()))
.client(getOkHttpClient())
.build();
this.podcastService = retrofit.create(PodcastService.class);
this.podcastService = new RetrofitClient(subsonic).getRetrofit().create(PodcastService.class);
}
public Call<ApiResponse> getPodcasts(boolean includeEpisodes, String channelId) {
@ -49,31 +43,4 @@ public class PodcastClient {
Log.d(TAG, "refreshPodcasts()");
return podcastService.refreshPodcasts(subsonic.getParams());
}
private OkHttpClient getOkHttpClient() {
CacheUtil cacheUtil = new CacheUtil(60, 60 * 60 * 24 * 30);
return new OkHttpClient.Builder()
.callTimeout(2, TimeUnit.MINUTES)
.connectTimeout(20, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.addInterceptor(getHttpLoggingInterceptor())
.addInterceptor(cacheUtil.offlineInterceptor)
.addNetworkInterceptor(cacheUtil.onlineInterceptor)
.cache(getCache())
.build();
}
private HttpLoggingInterceptor getHttpLoggingInterceptor() {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
return loggingInterceptor;
}
private Cache getCache() {
int cacheSize = 10 * 1024 * 1024;
return new Cache(App.getContext().getCacheDir(), cacheSize);
}
}

View file

@ -3,6 +3,7 @@ package com.cappielloantonio.play.subsonic.api.searching;
import android.util.Log;
import com.cappielloantonio.play.App;
import com.cappielloantonio.play.subsonic.RetrofitClient;
import com.cappielloantonio.play.subsonic.Subsonic;
import com.cappielloantonio.play.subsonic.base.ApiResponse;
import com.cappielloantonio.play.subsonic.utils.CacheUtil;
@ -25,14 +26,7 @@ public class SearchingClient {
public SearchingClient(Subsonic subsonic) {
this.subsonic = subsonic;
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(subsonic.getUrl())
.addConverterFactory(GsonConverterFactory.create(new GsonBuilder().setLenient().create()))
.client(getOkHttpClient())
.build();
this.searchingService = retrofit.create(SearchingService.class);
this.searchingService = new RetrofitClient(subsonic).getRetrofit().create(SearchingService.class);
}
public Call<ApiResponse> search2(String query, int songCount, int albumCount, int artistCount) {
@ -44,31 +38,4 @@ public class SearchingClient {
Log.d(TAG, "search3()");
return searchingService.search3(subsonic.getParams(), query, songCount, albumCount, artistCount);
}
private OkHttpClient getOkHttpClient() {
CacheUtil cacheUtil = new CacheUtil(60, 60 * 60 * 24 * 30);
return new OkHttpClient.Builder()
.callTimeout(2, TimeUnit.MINUTES)
.connectTimeout(20, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.addInterceptor(getHttpLoggingInterceptor())
.addInterceptor(cacheUtil.offlineInterceptor)
.addNetworkInterceptor(cacheUtil.onlineInterceptor)
.cache(getCache())
.build();
}
private HttpLoggingInterceptor getHttpLoggingInterceptor() {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
return loggingInterceptor;
}
private Cache getCache() {
int cacheSize = 10 * 1024 * 1024;
return new Cache(App.getContext().getCacheDir(), cacheSize);
}
}

View file

@ -3,6 +3,7 @@ package com.cappielloantonio.play.subsonic.api.system;
import android.util.Log;
import com.cappielloantonio.play.App;
import com.cappielloantonio.play.subsonic.RetrofitClient;
import com.cappielloantonio.play.subsonic.Subsonic;
import com.cappielloantonio.play.subsonic.base.ApiResponse;
import com.cappielloantonio.play.subsonic.utils.CacheUtil;
@ -25,14 +26,7 @@ public class SystemClient {
public SystemClient(Subsonic subsonic) {
this.subsonic = subsonic;
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(subsonic.getUrl())
.addConverterFactory(GsonConverterFactory.create(new GsonBuilder().setLenient().create()))
.client(getOkHttpClient())
.build();
this.systemService = retrofit.create(SystemService.class);
this.systemService = new RetrofitClient(subsonic).getRetrofit().create(SystemService.class);
}
public Call<ApiResponse> ping() {
@ -44,31 +38,4 @@ public class SystemClient {
Log.d(TAG, "getLicense()");
return systemService.getLicense(subsonic.getParams());
}
private OkHttpClient getOkHttpClient() {
CacheUtil cacheUtil = new CacheUtil(0, 60 * 60 * 24 * 30);
return new OkHttpClient.Builder()
.callTimeout(2, TimeUnit.MINUTES)
.connectTimeout(20, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.addInterceptor(getHttpLoggingInterceptor())
.addInterceptor(cacheUtil.offlineInterceptor)
.addNetworkInterceptor(cacheUtil.onlineInterceptor)
.cache(getCache())
.build();
}
private HttpLoggingInterceptor getHttpLoggingInterceptor() {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
return loggingInterceptor;
}
private Cache getCache() {
int cacheSize = 10 * 1024 * 1024;
return new Cache(App.getContext().getCacheDir(), cacheSize);
}
}