Я создаю клиента для службы REST. Служба имеет службу входа в систему, которая генерирует токен.
Служба входа имеет следующий формат:
$.post('http://xxx.xxx.xxx.xxx/?json=true',{machineID: "fMUVxYdG1X3hWb7GNkTd", mail: "user@user.com", pass: "123", function: "dash"},function(d){
console.log(d.$user)
})
Со следующим ответом. auth_token - это apiKey в этом сервисе.
{"ok":true,"auth_token":"078c302cecc90206fec20bc8306a93ba"}
Итак, в моем приложении для Android я создаю интерфейс типа
public interface RestService {
@POST("/")
Call<LoginResponse> login(@Query("json") boolean json, @Body Login login);
@GET("/{apiKey}/monitor/{groupKey}")
Call<List<Monitor>> getMonitors(@Path("apiKey") String apiKey, @Path("groupKey") String groupKey);
@GET("/{apiKey}/monitor/{groupKey}/{monitorId}")
Call<Monitor> getMonitor(@Path("apiKey") String apiKey, @Path("groupKey") String groupKey, @Path("monitorId") String monitorId);
@GET("/{apiKey}/videos/{groupKey}/{monitorId}")
Call<VideoObject> getMonitorVideos(@Path("apiKey") String apiKey, @Path("groupKey") String groupKey, @Path("monitorId") String monitorId);
@GET("/{apiKey}/videos/{groupKey}")
Call<VideoObject> getVideos(@Path("apiKey") String apiKey, @Path("groupKey") String groupKey, @QueryMap Map<String, String> options);
@GET("/{apiKey}/control/{groupKey}/{monitorId}/{action}")
Call<ResponseBody> control(@Path("apiKey") String apiKey, @Path("groupKey") String groupKey, @Path("monitorId") String monitorId, @Path("action") String action);
}
У меня есть другой класс, который инициирует службу.
public void init(String host, int port, boolean ssl) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
baseUrl = String.format(Locale.getDefault(), "%s://%s:%d", (ssl ? HTTPS : HTTP), host, port);
okHttpClient = new OkHttpClient().newBuilder().addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request originalRequest = chain.request();
return chain.proceed(originalRequest);
}
})
.addInterceptor(logging)
.build();
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(JacksonConverterFactory.create(mapper))
.client(okHttpClient)
.build();
restService = retrofit.create(RestService.class);
this.host = host;
this.port = port;
this.ssl = ssl;
}
Вот сервис входа в систему и другой сервис.
public void login(final Login login, final Callback<LoginResponse> callback) {
Call<LoginResponse> call = restService.login(true, login);
call.enqueue(callback);
}
public void getMonitors(Callback<List<Monitor>> callback) {
Call<List<Monitor>> call = restService.getMonitors(apiKey, groupKey);
call.enqueue(callback);
}
Однако я хочу иметь возможность вызывать службу входа в систему для каждой из других служб, и после успешного ответа я позвоню в настоящую службу.
В любом случае, я могу сделать это с помощью дооснащения?
Ценю любые отзывы.