package org.joinmastodon.android.api; import android.net.Uri; import com.google.gson.reflect.TypeToken; import org.joinmastodon.android.api.session.AccountSession; import org.joinmastodon.android.api.session.AccountSessionManager; import org.joinmastodon.android.model.BaseModel; import org.joinmastodon.android.model.Token; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; import androidx.annotation.CallSuper; import me.grishka.appkit.api.APIRequest; import me.grishka.appkit.api.Callback; import okhttp3.Call; import okhttp3.RequestBody; public abstract class MastodonAPIRequest extends APIRequest{ private String domain; private AccountSession account; private String path; private String method; private Object requestBody; private Map queryParams; Class respClass; TypeToken respTypeToken; Call okhttpCall; Token token; public MastodonAPIRequest(HttpMethod method, String path, Class respClass){ this.path=path; this.method=method.toString(); this.respClass=respClass; } public MastodonAPIRequest(HttpMethod method, String path, TypeToken respTypeToken){ this.path=path; this.method=method.toString(); this.respTypeToken=respTypeToken; } @Override public synchronized void cancel(){ if(okhttpCall!=null){ okhttpCall.cancel(); } } @Override public APIRequest exec(){ throw new UnsupportedOperationException("Use exec(accountID) or execNoAuth(domain)"); } public MastodonAPIRequest exec(String accountID){ account=AccountSessionManager.getInstance().getAccount(accountID); domain=account.domain; account.getApiController().submitRequest(this); return this; } public MastodonAPIRequest execNoAuth(String domain){ this.domain=domain; AccountSessionManager.getInstance().getUnauthenticatedApiController().submitRequest(this); return this; } public MastodonAPIRequest exec(String domain, Token token){ this.domain=domain; this.token=token; AccountSessionManager.getInstance().getUnauthenticatedApiController().submitRequest(this); return this; } protected void setRequestBody(Object body){ requestBody=body; } protected void addQueryParameter(String key, String value){ if(queryParams==null) queryParams=new HashMap<>(); queryParams.put(key, value); } protected String getPathPrefix(){ return "/api/v1"; } public Uri getURL(){ Uri.Builder builder=new Uri.Builder() .scheme("https") .authority(domain) .path(getPathPrefix()+path); if(queryParams!=null){ for(Map.Entry param:queryParams.entrySet()){ builder.appendQueryParameter(param.getKey(), param.getValue()); } } return builder.build(); } public String getMethod(){ return method; } public RequestBody getRequestBody(){ return requestBody==null ? null : new JsonObjectRequestBody(requestBody); } @Override public MastodonAPIRequest setCallback(Callback callback){ super.setCallback(callback); return this; } @CallSuper public void validateAndPostprocessResponse(T respObj) throws IOException{ if(respObj instanceof BaseModel){ ((BaseModel) respObj).postprocess(); }else if(respObj instanceof List){ for(Object item : ((List) respObj)){ if(item instanceof BaseModel) ((BaseModel) item).postprocess(); } } } void onError(String msg){ invokeErrorCallback(new MastodonErrorResponse(msg)); } void onSuccess(T resp){ invokeSuccessCallback(resp); } public enum HttpMethod{ GET, POST, PUT, DELETE } }