Search in sources :

Example 41 with Dispatcher

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);
            }
        }
    };
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) JsonNode(com.fasterxml.jackson.databind.JsonNode) Row(com.google.refine.model.Row) IOException(java.io.IOException) Properties(java.util.Properties) Dispatcher(okhttp3.mockwebserver.Dispatcher) Engine(com.google.refine.browsing.Engine) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 42 with Dispatcher

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);
    }
}
Also used : LongRunningProcessStub(com.google.refine.process.LongRunningProcessStub) EngineDependentOperation(com.google.refine.operations.EngineDependentOperation) MockWebServer(okhttp3.mockwebserver.MockWebServer) DataExtensionConfig(com.google.refine.model.recon.ReconciledDataExtensionJob.DataExtensionConfig) RefineTest(com.google.refine.RefineTest) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 43 with Dispatcher

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);
    }
}
Also used : LongRunningProcessStub(com.google.refine.process.LongRunningProcessStub) EngineDependentOperation(com.google.refine.operations.EngineDependentOperation) MockWebServer(okhttp3.mockwebserver.MockWebServer) DataExtensionConfig(com.google.refine.model.recon.ReconciledDataExtensionJob.DataExtensionConfig) RefineTest(com.google.refine.RefineTest) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 44 with Dispatcher

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;
}
Also used : Builder(okhttp3.OkHttpClient.Builder) OkHttpClient(okhttp3.OkHttpClient) Builder(okhttp3.OkHttpClient.Builder) Dispatcher(okhttp3.Dispatcher)

Example 45 with Dispatcher

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();
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Dispatcher(okhttp3.Dispatcher) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Cache(okhttp3.Cache)

Aggregations

Dispatcher (okhttp3.Dispatcher)42 OkHttpClient (okhttp3.OkHttpClient)34 MockResponse (okhttp3.mockwebserver.MockResponse)28 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)26 Dispatcher (okhttp3.mockwebserver.Dispatcher)25 IOException (java.io.IOException)19 Before (org.junit.Before)18 MockWebServer (okhttp3.mockwebserver.MockWebServer)16 Test (org.junit.Test)15 Call (okhttp3.Call)11 LruNormalizedCacheFactory (com.apollographql.apollo.cache.normalized.lru.LruNormalizedCacheFactory)10 ArrayList (java.util.ArrayList)8 Request (okhttp3.Request)8 Response (okhttp3.Response)8 Interceptor (okhttp3.Interceptor)7 ResponseBody (okhttp3.ResponseBody)7 Gson (com.google.gson.Gson)5 CloudantClient (com.cloudant.client.api.CloudantClient)4 RefineTest (com.google.refine.RefineTest)4 DataExtensionConfig (com.google.refine.model.recon.ReconciledDataExtensionJob.DataExtensionConfig)4