Added Android code
[wl-app.git] / Android / app / src / main / java / com / moiseum / wolnelektury / connection / interceptors / NewApiInterceptor.java
1 package com.moiseum.wolnelektury.connection.interceptors;
2
3 import android.support.annotation.NonNull;
4
5 import java.io.IOException;
6
7 import okhttp3.HttpUrl;
8 import okhttp3.Interceptor;
9 import okhttp3.Request;
10 import okhttp3.Response;
11
12 /**
13  * Created by Piotr Ostrowski on 24.09.2018.
14  */
15 public class NewApiInterceptor implements Interceptor {
16
17         private static final String NEW_API_HEADER = "New-Api";
18         private static final String NEW_API_PARAM = "new_api";
19
20         @Override
21         public Response intercept(@NonNull Chain chain) throws IOException {
22                 if (chain.request().header(NEW_API_HEADER) != null) {
23                         HttpUrl httpUrl = chain.request()
24                                         .url()
25                                         .newBuilder()
26                                         .addQueryParameter(NEW_API_PARAM, Boolean.toString(true))
27                                         .build();
28                         Request newRequest = chain.request()
29                                         .newBuilder()
30                                         .url(httpUrl)
31                                         .build();
32                         return chain.proceed(newRequest);
33                 }
34
35                 return chain.proceed(chain.request());
36         }
37 }