Brion VIBBER has uploaded a new change for review.
https://gerrit.wikimedia.org/r/93097
Change subject: Split ApiResult into ApiRequest and ApiResult
......................................................................
Split ApiResult into ApiRequest and ApiResult
This'll let us create and test ApiResult objects independently of the
networking code,
while still giving us an ApiRequest we can keep around to cancel from another
thread.
Change-Id: I445db45848a8f2ff5054a350d4bcef9ec9db7fb9
---
M src/main/java/org/mediawiki/api/json/Api.java
A src/main/java/org/mediawiki/api/json/ApiRequest.java
M 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
5 files changed, 78 insertions(+), 35 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/apps/android/java-mwapi
refs/changes/97/93097/1
diff --git a/src/main/java/org/mediawiki/api/json/Api.java
b/src/main/java/org/mediawiki/api/json/Api.java
index 1ca5da1..28fa058 100644
--- a/src/main/java/org/mediawiki/api/json/Api.java
+++ b/src/main/java/org/mediawiki/api/json/Api.java
@@ -106,7 +106,7 @@
* @param requestBuilder The requestBuilder to use to construct the request
* @return An {@link ApiResult} object which can be used to get the result
of this query.
*/
- public ApiResult setupRequest(final int method, final RequestBuilder
requestBuilder) {
+ public ApiRequest setupRequest(final int method, final RequestBuilder
requestBuilder) {
HttpRequest request;
switch(method) {
case METHOD_GET:
@@ -118,7 +118,7 @@
default:
throw new IllegalArgumentException("Unkown argument passed for
parameter method");
}
- return new ApiResult(request);
+ return new ApiRequest(request);
}
}
diff --git a/src/main/java/org/mediawiki/api/json/ApiRequest.java
b/src/main/java/org/mediawiki/api/json/ApiRequest.java
new file mode 100644
index 0000000..a33f83d
--- /dev/null
+++ b/src/main/java/org/mediawiki/api/json/ApiRequest.java
@@ -0,0 +1,46 @@
+package org.mediawiki.api.json;
+
+import com.github.kevinsawicki.http.HttpRequest;
+
+/**
+ * Encapsulates the action of performing an API call.
+ *
+ * The network request is made *only* when {@link #send()} is called.
+ */
+public class ApiRequest {
+ /**
+ * 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.
+ */
+ ApiRequest(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 an ApiResult.
+ *
+ * @return A {@link ApiResult} 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 ApiResult send() throws ApiException {
+ try {
+ String body = request.body();
+ return new ApiResult(body);
+ } catch (HttpRequest.HttpRequestException e) {
+ throw new ApiException(e.getCause());
+ }
+ }
+}
diff --git a/src/main/java/org/mediawiki/api/json/ApiResult.java
b/src/main/java/org/mediawiki/api/json/ApiResult.java
index 01a2b29..f5287f9 100644
--- a/src/main/java/org/mediawiki/api/json/ApiResult.java
+++ b/src/main/java/org/mediawiki/api/json/ApiResult.java
@@ -7,30 +7,20 @@
/**
* 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;
+ private final String responseBody;
/**
* 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.
+ * @param responseBody string containing a legit JSON response body
*/
- ApiResult(final HttpRequest request) {
- this.request = request;
- }
-
- /**
- * Disconnect the request if it is in progress.
- */
- public void cancel() {
- request.disconnect();
+ ApiResult(final String responseBody) {
+ this.responseBody = responseBody;
}
/**
@@ -43,7 +33,7 @@
*/
public JSONArray asArray() throws ApiException {
try {
- return new JSONArray(request.body());
+ return new JSONArray(responseBody);
} catch (JSONException e) {
throw new ApiException(e);
} catch (HttpRequest.HttpRequestException e) {
@@ -60,7 +50,7 @@
*/
public JSONObject asObject() throws ApiException {
try {
- return new JSONObject(request.body());
+ return new JSONObject(responseBody);
} catch (JSONException e) {
throw new ApiException(e);
} catch (HttpRequest.HttpRequestException e) {
diff --git a/src/main/java/org/mediawiki/api/json/RequestBuilder.java
b/src/main/java/org/mediawiki/api/json/RequestBuilder.java
index 7a0fd17..c7e3ea3 100644
--- a/src/main/java/org/mediawiki/api/json/RequestBuilder.java
+++ b/src/main/java/org/mediawiki/api/json/RequestBuilder.java
@@ -51,36 +51,37 @@
/**
* 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.
+ * Use {@link org.mediawiki.api.json.ApiRequest#send()} to actually
+ * start the network request and get the response.
*
* @param method HTTP Method to use when performing the request
- * @return An {@link ApiResult} object which can be used to get the result
of this query.
+ * @return An {@link ApiRequest} object which can be used to get the
result of this query.
*/
- private ApiResult setupRequest(final int method) {
+ private ApiRequest setupRequest(final int method) {
return api.setupRequest(method, this);
}
/**
* 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.
*
- * @return An {@link ApiResult} object which can be used to get the result
of this query.
+ * Use {@link org.mediawiki.api.json.ApiRequest#send()} to actually
+ * start the network request and get the response.
+ *
+ * @return An {@link ApiRequest} object which can be used to get the
result of this query.
*/
- public ApiResult get() {
+ public ApiRequest get() {
return setupRequest(Api.METHOD_GET);
}
/**
* Sets up a POST 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.
+ * Use {@link org.mediawiki.api.json.ApiRequest#send()} 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.
+ * @return An {@link ApiRequest} object which can be used to get the
result of this query.
*/
- public ApiResult post() {
+ public ApiRequest 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 0a5a67c..af2cdb7 100644
--- a/src/test/java/org/mediawiki/api/json/ApiTest.java
+++ b/src/test/java/org/mediawiki/api/json/ApiTest.java
@@ -24,7 +24,9 @@
JSONObject resp = api.action("login")
.param("lgname", testUsername)
.param("lgpassword", testPassword)
- .post().asObject();
+ .post()
+ .send()
+ .asObject();
assertEquals(resp.optJSONObject("login").optString("result"),
"NeedToken");
assertNull(resp.optJSONObject("error"));
}
@@ -33,7 +35,9 @@
public void testWrongMethod() throws Exception {
Api api = new Api("test.wikipedia.org");
JSONObject resp = api.action("login")
- .get().asObject();
+ .get()
+ .send()
+ .asObject();
assertEquals(resp.optJSONObject("error").optString("code"),
"mustbeposted");
}
@@ -47,7 +51,9 @@
.param("title", inputTitle)
.param("text", inputText)
.param("prop", "wikitext")
- .get().asObject();
+ .get()
+ .send()
+ .asObject();
assertEquals(resp.optJSONObject("parse").optJSONObject("wikitext").optString("*"),
inputText);
}
@@ -69,7 +75,7 @@
public void testJSONException() {
Api api = new Api("test.wikipedia.org");
try {
- api.action("somethingdoesnmtatter").param("format",
"xml").get().asObject();
+ api.action("somethingdoesnmtatter").param("format",
"xml").get().send().asObject();
} catch (ApiException e) {
assertTrue(e.getCause() instanceof JSONException);
return;
@@ -88,7 +94,7 @@
public void testNetworkException() {
Api api = new Api("blashblahblahdoesnotexist");
try {
- api.action("somethingdoesnmtatter").param("format",
"xml").get().asObject();
+ api.action("somethingdoesnmtatter").param("format",
"xml").get().send().asObject();
} catch (ApiException e) {
assertTrue(e.getCause() instanceof IOException);
return;
--
To view, visit https://gerrit.wikimedia.org/r/93097
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I445db45848a8f2ff5054a350d4bcef9ec9db7fb9
Gerrit-PatchSet: 1
Gerrit-Project: apps/android/java-mwapi
Gerrit-Branch: master
Gerrit-Owner: Brion VIBBER <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits