Skip to content

Commit 0b26279

Browse files
robertvazanTpt
authored andcommitted
Expose exceptions in WbSearchEntitiesAction
1 parent 2b7768a commit 0b26279

File tree

1 file changed

+127
-131
lines changed

1 file changed

+127
-131
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
package org.wikidata.wdtk.wikibaseapi;
2-
31
/*
42
* #%L
53
* Wikidata Toolkit Wikibase API
@@ -20,14 +18,14 @@
2018
* #L%
2119
*/
2220

21+
package org.wikidata.wdtk.wikibaseapi;
22+
2323
import java.io.IOException;
2424
import java.util.ArrayList;
2525
import java.util.HashMap;
2626
import java.util.List;
2727
import java.util.Map;
2828

29-
import org.slf4j.Logger;
30-
import org.slf4j.LoggerFactory;
3129
import org.wikidata.wdtk.wikibaseapi.apierrors.MediaWikiApiErrorException;
3230

3331
import com.fasterxml.jackson.core.JsonProcessingException;
@@ -42,132 +40,130 @@
4240
*/
4341
public class WbSearchEntitiesAction {
4442

45-
private static final Logger LOGGER = LoggerFactory
46-
.getLogger(WbSearchEntitiesAction.class);
47-
48-
/**
49-
* Connection to a Wikibase API.
50-
*/
51-
private final ApiConnection connection;
52-
53-
/**
54-
* Mapper object used for deserializing JSON data.
55-
*/
56-
private final ObjectMapper mapper = new ObjectMapper();
57-
58-
/**
59-
* Creates an object to fetch data from the given ApiConnection. The site
60-
* URI is necessary since it is not contained in the data retrieved from the
61-
* API.
62-
*
63-
* @param connection
64-
* {@link ApiConnection} Object to send the requests
65-
* @param siteUri
66-
* the URI identifying the site that is accessed (usually the
67-
* prefix of entity URIs), e.g.,
68-
* "http://www.wikidata.org/entity/"
69-
*/
70-
public WbSearchEntitiesAction(ApiConnection connection, String siteUri) {
71-
this.connection = connection;
72-
}
73-
74-
public List<WbSearchEntitiesResult> wbSearchEntities(WbGetEntitiesSearchData properties)
75-
throws MediaWikiApiErrorException, IOException {
76-
return wbSearchEntities(properties.search, properties.language,
77-
properties.strictlanguage, properties.type, properties.limit, properties.offset);
78-
}
79-
80-
/**
81-
* Executes the API action "wbsearchentity" for the given parameters.
82-
* Searches for entities using labels and aliases. Returns a label and
83-
* description for the entity in the user language if possible. Returns
84-
* details of the matched term. The matched term text is also present in the
85-
* aliases key if different from the display label.
86-
*
87-
* <p>
88-
* See the <a href=
89-
* "https://www.wikidata.org/w/api.php?action=help&modules=wbsearchentity"
90-
* >online API documentation</a> for further information.
91-
* <p>
92-
*
93-
* @param search
94-
* (required) search for this text
95-
* @param language
96-
* (required) search in this language
97-
* @param strictLanguage
98-
* (optional) whether to disable language fallback
99-
* @param type
100-
* (optional) search for this type of entity
101-
* One of the following values: item, property
102-
* Default: item
103-
* @param limit
104-
* (optional) maximal number of results
105-
* no more than 50 (500 for bots) allowed
106-
* Default: 7
107-
* @param offset
108-
* (optional) offset where to continue a search
109-
* Default: 0
110-
* this parameter is called "continue" in the API (which is a Java keyword)
111-
*
112-
* @return list of matching entities retrieved via the API URL
113-
* @throws MediaWikiApiErrorException
114-
* if the API returns an error
115-
* @throws IllegalArgumentException
116-
* if the given combination of parameters does not make sense
117-
*/
118-
public List<WbSearchEntitiesResult> wbSearchEntities(String search, String language,
119-
Boolean strictLanguage, String type, Long limit, Long offset)
120-
throws MediaWikiApiErrorException, IOException {
121-
122-
Map<String, String> parameters = new HashMap<>();
123-
parameters.put(ApiConnection.PARAM_ACTION, "wbsearchentities");
124-
125-
if (search != null) {
126-
parameters.put("search", search);
127-
} else {
128-
throw new IllegalArgumentException(
129-
"Search parameter must be specified for this action.");
130-
}
131-
132-
if (language != null) {
133-
parameters.put("language", language);
134-
} else {
135-
throw new IllegalArgumentException(
136-
"Language parameter must be specified for this action.");
137-
}
138-
if (strictLanguage != null) {
139-
parameters.put("strictlanguage", Boolean.toString(strictLanguage));
140-
}
141-
142-
if (type != null) {
143-
parameters.put("type", type);
144-
}
145-
146-
if (limit != null) {
147-
parameters.put("limit", Long.toString(limit));
148-
}
149-
150-
if (offset != null) {
151-
parameters.put("continue", Long.toString(offset));
152-
}
153-
154-
List<WbSearchEntitiesResult> results = new ArrayList<>();
155-
156-
JsonNode root = this.connection.sendJsonRequest("POST", parameters);
157-
JsonNode entities = root.path("search");
158-
for (JsonNode entityNode : entities) {
159-
try {
160-
JacksonWbSearchEntitiesResult ed = mapper.treeToValue(entityNode,
161-
JacksonWbSearchEntitiesResult.class);
162-
results.add(ed);
163-
} catch (JsonProcessingException e) {
164-
LOGGER.error("Error when reading JSON for entity "
165-
+ entityNode.path("id").asText("UNKNOWN") + ": "
166-
+ e.toString());
167-
}
168-
}
169-
170-
return results;
171-
}
43+
/**
44+
* Connection to a Wikibase API.
45+
*/
46+
private final ApiConnection connection;
47+
48+
/**
49+
* Mapper object used for deserializing JSON data.
50+
*/
51+
private final ObjectMapper mapper = new ObjectMapper();
52+
53+
/**
54+
* Creates an object to fetch data from the given ApiConnection. The site
55+
* URI is necessary since it is not contained in the data retrieved from the
56+
* API.
57+
*
58+
* @param connection
59+
* {@link ApiConnection} Object to send the requests
60+
* @param siteUri
61+
* the URI identifying the site that is accessed (usually the
62+
* prefix of entity URIs), e.g.,
63+
* "http://www.wikidata.org/entity/"
64+
*/
65+
public WbSearchEntitiesAction(ApiConnection connection, String siteUri) {
66+
this.connection = connection;
67+
}
68+
69+
public List<WbSearchEntitiesResult> wbSearchEntities(WbGetEntitiesSearchData properties)
70+
throws MediaWikiApiErrorException, IOException {
71+
return wbSearchEntities(properties.search, properties.language,
72+
properties.strictlanguage, properties.type, properties.limit, properties.offset);
73+
}
74+
75+
/**
76+
* Executes the API action "wbsearchentity" for the given parameters.
77+
* Searches for entities using labels and aliases. Returns a label and
78+
* description for the entity in the user language if possible. Returns
79+
* details of the matched term. The matched term text is also present in the
80+
* aliases key if different from the display label.
81+
*
82+
* <p>
83+
* See the <a href=
84+
* "https://www.wikidata.org/w/api.php?action=help&modules=wbsearchentity"
85+
* >online API documentation</a> for further information.
86+
* <p>
87+
*
88+
* @param search
89+
* (required) search for this text
90+
* @param language
91+
* (required) search in this language
92+
* @param strictLanguage
93+
* (optional) whether to disable language fallback
94+
* @param type
95+
* (optional) search for this type of entity
96+
* One of the following values: item, property
97+
* Default: item
98+
* @param limit
99+
* (optional) maximal number of results
100+
* no more than 50 (500 for bots) allowed
101+
* Default: 7
102+
* @param offset
103+
* (optional) offset where to continue a search
104+
* Default: 0
105+
* this parameter is called "continue" in the API (which is a Java keyword)
106+
*
107+
* @return list of matching entities retrieved via the API URL
108+
* @throws MediaWikiApiErrorException
109+
* if the API returns an error
110+
* @throws IllegalArgumentException
111+
* if the given combination of parameters does not make sense
112+
* @throws MalformedResponseException
113+
* if response JSON cannot be parsed
114+
*/
115+
public List<WbSearchEntitiesResult> wbSearchEntities(String search, String language,
116+
Boolean strictLanguage, String type, Long limit, Long offset)
117+
throws MediaWikiApiErrorException, IOException {
118+
119+
Map<String, String> parameters = new HashMap<>();
120+
parameters.put(ApiConnection.PARAM_ACTION, "wbsearchentities");
121+
122+
if (search != null) {
123+
parameters.put("search", search);
124+
} else {
125+
throw new IllegalArgumentException(
126+
"Search parameter must be specified for this action.");
127+
}
128+
129+
if (language != null) {
130+
parameters.put("language", language);
131+
} else {
132+
throw new IllegalArgumentException(
133+
"Language parameter must be specified for this action.");
134+
}
135+
if (strictLanguage != null) {
136+
parameters.put("strictlanguage", Boolean.toString(strictLanguage));
137+
}
138+
139+
if (type != null) {
140+
parameters.put("type", type);
141+
}
142+
143+
if (limit != null) {
144+
parameters.put("limit", Long.toString(limit));
145+
}
146+
147+
if (offset != null) {
148+
parameters.put("continue", Long.toString(offset));
149+
}
150+
151+
List<WbSearchEntitiesResult> results = new ArrayList<>();
152+
153+
JsonNode root = this.connection.sendJsonRequest("POST", parameters);
154+
JsonNode entities = root.path("search");
155+
for (JsonNode entityNode : entities) {
156+
try {
157+
JacksonWbSearchEntitiesResult ed = mapper.treeToValue(entityNode,
158+
JacksonWbSearchEntitiesResult.class);
159+
results.add(ed);
160+
} catch (JsonProcessingException e) {
161+
throw new MalformedResponseException(
162+
"Error when reading JSON for entity " + entityNode.path("id").asText("UNKNOWN"), e);
163+
}
164+
}
165+
166+
return results;
167+
}
172168

173169
}

0 commit comments

Comments
 (0)