use of com.pushtorefresh.storio3.Interceptor in project apollo-android by apollographql.
the class SendOperationIdentifiersTest method operationIdHttpRequestHeader.
@Test
public void operationIdHttpRequestHeader() throws Exception {
final HeroAndFriendsNamesQuery heroAndFriendsNamesQuery = new HeroAndFriendsNamesQuery(Input.fromNullable(EMPIRE));
final AtomicBoolean applicationInterceptorHeader = new AtomicBoolean();
final AtomicBoolean networkInterceptorHeader = new AtomicBoolean();
OkHttpClient okHttpClient = new OkHttpClient.Builder().dispatcher(new Dispatcher(Utils.immediateExecutorService())).addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (request.header("X-APOLLO-OPERATION-ID").equals(heroAndFriendsNamesQuery.operationId())) {
applicationInterceptorHeader.set(true);
}
return chain.proceed(chain.request());
}
}).addNetworkInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (request.header("X-APOLLO-OPERATION-ID").equals(heroAndFriendsNamesQuery.operationId())) {
networkInterceptorHeader.set(true);
}
return chain.proceed(chain.request());
}
}).build();
ApolloClient apolloClient = ApolloClient.builder().serverUrl(server.url("/")).okHttpClient(okHttpClient).dispatcher(Utils.immediateExecutor()).build();
enqueueAndAssertResponse(server, "HeroAndFriendsNameResponse.json", apolloClient.query(heroAndFriendsNamesQuery), new Predicate<com.apollographql.apollo.api.Response<HeroAndFriendsNamesQuery.Data>>() {
@Override
public boolean test(com.apollographql.apollo.api.Response<HeroAndFriendsNamesQuery.Data> response) throws Exception {
return !response.hasErrors();
}
});
assertThat(applicationInterceptorHeader.get()).isTrue();
assertThat(networkInterceptorHeader.get()).isTrue();
}
use of com.pushtorefresh.storio3.Interceptor in project apollo-android by apollographql.
the class ApolloCallTrackerTest method setUp.
@Before
public void setUp() throws Exception {
activeCallCounts = new ArrayList<>();
Interceptor interceptor = new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
activeCallCounts.add(apolloClient.activeCallsCount());
return chain.proceed(chain.request());
}
};
OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(interceptor).build();
apolloClient = ApolloClient.builder().serverUrl(SERVER_URL).okHttpClient(okHttpClient).build();
}
use of com.pushtorefresh.storio3.Interceptor in project Ency by xiarunhao123.
the class HttpModule method provideOkHttpClient.
@Provides
@Singleton
OkHttpClient provideOkHttpClient(OkHttpClient.Builder builder, final Context context) {
if (BuildConfig.DEBUG) {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
builder.addInterceptor(loggingInterceptor);
}
File cacheFile = new File(Constants.PATH_CACHE);
Cache cache = new Cache(cacheFile, 1024 * 1024 * 50);
Interceptor cacheInterceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (!AppNetWorkUtil.isNetworkConnected(context)) {
request = request.newBuilder().cacheControl(CacheControl.FORCE_CACHE).build();
}
Response response = chain.proceed(request);
if (AppNetWorkUtil.isNetworkConnected(context)) {
int maxAge = 0;
// 有网络时, 不缓存, 最大保存时长为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;
}
};
// 设置缓存
builder.addNetworkInterceptor(cacheInterceptor);
builder.addInterceptor(cacheInterceptor);
builder.cache(cache);
// 设置超时
builder.connectTimeout(10, TimeUnit.SECONDS);
builder.readTimeout(20, TimeUnit.SECONDS);
builder.writeTimeout(20, TimeUnit.SECONDS);
// 错误重连
builder.retryOnConnectionFailure(true);
return builder.build();
}
use of com.pushtorefresh.storio3.Interceptor in project osm-contributor by jawg.
the class FlickrPhotoUtils method getAdapter.
public static Retrofit getAdapter(final Map<String, String> oAuthParams) {
Retrofit adapterOauth = null;
try {
SSLSocketFactory NoSSLv3Factory = new NoSSLv3SocketFactory(new URL(FLICKR_API_URL));
adapterOauth = new Retrofit.Builder().addConverterFactory(ScalarsConverterFactory.create()).baseUrl(FLICKR_API_URL).client(new OkHttpClient().newBuilder().sslSocketFactory(NoSSLv3Factory).addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Request newRequest = request.newBuilder().addHeader("Authorization", FlickrSecurityUtils.getAuthorizationHeader(oAuthParams)).build();
return chain.proceed(newRequest);
}
}).build()).build();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return adapterOauth;
}
use of com.pushtorefresh.storio3.Interceptor in project osm-contributor by jawg.
the class FlickrUploadUtils method getRestAdapter.
public static Retrofit getRestAdapter(final Map<String, String> oAuthParams) {
if (adapter == null) {
try {
SSLSocketFactory NoSSLv3Factory = new NoSSLv3SocketFactory(new URL("https://up.flickr.com/services"));
adapter = new Retrofit.Builder().addConverterFactory(ScalarsConverterFactory.create()).baseUrl("https://up.flickr.com/services/").client(new okhttp3.OkHttpClient().newBuilder().sslSocketFactory(NoSSLv3Factory).addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Request newRequest = request.newBuilder().addHeader("Authorization", FlickrSecurityUtils.getAuthorizationHeader(oAuthParams)).build();
return chain.proceed(newRequest);
}
}).build()).build();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
}
return adapter;
}
Aggregations