Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 8.64 KB | None | 0 0
  1. package com.omgapps.gazetki.rest
  2.  
  3. import com.google.android.gms.common.api.Api
  4. import com.google.gson.Gson
  5. import com.omgapps.gazetki.App
  6. import com.omgapps.gazetki.Cfg
  7. import com.omgapps.gazetki.rest.models.LeafletResponse
  8. import com.omgapps.gazetki.rest.models.ShopResponse
  9. import com.omgapps.gazetki.views.pages.homeFragment.HomeFragment
  10. import com.omgapps.gazetki.views.splash.DownloadBulletins
  11. import com.omgapps.gazetki.views.splash.DownloadShops
  12. import dagger.Component
  13. import dagger.Module
  14. import dagger.Provides
  15. import okhttp3.*
  16. import retrofit2.Call
  17. import retrofit2.Retrofit
  18. import retrofit2.converter.gson.GsonConverterFactory
  19. import retrofit2.http.GET
  20. import java.util.concurrent.TimeUnit
  21. import javax.inject.Inject
  22. import javax.inject.Singleton
  23.  
  24. const val BASE_URL = "http://api.admin.appsoup.io/"
  25.  
  26. interface RestInterface  {
  27.     @GET(Cfg.API.LEAFLETS)
  28.     fun getLeaflets(): Call<LeafletResponse>
  29.  
  30.     @GET(Cfg.API.SHOPS)
  31.     fun getShops(): Call<ShopResponse>
  32. }
  33.  
  34. @Module
  35. class RestModule {
  36.  
  37.     @Provides
  38.     fun provideAppSoupInterceptor() = AppSoupInterceptor()
  39.  
  40.     @Provides
  41.     fun provideGson() = Gson()
  42.  
  43.     @Provides
  44.     fun provideHttpClient(interceptor: AppSoupInterceptor) : OkHttpClient {
  45.         return OkHttpClient.Builder()
  46.                 .addInterceptor(interceptor)
  47.                 .connectTimeout(30, TimeUnit.SECONDS)
  48.                 .readTimeout(30, TimeUnit.SECONDS)
  49.                 .writeTimeout(30, TimeUnit.SECONDS)
  50.                 .build()
  51.     }
  52.  
  53.     @Provides
  54.     fun provideRetrofit(httpClient: OkHttpClient, gson: Gson) : Retrofit {
  55.         return Retrofit.Builder()
  56.                 .baseUrl(BASE_URL)
  57.                 .addConverterFactory(GsonConverterFactory.create(gson))
  58.                 .client(httpClient)
  59.                 .build()
  60.     }
  61.  
  62.     @Provides
  63.     fun provideRest(retrofit: Retrofit) : RestInterface {
  64.         return retrofit
  65.                 .create(RestInterface::class.java)
  66.     }
  67. }
  68.  
  69. @Module
  70. class RestTestModule {
  71.  
  72.     @Provides
  73.     fun provideAppSoupInterceptor() : Interceptor = FakeInterceptor()
  74.  
  75.     @Provides
  76.     fun provideGson() = Gson()
  77.  
  78.     @Provides
  79.     fun provideHttpClient(interceptor: Interceptor) : OkHttpClient {
  80.         return OkHttpClient.Builder()
  81.                 .addInterceptor(interceptor)
  82.                 .connectTimeout(30, TimeUnit.SECONDS)
  83.                 .readTimeout(30, TimeUnit.SECONDS)
  84.                 .writeTimeout(30, TimeUnit.SECONDS)
  85.                 .build()
  86.     }
  87.  
  88.     @Provides
  89.     fun provideRetrofit(httpClient: OkHttpClient, gson: Gson) : Retrofit {
  90.         return Retrofit.Builder()
  91.                 .baseUrl(BASE_URL)
  92.                 .addConverterFactory(GsonConverterFactory.create(gson))
  93.                 .client(httpClient)
  94.                 .build()
  95.     }
  96.  
  97.     @Provides
  98.     fun provideRest(retrofit: Retrofit) : RestInterface {
  99.         return retrofit
  100.                 .create(RestInterface::class.java)
  101.     }
  102. }
  103.  
  104. @Component (modules = [RestModule::class])
  105. interface AppComponent {
  106.     @Singleton
  107.     fun rest() : RestInterface
  108.  
  109.     fun inject(fragment: HomeFragment)
  110.     fun inject(downloadBulletins: DownloadBulletins)
  111.     fun inject(downloadShops: DownloadShops)
  112. }
  113.  
  114. @Component (modules = [RestTestModule::class])
  115. interface AppTestComponent : AppComponent
  116.  
  117. class FakeInterceptor : Interceptor {
  118.     override fun intercept(chain: Interceptor.Chain?): Response {
  119.         val responseString : String = MOCK_JSON_RESPONSE_STRING
  120.         return Response.Builder()
  121.                 .code(200)
  122.                 .message("OK")
  123.                 .request(chain?.request())
  124.                 .protocol(Protocol.HTTP_1_0)
  125.                 .body(ResponseBody.create(MediaType.parse("application/json"), responseString.toByteArray()))
  126.                 .addHeader("content-type", "application/json")
  127.                 .build()
  128.     }
  129. }
  130.  
  131. val MOCK_JSON_RESPONSE_STRING = "{\n" +
  132.         "  \"status\": \"success\",\n" +
  133.         "  \"status_human_message\": null,\n" +
  134.         "  \"data\": [\n" +
  135.         "    {\n" +
  136.         "      \"id\": 182,\n" +
  137.         "      \"start_date\": 1525125600,\n" +
  138.         "      \"end_date\": 1527803999,\n" +
  139.         "      \"name\": \"Wielki finaÅ‚ 10. urodzin\",\n" +
  140.         "      \"newsletter_url\": null,\n" +
  141.         "      \"types\": \"aldi\",\n" +
  142.         "      \"promotion\": null,\n" +
  143.         "      \"cover_image_url\": \"http://api.admin.appsoup.io/uploads/8d0d2b1e/e7/e778764fe71b4c65026ae980c335e1ba/0.jpg\",\n" +
  144.         "      \"pages\": [\n" +
  145.         "        {\n" +
  146.         "          \"id\": 475,\n" +
  147.         "          \"order\": 475,\n" +
  148.         "          \"name\": \"0\",\n" +
  149.         "          \"products\": null,\n" +
  150.         "          \"recipe\": null,\n" +
  151.         "          \"image_url\": \"http://api.admin.appsoup.io/uploads/8d0d2b1e/e7/e778764fe71b4c65026ae980c335e1ba/0.jpg\",\n" +
  152.         "          \"type\": null\n" +
  153.         "        },\n" +
  154.         "        {\n" +
  155.         "          \"id\": 476,\n" +
  156.         "          \"order\": 476,\n" +
  157.         "          \"name\": \"1\",\n" +
  158.         "          \"products\": null,\n" +
  159.         "          \"recipe\": null,\n" +
  160.         "          \"image_url\": \"http://api.admin.appsoup.io/uploads/8d0d2b1e/03/0371de832297b36bd6cf5a2a08aad225/1.jpg\",\n" +
  161.         "          \"type\": null\n" +
  162.         "        },\n" +
  163.         "        {\n" +
  164.         "          \"id\": 477,\n" +
  165.         "          \"order\": 477,\n" +
  166.         "          \"name\": \"2\",\n" +
  167.         "          \"products\": null,\n" +
  168.         "          \"recipe\": null,\n" +
  169.         "          \"image_url\": \"http://api.admin.appsoup.io/uploads/8d0d2b1e/e0/e0ddf4298c994da30348c99e12ed7d33/2.jpg\",\n" +
  170.         "          \"type\": null\n" +
  171.         "        },\n" +
  172.         "        {\n" +
  173.         "          \"id\": 478,\n" +
  174.         "          \"order\": 478,\n" +
  175.         "          \"name\": \"3\",\n" +
  176.         "          \"products\": null,\n" +
  177.         "          \"recipe\": null,\n" +
  178.         "          \"image_url\": \"http://api.admin.appsoup.io/uploads/8d0d2b1e/17/175fa4a8c0b62166b83c4d925f0eebeb/3.jpg\",\n" +
  179.         "          \"type\": null\n" +
  180.         "        },\n" +
  181.         "        {\n" +
  182.         "          \"id\": 479,\n" +
  183.         "          \"order\": 479,\n" +
  184.         "          \"name\": \"4\",\n" +
  185.         "          \"products\": null,\n" +
  186.         "          \"recipe\": null,\n" +
  187.         "          \"image_url\": \"http://api.admin.appsoup.io/uploads/8d0d2b1e/8e/8e55e20f7e20e069ee3a12100eff911d/4.jpg\",\n" +
  188.         "          \"type\": null\n" +
  189.         "        },\n" +
  190.         "        {\n" +
  191.         "          \"id\": 480,\n" +
  192.         "          \"order\": 480,\n" +
  193.         "          \"name\": \"5\",\n" +
  194.         "          \"products\": null,\n" +
  195.         "          \"recipe\": null,\n" +
  196.         "          \"image_url\": \"http://api.admin.appsoup.io/uploads/8d0d2b1e/f4/f445ff7cb06d9ff53e9eccec7feea061/5.jpg\",\n" +
  197.         "          \"type\": null\n" +
  198.         "        }\n" +
  199.         "      ]\n" +
  200.         "    },\n" +
  201.         "    {\n" +
  202.         "      \"id\": 158,\n" +
  203.         "      \"start_date\": 1524175200,\n" +
  204.         "      \"end_date\": 1525298399,\n" +
  205.         "      \"name\": \"Tesco Value\",\n" +
  206.         "      \"newsletter_url\": \"http://www.pdf995.com/samples/pdf.pdf\",\n" +
  207.         "      \"types\": \"tesco\",\n" +
  208.         "      \"promotion\": null,\n" +
  209.         "      \"cover_image_url\": \"http://api.admin.appsoup.io/uploads/8d0d2b1e/83/83da9aa1f76e2fd25c018e9ff124c487/gazetka-2016-12-09-2223-str-0000.jpg\",\n" +
  210.         "      \"pages\": [\n" +
  211.         "        {\n" +
  212.         "          \"id\": 291,\n" +
  213.         "          \"order\": 291,\n" +
  214.         "          \"name\": \"Strona 1\",\n" +
  215.         "          \"products\": null,\n" +
  216.         "          \"recipe\": null,\n" +
  217.         "          \"image_url\": \"http://api.admin.appsoup.io/uploads/8d0d2b1e/83/83da9aa1f76e2fd25c018e9ff124c487/gazetka-2016-12-09-2223-str-0000.jpg\",\n" +
  218.         "          \"type\": null\n" +
  219.         "        },\n" +
  220.         "        {\n" +
  221.         "          \"id\": 292,\n" +
  222.         "          \"order\": 292,\n" +
  223.         "          \"name\": \"Strona 2\",\n" +
  224.         "          \"products\": null,\n" +
  225.         "          \"recipe\": null,\n" +
  226.         "          \"image_url\": \"http://api.admin.appsoup.io/uploads/8d0d2b1e/fc/fcdb4d8ed0cf8d6d2f4e9ccc38af07e1/j1.jpg\",\n" +
  227.         "          \"type\": null\n" +
  228.         "        }\n" +
  229.         "      ]\n" +
  230.         "    }\n" +
  231.         "  ],\n" +
  232.         "  \"app_version\": null\n" +
  233.         "}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement