Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.util.Log;
- import com.google.gson.Gson;
- import com.google.gson.GsonBuilder;
- import com.google.gson.JsonObject;
- import com.jakewharton.retrofit.Ok3Client;
- import okhttp3.HttpUrl;
- import okhttp3.Interceptor;
- import okhttp3.JavaNetCookieJar;
- import okhttp3.OkHttpClient;
- import okhttp3.Request;
- import okhttp3.Response;
- import okhttp3.logging.HttpLoggingInterceptor;
- import java.io.IOException;
- import java.net.CookieHandler;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.concurrent.TimeUnit;
- import retrofit2.Call;
- import retrofit2.Retrofit ;
- import retrofit2.converter.gson.GsonConverterFactory;
- import retrofit.RestAdapter;
- import retrofit.Callback;
- import retrofit.RequestInterceptor;
- import retrofit.converter.GsonConverter;
- import retrofit.http.Body;
- import retrofit.http.DELETE;
- import retrofit.http.GET;
- import retrofit.http.HEAD;
- import retrofit.http.Headers;
- import retrofit.http.POST;
- import retrofit.http.PUT;
- import retrofit.http.Path;
- import retrofit.http.Query;
- import retrofit.mime.MultipartTypedOutput;
- import rx.Observable;
- public class ApiManager {
- private static final String TAG = "API MANAGER";
- private static final String API_URL = BuildConfig.API_URL;
- private static RequestInterceptor requestInterceptor = new RequestInterceptor() {
- @Override
- public void intercept(RequestInterceptor.RequestFacade request) {
- SessionManager sessionManager = new SessionManager(ContextHandler.getContext());
- HashMap session = sessionManager.getUserDetails();
- Object session_id = session.get("session_id");
- Object token = session.get("token");
- if (session_id != null && token != null) {
- request.addHeader("Cookie", "session_id=" + session_id + ";");
- request.addHeader("Cookie", "token=" + token + ";");
- Log.i("INTERCEPT", "Sent Cookies");
- }
- request.addHeader("Accept", "application/json");
- }
- };
- public static OkHttpClient getClient() {
- // init okhttp 3 logger
- HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
- logging.setLevel(HttpLoggingInterceptor.Level.BODY);
- JavaNetCookieJar jncj = new JavaNetCookieJar(CookieHandler.getDefault());
- OkHttpClient client = new OkHttpClient();
- client.newBuilder()
- .addInterceptor(new AddCookiesInterceptor(ContextHandler.getContext()))
- .addInterceptor(new ReceivedCookiesInterceptor(ContextHandler.getContext()))
- .addNetworkInterceptor(logging)
- .cookieJar(jncj)
- .connectTimeout(10, TimeUnit.SECONDS)
- .writeTimeout(10, TimeUnit.SECONDS)
- .readTimeout(30, TimeUnit.MINUTES);
- return client;
- }
- private static Gson gson = new GsonBuilder()
- .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
- .setLenient()
- .create();
- public static OkHttpClient getHeader() {
- HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
- interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
- OkHttpClient okClient = new OkHttpClient.Builder()
- .addInterceptor(interceptor)
- .addInterceptor(new AddCookiesInterceptor(ContextHandler.getContext()))
- .addInterceptor(new ReceivedCookiesInterceptor(ContextHandler.getContext()))
- .cookieJar(cookieJar)
- .connectTimeout(10, TimeUnit.SECONDS)
- .writeTimeout(10, TimeUnit.SECONDS)
- .readTimeout(30, TimeUnit.MINUTES)
- .addNetworkInterceptor(
- new Interceptor() {
- @Override
- public Response intercept(Interceptor.Chain chain) throws IOException {
- Request request = null;
- Log.d("--Authorization-- ", "authorizationValue");
- Request original = chain.request();
- // Request customization: add request headers
- Request.Builder requestBuilder = original.newBuilder();
- SessionManager sessionManager = new SessionManager(ContextHandler.getContext());
- HashMap session = sessionManager.getUserDetails();
- Object session_id = session.get("session_id");
- Object token = session.get("token");
- if (session_id != null && token != null) {
- requestBuilder.addHeader("Cookie", "session_id=" + session_id + ";");
- requestBuilder.addHeader("Cookie", "token=" + token + ";");
- Log.i("INTERCEPT", "Sent Cookies");
- }
- requestBuilder.addHeader("Accept", "application/json");
- request = requestBuilder.build();
- return chain.proceed(request);
- }
- })
- .build();
- return okClient;
- }
- private static final Retrofit REST_ADAPTER2 = new Retrofit.Builder()
- .baseUrl(API_URL) // On device
- .client(getHeader())
- .addConverterFactory(GsonConverterFactory.create(gson))
- .build();
- // Retrofit 1.9
- private static final RestAdapter REST_ADAPTER = new RestAdapter.Builder()
- .setEndpoint(API_URL) // On device
- .setRequestInterceptor(requestInterceptor)
- .setClient(new Ok3Client(getClient()))
- .setConverter(new GsonConverter(gson))
- .setLogLevel(RestAdapter.LogLevel.FULL) //log the request
- .build();
- // Retrofit 2.9.0
- private static final Retrofit REST_ADAPTER2 = new Retrofit.Builder()
- .baseUrl(API_URL) // On device
- .client(getHeader())
- .addConverterFactory(GsonConverterFactory.create(gson))
- .build();
- public interface AuthenticationInterface {
- @Headers("Content-type: application/json")
- @POST("/auth/getsession")
- void Authenticate(@Body Authentication Auth, Callback<SessionStore> response);
- @Headers("Content-type: application/json")
- @GET("/auth/logout")
- void logout(Callback<String> response);
- @Headers("Content-type: application/json")
- @GET("/auth/logout")
- String logout();
- }
- public interface JasperReportsInterface {
- /**
- *
- * @param agent_id
- * @param report_id
- */
- @retrofit2.http.Headers("Content-type: application/json")
- @retrofit2.http.GET("/agents/{agent_id}/reports/{report_id}/")
- Call<Reports> GetAgentReportView(@retrofit2.http.Path("agent_id") String agent_id, @retrofit2.http.Path("report_id") String report_id);
- /**
- *
- * @param agent_id
- * @param report_id
- */
- @retrofit2.http.Headers("Content-type: application/json")
- @retrofit2.http.GET("/agents/{agent_id}/reports/{report_id}/jobs")
- Call<Jobs> PollAgentReportData(@retrofit2.http.Path("agent_id") String agent_id, @retrofit2.http.Path("report_id") String report_id);
- /**
- *
- * @param agent_id
- * @param report_id
- * @param jsonBody
- */
- @retrofit2.http.Headers("Content-type: application/json")
- @retrofit2.http.POST("/agents/{agent_id}/reports/{report_id}/jobs")
- Call<String> PostAgentReportData(@retrofit2.http.Path("agent_id") String agent_id, @retrofit2.http.Path("report_id") String report_id, @retrofit2.http.Body JsonObject jsonBody);
- /**
- *
- * @param agent_id
- * @param report_id
- * @param jsonBody
- */
- @retrofit2.http.Headers("Content-type: application/json")
- @retrofit2.http.POST("/agents/{agent_id}/reports/{report_id}/jobs")
- Call<String> DownloadAgentReportData(@retrofit2.http.Path("agent_id") String agent_id, @retrofit2.http.Path("report_id") String report_id, @retrofit2.http.Body JsonObject jsonBody);
- }
- // Bind REST_ADAPTER to Interface
- public static final AuthenticationInterface AUTHENTICATION_INTERFACE = REST_ADAPTER.create(AuthenticationInterface.class);
- // Use this when you want to run the request.
- public static AuthenticationInterface getAuthenticationService(){ return AUTHENTICATION_INTERFACE; }
- // Bind REST_ADAPTER2 to Interface
- public static final JasperReportsInterface JASPER_REPORTS_INTERFACE = REST_ADAPTER2.create(JasperReportsInterface.class);
- // Use this when you want to run the request.
- public static JasperReportsInterface getJasperReportsService(){ return JASPER_REPORTS_INTERFACE; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement