|
|
@ -13,8 +13,6 @@ import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
|
|
|
|
import com.todoroo.andlib.service.RestClient;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("nls")
|
|
|
|
@SuppressWarnings("nls")
|
|
|
|
public class ProducteevInvoker {
|
|
|
|
public class ProducteevInvoker {
|
|
|
|
|
|
|
|
|
|
|
@ -319,7 +317,7 @@ public class ProducteevInvoker {
|
|
|
|
|
|
|
|
|
|
|
|
// --- invocation
|
|
|
|
// --- invocation
|
|
|
|
|
|
|
|
|
|
|
|
private final RestClient restClient = new ProducteevRestClient();
|
|
|
|
private final ProducteevRestClient restClient = new ProducteevRestClient();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Invokes authenticated method using HTTP GET. Will retry after re-authenticating if service exception encountered
|
|
|
|
* Invokes authenticated method using HTTP GET. Will retry after re-authenticating if service exception encountered
|
|
|
@ -334,19 +332,28 @@ public class ProducteevInvoker {
|
|
|
|
throws IOException, ApiServiceException {
|
|
|
|
throws IOException, ApiServiceException {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
String request = createFetchUrl(method, getParameters);
|
|
|
|
String request = createFetchUrl(method, getParameters);
|
|
|
|
String response;
|
|
|
|
String response = null;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
response = restClient.get(request);
|
|
|
|
response = restClient.get(request);
|
|
|
|
} catch (ApiServiceException e) {
|
|
|
|
} catch (ApiSignatureException e) {
|
|
|
|
String oldToken = getToken();
|
|
|
|
// clear cookies, get new token, retry
|
|
|
|
System.err.println("PDV: retrying due to exception: " + e);
|
|
|
|
for(int retry = 0; retry < 2; retry++) {
|
|
|
|
authenticate(retryEmail, retryPassword);
|
|
|
|
String oldToken = token;
|
|
|
|
for(int i = 0; i < getParameters.length; i++)
|
|
|
|
restClient.reset();
|
|
|
|
if(oldToken.equals(getParameters[i]))
|
|
|
|
authenticate(retryEmail, retryPassword);
|
|
|
|
getParameters[i] = getToken();
|
|
|
|
for(int i = 0; i < getParameters.length; i++)
|
|
|
|
|
|
|
|
if(oldToken.equals(getParameters[i])) {
|
|
|
|
request = createFetchUrl(method, getParameters);
|
|
|
|
getParameters[i] = getToken();
|
|
|
|
response = restClient.get(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
request = createFetchUrl(method, getParameters);
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
response = restClient.get(request);
|
|
|
|
|
|
|
|
} catch (ApiSignatureException newException) {
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(response == null)
|
|
|
|
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(response.startsWith("DEBUG MESSAGE")) {
|
|
|
|
if(response.startsWith("DEBUG MESSAGE")) {
|
|
|
|
System.err.println(response);
|
|
|
|
System.err.println(response);
|
|
|
@ -369,13 +376,14 @@ public class ProducteevInvoker {
|
|
|
|
* Name/Value pairs. Values will be URL encoded.
|
|
|
|
* Name/Value pairs. Values will be URL encoded.
|
|
|
|
* @return response object
|
|
|
|
* @return response object
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private JSONObject invokeGet(String method, Object... getParameters)
|
|
|
|
JSONObject invokeGet(String method, Object... getParameters) throws IOException, ApiServiceException {
|
|
|
|
throws IOException, ApiServiceException {
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
String request = createFetchUrl(method, getParameters);
|
|
|
|
String request = createFetchUrl(method, getParameters);
|
|
|
|
String response = restClient.get(request);
|
|
|
|
String response = restClient.get(request);
|
|
|
|
if(response.startsWith("DEBUG MESSAGE"))
|
|
|
|
if(response.startsWith("DEBUG MESSAGE")) {
|
|
|
|
throw new ApiServiceException(response);
|
|
|
|
System.err.println(response);
|
|
|
|
|
|
|
|
return new JSONObject();
|
|
|
|
|
|
|
|
}
|
|
|
|
return new JSONObject(response);
|
|
|
|
return new JSONObject(response);
|
|
|
|
} catch (JSONException e) {
|
|
|
|
} catch (JSONException e) {
|
|
|
|
throw new ApiResponseParseException(e);
|
|
|
|
throw new ApiResponseParseException(e);
|
|
|
@ -392,7 +400,7 @@ public class ProducteevInvoker {
|
|
|
|
* @throws UnsupportedEncodingException
|
|
|
|
* @throws UnsupportedEncodingException
|
|
|
|
* @throws NoSuchAlgorithmException
|
|
|
|
* @throws NoSuchAlgorithmException
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public String createFetchUrl(String method, Object... getParameters) throws UnsupportedEncodingException, NoSuchAlgorithmException {
|
|
|
|
String createFetchUrl(String method, Object... getParameters) throws UnsupportedEncodingException, NoSuchAlgorithmException {
|
|
|
|
TreeMap<String, Object> treeMap = new TreeMap<String, Object>();
|
|
|
|
TreeMap<String, Object> treeMap = new TreeMap<String, Object>();
|
|
|
|
for(int i = 0; i < getParameters.length; i += 2)
|
|
|
|
for(int i = 0; i < getParameters.length; i += 2)
|
|
|
|
treeMap.put(getParameters[i].toString(), getParameters[i+1]);
|
|
|
|
treeMap.put(getParameters[i].toString(), getParameters[i+1]);
|
|
|
|