use of okhttp3.Dispatcher in project OpenRefine by OpenRefine.
the class ExtendDataOperationTests method SetUp.
@BeforeMethod
public void SetUp() throws IOException, ModelException {
OperationRegistry.registerOperation(getCoreModule(), "extend-reconciled-data", ExtendDataOperation.class);
project = createProjectWithColumns("DataExtensionTests", "country");
options = mock(Properties.class);
engine = new Engine(project);
engine_config = EngineConfig.reconstruct(ENGINE_JSON_URLS);
engine.initializeFromConfig(engine_config);
engine.setMode(Engine.Mode.RowBased);
Row row = new Row(2);
row.setCell(0, reconciledCell("Iran", "Q794"));
project.rows.add(row);
row = new Row(2);
row.setCell(0, reconciledCell("Japan", "Q17"));
project.rows.add(row);
row = new Row(2);
row.setCell(0, reconciledCell("Tajikistan", "Q863"));
project.rows.add(row);
row = new Row(2);
row.setCell(0, reconciledCell("United States of America", "Q30"));
project.rows.add(row);
dispatcher = new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
String json = URLDecoder.decode(request.getBody().readUtf8().split("=")[1], StandardCharsets.UTF_8);
JsonNode parsedQuery;
try {
parsedQuery = ParsingUtilities.mapper.readTree(json);
} catch (IOException e) {
throw new IllegalArgumentException("HTTP call with invalid JSON payload: " + json);
}
if (mockedResponses.containsKey(parsedQuery)) {
return new MockResponse().setResponseCode(200).setBody(mockedResponses.get(parsedQuery));
} else {
throw new IllegalArgumentException("HTTP call not mocked for query: " + json);
}
}
};
}
use of okhttp3.Dispatcher in project OpenRefine by OpenRefine.
the class ExtendDataOperationTests method testFetchCurrent.
/**
* Test fetch only the best statements
*/
@Test
public void testFetchCurrent() throws Exception {
DataExtensionConfig extension = DataExtensionConfig.reconstruct("{\"properties\":[{\"id\":\"P38\",\"name\":\"currency\",\"settings\":{\"rank\":\"best\"}}]}");
mockHttpCall("{\"ids\":[\"Q863\",\"Q794\",\"Q17\",\"Q30\"],\"properties\":[{\"id\":\"P38\",\"settings\":{\"rank\":\"best\"}}]}", "{\"rows\":{" + " \"Q794\": {\"P38\": [{\"name\": \"Iranian rial\", \"id\": \"Q188608\"}]}," + " \"Q863\": {\"P38\": [{\"name\": \"Tajikistani somoni\", \"id\": \"Q199886\"}]}," + " \"Q30\": {\"P38\": [{\"name\": \"United States dollar\", \"id\": \"Q4917\"}]}," + " \"Q17\": {\"P38\": [{\"name\": \"Japanese yen\", \"id\": \"Q8146\"}]}" + "}, \"meta\": [" + " {\"settings\": {\"rank\": \"best\"}, \"name\": \"currency\", \"id\": \"P38\"}" + "]}");
try (MockWebServer server = new MockWebServer()) {
server.start();
server.setDispatcher(dispatcher);
EngineDependentOperation op = new ExtendDataOperation(engine_config, "country", server.url("/reconcile").url().toString(), RECON_IDENTIFIER_SPACE, RECON_SCHEMA_SPACE, extension, 1);
LongRunningProcessStub process = new LongRunningProcessStub(op.createProcess(project, options));
process.run();
/*
* Tajikistan has one "preferred" currency and one "normal" one (in terms of statement ranks). But thanks to
* our setting in the extension configuration, we only fetch the current one, so the one just after it is
* the one for the US (USD).
*/
Assert.assertTrue("Tajikistani somoni".equals(project.rows.get(2).getCellValue(1)));
Assert.assertTrue("United States dollar".equals(project.rows.get(3).getCellValue(1)));
// Make sure all the values are reconciled
Assert.assertTrue(project.columnModel.getColumnByName("currency").getReconStats().matchedTopics == 4);
}
}
use of okhttp3.Dispatcher in project OpenRefine by OpenRefine.
the class ExtendDataOperationTests method testFetchRecord.
/**
* Test fetch records (multiple values per reconciled cell)
*/
@Test
public void testFetchRecord() throws Exception {
DataExtensionConfig extension = DataExtensionConfig.reconstruct("{\"properties\":[{\"id\":\"P38\",\"name\":\"currency\",\"settings\":{\"rank\":\"any\"}}]}");
mockHttpCall("{\"ids\":[\"Q863\",\"Q794\",\"Q17\",\"Q30\"],\"properties\":[{\"id\":\"P38\",\"settings\":{\"rank\":\"any\"}}]}", "{\"rows\": {" + " \"Q794\": {\"P38\": [{\"name\": \"Iranian rial\", \"id\": \"Q188608\"}]}," + " \"Q863\": {\"P38\": [{\"name\": \"Tajikistani somoni\", \"id\": \"Q199886\"}, {\"name\": \"Tajikistani ruble\", \"id\": \"Q2423956\"}]}," + " \"Q30\": {\"P38\": [{\"name\": \"United States dollar\", \"id\": \"Q4917\"}]}," + " \"Q17\": {\"P38\": [{\"name\": \"Japanese yen\", \"id\": \"Q8146\"}]}" + "}," + "\"meta\": [" + " {\"settings\": {\"rank\": \"any\"}, \"name\": \"currency\", \"id\": \"P38\"}" + "]}");
try (MockWebServer server = new MockWebServer()) {
server.start();
server.setDispatcher(dispatcher);
EngineDependentOperation op = new ExtendDataOperation(engine_config, "country", server.url("/reconcile").url().toString(), RECON_IDENTIFIER_SPACE, RECON_SCHEMA_SPACE, extension, 1);
LongRunningProcessStub process = new LongRunningProcessStub(op.createProcess(project, options));
process.run();
/*
* Tajikistan has one "preferred" currency and one "normal" one (in terms of statement ranks). The second
* currency is fetched as well, which creates a record (the cell to the left of it is left blank).
*/
Assert.assertTrue("Tajikistani somoni".equals(project.rows.get(2).getCellValue(1)), "Bad currency name for Tajikistan");
Assert.assertTrue("Tajikistani ruble".equals(project.rows.get(3).getCellValue(1)), "Bad currency name for Tajikistan");
Assert.assertTrue(null == project.rows.get(3).getCellValue(0));
// Make sure all the values are reconciled
Assert.assertTrue(project.columnModel.getColumnByName("currency").getReconStats().matchedTopics == 5);
}
}
use of okhttp3.Dispatcher in project alluxio by Alluxio.
the class KodoUnderFileSystem method initializeKodoClientConfig.
private static Builder initializeKodoClientConfig(UnderFileSystemConfiguration conf) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
Dispatcher dispatcher = new Dispatcher();
dispatcher.setMaxRequests(conf.getInt(PropertyKey.UNDERFS_KODO_REQUESTS_MAX));
builder.connectTimeout(conf.getMs(PropertyKey.UNDERFS_KODO_CONNECT_TIMEOUT), TimeUnit.SECONDS);
return builder;
}
use of okhttp3.Dispatcher in project AndLang by wugemu.
the class HttpU method newOkHttpClient.
public OkHttpClient newOkHttpClient() {
HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor(new HttpLogger());
logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
if (openDns) {
return // 设置读取超时时间
new OkHttpClient.Builder().dispatcher(new Dispatcher(ExecutorServiceUtil.getInstence().getExecutorService())).readTimeout(10, TimeUnit.SECONDS).writeTimeout(10, // 设置写的超时时间
TimeUnit.SECONDS).connectTimeout(20, // 设置连接超时时间
TimeUnit.SECONDS).addNetworkInterceptor(// 添加网络请求日志
logInterceptor).dns(new HttpDns()).build();
} else if (openLog) {
return // 设置读取超时时间
new OkHttpClient.Builder().dispatcher(new Dispatcher(ExecutorServiceUtil.getInstence().getExecutorService())).readTimeout(10, TimeUnit.SECONDS).writeTimeout(10, // 设置写的超时时间
TimeUnit.SECONDS).connectTimeout(20, // 设置连接超时时间
TimeUnit.SECONDS).addNetworkInterceptor(// 添加网络请求日志
logInterceptor).build();
} else if (BaseLangUtil.isHaveSDPer()) {
// 开启本地存储权限后使用缓存机制
// 新建一个cache,指定目录为外部目录下的okhttp_cache目录,大小为100M
Cache cache = new Cache(PicSelUtil.getCacheDir(), 100 * 1024 * 1024);
return // 设置读取超时时间
new OkHttpClient.Builder().dispatcher(new Dispatcher(ExecutorServiceUtil.getInstence().getExecutorService())).readTimeout(10, TimeUnit.SECONDS).writeTimeout(10, // 设置写的超时时间
TimeUnit.SECONDS).connectTimeout(20, // 设置连接超时时间
TimeUnit.SECONDS).cache(// 缓存设置
cache).addInterceptor(// 请求网络拦截
new RequestCacheI()).build();
} else {
// 新建一个cache,指定目录为外部目录下的okhttp_cache目录,大小为100M
return // 设置读取超时时间
new OkHttpClient.Builder().dispatcher(new Dispatcher(ExecutorServiceUtil.getInstence().getExecutorService())).readTimeout(10, TimeUnit.SECONDS).writeTimeout(10, // 设置写的超时时间
TimeUnit.SECONDS).connectTimeout(20, // 设置连接超时时间
TimeUnit.SECONDS).build();
}
}
Aggregations