Advertisement
Guest User

Retrofit API Manager

a guest
Jun 19th, 2020
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.15 KB | None | 0 0
  1. import android.util.Log;
  2.  
  3. import com.google.gson.Gson;
  4. import com.google.gson.GsonBuilder;
  5. import com.google.gson.JsonObject;
  6.  
  7. import com.jakewharton.retrofit.Ok3Client;
  8.  
  9. import okhttp3.HttpUrl;
  10. import okhttp3.Interceptor;
  11. import okhttp3.JavaNetCookieJar;
  12. import okhttp3.OkHttpClient;
  13. import okhttp3.Request;
  14. import okhttp3.Response;
  15. import okhttp3.logging.HttpLoggingInterceptor;
  16.  
  17. import java.io.IOException;
  18. import java.net.CookieHandler;
  19. import java.util.ArrayList;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. import java.util.concurrent.TimeUnit;
  23.  
  24.  
  25. import retrofit2.Call;
  26. import retrofit2.Retrofit ;
  27. import retrofit2.converter.gson.GsonConverterFactory;
  28.  
  29. import retrofit.RestAdapter;
  30. import retrofit.Callback;
  31. import retrofit.RequestInterceptor;
  32. import retrofit.converter.GsonConverter;
  33. import retrofit.http.Body;
  34. import retrofit.http.DELETE;
  35. import retrofit.http.GET;
  36. import retrofit.http.HEAD;
  37. import retrofit.http.Headers;
  38. import retrofit.http.POST;
  39. import retrofit.http.PUT;
  40. import retrofit.http.Path;
  41. import retrofit.http.Query;
  42. import retrofit.mime.MultipartTypedOutput;
  43. import rx.Observable;
  44.  
  45. public class ApiManager {
  46.  
  47.     private static final String TAG = "API MANAGER";
  48.  
  49.     private static final String API_URL = BuildConfig.API_URL;
  50.  
  51.         private static RequestInterceptor requestInterceptor = new RequestInterceptor() {
  52.         @Override
  53.         public void intercept(RequestInterceptor.RequestFacade request) {
  54.             SessionManager sessionManager = new SessionManager(ContextHandler.getContext());
  55.             HashMap session = sessionManager.getUserDetails();
  56.             Object session_id = session.get("session_id");
  57.             Object token = session.get("token");
  58.  
  59.             if (session_id != null && token != null) {
  60.                 request.addHeader("Cookie", "session_id=" + session_id + ";");
  61.                 request.addHeader("Cookie", "token=" + token + ";");
  62.                 Log.i("INTERCEPT", "Sent Cookies");
  63.             }
  64.             request.addHeader("Accept", "application/json");
  65.         }
  66.     };
  67.  
  68.     public static OkHttpClient getClient() {
  69.  
  70.         // init okhttp 3 logger
  71.         HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
  72.         logging.setLevel(HttpLoggingInterceptor.Level.BODY);
  73.  
  74.         JavaNetCookieJar jncj = new JavaNetCookieJar(CookieHandler.getDefault());
  75.  
  76.  
  77.         OkHttpClient client = new OkHttpClient();
  78.  
  79.         client.newBuilder()
  80.                 .addInterceptor(new AddCookiesInterceptor(ContextHandler.getContext()))
  81.                 .addInterceptor(new ReceivedCookiesInterceptor(ContextHandler.getContext()))
  82.                 .addNetworkInterceptor(logging)
  83.                 .cookieJar(jncj)
  84.                 .connectTimeout(10, TimeUnit.SECONDS)
  85.                 .writeTimeout(10, TimeUnit.SECONDS)
  86.                 .readTimeout(30, TimeUnit.MINUTES);
  87.  
  88.         return client;
  89.     }
  90.  
  91.     private static Gson gson = new GsonBuilder()
  92.         .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
  93.         .setLenient()
  94.         .create();
  95.  
  96.     public static OkHttpClient getHeader() {
  97.         HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
  98.         interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
  99.         OkHttpClient okClient = new OkHttpClient.Builder()
  100.                 .addInterceptor(interceptor)
  101.                 .addInterceptor(new AddCookiesInterceptor(ContextHandler.getContext()))
  102.                 .addInterceptor(new ReceivedCookiesInterceptor(ContextHandler.getContext()))
  103.                 .cookieJar(cookieJar)
  104.                 .connectTimeout(10, TimeUnit.SECONDS)
  105.                 .writeTimeout(10, TimeUnit.SECONDS)
  106.                 .readTimeout(30, TimeUnit.MINUTES)
  107.                 .addNetworkInterceptor(
  108.                         new Interceptor() {
  109.                             @Override
  110.                             public Response intercept(Interceptor.Chain chain) throws IOException {
  111.                                 Request request = null;
  112.                                     Log.d("--Authorization-- ", "authorizationValue");
  113.  
  114.                                 Request original = chain.request();
  115.                                 // Request customization: add request headers
  116.                                 Request.Builder requestBuilder = original.newBuilder();
  117.  
  118.  
  119.                                 SessionManager sessionManager = new SessionManager(ContextHandler.getContext());
  120.  
  121.                                 HashMap session = sessionManager.getUserDetails();
  122.                                 Object session_id = session.get("session_id");
  123.                                 Object token = session.get("token");
  124.  
  125.                                 if (session_id != null && token != null) {
  126.                                     requestBuilder.addHeader("Cookie", "session_id=" + session_id + ";");
  127.                                     requestBuilder.addHeader("Cookie", "token=" + token + ";");
  128.                                     Log.i("INTERCEPT", "Sent Cookies");
  129.                                 }
  130.                                 requestBuilder.addHeader("Accept", "application/json");
  131.  
  132.                                     request = requestBuilder.build();
  133.  
  134.                                 return chain.proceed(request);
  135.                             }
  136.                         })
  137.                 .build();
  138.         return okClient;
  139.  
  140.     }
  141.  
  142.     private static final Retrofit REST_ADAPTER2 = new Retrofit.Builder()
  143.         .baseUrl(API_URL) // On device
  144.         .client(getHeader())
  145.         .addConverterFactory(GsonConverterFactory.create(gson))
  146.         .build();
  147.  
  148.  
  149.     // Retrofit 1.9
  150.     private static final RestAdapter REST_ADAPTER = new RestAdapter.Builder()
  151.         .setEndpoint(API_URL) // On device
  152.         .setRequestInterceptor(requestInterceptor)
  153.         .setClient(new Ok3Client(getClient()))
  154.         .setConverter(new GsonConverter(gson))
  155.         .setLogLevel(RestAdapter.LogLevel.FULL) //log the request
  156.         .build();
  157.  
  158.  
  159.     // Retrofit 2.9.0
  160.     private static final Retrofit REST_ADAPTER2 = new Retrofit.Builder()
  161.         .baseUrl(API_URL) // On device
  162.         .client(getHeader())
  163.         .addConverterFactory(GsonConverterFactory.create(gson))
  164.         .build();
  165.  
  166.  
  167.     public interface AuthenticationInterface {
  168.         @Headers("Content-type: application/json")
  169.         @POST("/auth/getsession")
  170.         void Authenticate(@Body Authentication Auth, Callback<SessionStore> response);
  171.  
  172.         @Headers("Content-type: application/json")
  173.         @GET("/auth/logout")
  174.         void logout(Callback<String> response);
  175.  
  176.         @Headers("Content-type: application/json")
  177.         @GET("/auth/logout")
  178.         String logout();
  179.     }
  180.  
  181.         public interface JasperReportsInterface {
  182.  
  183.             /**
  184.             *
  185.             * @param agent_id
  186.             * @param report_id
  187.             */
  188.             @retrofit2.http.Headers("Content-type: application/json")
  189.             @retrofit2.http.GET("/agents/{agent_id}/reports/{report_id}/")
  190.             Call<Reports> GetAgentReportView(@retrofit2.http.Path("agent_id") String agent_id, @retrofit2.http.Path("report_id") String report_id);
  191.  
  192.             /**
  193.             *
  194.             * @param agent_id
  195.             * @param report_id
  196.             */
  197.             @retrofit2.http.Headers("Content-type: application/json")
  198.             @retrofit2.http.GET("/agents/{agent_id}/reports/{report_id}/jobs")
  199.             Call<Jobs> PollAgentReportData(@retrofit2.http.Path("agent_id") String agent_id, @retrofit2.http.Path("report_id") String report_id);
  200.  
  201.             /**
  202.             *
  203.             * @param agent_id
  204.             * @param report_id
  205.             * @param jsonBody
  206.             */
  207.             @retrofit2.http.Headers("Content-type: application/json")
  208.             @retrofit2.http.POST("/agents/{agent_id}/reports/{report_id}/jobs")
  209.             Call<String> PostAgentReportData(@retrofit2.http.Path("agent_id") String agent_id, @retrofit2.http.Path("report_id") String report_id, @retrofit2.http.Body JsonObject jsonBody);
  210.  
  211.             /**
  212.             *
  213.             * @param agent_id
  214.             * @param report_id
  215.             * @param jsonBody
  216.             */
  217.             @retrofit2.http.Headers("Content-type: application/json")
  218.             @retrofit2.http.POST("/agents/{agent_id}/reports/{report_id}/jobs")
  219.             Call<String> DownloadAgentReportData(@retrofit2.http.Path("agent_id") String agent_id, @retrofit2.http.Path("report_id") String report_id, @retrofit2.http.Body JsonObject jsonBody);
  220.  
  221.  
  222.  
  223.  
  224.         }
  225.  
  226.  
  227.     // Bind REST_ADAPTER to Interface
  228.     public static final AuthenticationInterface AUTHENTICATION_INTERFACE = REST_ADAPTER.create(AuthenticationInterface.class);
  229.     // Use this when you want to run the request.
  230.     public static AuthenticationInterface getAuthenticationService(){ return AUTHENTICATION_INTERFACE;  }
  231.  
  232.     // Bind REST_ADAPTER2 to Interface
  233.     public static final JasperReportsInterface JASPER_REPORTS_INTERFACE = REST_ADAPTER2.create(JasperReportsInterface.class);
  234.     // Use this when you want to run the request.
  235.     public static JasperReportsInterface getJasperReportsService(){  return JASPER_REPORTS_INTERFACE;  }
  236.  
  237.  
  238.  
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement