Brion VIBBER has submitted this change and it was merged.
Change subject: Encapsulate api results in an object.
......................................................................
Encapsulate api results in an object.
This lets us access them as either Arrays or Objects. Also provides
a simple (untested) API for cancellation.
Change-Id: Iafdcc85d8c8ac030873bfe4e7f3675e8c868c45f
---
M src/main/java/org/mediawiki/api/json/Api.java
A src/main/java/org/mediawiki/api/json/ApiResult.java
M src/main/java/org/mediawiki/api/json/RequestBuilder.java
M src/test/java/org/mediawiki/api/json/ApiTest.java
4 files changed, 103 insertions(+), 38 deletions(-)
Approvals:
Brion VIBBER: Verified; Looks good to me, approved
diff --git a/src/main/java/org/mediawiki/api/json/Api.java
b/src/main/java/org/mediawiki/api/json/Api.java
index 31844be..1ca5da1 100644
--- a/src/main/java/org/mediawiki/api/json/Api.java
+++ b/src/main/java/org/mediawiki/api/json/Api.java
@@ -1,8 +1,6 @@
package org.mediawiki.api.json;
import com.github.kevinsawicki.http.HttpRequest;
-import org.json.JSONException;
-import org.json.JSONObject;
import java.net.MalformedURLException;
import java.net.URL;
@@ -97,18 +95,18 @@
}
/**
- * Performs the HTTP request from the given requestBuilder, with the given
HTTP method.
+ * Sets up the HTTP request that needs to be made to produce results for
this API query.
*
- * Supports GET, POST and HEAD only currently, since that is all that the
MW API supports.
+ * Use {@link org.mediawiki.api.json.ApiResult#asArray()} or {@link
org.mediawiki.api.json.ApiResult#asObject()} to
+ * actually start the network request and get the response.
*
- * TODO: Figure out how to do error handling (network!)
+ * Supports GET and POST only currently, since that is all that the MW API
supports.
*
* @param method HTTP method to use when performing the request
* @param requestBuilder The requestBuilder to use to construct the request
- * @throws ApiException In case of NetworkError or JSON parsing error
- * @return A JSONObject with the response from the server
+ * @return An {@link ApiResult} object which can be used to get the result
of this query.
*/
- public JSONObject makeRequest(final int method, final RequestBuilder
requestBuilder) throws ApiException {
+ public ApiResult setupRequest(final int method, final RequestBuilder
requestBuilder) {
HttpRequest request;
switch(method) {
case METHOD_GET:
@@ -120,13 +118,7 @@
default:
throw new IllegalArgumentException("Unkown argument passed for
parameter method");
}
- try {
- return new JSONObject(request.body());
- } catch (JSONException e) {
- throw new ApiException(e);
- } catch (HttpRequest.HttpRequestException e) {
- throw new ApiException(e.getCause());
- }
+ return new ApiResult(request);
}
}
diff --git a/src/main/java/org/mediawiki/api/json/ApiResult.java
b/src/main/java/org/mediawiki/api/json/ApiResult.java
new file mode 100644
index 0000000..01a2b29
--- /dev/null
+++ b/src/main/java/org/mediawiki/api/json/ApiResult.java
@@ -0,0 +1,70 @@
+package org.mediawiki.api.json;
+
+import com.github.kevinsawicki.http.HttpRequest;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+/**
+ * Encapsulates the result of performing an API call.
+ *
+ * The network request is made *only* when {@link #asArray()} or {@link
#asObject()}
+ * is called.
+ */
+public class ApiResult {
+ /**
+ * Request for which this object holds the results.
+ */
+ private final HttpRequest request;
+
+ /**
+ * Create an APIResult object corresponding to this request object.
+ *
+ * @param request {@link HttpRequest} object which can perform the query
for which this object holds the results.
+ */
+ ApiResult(final HttpRequest request) {
+ this.request = request;
+ }
+
+ /**
+ * Disconnect the request if it is in progress.
+ */
+ public void cancel() {
+ request.disconnect();
+ }
+
+ /**
+ * Start the network request & return the response as a JSON Array.
+ *
+ * Use this when a JSON Array is returned by the API. So far, only
action=opensearch.
+ *
+ * @return A {@link JSONArray} object with the results of the API query.
+ * @throws ApiException Thrown in the case of a network error, or if the
response is not a JSON Array.
+ */
+ public JSONArray asArray() throws ApiException {
+ try {
+ return new JSONArray(request.body());
+ } catch (JSONException e) {
+ throw new ApiException(e);
+ } catch (HttpRequest.HttpRequestException e) {
+ throw new ApiException(e.getCause());
+ }
+ }
+
+ /**
+ * Start the network request & return the response as a JSON Object.
+ *
+ *
+ * @return A {@link JSONObject} object with the results of the API query.
+ * @throws ApiException Thrown in the case of a network error, or if the
response is not a JSON Object.
+ */
+ public JSONObject asObject() throws ApiException {
+ try {
+ return new JSONObject(request.body());
+ } catch (JSONException e) {
+ throw new ApiException(e);
+ } catch (HttpRequest.HttpRequestException e) {
+ throw new ApiException(e.getCause());
+ }
+ }
+}
diff --git a/src/main/java/org/mediawiki/api/json/RequestBuilder.java
b/src/main/java/org/mediawiki/api/json/RequestBuilder.java
index 6130646..7a0fd17 100644
--- a/src/main/java/org/mediawiki/api/json/RequestBuilder.java
+++ b/src/main/java/org/mediawiki/api/json/RequestBuilder.java
@@ -1,7 +1,5 @@
package org.mediawiki.api.json;
-import org.json.JSONObject;
-
import java.util.HashMap;
/**
@@ -51,33 +49,38 @@
}
/**
- * Performs the request that has been built up so far and returns the JSON
Object.
+ * Sets up the request that has been constructed so far.
+ *
+ * Use {@link org.mediawiki.api.json.ApiResult#asArray()} or {@link
org.mediawiki.api.json.ApiResult#asObject()} to
+ * actually start the network request and get the response.
*
* @param method HTTP Method to use when performing the request
- * @throws ApiException In case of NetworkError or JSON parsing error
- * @return The result of the API request
+ * @return An {@link ApiResult} object which can be used to get the result
of this query.
*/
- private JSONObject makeRequest(final int method) throws ApiException {
- return api.makeRequest(method, this);
+ private ApiResult setupRequest(final int method) {
+ return api.setupRequest(method, this);
}
/**
- * Performs a GET request using the parameters so far specified.
+ * Sets up a GET request using the parameters so far specified.
+ * Use {@link org.mediawiki.api.json.ApiResult#asArray()} or {@link
org.mediawiki.api.json.ApiResult#asObject()} to
+ * actually start the network request and get the response.
*
- * @throws ApiException In case of NetworkError or JSON parsing error
- * @return The result of the API request
+ * @return An {@link ApiResult} object which can be used to get the result
of this query.
*/
- public JSONObject get() throws ApiException {
- return makeRequest(Api.METHOD_GET);
+ public ApiResult get() {
+ return setupRequest(Api.METHOD_GET);
}
/**
- * Performs a POST request using the parameters so far specified.
+ * Sets up a POST request using the parameters so far specified.
*
- * @throws ApiException In case of NetworkError or JSON parsing error
- * @return The result of the API request
+ * Use {@link org.mediawiki.api.json.ApiResult#asArray()} or {@link
org.mediawiki.api.json.ApiResult#asObject()} to
+ * actually start the network request and get the response.
+ *
+ * @return An {@link ApiResult} object which can be used to get the result
of this query.
*/
- public JSONObject post() throws ApiException {
- return makeRequest(Api.METHOD_POST);
+ public ApiResult post() {
+ return setupRequest(Api.METHOD_POST);
}
}
diff --git a/src/test/java/org/mediawiki/api/json/ApiTest.java
b/src/test/java/org/mediawiki/api/json/ApiTest.java
index 4fe8fac..0a5a67c 100644
--- a/src/test/java/org/mediawiki/api/json/ApiTest.java
+++ b/src/test/java/org/mediawiki/api/json/ApiTest.java
@@ -24,7 +24,7 @@
JSONObject resp = api.action("login")
.param("lgname", testUsername)
.param("lgpassword", testPassword)
- .post();
+ .post().asObject();
assertEquals(resp.optJSONObject("login").optString("result"),
"NeedToken");
assertNull(resp.optJSONObject("error"));
}
@@ -33,7 +33,7 @@
public void testWrongMethod() throws Exception {
Api api = new Api("test.wikipedia.org");
JSONObject resp = api.action("login")
- .get();
+ .get().asObject();
assertEquals(resp.optJSONObject("error").optString("code"),
"mustbeposted");
}
@@ -47,14 +47,14 @@
.param("title", inputTitle)
.param("text", inputText)
.param("prop", "wikitext")
- .get();
+ .get().asObject();
assertEquals(resp.optJSONObject("parse").optJSONObject("wikitext").optString("*"),
inputText);
}
@Test(expected = IllegalArgumentException.class)
public void testInvalidMethod() throws Exception {
Api api = new Api("test.wikipedia.org");
- api.makeRequest(404, null);
+ api.setupRequest(404, null);
}
/**
@@ -69,7 +69,7 @@
public void testJSONException() {
Api api = new Api("test.wikipedia.org");
try {
- api.action("somethingdoesnmtatter").param("format", "xml").get();
+ api.action("somethingdoesnmtatter").param("format",
"xml").get().asObject();
} catch (ApiException e) {
assertTrue(e.getCause() instanceof JSONException);
return;
@@ -88,7 +88,7 @@
public void testNetworkException() {
Api api = new Api("blashblahblahdoesnotexist");
try {
- api.action("somethingdoesnmtatter").param("format", "xml").get();
+ api.action("somethingdoesnmtatter").param("format",
"xml").get().asObject();
} catch (ApiException e) {
assertTrue(e.getCause() instanceof IOException);
return;
--
To view, visit https://gerrit.wikimedia.org/r/93007
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Iafdcc85d8c8ac030873bfe4e7f3675e8c868c45f
Gerrit-PatchSet: 1
Gerrit-Project: apps/android/java-mwapi
Gerrit-Branch: master
Gerrit-Owner: Yuvipanda <[email protected]>
Gerrit-Reviewer: Brion VIBBER <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits