Cache donation campaign responses
This commit is contained in:
parent
b9956950b6
commit
b5aa1a4598
|
@ -12,10 +12,12 @@ import com.google.gson.JsonParser;
|
||||||
import com.google.gson.JsonSyntaxException;
|
import com.google.gson.JsonSyntaxException;
|
||||||
|
|
||||||
import org.joinmastodon.android.BuildConfig;
|
import org.joinmastodon.android.BuildConfig;
|
||||||
|
import org.joinmastodon.android.MastodonApp;
|
||||||
import org.joinmastodon.android.api.gson.IsoInstantTypeAdapter;
|
import org.joinmastodon.android.api.gson.IsoInstantTypeAdapter;
|
||||||
import org.joinmastodon.android.api.gson.IsoLocalDateTypeAdapter;
|
import org.joinmastodon.android.api.gson.IsoLocalDateTypeAdapter;
|
||||||
import org.joinmastodon.android.api.session.AccountSession;
|
import org.joinmastodon.android.api.session.AccountSession;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
@ -29,6 +31,8 @@ import java.util.concurrent.TimeUnit;
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import me.grishka.appkit.utils.WorkerThread;
|
import me.grishka.appkit.utils.WorkerThread;
|
||||||
|
import okhttp3.Cache;
|
||||||
|
import okhttp3.CacheControl;
|
||||||
import okhttp3.Call;
|
import okhttp3.Call;
|
||||||
import okhttp3.Callback;
|
import okhttp3.Callback;
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
|
@ -49,8 +53,11 @@ public class MastodonAPIController{
|
||||||
.connectTimeout(60, TimeUnit.SECONDS)
|
.connectTimeout(60, TimeUnit.SECONDS)
|
||||||
.writeTimeout(60, TimeUnit.SECONDS)
|
.writeTimeout(60, TimeUnit.SECONDS)
|
||||||
.readTimeout(60, TimeUnit.SECONDS)
|
.readTimeout(60, TimeUnit.SECONDS)
|
||||||
|
.cache(new Cache(new File(MastodonApp.context.getCacheDir(), "http"), 10*1024*1024))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
private static final CacheControl NO_CACHE_WHATSOEVER=new CacheControl.Builder().noCache().noStore().build();
|
||||||
|
|
||||||
private AccountSession session;
|
private AccountSession session;
|
||||||
|
|
||||||
static{
|
static{
|
||||||
|
@ -80,6 +87,9 @@ public class MastodonAPIController{
|
||||||
if(token!=null)
|
if(token!=null)
|
||||||
builder.header("Authorization", "Bearer "+token);
|
builder.header("Authorization", "Bearer "+token);
|
||||||
|
|
||||||
|
if(!req.cacheable)
|
||||||
|
builder.cacheControl(NO_CACHE_WHATSOEVER);
|
||||||
|
|
||||||
if(req.headers!=null){
|
if(req.headers!=null){
|
||||||
for(Map.Entry<String, String> header:req.headers.entrySet()){
|
for(Map.Entry<String, String> header:req.headers.entrySet()){
|
||||||
builder.header(header.getKey(), header.getValue());
|
builder.header(header.getKey(), header.getValue());
|
||||||
|
|
|
@ -46,6 +46,7 @@ public abstract class MastodonAPIRequest<T> extends APIRequest<T>{
|
||||||
boolean canceled;
|
boolean canceled;
|
||||||
Map<String, String> headers;
|
Map<String, String> headers;
|
||||||
long timeout;
|
long timeout;
|
||||||
|
boolean cacheable;
|
||||||
private ProgressDialog progressDialog;
|
private ProgressDialog progressDialog;
|
||||||
protected boolean removeUnsupportedItems;
|
protected boolean removeUnsupportedItems;
|
||||||
|
|
||||||
|
@ -132,6 +133,10 @@ public abstract class MastodonAPIRequest<T> extends APIRequest<T>{
|
||||||
this.timeout=timeout;
|
this.timeout=timeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void setCacheable(){
|
||||||
|
cacheable=true;
|
||||||
|
}
|
||||||
|
|
||||||
protected String getPathPrefix(){
|
protected String getPathPrefix(){
|
||||||
return "/api/v1";
|
return "/api/v1";
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,7 @@ public class GetDonationCampaigns extends MastodonAPIRequest<DonationCampaign>{
|
||||||
super(HttpMethod.GET, null, DonationCampaign.class);
|
super(HttpMethod.GET, null, DonationCampaign.class);
|
||||||
this.locale=locale;
|
this.locale=locale;
|
||||||
this.seed=seed;
|
this.seed=seed;
|
||||||
|
setCacheable();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue