@@ -43,7 +43,6 @@ public FileFetcher(String apiUrl, String categoryName, boolean subcategories) {
4343 */
4444 public void getCallResults (String category ) throws IOException {
4545
46- OkHttpClient client = HttpClient .getClient ();
4746 urlBase = HttpUrl .parse (apiUrl ).newBuilder ()
4847 .addQueryParameter ("action" , "query" )
4948 .addQueryParameter ("list" , "categorymembers" )
@@ -52,9 +51,7 @@ public void getCallResults(String category) throws IOException {
5251 .addQueryParameter ("cmprop" , "title|type|ids" )
5352 .addQueryParameter ("cmlimit" , "500" )
5453 .addQueryParameter ("format" , "json" ).build ();
55- Request request = new Request .Builder ().url (urlBase ).build ();
56- Response response = client .newCall (request ).execute ();
57- JsonNode jsonNode = new ObjectMapper ().readTree (response .body ().string ());
54+ JsonNode jsonNode = getJson (urlBase );
5855 callResults = jsonNode .path ("query" ).path ("categorymembers" );
5956 cmcontinue = jsonNode .path ("continue" ).path ("cmcontinue" ).asText ();
6057
@@ -64,17 +61,31 @@ public void getCallResults(String category) throws IOException {
6461 * API call when a cmcontinue token is part of the response
6562 * @param urlContinue: URL containing the cmcontinue token
6663 */
67- private void getCallResults (HttpUrl urlContinue ) throws IOException {
64+ private void getContinuationResults (HttpUrl urlContinue ) throws IOException {
6865
69- OkHttpClient client = new OkHttpClient .Builder ().build ();
70- Request request = new Request .Builder ().url (urlContinue ).build ();
71- Response response = client .newCall (request ).execute ();
72- JsonNode jsonNode = new ObjectMapper ().readTree (response .body ().string ());
66+ JsonNode jsonNode = getJson (urlContinue );
7367 callResults = jsonNode .path ("query" ).path ("categorymembers" );
7468 cmcontinue = jsonNode .path ("continue" ).path ("cmcontinue" ).asText ();
7569
7670 }
7771
72+ private JsonNode getJson (HttpUrl url ) throws IOException {
73+
74+ Request request = new Request .Builder ().url (url ).build ();
75+ try (Response response = HttpClient .getClient ().newCall (request ).execute ()) {
76+ if (response .isSuccessful ()) {
77+ if (response .body () != null ) {
78+ return new ObjectMapper ().readTree (response .body ().string ());
79+ } else {
80+ return new ObjectMapper ().readTree ("[]" );
81+ }
82+ } else {
83+ throw new IOException ("API request failed with status code: " + response .code () + ", body: " + response .message ());
84+ }
85+ }
86+
87+ }
88+
7889 /**
7990 * Internal function used to iterate over the paginated results of the MediaWiki API
8091 * when fetching files or categories.
@@ -164,7 +175,7 @@ public JsonNode next() {
164175 urlContinue = HttpUrl .parse (urlBase .toString ()).newBuilder ()
165176 .addQueryParameter ("cmcontinue" , cmcontinue ).build ();
166177 try {
167- getCallResults (urlContinue );
178+ getContinuationResults (urlContinue );
168179 } catch (IOException e ) {
169180 // TODO Auto-generated catch block
170181 e .printStackTrace ();
0 commit comments