Search in sources :

Example 41 with Provides

use of dagger.Provides in project dev-summit-architecture-demo by yigit.

the class ApiModule method apiService.

@Provides
@Singleton
public ApiService apiService(DemoConfig demoConfig) {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    return new Retrofit.Builder().baseUrl(demoConfig.getApiUrl()).addConverterFactory(JacksonConverterFactory.create(mapper)).build().create(ApiService.class);
}
Also used : ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Example 42 with Provides

use of dagger.Provides in project dobby-android by InceptAi.

the class ProdModule method providesDobbyAi.

@Singleton
@Provides
public DobbyAi providesDobbyAi(DobbyApplication application, DobbyThreadpool threadpool, InferenceDatabaseWriter inferenceDatabaseWriter, FailureDatabaseWriter failureDatabaseWriter) {
    DobbyAi dobbyAi = new DobbyAi(threadpool, inferenceDatabaseWriter, failureDatabaseWriter, application);
    application.getProdComponent().inject(dobbyAi);
    return dobbyAi;
}
Also used : DobbyAi(com.inceptai.dobby.ai.DobbyAi) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Example 43 with Provides

use of dagger.Provides in project Atom_Android by Rogrand-Dev.

the class HttpModule method provideClient.

@Singleton
@Provides
OkHttpClient provideClient(OkHttpClient.Builder builder) {
    if (DemoApp.getAppContext().isDebug()) {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {

            @Override
            public void log(String message) {
                LogUtils.i(TAG, message);
            }
        });
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        // 设置 Log 拦截器
        builder.addInterceptor(loggingInterceptor);
    }
    File cacheFile = new File(DataUtils.getCacheFile(DemoApp.getAppContext()), "RograndCache");
    // 100 MB
    int cacheSize = 100 * 1024 * 1024;
    Cache cache = new Cache(cacheFile, cacheSize);
    Interceptor cacheInterceptor = new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            CacheControl.Builder cacheBuilder = new CacheControl.Builder();
            cacheBuilder.maxAge(0, TimeUnit.SECONDS);
            cacheBuilder.maxStale(365, TimeUnit.DAYS);
            CacheControl cacheControl = cacheBuilder.build();
            Request request = chain.request();
            if (!NetUtils.isNetworkAvailable(DemoApp.getAppContext())) {
                request = request.newBuilder().cacheControl(cacheControl).build();
            }
            Response response = chain.proceed(request);
            if (NetUtils.isNetworkAvailable(DemoApp.getAppContext())) {
                // 有网络时, 不缓存, 最大保存时长为0
                int maxAge = 0;
                response.newBuilder().header("Cache-Control", "public, max-age=" + maxAge).removeHeader("Pragma").build();
            } else {
                // 无网络时,设置超时为4周
                int maxStale = 60 * 60 * 24 * 28;
                response.newBuilder().header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale).removeHeader("Pragma").build();
            }
            return response;
        }
    };
    // 设置 header(设置 Token、API 版本号)
    Interceptor headerInterceptor = new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            request = request.newBuilder().addHeader("Authorization", "Bearer " + SPUtils.get(DemoApp.getAppContext(), Constants.ACCESS_TOKEN, "")).addHeader("api_version", UrlConstants.API_VERSION).build();
            return chain.proceed(request);
        }
    };
    // 设置统一的请求头部参数
    builder.addInterceptor(headerInterceptor);
    // 设置缓存
    builder.addNetworkInterceptor(cacheInterceptor);
    builder.addInterceptor(cacheInterceptor);
    builder.cache(cache);
    // 设置连接超时
    builder.connectTimeout(TIME_OUT, TimeUnit.SECONDS);
    // 设置读取数据超时
    builder.readTimeout(TIME_OUT, TimeUnit.SECONDS);
    // 设置写入数据超时
    builder.writeTimeout(TIME_OUT, TimeUnit.SECONDS);
    // 设置 Stetho
    builder.addNetworkInterceptor(new StethoInterceptor());
    // 设置 Cookie
    builder.cookieJar(new CookieManger(DemoApp.getAppContext()));
    // 错误重连
    builder.retryOnConnectionFailure(true);
    return builder.build();
}
Also used : CookieManger(com.rogrand.demo.http.cookie.CookieManger) Request(okhttp3.Request) Response(okhttp3.Response) CacheControl(okhttp3.CacheControl) StethoInterceptor(com.facebook.stetho.okhttp3.StethoInterceptor) File(java.io.File) StethoInterceptor(com.facebook.stetho.okhttp3.StethoInterceptor) Interceptor(okhttp3.Interceptor) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Cache(okhttp3.Cache) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Example 44 with Provides

use of dagger.Provides in project coins-android by bubelov.

the class MainModule method httpClient.

@Provides
@Singleton
OkHttpClient httpClient() {
    OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder().connectTimeout(10, TimeUnit.SECONDS).readTimeout(30, TimeUnit.SECONDS).writeTimeout(30, TimeUnit.SECONDS);
    if (BuildConfig.DEBUG) {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
        httpClientBuilder.addInterceptor(loggingInterceptor);
        httpClientBuilder.addNetworkInterceptor(new StethoInterceptor());
    }
    return httpClientBuilder.build();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) StethoInterceptor(com.facebook.stetho.okhttp3.StethoInterceptor) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Example 45 with Provides

use of dagger.Provides in project Varis-Android by dkhmelenko.

the class TestNetworkModule method provideRawRestClient.

@Singleton
@Provides
public RawClient provideRawRestClient() {
    RawClient rawClient = Mockito.mock(RawClient.class);
    RawApiService rawApiService = Mockito.mock(RawApiService.class);
    Mockito.when(rawClient.getApiService()).thenReturn(rawApiService);
    return rawClient;
}
Also used : RawApiService(com.khmelenko.lab.varis.network.retrofit.raw.RawApiService) RawClient(com.khmelenko.lab.varis.network.retrofit.raw.RawClient) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Aggregations

Provides (dagger.Provides)46 Singleton (javax.inject.Singleton)35 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)11 OkHttpClient (okhttp3.OkHttpClient)10 OkHttpClient (com.squareup.okhttp.OkHttpClient)5 StethoInterceptor (com.facebook.stetho.okhttp3.StethoInterceptor)3 GsonBuilder (com.google.gson.GsonBuilder)3 Cache (okhttp3.Cache)3 LoginManager (com.byoutline.kickmaterial.managers.LoginManager)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 PlaceRenderer (com.github.pedrovgs.sample.renderer.PlaceRenderer)2 TvShowRenderer (com.github.pedrovgs.sample.renderer.TvShowRenderer)2 Gson (com.google.gson.Gson)2 BindingsGroup (dagger.internal.BindingsGroup)2 SetBinding (dagger.internal.SetBinding)2 PerApplication (io.hefuyi.listener.injector.scope.PerApplication)2 File (java.io.File)2 LinkedHashMap (java.util.LinkedHashMap)2 Named (javax.inject.Named)2 ExecutableElement (javax.lang.model.element.ExecutableElement)2