Skip to content

Commit 0444b42

Browse files
committed
fix: ensure http calls to Wikimedia has an User-agent
Fixes #192
1 parent f7e482a commit 0444b42

3 files changed

Lines changed: 37 additions & 2 deletions

File tree

src/main/java/org/openrefine/extensions/commons/importer/FileFetcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public FileFetcher(String apiUrl, String categoryName, boolean subcategories) {
4343
*/
4444
public void getCallResults(String category) throws IOException {
4545

46-
OkHttpClient client = new OkHttpClient.Builder().build();
46+
OkHttpClient client = HttpClient.getClient();
4747
urlBase = HttpUrl.parse(apiUrl).newBuilder()
4848
.addQueryParameter("action", "query")
4949
.addQueryParameter("list", "categorymembers")
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.openrefine.extensions.commons.importer;
2+
3+
import java.io.IOException;
4+
5+
import okhttp3.Interceptor;
6+
import okhttp3.OkHttpClient;
7+
import okhttp3.Request;
8+
import okhttp3.Response;
9+
10+
public class HttpClient {
11+
12+
private static final String USER_AGENT = "OpenRefine-Commons-Extension/0.1.* (https://github.com/OpenRefine/CommonsExtension)";
13+
14+
public static OkHttpClient getClient() {
15+
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new UserAgentInterceptor(USER_AGENT)).build();
16+
return client;
17+
}
18+
19+
static class UserAgentInterceptor implements Interceptor {
20+
21+
private final String userAgent;
22+
23+
public UserAgentInterceptor(String userAgent) {
24+
this.userAgent = userAgent;
25+
}
26+
27+
@Override
28+
public Response intercept(Chain chain) throws IOException {
29+
return chain.proceed(chain.request().newBuilder()
30+
.header("User-Agent", userAgent)
31+
.build());
32+
}
33+
}
34+
private static OkHttpClient client;
35+
}

src/main/java/org/openrefine/extensions/commons/importer/RelatedCategoryFetcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public List<List<String>> getRelatedCategories(List <FileRecord> fileRecordOrigi
6060
while (titlesIndex < fileRecordOriginal.size()) {
6161
titles += "|" + fileRecordOriginal.get(titlesIndex++).fileName;
6262
}
63-
OkHttpClient client = new OkHttpClient.Builder().build();
63+
OkHttpClient client = HttpClient.getClient();
6464
HttpUrl urlRelatedCategoriesBase = HttpUrl.parse(apiUrl).newBuilder()
6565
.addQueryParameter("action", "query")
6666
.addQueryParameter("prop", "categories")

0 commit comments

Comments
 (0)