Brion VIBBER has submitted this change and it was merged.

Change subject: Add code to actuallty perform API request when asked
......................................................................


Add code to actuallty perform API request when asked

Change-Id: Ieeec4db349389371fc40298f25cc2b5c613c3438
---
M src/main/java/org/mediawiki/api/Api.java
M src/main/java/org/mediawiki/api/RequestBuilder.java
M src/test/java/org/mediawiki/api/ApiTests.java
3 files changed, 82 insertions(+), 16 deletions(-)

Approvals:
  Brion VIBBER: Verified; Looks good to me, approved



diff --git a/src/main/java/org/mediawiki/api/Api.java 
b/src/main/java/org/mediawiki/api/Api.java
index ef6d9fa..a39537e 100644
--- a/src/main/java/org/mediawiki/api/Api.java
+++ b/src/main/java/org/mediawiki/api/Api.java
@@ -1,5 +1,9 @@
 package org.mediawiki.api;
 
+import com.github.kevinsawicki.http.HttpRequest;
+import org.json.JSONException;
+import org.json.JSONObject;
+
 import java.net.MalformedURLException;
 import java.net.URL;
 
@@ -10,6 +14,15 @@
  * - Uses JSON for everything
  */
 public class Api {
+
+    /**
+     * Parameter to {@link #makeRequest(int, RequestBuilder)}, performs GET 
request.
+     */
+    public static final int METHOD_GET = 1;
+    /**
+     * Parameter to {@link #makeRequest(int, RequestBuilder)}, performs POST 
request.
+     */
+    public static final int METHOD_POST = 2;
 
     /**
      * The exact URL to which API requests are made.
@@ -80,7 +93,38 @@
      * @return A {@link RequestBuilder} instance that can be used to add 
parameters & execute the request.
      */
     public RequestBuilder action(final String action) {
-        return null;
+        return new RequestBuilder(this, action);
+    }
+
+    /**
+     * Performs the HTTP request from the given requestBuilder, with the given 
HTTP method.
+     *
+     * Supports GET, POST and HEAD only currently, since that is all that the 
MW API supports.
+     *
+     * TODO: Figure out how to do error handling (network!)
+     *
+     * @param method HTTP method to use when performing the request
+     * @param requestBuilder The requestBuilder to use to construct the request
+     * @return A JSONObject with the response from the server
+     */
+    public JSONObject makeRequest(final int method, final RequestBuilder 
requestBuilder) {
+        HttpRequest request;
+        switch(method) {
+            case METHOD_GET:
+                request = HttpRequest.get(getApiUrl().toString(), 
requestBuilder.getParams(), true);
+                break;
+            case METHOD_POST:
+                request = HttpRequest.post(getApiUrl().toString(), 
requestBuilder.getParams(), true);
+                break;
+            default:
+                throw new IllegalArgumentException("Unkown argument passed for 
parameter method");
+        }
+        try {
+            return new JSONObject(request.body());
+        } catch (JSONException e) {
+            // Well, if the request *does* complete successfully but the
+            throw new RuntimeException(e);
+        }
     }
 }
 
diff --git a/src/main/java/org/mediawiki/api/RequestBuilder.java 
b/src/main/java/org/mediawiki/api/RequestBuilder.java
index 4ecfb44..a338ac2 100644
--- a/src/main/java/org/mediawiki/api/RequestBuilder.java
+++ b/src/main/java/org/mediawiki/api/RequestBuilder.java
@@ -2,17 +2,40 @@
 
 import org.json.JSONObject;
 
+import java.util.HashMap;
+
 /**
  * Fluent interface to easily build up an API request from params.
  */
 public class RequestBuilder {
     /**
+     * Hashmap used to hold the parameters with which to make the API call.
+     */
+    private final HashMap<String, String> params;
+
+    /**
+     * Api object with which the request being built is associated.
+     */
+    private final Api api;
+
+    /**
      * Create a new RequestBuilder to build API requests.
      *
-     * @param api The Api that will be used to perform the request.
+     * @param apiToUse The Api that will be used to perform the request.
      * @param action The action this API query is for
      */
-    RequestBuilder(final Api api, final String action) {
+    RequestBuilder(final Api apiToUse, final String action) {
+        this.api = apiToUse;
+        params = new HashMap<String, String>();
+        params.put("format", "json"); // Force everything to be JSON
+        params.put("action", action);
+    }
+
+    /**
+     * @return A copy of the curent set of parameters for this request
+     */
+    public HashMap<String, String> getParams() {
+        return new HashMap<String, String>(params);
     }
 
     /**
@@ -23,7 +46,8 @@
      * @return The `this` object, so you can chain params together
      */
     public RequestBuilder param(final String key, final String value) {
-        return null;
+        params.put(key, value);
+        return this;
     }
 
     /**
@@ -32,8 +56,8 @@
      * @param method HTTP Method to use when performing the request
      * @return The result of the API request
      */
-    private JSONObject makeRequest(final String method) {
-        return null;
+    private JSONObject makeRequest(final int method) {
+        return api.makeRequest(method, this);
     }
 
     /**
@@ -42,7 +66,7 @@
      * @return The result of the API request
      */
     public JSONObject get() {
-        return null;
+        return makeRequest(Api.METHOD_GET);
     }
 
     /**
@@ -51,6 +75,6 @@
      * @return The result of the API request
      */
     public JSONObject post() {
-        return null;
+        return makeRequest(Api.METHOD_POST);
     }
 }
diff --git a/src/test/java/org/mediawiki/api/ApiTests.java 
b/src/test/java/org/mediawiki/api/ApiTests.java
index fbf01c8..80a6548 100644
--- a/src/test/java/org/mediawiki/api/ApiTests.java
+++ b/src/test/java/org/mediawiki/api/ApiTests.java
@@ -10,18 +10,16 @@
  */
 public class ApiTests {
 
-    Api api;
-
-    @Before
-    public void setUp() throws Exception {
-        api = new Api("test.wikipedia.org");
-    }
-
     @Test
     public void testBasicParse() throws Exception {
+        Api api = new Api("test.wikipedia.org");
         String inputText = "Test String";
         String inputTitle = "Test Title";
-        JSONObject resp = api.action("parse").param("title", 
inputTitle).param("text", inputText).param("prop", "wikitect").get();
+        JSONObject resp = api.action("parse")
+                .param("title", inputTitle)
+                .param("text", inputText)
+                .param("prop", "wikitext")
+                .get();
         
assertEquals(resp.optJSONObject("parse").optJSONObject("wikitext").optString("*"),
 inputText);
     }
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/91656
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ieeec4db349389371fc40298f25cc2b5c613c3438
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

Reply via email to