我当前的Android应用程序使用改造和RxJava访问大量的后端RESTful API。
我的用户登录并获取访问令牌后,所有后续API调用都会具有HTTP授权HEADER。
由于我的改装服务类是单例,因此我已将授权标头作为方法参数传递给每个 我的API调用如下:-
String HEADER_CONTENT_TYPE_JSON = "Content-Type: application/json";
String HEADER_AUTHORIZATION = "authorization";
@Headers(HEADER_CONTENT_TYPE_JSON)
@POST("my/api/endpoint")
Single<Response<MyResponse>> myGet(@Header(HEADER_AUTHORIZATION) @NonNull final String authenticationToken, @Body final MyRequest request);
我的服务基类类似于:-
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.logging.HttpLoggingInterceptor;
public abstract class BaseService {
private static final long OK_HTTP_CLIENT_TIMEOUT = 120;
private static final HttpLoggingInterceptor HTTP_LOGGING_INTERCEPTOR = new HttpLoggingInterceptor();
private static final Interceptor HTTP_HEADERS = constructHeaderInterceptor();
private static final String HTTP_ORIGIN_HEADER = "Origin";
private static final String HTTP_ORIGIN_VALUE = "my-origin";
private static Interceptor constructHeaderInterceptor() {
return chain -> {
final Request request = chain.request();
final Request newRequest = request.newBuilder().addHeader(HTTP_ORIGIN_HEADER, HTTP_ORIGIN_VALUE).build();
return chain.proceed(newRequest);
};
}
static {
HTTP_LOGGING_INTERCEPTOR.setLevel(HttpLoggingInterceptor.Level.HEADERS);
}
final OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(OK_HTTP_CLIENT_TIMEOUT, TimeUnit.SECONDS)
.readTimeout(OK_HTTP_CLIENT_TIMEOUT, TimeUnit.SECONDS)
.followRedirects(true)
.addInterceptor(HTTP_HEADERS)
.addInterceptor(HTTP_LOGGING_INTERCEPTOR)
.build();
}
我的服务类类似于:-
import io.reactivex.Single;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.jackson.JacksonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;
public final class Service extends BaseService implements Api {
private static final String BASE_URL = BuildConfig.SDK_BASE_URL;
private static final Api INSTANCE = new Service();
/**
* @return
*/
public static Api instance() {
return INSTANCE;
}
private final Api service;
/**
*
*/
private Service() {
final Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(JacksonConverterFactory.create())
.addConverterFactory(ScalarsConverterFactory.create())
.build();
service = retrofit.create(Api.class);
}
@Override
public Single<Response<MyResponse>> myGet(@NonNull final String authenticationToken, final MyRequest request) {
return service.query(authenticationToken, request);
}
}
我想要实现的是通过OkHttpClient addInterceptor()调用添加授权标头,并且仍然保持我的服务单例。
是否可以通过授权访问代码“懒惰地”设置OkHttpClient addInterceptor()?仍然保持单身?
事件的顺序将是
1). User logs in to receive Access Token (and Refresh Token)
2). Manufacture the Retrofit service for API calls with an OkHttpClient addInterceptor() setting the HTTP Authorisation Header.
3). Make all API calls until ACCESS Token expires
4). Obtain new Access Token using refresh token -> repeat Step 2).