Skip to content

Commit 31d6a0e

Browse files
tfmorrisAbbe98
authored andcommitted
Refactor URL building
1 parent a990e32 commit 31d6a0e

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import com.google.common.collect.Iterators;
1010

1111
import okhttp3.HttpUrl;
12-
import okhttp3.OkHttpClient;
1312
import okhttp3.Request;
1413
import okhttp3.Response;
1514

@@ -19,18 +18,17 @@
1918
public class FileFetcher implements Iterator<JsonNode>{
2019
String apiUrl;
2120
String categoryName;
22-
boolean subcategories;
23-
HttpUrl urlBase;
24-
HttpUrl urlContinue;
21+
String cmType;
2522
JsonNode callResults;
2623
private int indexRow = 0;
2724
String cmcontinue;
2825

2926
public FileFetcher(String apiUrl, String categoryName, boolean subcategories) {
3027
this.apiUrl = apiUrl;
3128
this.categoryName = categoryName;
32-
this.subcategories = subcategories;
29+
this.cmType = subcategories ? "subcat" : "file";
3330
try {
31+
// TODO: it's weird that this uses categoryName as a parameter, but subcategories implicitly from the field
3432
getCallResults(categoryName);
3533
} catch (IOException e) {
3634
throw new UncheckedIOException(e);
@@ -43,18 +41,22 @@ public FileFetcher(String apiUrl, String categoryName, boolean subcategories) {
4341
*/
4442
public void getCallResults(String category) throws IOException {
4543

46-
urlBase = HttpUrl.parse(apiUrl).newBuilder()
44+
HttpUrl urlBase = buildBaseUrl(category, cmType);
45+
JsonNode jsonNode = getJson(urlBase);
46+
callResults = jsonNode.path("query").path("categorymembers");
47+
cmcontinue = jsonNode.path("continue").path("cmcontinue").asText();
48+
49+
}
50+
51+
private HttpUrl buildBaseUrl(String category, String cmtype) {
52+
return HttpUrl.parse(apiUrl).newBuilder()
4753
.addQueryParameter("action", "query")
4854
.addQueryParameter("list", "categorymembers")
4955
.addQueryParameter("cmtitle", category)
50-
.addQueryParameter("cmtype", subcategories ? "subcat":"file")
56+
.addQueryParameter("cmtype", cmtype)
5157
.addQueryParameter("cmprop", "title|type|ids")
5258
.addQueryParameter("cmlimit", "500")
5359
.addQueryParameter("format", "json").build();
54-
JsonNode jsonNode = getJson(urlBase);
55-
callResults = jsonNode.path("query").path("categorymembers");
56-
cmcontinue = jsonNode.path("continue").path("cmcontinue").asText();
57-
5860
}
5961

6062
/**
@@ -172,7 +174,7 @@ public JsonNode next() {
172174
indexRow++;
173175

174176
if ((indexRow == callResults.size()) && !cmcontinue.isBlank()) {
175-
urlContinue = HttpUrl.parse(urlBase.toString()).newBuilder()
177+
HttpUrl urlContinue = buildBaseUrl(categoryName, cmType).newBuilder()
176178
.addQueryParameter("cmcontinue", cmcontinue).build();
177179
try {
178180
getContinuationResults(urlContinue);

0 commit comments

Comments
 (0)