Java Code Examples for okhttp3.OkHttpClient.Builder#addNetworkInterceptor()

The following examples show how to use okhttp3.OkHttpClient.Builder#addNetworkInterceptor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LocateMaleFemaleFaces.java    From Azure-Serverless-Computing-Cookbook-Second-Edition with MIT License 6 votes vote down vote up
private static ComputerVisionClient getClient() {
    return new ComputerVisionClientImpl(
        System.getenv("CognitiveServicesApiEndpoint"),
        new ServiceClientCredentials() {
            @Override
            public void applyCredentialsFilter(Builder builder) {
                builder.addNetworkInterceptor(
                    new Interceptor() {
                        @Override
                        public Response intercept(Chain chain) throws IOException {
                            Request request = null;
                            Request original = chain.request();
                            Request.Builder requestBuilder = original.newBuilder();
                            requestBuilder.addHeader("Ocp-Apim-Subscription-Key", System.getenv("CognitiveServicesApiKey"));
                            request = requestBuilder.build();
                            return chain.proceed(request);
                        }
                    }
                );
            }
        }
    );
}
 
Example 2
Source File: EntityApi.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
EntityClient(ISession session, boolean activeCaching) {
	super();

	ExecutorService executor = RateLimitExecutor.create(30, BitbucketSession.class, session.id());
	BitbucketInterceptor interceptors = new BitbucketInterceptor(session);
	String baseurl = BitbucketPropertiesUtil.get(API_BASE_URL);

	if (!baseurl.endsWith("/")) baseurl += "/"; // FIXME Validate in Model with EVL 

	Builder clientBuilder = AbstractClient.okHttp(executor);
	
	ICache localcache = new BitbucketCacheManager().getCacheInstance();
	if (activeCaching && localcache != null && !localcache.isDistributed()) {
		clientBuilder = clientBuilder.cache(localcache.initializeLocal());
		LOG.info("enabling local okhttp cache");
	}
				
	clientBuilder = clientBuilder.addNetworkInterceptor(CacheControlInterceptor.REWRITE_CACHE_CONTROL_INTERCEPTOR);
	clientBuilder = clientBuilder.addInterceptor(interceptors.mainInterceptor(activeCaching));
				
	this.client = clientBuilder.build();

	this.callbackEndpoint = AbstractClient.retrofit(client, baseurl).create(IEntityEndpoint.class);
	this.paginationPolicy = BitbucketPagination.get();
}
 
Example 3
Source File: SearchApi.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
SearchClient(ISession session, boolean activeCaching) {
	super();

	ExecutorService executor = RateLimitExecutor.create(30, BitbucketSession.class, session.id());
	BitbucketInterceptor interceptors = new BitbucketInterceptor(session);
	String baseurl = BitbucketPropertiesUtil.get(API_BASE_URL);

	if (!baseurl.endsWith("/")) baseurl += "/"; // FIXME Validate in Model with EVL 

	Builder clientBuilder = AbstractClient.okHttp(executor);
	
	ICache localcache = new BitbucketCacheManager().getCacheInstance();
	if (activeCaching && localcache != null && !localcache.isDistributed()) {
		clientBuilder = clientBuilder.cache(localcache.initializeLocal());
		LOG.info("enabling local okhttp cache");
	}
				
	clientBuilder = clientBuilder.addNetworkInterceptor(CacheControlInterceptor.REWRITE_CACHE_CONTROL_INTERCEPTOR);
	clientBuilder = clientBuilder.addInterceptor(interceptors.mainInterceptor(activeCaching));
				
	this.client = clientBuilder.build();

	this.callbackEndpoint = AbstractClient.retrofit(client, baseurl).create(ISearchEndpoint.class);
	this.paginationPolicy = BitbucketPagination.get();
}
 
Example 4
Source File: EntityApi.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
EntityClient(ISession session, boolean activeCaching) {
	super();

	ExecutorService executor = RateLimitExecutor.create(30, StackExchangeSession.class, session.id());
	StackExchangeInterceptor interceptors = new StackExchangeInterceptor(session);
	String baseurl = StackExchangePropertiesUtil.get(API_BASE_URL);

	if (!baseurl.endsWith("/")) baseurl += "/"; // FIXME Validate in Model with EVL 

	Builder clientBuilder = AbstractClient.okHttp(executor);
	
	ICache localcache = new StackExchangeCacheManager().getCacheInstance();
	if (activeCaching && localcache != null && !localcache.isDistributed()) {
		clientBuilder = clientBuilder.cache(localcache.initializeLocal());
		LOG.info("enabling local okhttp cache");
	}
				
	clientBuilder = clientBuilder.addNetworkInterceptor(CacheControlInterceptor.REWRITE_CACHE_CONTROL_INTERCEPTOR);
	clientBuilder = clientBuilder.addInterceptor(interceptors.mainInterceptor(activeCaching));
				
	this.client = clientBuilder.build();

	this.callbackEndpoint = AbstractClient.retrofit(client, baseurl).create(IEntityEndpoint.class);
	this.paginationPolicy = StackExchangePagination.get();
}
 
Example 5
Source File: SearchApi.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
SearchClient(ISession session, boolean activeCaching) {
	super();

	ExecutorService executor = RateLimitExecutor.create(30, StackExchangeSession.class, session.id());
	StackExchangeInterceptor interceptors = new StackExchangeInterceptor(session);
	String baseurl = StackExchangePropertiesUtil.get(API_BASE_URL);

	if (!baseurl.endsWith("/")) baseurl += "/"; // FIXME Validate in Model with EVL 

	Builder clientBuilder = AbstractClient.okHttp(executor);
	
	ICache localcache = new StackExchangeCacheManager().getCacheInstance();
	if (activeCaching && localcache != null && !localcache.isDistributed()) {
		clientBuilder = clientBuilder.cache(localcache.initializeLocal());
		LOG.info("enabling local okhttp cache");
	}
				
	clientBuilder = clientBuilder.addNetworkInterceptor(CacheControlInterceptor.REWRITE_CACHE_CONTROL_INTERCEPTOR);
	clientBuilder = clientBuilder.addInterceptor(interceptors.mainInterceptor(activeCaching));
				
	this.client = clientBuilder.build();

	this.callbackEndpoint = AbstractClient.retrofit(client, baseurl).create(ISearchEndpoint.class);
	this.paginationPolicy = StackExchangePagination.get();
}
 
Example 6
Source File: EntityApi.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
EntityClient(ISession session, boolean activeCaching) {
	super();

	ExecutorService executor = RateLimitExecutor.create(30, GitlabSession.class, session.id());
	GitlabInterceptor interceptors = new GitlabInterceptor(session);
	String baseurl = GitlabPropertiesUtil.get(API_BASE_URL);

	if (!baseurl.endsWith("/")) baseurl += "/"; // FIXME Validate in Model with EVL 

	Builder clientBuilder = AbstractClient.okHttp(executor);
	
	ICache localcache = new GitlabCacheManager().getCacheInstance();
	if (activeCaching && localcache != null && !localcache.isDistributed()) {
		clientBuilder = clientBuilder.cache(localcache.initializeLocal());
		LOG.info("enabling local okhttp cache");
	}
				
	clientBuilder = clientBuilder.addNetworkInterceptor(CacheControlInterceptor.REWRITE_CACHE_CONTROL_INTERCEPTOR);
	clientBuilder = clientBuilder.addInterceptor(interceptors.mainInterceptor(activeCaching));
				
	this.client = clientBuilder.build();

	this.callbackEndpoint = AbstractClient.retrofit(client, baseurl).create(IEntityEndpoint.class);
	this.paginationPolicy = GitlabPagination.get();
}
 
Example 7
Source File: SearchApi.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
SearchClient(ISession session, boolean activeCaching) {
	super();

	ExecutorService executor = RateLimitExecutor.create(30, GitlabSession.class, session.id());
	GitlabInterceptor interceptors = new GitlabInterceptor(session);
	String baseurl = GitlabPropertiesUtil.get(API_BASE_URL);

	if (!baseurl.endsWith("/")) baseurl += "/"; // FIXME Validate in Model with EVL 

	Builder clientBuilder = AbstractClient.okHttp(executor);
	
	ICache localcache = new GitlabCacheManager().getCacheInstance();
	if (activeCaching && localcache != null && !localcache.isDistributed()) {
		clientBuilder = clientBuilder.cache(localcache.initializeLocal());
		LOG.info("enabling local okhttp cache");
	}
				
	clientBuilder = clientBuilder.addNetworkInterceptor(CacheControlInterceptor.REWRITE_CACHE_CONTROL_INTERCEPTOR);
	clientBuilder = clientBuilder.addInterceptor(interceptors.mainInterceptor(activeCaching));
				
	this.client = clientBuilder.build();

	this.callbackEndpoint = AbstractClient.retrofit(client, baseurl).create(ISearchEndpoint.class);
	this.paginationPolicy = GitlabPagination.get();
}
 
Example 8
Source File: LocateMaleFemaleFaces.java    From Azure-Serverless-Computing-Cookbook-Second-Edition with MIT License 6 votes vote down vote up
private static ComputerVisionClient getClient() {
    return new ComputerVisionClientImpl(
        System.getenv("CognitiveServicesApiEndpoint"),
        new ServiceClientCredentials() {
            @Override
            public void applyCredentialsFilter(Builder builder) {
                builder.addNetworkInterceptor(
                    new Interceptor() {
                        @Override
                        public Response intercept(Chain chain) throws IOException {
                            Request request = null;
                            Request original = chain.request();
                            Request.Builder requestBuilder = original.newBuilder();
                            requestBuilder.addHeader("Ocp-Apim-Subscription-Key", System.getenv("CognitiveServicesApiKey"));
                            request = requestBuilder.build();
                            return chain.proceed(request);
                        }
                    }
                );
            }
        }
    );
}
 
Example 9
Source File: LocateMaleFemaleFaces.java    From Azure-Serverless-Computing-Cookbook-Second-Edition with MIT License 6 votes vote down vote up
private static ComputerVisionClient getClient() {
    return new ComputerVisionClientImpl(
        System.getenv("CognitiveServicesApiEndpoint"),
        new ServiceClientCredentials() {
            @Override
            public void applyCredentialsFilter(Builder builder) {
                builder.addNetworkInterceptor(
                    new Interceptor() {
                        @Override
                        public Response intercept(Chain chain) throws IOException {
                            Request request = null;
                            Request original = chain.request();
                            Request.Builder requestBuilder = original.newBuilder();
                            requestBuilder.addHeader("Ocp-Apim-Subscription-Key", System.getenv("CognitiveServicesApiKey"));
                            request = requestBuilder.build();
                            return chain.proceed(request);
                        }
                    }
                );
            }
        }
    );
}
 
Example 10
Source File: LocateMaleFemaleFaces.java    From Azure-Serverless-Computing-Cookbook-Second-Edition with MIT License 6 votes vote down vote up
private static ComputerVisionClient getClient() {
    return new ComputerVisionClientImpl(
        System.getenv("CognitiveServicesApiEndpoint"),
        new ServiceClientCredentials() {
            @Override
            public void applyCredentialsFilter(Builder builder) {
                builder.addNetworkInterceptor(
                    new Interceptor() {
                        @Override
                        public Response intercept(Chain chain) throws IOException {
                            Request request = null;
                            Request original = chain.request();
                            Request.Builder requestBuilder = original.newBuilder();
                            requestBuilder.addHeader("Ocp-Apim-Subscription-Key", System.getenv("CognitiveServicesApiKey"));
                            request = requestBuilder.build();
                            return chain.proceed(request);
                        }
                    }
                );
            }
        }
    );
}
 
Example 11
Source File: LocateMaleFemaleFaces.java    From Azure-Serverless-Computing-Cookbook-Second-Edition with MIT License 6 votes vote down vote up
private static ComputerVisionClient getClient() {
    return new ComputerVisionClientImpl(
        System.getenv("CognitiveServicesApiEndpoint"),
        new ServiceClientCredentials() {
            @Override
            public void applyCredentialsFilter(Builder builder) {
                builder.addNetworkInterceptor(
                    new Interceptor() {
                        @Override
                        public Response intercept(Chain chain) throws IOException {
                            Request request = null;
                            Request original = chain.request();
                            Request.Builder requestBuilder = original.newBuilder();
                            requestBuilder.addHeader("Ocp-Apim-Subscription-Key", System.getenv("CognitiveServicesApiKey"));
                            request = requestBuilder.build();
                            return chain.proceed(request);
                        }
                    }
                );
            }
        }
    );
}
 
Example 12
Source File: LocateMaleFemaleFaces.java    From Azure-Serverless-Computing-Cookbook-Second-Edition with MIT License 6 votes vote down vote up
private static ComputerVisionClient getClient() {
    return new ComputerVisionClientImpl(
        System.getenv("CognitiveServicesApiEndpoint"),
        new ServiceClientCredentials() {
            @Override
            public void applyCredentialsFilter(Builder builder) {
                builder.addNetworkInterceptor(
                    new Interceptor() {
                        @Override
                        public Response intercept(Chain chain) throws IOException {
                            Request request = null;
                            Request original = chain.request();
                            Request.Builder requestBuilder = original.newBuilder();
                            requestBuilder.addHeader("Ocp-Apim-Subscription-Key", System.getenv("CognitiveServicesApiKey"));
                            request = requestBuilder.build();
                            return chain.proceed(request);
                        }
                    }
                );
            }
        }
    );
}
 
Example 13
Source File: LocateMaleFemaleFaces.java    From Azure-Serverless-Computing-Cookbook-Second-Edition with MIT License 6 votes vote down vote up
private static ComputerVisionClient getClient() {
    return new ComputerVisionClientImpl(
        System.getenv("CognitiveServicesApiEndpoint"),
        new ServiceClientCredentials() {
            @Override
            public void applyCredentialsFilter(Builder builder) {
                builder.addNetworkInterceptor(
                    new Interceptor() {
                        @Override
                        public Response intercept(Chain chain) throws IOException {
                            Request request = null;
                            Request original = chain.request();
                            Request.Builder requestBuilder = original.newBuilder();
                            requestBuilder.addHeader("Ocp-Apim-Subscription-Key", System.getenv("CognitiveServicesApiKey"));
                            request = requestBuilder.build();
                            return chain.proceed(request);
                        }
                    }
                );
            }
        }
    );
}
 
Example 14
Source File: LocateMaleFemaleFaces.java    From Azure-Serverless-Computing-Cookbook-Second-Edition with MIT License 6 votes vote down vote up
private static ComputerVisionClient getClient() {
    return new ComputerVisionClientImpl(
        System.getenv("CognitiveServicesApiEndpoint"),
        new ServiceClientCredentials() {
            @Override
            public void applyCredentialsFilter(Builder builder) {
                builder.addNetworkInterceptor(
                    new Interceptor() {
                        @Override
                        public Response intercept(Chain chain) throws IOException {
                            Request request = null;
                            Request original = chain.request();
                            Request.Builder requestBuilder = original.newBuilder();
                            requestBuilder.addHeader("Ocp-Apim-Subscription-Key", System.getenv("CognitiveServicesApiKey"));
                            request = requestBuilder.build();
                            return chain.proceed(request);
                        }
                    }
                );
            }
        }
    );
}
 
Example 15
Source File: LocateMaleFemaleFaces.java    From Azure-Serverless-Computing-Cookbook-Second-Edition with MIT License 6 votes vote down vote up
private static ComputerVisionClient getClient() {
    return new ComputerVisionClientImpl(
        System.getenv("CognitiveServicesApiEndpoint"),
        new ServiceClientCredentials() {
            @Override
            public void applyCredentialsFilter(Builder builder) {
                builder.addNetworkInterceptor(
                    new Interceptor() {
                        @Override
                        public Response intercept(Chain chain) throws IOException {
                            Request request = null;
                            Request original = chain.request();
                            Request.Builder requestBuilder = original.newBuilder();
                            requestBuilder.addHeader("Ocp-Apim-Subscription-Key", System.getenv("CognitiveServicesApiKey"));
                            request = requestBuilder.build();
                            return chain.proceed(request);
                        }
                    }
                );
            }
        }
    );
}
 
Example 16
Source File: LocateMaleFemaleFaces.java    From Azure-Serverless-Computing-Cookbook-Second-Edition with MIT License 6 votes vote down vote up
private static ComputerVisionClient getClient() {
    return new ComputerVisionClientImpl(
        System.getenv("CognitiveServicesApiEndpoint"),
        new ServiceClientCredentials() {
            @Override
            public void applyCredentialsFilter(Builder builder) {
                builder.addNetworkInterceptor(
                    new Interceptor() {
                        @Override
                        public Response intercept(Chain chain) throws IOException {
                            Request request = null;
                            Request original = chain.request();
                            Request.Builder requestBuilder = original.newBuilder();
                            requestBuilder.addHeader("Ocp-Apim-Subscription-Key", System.getenv("CognitiveServicesApiKey"));
                            request = requestBuilder.build();
                            return chain.proceed(request);
                        }
                    }
                );
            }
        }
    );
}
 
Example 17
Source File: LocateMaleFemaleFaces.java    From Azure-Serverless-Computing-Cookbook-Second-Edition with MIT License 6 votes vote down vote up
private static ComputerVisionClient getClient() {
    return new ComputerVisionClientImpl(
        System.getenv("CognitiveServicesApiEndpoint"),
        new ServiceClientCredentials() {
            @Override
            public void applyCredentialsFilter(Builder builder) {
                builder.addNetworkInterceptor(
                    new Interceptor() {
                        @Override
                        public Response intercept(Chain chain) throws IOException {
                            Request request = null;
                            Request original = chain.request();
                            Request.Builder requestBuilder = original.newBuilder();
                            requestBuilder.addHeader("Ocp-Apim-Subscription-Key", System.getenv("CognitiveServicesApiKey"));
                            request = requestBuilder.build();
                            return chain.proceed(request);
                        }
                    }
                );
            }
        }
    );
}
 
Example 18
Source File: LocateMaleFemaleFaces.java    From Azure-Serverless-Computing-Cookbook-Second-Edition with MIT License 6 votes vote down vote up
private static ComputerVisionClient getClient() {
    return new ComputerVisionClientImpl(
        System.getenv("CognitiveServicesApiEndpoint"),
        new ServiceClientCredentials() {
            @Override
            public void applyCredentialsFilter(Builder builder) {
                builder.addNetworkInterceptor(
                    new Interceptor() {
                        @Override
                        public Response intercept(Chain chain) throws IOException {
                            Request request = null;
                            Request original = chain.request();
                            Request.Builder requestBuilder = original.newBuilder();
                            requestBuilder.addHeader("Ocp-Apim-Subscription-Key", System.getenv("CognitiveServicesApiKey"));
                            request = requestBuilder.build();
                            return chain.proceed(request);
                        }
                    }
                );
            }
        }
    );
}
 
Example 19
Source File: SearchApi.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
SearchClient(ISession session, boolean activeCaching) {
	super();

	ExecutorService executor = RateLimitExecutor.create(30, GitHubSession.class, session.id());
	GitHubInterceptor interceptors = new GitHubInterceptor(session);
	String baseurl = GitHubPropertiesUtil.get(API_BASE_URL);

	if (!baseurl.endsWith("/"))
		baseurl += "/"; // FIXME Validate in Model with EVL

	Builder clientBuilder = AbstractClient.okHttp(executor);

	ICache localcache = new GitHubCacheManager().getCacheInstance();
	if (activeCaching && localcache != null && !localcache.isDistributed()) {
		clientBuilder = clientBuilder.cache(localcache.initializeLocal());
		LOG.info("enabling local okhttp cache");
	}

	// FIXME update generator
	clientBuilder = clientBuilder
			.addNetworkInterceptor(CacheControlInterceptor.REWRITE_CACHE_CONTROL_INTERCEPTOR);
	clientBuilder = clientBuilder.addInterceptor(interceptors.mainInterceptor(activeCaching));

	this.client = clientBuilder.build();

	this.callbackEndpoint = AbstractClient.retrofit(client, baseurl).create(ISearchEndpoint.class);
	this.paginationPolicy = GitHubSearchAPIPagination.get();
}
 
Example 20
Source File: EntityApi.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
EntityClient(ISession session, boolean activeCaching) {
	super();

	ExecutorService executor = RateLimitExecutor.create(30, GitHubSession.class, session.id());
	GitHubInterceptor interceptors = new GitHubInterceptor(session);
	String baseurl = GitHubPropertiesUtil.get(API_BASE_URL);

	if (!baseurl.endsWith("/"))
		baseurl += "/"; // FIXME Validate in Model with EVL

	Builder clientBuilder = AbstractClient.okHttp(executor);

	ICache localcache = new GitHubCacheManager().getCacheInstance();
	if (activeCaching && localcache != null && !localcache.isDistributed()){
		clientBuilder = clientBuilder.cache(localcache.initializeLocal());
		LOG.info("enabling local okhttp cache");	
	}

	clientBuilder = clientBuilder
			.addNetworkInterceptor(CacheControlInterceptor.REWRITE_CACHE_CONTROL_INTERCEPTOR);
	clientBuilder = clientBuilder.addInterceptor(interceptors.mainInterceptor(activeCaching));

	this.client = clientBuilder.build();

	this.callbackEndpoint = AbstractClient.retrofit(client, baseurl).create(IEntityEndpoint.class);
	this.paginationPolicy = GitHubEntityAPIPagination.get();
}