Created
July 19, 2018 15:09
-
-
Save CyxouD/12fdc82f2179fd037e9149dbc781d70f to your computer and use it in GitHub Desktop.
Interface, which declare PUT, GET, POST etc methods, which Retrofit (Http network client) will use to create network requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface RetrofitService { | |
companion object { | |
fun create(): DevGetVoilaService { | |
val clientBuilder = OkHttpClient.Builder().apply { | |
val loggingInterceptor = HttpLoggingInterceptor() | |
loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY | |
addInterceptor(loggingInterceptor) | |
addInterceptor(AuthenticationInterceptor()) | |
addInterceptor(AuditInterceptor()) | |
} | |
val retrofit = Retrofit.Builder() | |
.addCallAdapterFactory(RxJava2CallAdapterFactory.create()) | |
.client(clientBuilder.build()) | |
.baseUrl(BuildConfig.API_BASE_URL) | |
.addConverterFactory(GsonConverterFactory.create()) | |
.build() | |
return retrofit.create(DevGetVoilaService::class.java) | |
} | |
private class AuthenticationInterceptor : Interceptor { | |
override fun intercept(chain: Interceptor.Chain): Response { | |
val originalRequest = chain.request() | |
var response: Response = chain.proceed(originalRequest) | |
LocalDataSource.getInstance().getToken() | |
.subscribe( | |
{ token -> | |
val newRequest = originalRequest.newBuilder() | |
.header(Header.TOKEN.headerName, token.toNullable()) | |
.build() | |
response = chain.proceed(newRequest) | |
}, | |
{ response = response } //if error, then there is no token, so use the original response | |
) | |
return response | |
} | |
} | |
private class AuditInterceptor : Interceptor { | |
override fun intercept(chain: Interceptor.Chain): Response { | |
val originalRequest = chain.request() | |
val newRequest = originalRequest.newBuilder() | |
.header(Header.APP_PLATFORM.headerName, "android") | |
.header(Header.APP_VERSION.headerName, BuildConfig.VERSION_CODE.toString()) | |
.build() | |
return chain.proceed(newRequest) | |
} | |
} | |
} | |
//... some other methods | |
@FormUrlEncoded | |
@POST("authenticate/") | |
fun authenticate(@Field("username") username: String, | |
@Field("password") password: String): | |
Flowable<AuthenticateResult> | |
@POST("sessions/conversation/v2/") | |
fun getConversationId(): Flowable<ConversationId> | |
@POST("layer/identity/") | |
fun getIdentityToken(@Body identityTokenBody: IdentityTokenBody): | |
Flowable<IdentityToken> | |
@POST("flight/task/select/{flightId}") | |
fun selectFlightWidget(@Path("flightId") flightId: Int): Completable | |
@POST("hotel/task/select/{hotelId}") | |
fun selectHotelWidget(@Path("hotelId") hotelId: Int): Completable | |
@POST("car/task/select/{carId}") | |
fun selectCarWidget(@Path("carId") carId: Int): Completable | |
@POST("train/task/select/{trainId}") | |
fun selectTrainWidget(@Path("trainId") trainId: Int): Completable | |
@POST("flight/task/select") | |
fun selectFlightWidgetWithCF(@Body selectCFBody: SelectCFBody): Completable | |
@POST("hotel/task/select") | |
fun selectHotelWidgetWithCF(@Body selectCFBody: SelectCFBody): Completable | |
@POST("car/task/select") | |
fun selectCarWidgetWithCF(@Body selectCFBody: SelectCFBody): Completable | |
//... some other methods | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment