Added Android code
[wl-app.git] / Android / app / src / main / java / com / moiseum / wolnelektury / connection / interceptors / NewApiInterceptor.java
diff --git a/Android/app/src/main/java/com/moiseum/wolnelektury/connection/interceptors/NewApiInterceptor.java b/Android/app/src/main/java/com/moiseum/wolnelektury/connection/interceptors/NewApiInterceptor.java
new file mode 100644 (file)
index 0000000..2e874eb
--- /dev/null
@@ -0,0 +1,37 @@
+package com.moiseum.wolnelektury.connection.interceptors;
+
+import android.support.annotation.NonNull;
+
+import java.io.IOException;
+
+import okhttp3.HttpUrl;
+import okhttp3.Interceptor;
+import okhttp3.Request;
+import okhttp3.Response;
+
+/**
+ * Created by Piotr Ostrowski on 24.09.2018.
+ */
+public class NewApiInterceptor implements Interceptor {
+
+       private static final String NEW_API_HEADER = "New-Api";
+       private static final String NEW_API_PARAM = "new_api";
+
+       @Override
+       public Response intercept(@NonNull Chain chain) throws IOException {
+               if (chain.request().header(NEW_API_HEADER) != null) {
+                       HttpUrl httpUrl = chain.request()
+                                       .url()
+                                       .newBuilder()
+                                       .addQueryParameter(NEW_API_PARAM, Boolean.toString(true))
+                                       .build();
+                       Request newRequest = chain.request()
+                                       .newBuilder()
+                                       .url(httpUrl)
+                                       .build();
+                       return chain.proceed(newRequest);
+               }
+
+               return chain.proceed(chain.request());
+       }
+}