M - Type of the service manager used by the ServiceApi implementation.public class ServiceApi<M extends ServiceManager> extends Object
// Api interface that is visible across whole application.
public interface Api {
String END_POINT = "http://messenger.com";
String VERSION = "1";
String URL = "/api/v" + VERSION;
void signIn(@NonNull SignInRequest request);
void forgotPassword(@NonNull ForgotPasswordRequest request);
}
// Retrofit services definition interface.
interface Services {
@POST(Api.URL + "/signIn")
void signIn(
@Body @NonNull SignInRequest request,
@NonNull Callback<SignInResponse> callback
);
@POST(Api.URL + "/forgotPassword")
void forgotPassword(
@Body @NonNull ForgotPasswordRequest request,
@NonNull Callback<ForgotPasswordResponse> callback
);
}
// Api implementation provided by ApiProvider.
final class ApiImpl implements Api {
ApiImpl(@NonNull Bus bus) {
super(new ServiceManager(), bus);
}
@Override
public void signIn(@NonNull SignInRequest request) {
services(Services.class).signIn(
request,
new ServiceCallback<SignInResponse>(1, mBus)
);
}
@Override
public void forgotPassword(@NonNull ForgotPasswordRequest request) {
services(Services.class).forgotPassword(
request,
new ServiceCallback<ForgotPasswordResponse>(2, mBus)
);
}
}
// Provider that provides instance of ApiImpl for application clients.
public final class ApiProvider extends ServiceApiProvider<Api> {
public ApiProvider(@NonNull Bus bus) {
super(bus);
}
@NonNull
@Override
protected Api onCreateApi(@NonNull Bus bus) {
return new ApiImpl(bus);
}
}
ServiceApiProvider| Modifier and Type | Field and Description |
|---|---|
protected M |
mManager
Instance of
ServiceManager used to configure and access services provided by this API. |
| Modifier | Constructor and Description |
|---|---|
protected |
ServiceApi(M manager)
Creates a new instance of ServiceApi with the specified service manager.
|
| Modifier and Type | Method and Description |
|---|---|
protected <S> S |
services(Class<S> servicesInterface)
Returns an instance of services PROXY for the specified servicesInterface.
|
protected <S> ServiceManager.ServicesConfiguration<S> |
servicesConfiguration(Class<S> servicesInterface)
Returns the configuration for services with the specified servicesInterface.
|
protected final M extends ServiceManager mManager
ServiceManager used to configure and access services provided by this API.protected ServiceApi(M manager)
manager - The desired service manager used to configure and access services.protected final <S> S services(Class<S> servicesInterface)
S - Type of the requested services.servicesInterface - The services interface for which to the associated PROXY.ServiceManager.services(Class)protected final <S> ServiceManager.ServicesConfiguration<S> servicesConfiguration(Class<S> servicesInterface)
Returned configuration object can be used to change current configuration of a specific services PROXY instance.
S - Type of the services of the requested services configuration.servicesInterface - The services interface for which to return theirs configuration.ServiceManager.servicesConfiguration(Class)