summaryrefslogtreecommitdiff
path: root/src/org/fox/ttrss/ApiRequest.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/fox/ttrss/ApiRequest.java')
-rw-r--r--src/org/fox/ttrss/ApiRequest.java128
1 files changed, 117 insertions, 11 deletions
diff --git a/src/org/fox/ttrss/ApiRequest.java b/src/org/fox/ttrss/ApiRequest.java
index 3495c737..ce8ba546 100644
--- a/src/org/fox/ttrss/ApiRequest.java
+++ b/src/org/fox/ttrss/ApiRequest.java
@@ -4,6 +4,8 @@ import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
+import java.util.SortedMap;
+import java.util.TreeMap;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
@@ -15,6 +17,7 @@ import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class ApiRequest extends AsyncTask<HashMap<String,String>, Integer, JsonElement> {
@@ -22,24 +25,92 @@ public class ApiRequest extends AsyncTask<HashMap<String,String>, Integer, JsonE
protected String m_sessionId;
protected String m_apiEndpoint;
+ protected String m_login;
+ protected String m_password;
+ protected int m_authStatus;
+ protected Gson m_gson = new Gson();
+
+ protected static final int STATUS_LOGIN_FAILED = 0;
+ protected static final int STATUS_OK = 1;
+ protected static final int STATUS_API_DISABLED = 2;
+ protected static final int STATUS_OTHER_ERROR = 3;
- protected ApiRequest(String sessionId, String apiEndpoint) {
+ protected ApiRequest(String sessionId, String apiEndpoint, String login, String password) {
super();
m_sessionId = sessionId;
m_apiEndpoint = apiEndpoint;
+ m_authStatus = STATUS_OK;
+ m_login = login;
+ m_password = password;
+
+ Log.d(TAG, "initial SID=" + sessionId);
}
- @Override
- protected JsonElement doInBackground(HashMap<String,String>... params) {
-
- Gson gson = new Gson();
-
- String requestStr = gson.toJson(params);
+ protected int tryAuthenticate() {
+ JsonElement result = sendRequest(new HashMap<String,String>() {
+ {
+ put("op", "login");
+ put("user", m_login);
+ put("password", m_password);
+ }
+ });
- // FIXME ugly hack
- requestStr = requestStr.substring(1).substring(0, requestStr.length()-2);
+ if (result != null) {
+ try {
+ JsonObject rv = result.getAsJsonObject();
+
+ int status = rv.get("status").getAsInt();
+
+ if (status == 0) {
+ JsonObject content = rv.get("content").getAsJsonObject();
+ if (content != null) {
+ m_sessionId = content.get("session_id").getAsString();
+
+ Log.d(TAG, "<<< Authentified, sessionId=" + m_sessionId);
+
+ return STATUS_OK;
+ }
+ } else {
+ JsonObject content = rv.get("content").getAsJsonObject();
+
+ if (content != null) {
+ String error = content.get("error").getAsString();
+
+ if (error.equals("LOGIN_ERROR")) {
+ m_sessionId = null;
+ return STATUS_LOGIN_FAILED;
+ } else if (error.equals("API_DISABLED")) {
+ m_sessionId = null;
+ return STATUS_API_DISABLED;
+ }
+ }
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ m_sessionId = null;
+ return STATUS_OTHER_ERROR;
+ }
+
+ protected String getSessionId() {
+ return m_sessionId;
+ }
+
+ protected int getAuthStatus() {
+ return m_authStatus;
+ }
+
+ protected JsonElement sendRequest(HashMap<String,String> param) {
+
+ HashMap<String,String> tmp = new HashMap<String,String>(param);
+
+ if (m_sessionId != null)
+ tmp.put("sid", m_sessionId);
+
+ String requestStr = m_gson.toJson(tmp);
- Log.d(TAG, "executing API request...: " + requestStr + " " + m_apiEndpoint);
+ Log.d(TAG, ">>> (" + requestStr + ") " + m_apiEndpoint);
DefaultHttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(m_apiEndpoint + "/api/");
@@ -60,7 +131,7 @@ public class ApiRequest extends AsyncTask<HashMap<String,String>, Integer, JsonE
response += s;
}
- Log.d(TAG, "Server returned: " + response);
+ Log.d(TAG, "<<< " + response);
JsonParser parser = new JsonParser();
@@ -72,4 +143,39 @@ public class ApiRequest extends AsyncTask<HashMap<String,String>, Integer, JsonE
return null;
}
+
+ @Override
+ protected JsonElement doInBackground(HashMap<String, String>... params) {
+
+ JsonElement result = sendRequest(params[0]);
+
+ try {
+ JsonElement content = result.getAsJsonObject().get("content");
+ int status = result.getAsJsonObject().get("status").getAsInt();
+
+ if (status == 1) {
+ String error = content.getAsJsonObject().get("error").getAsString();
+
+ if (error.equals("NOT_LOGGED_IN")) {
+ Log.d(TAG, "<<< Session invalid, trying to authenticate...");
+
+ m_sessionId = null;
+ m_authStatus = tryAuthenticate();
+
+ if (m_authStatus == STATUS_OK) {
+ result = sendRequest(params[0]);
+
+ return result.getAsJsonObject().get("content");
+ }
+ }
+ } else {
+ return content;
+ }
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ return null;
+ }
}