Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.omgapps.gazetki.rest
- import com.google.android.gms.common.api.Api
- import com.google.gson.Gson
- import com.omgapps.gazetki.App
- import com.omgapps.gazetki.Cfg
- import com.omgapps.gazetki.rest.models.LeafletResponse
- import com.omgapps.gazetki.rest.models.ShopResponse
- import com.omgapps.gazetki.views.pages.homeFragment.HomeFragment
- import com.omgapps.gazetki.views.splash.DownloadBulletins
- import com.omgapps.gazetki.views.splash.DownloadShops
- import dagger.Component
- import dagger.Module
- import dagger.Provides
- import okhttp3.*
- import retrofit2.Call
- import retrofit2.Retrofit
- import retrofit2.converter.gson.GsonConverterFactory
- import retrofit2.http.GET
- import java.util.concurrent.TimeUnit
- import javax.inject.Inject
- import javax.inject.Singleton
- const val BASE_URL = "http://api.admin.appsoup.io/"
- interface RestInterface {
- @GET(Cfg.API.LEAFLETS)
- fun getLeaflets(): Call<LeafletResponse>
- @GET(Cfg.API.SHOPS)
- fun getShops(): Call<ShopResponse>
- }
- @Module
- class RestModule {
- @Provides
- fun provideAppSoupInterceptor() = AppSoupInterceptor()
- @Provides
- fun provideGson() = Gson()
- @Provides
- fun provideHttpClient(interceptor: AppSoupInterceptor) : OkHttpClient {
- return OkHttpClient.Builder()
- .addInterceptor(interceptor)
- .connectTimeout(30, TimeUnit.SECONDS)
- .readTimeout(30, TimeUnit.SECONDS)
- .writeTimeout(30, TimeUnit.SECONDS)
- .build()
- }
- @Provides
- fun provideRetrofit(httpClient: OkHttpClient, gson: Gson) : Retrofit {
- return Retrofit.Builder()
- .baseUrl(BASE_URL)
- .addConverterFactory(GsonConverterFactory.create(gson))
- .client(httpClient)
- .build()
- }
- @Provides
- fun provideRest(retrofit: Retrofit) : RestInterface {
- return retrofit
- .create(RestInterface::class.java)
- }
- }
- @Module
- class RestTestModule {
- @Provides
- fun provideAppSoupInterceptor() : Interceptor = FakeInterceptor()
- @Provides
- fun provideGson() = Gson()
- @Provides
- fun provideHttpClient(interceptor: Interceptor) : OkHttpClient {
- return OkHttpClient.Builder()
- .addInterceptor(interceptor)
- .connectTimeout(30, TimeUnit.SECONDS)
- .readTimeout(30, TimeUnit.SECONDS)
- .writeTimeout(30, TimeUnit.SECONDS)
- .build()
- }
- @Provides
- fun provideRetrofit(httpClient: OkHttpClient, gson: Gson) : Retrofit {
- return Retrofit.Builder()
- .baseUrl(BASE_URL)
- .addConverterFactory(GsonConverterFactory.create(gson))
- .client(httpClient)
- .build()
- }
- @Provides
- fun provideRest(retrofit: Retrofit) : RestInterface {
- return retrofit
- .create(RestInterface::class.java)
- }
- }
- @Component (modules = [RestModule::class])
- interface AppComponent {
- @Singleton
- fun rest() : RestInterface
- fun inject(fragment: HomeFragment)
- fun inject(downloadBulletins: DownloadBulletins)
- fun inject(downloadShops: DownloadShops)
- }
- @Component (modules = [RestTestModule::class])
- interface AppTestComponent : AppComponent
- class FakeInterceptor : Interceptor {
- override fun intercept(chain: Interceptor.Chain?): Response {
- val responseString : String = MOCK_JSON_RESPONSE_STRING
- return Response.Builder()
- .code(200)
- .message("OK")
- .request(chain?.request())
- .protocol(Protocol.HTTP_1_0)
- .body(ResponseBody.create(MediaType.parse("application/json"), responseString.toByteArray()))
- .addHeader("content-type", "application/json")
- .build()
- }
- }
- val MOCK_JSON_RESPONSE_STRING = "{\n" +
- " \"status\": \"success\",\n" +
- " \"status_human_message\": null,\n" +
- " \"data\": [\n" +
- " {\n" +
- " \"id\": 182,\n" +
- " \"start_date\": 1525125600,\n" +
- " \"end_date\": 1527803999,\n" +
- " \"name\": \"Wielki finał 10. urodzin\",\n" +
- " \"newsletter_url\": null,\n" +
- " \"types\": \"aldi\",\n" +
- " \"promotion\": null,\n" +
- " \"cover_image_url\": \"http://api.admin.appsoup.io/uploads/8d0d2b1e/e7/e778764fe71b4c65026ae980c335e1ba/0.jpg\",\n" +
- " \"pages\": [\n" +
- " {\n" +
- " \"id\": 475,\n" +
- " \"order\": 475,\n" +
- " \"name\": \"0\",\n" +
- " \"products\": null,\n" +
- " \"recipe\": null,\n" +
- " \"image_url\": \"http://api.admin.appsoup.io/uploads/8d0d2b1e/e7/e778764fe71b4c65026ae980c335e1ba/0.jpg\",\n" +
- " \"type\": null\n" +
- " },\n" +
- " {\n" +
- " \"id\": 476,\n" +
- " \"order\": 476,\n" +
- " \"name\": \"1\",\n" +
- " \"products\": null,\n" +
- " \"recipe\": null,\n" +
- " \"image_url\": \"http://api.admin.appsoup.io/uploads/8d0d2b1e/03/0371de832297b36bd6cf5a2a08aad225/1.jpg\",\n" +
- " \"type\": null\n" +
- " },\n" +
- " {\n" +
- " \"id\": 477,\n" +
- " \"order\": 477,\n" +
- " \"name\": \"2\",\n" +
- " \"products\": null,\n" +
- " \"recipe\": null,\n" +
- " \"image_url\": \"http://api.admin.appsoup.io/uploads/8d0d2b1e/e0/e0ddf4298c994da30348c99e12ed7d33/2.jpg\",\n" +
- " \"type\": null\n" +
- " },\n" +
- " {\n" +
- " \"id\": 478,\n" +
- " \"order\": 478,\n" +
- " \"name\": \"3\",\n" +
- " \"products\": null,\n" +
- " \"recipe\": null,\n" +
- " \"image_url\": \"http://api.admin.appsoup.io/uploads/8d0d2b1e/17/175fa4a8c0b62166b83c4d925f0eebeb/3.jpg\",\n" +
- " \"type\": null\n" +
- " },\n" +
- " {\n" +
- " \"id\": 479,\n" +
- " \"order\": 479,\n" +
- " \"name\": \"4\",\n" +
- " \"products\": null,\n" +
- " \"recipe\": null,\n" +
- " \"image_url\": \"http://api.admin.appsoup.io/uploads/8d0d2b1e/8e/8e55e20f7e20e069ee3a12100eff911d/4.jpg\",\n" +
- " \"type\": null\n" +
- " },\n" +
- " {\n" +
- " \"id\": 480,\n" +
- " \"order\": 480,\n" +
- " \"name\": \"5\",\n" +
- " \"products\": null,\n" +
- " \"recipe\": null,\n" +
- " \"image_url\": \"http://api.admin.appsoup.io/uploads/8d0d2b1e/f4/f445ff7cb06d9ff53e9eccec7feea061/5.jpg\",\n" +
- " \"type\": null\n" +
- " }\n" +
- " ]\n" +
- " },\n" +
- " {\n" +
- " \"id\": 158,\n" +
- " \"start_date\": 1524175200,\n" +
- " \"end_date\": 1525298399,\n" +
- " \"name\": \"Tesco Value\",\n" +
- " \"newsletter_url\": \"http://www.pdf995.com/samples/pdf.pdf\",\n" +
- " \"types\": \"tesco\",\n" +
- " \"promotion\": null,\n" +
- " \"cover_image_url\": \"http://api.admin.appsoup.io/uploads/8d0d2b1e/83/83da9aa1f76e2fd25c018e9ff124c487/gazetka-2016-12-09-2223-str-0000.jpg\",\n" +
- " \"pages\": [\n" +
- " {\n" +
- " \"id\": 291,\n" +
- " \"order\": 291,\n" +
- " \"name\": \"Strona 1\",\n" +
- " \"products\": null,\n" +
- " \"recipe\": null,\n" +
- " \"image_url\": \"http://api.admin.appsoup.io/uploads/8d0d2b1e/83/83da9aa1f76e2fd25c018e9ff124c487/gazetka-2016-12-09-2223-str-0000.jpg\",\n" +
- " \"type\": null\n" +
- " },\n" +
- " {\n" +
- " \"id\": 292,\n" +
- " \"order\": 292,\n" +
- " \"name\": \"Strona 2\",\n" +
- " \"products\": null,\n" +
- " \"recipe\": null,\n" +
- " \"image_url\": \"http://api.admin.appsoup.io/uploads/8d0d2b1e/fc/fcdb4d8ed0cf8d6d2f4e9ccc38af07e1/j1.jpg\",\n" +
- " \"type\": null\n" +
- " }\n" +
- " ]\n" +
- " }\n" +
- " ],\n" +
- " \"app_version\": null\n" +
- "}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement