use of com.android.volley.DefaultRetryPolicy in project Saiy-PS by brandall76.
the class CreateIDProfile method getID.
/**
* Method to get an enrollment id.
*
* @return an {@link Pair} of which the first parameter will denote success and the second an
* {@link EnrollmentID} object, containing the id. If the request was unsuccessful,
* the second parameter may be null.
*/
public Pair<Boolean, EnrollmentID> getID() {
if (DEBUG) {
MyLog.i(CLS_NAME, "getID");
}
final long then = System.nanoTime();
final JSONObject object = new JSONObject();
try {
object.put(LOCALE_PARAM, "en-us");
} catch (final JSONException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "getID: JSONException");
e.printStackTrace();
}
}
final RequestFuture<JSONObject> future = RequestFuture.newFuture();
final RequestQueue queue = Volley.newRequestQueue(mContext);
queue.start();
final JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST, CREATE_URL, object, future, new Response.ErrorListener() {
@Override
public void onErrorResponse(final VolleyError error) {
if (DEBUG) {
MyLog.w(CLS_NAME, "onErrorResponse: " + error.toString());
CreateIDProfile.this.verboseError(error);
}
queue.stop();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
final Map<String, String> params = new HashMap<>();
params.put(CHARSET, ENCODING);
params.put(JSON_HEADER_ACCEPT, JSON_HEADER_VALUE_ACCEPT);
params.put(OCP_SUBSCRIPTION_KEY_HEADER, apiKey);
return params;
}
};
jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
queue.add(jsonObjReq);
JSONObject response = null;
try {
response = future.get(THREAD_TIMEOUT, TimeUnit.SECONDS);
} catch (final InterruptedException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "getID: InterruptedException");
e.printStackTrace();
}
} catch (final ExecutionException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "getID: ExecutionException");
e.printStackTrace();
}
} catch (final TimeoutException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "getID: TimeoutException");
e.printStackTrace();
}
} finally {
queue.stop();
}
if (DEBUG) {
MyLog.getElapsed(CLS_NAME, then);
}
if (response != null) {
if (DEBUG) {
MyLog.i(CLS_NAME, "response: " + response.toString());
}
final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
final EnrollmentID enrollmentID = gson.fromJson(response.toString(), EnrollmentID.class);
if (DEBUG) {
MyLog.i(CLS_NAME, "onResponse: getId: " + enrollmentID.getId());
}
return new Pair<>(true, enrollmentID);
} else {
if (DEBUG) {
MyLog.w(CLS_NAME, "response: failed");
}
return new Pair<>(false, null);
}
}
use of com.android.volley.DefaultRetryPolicy in project Saiy-PS by brandall76.
the class DeleteIDProfile method delete.
public void delete() {
final long then = System.nanoTime();
String url = null;
try {
url = DELETE_URL + URLEncoder.encode(profileId, Constants.ENCODING_UTF8);
} catch (final UnsupportedEncodingException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "delete: UnsupportedEncodingException");
e.printStackTrace();
}
}
final RequestQueue queue = Volley.newRequestQueue(mContext);
queue.start();
final StringRequest stringRequest = new StringRequest(Request.Method.DELETE, url, new Response.Listener<String>() {
@Override
public void onResponse(final String response) {
if (DEBUG) {
MyLog.i(CLS_NAME, "onResponse: success");
}
queue.stop();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(final VolleyError error) {
if (DEBUG) {
MyLog.w(CLS_NAME, "onErrorResponse: " + error.toString());
DeleteIDProfile.this.verboseError(error);
}
queue.stop();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
final Map<String, String> params = new HashMap<>();
params.put(CHARSET, Constants.ENCODING_UTF8);
params.put(OCP_SUBSCRIPTION_KEY_HEADER, apiKey);
return params;
}
};
stringRequest.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
queue.add(stringRequest);
if (DEBUG) {
MyLog.getElapsed(CLS_NAME, then);
}
}
use of com.android.volley.DefaultRetryPolicy in project Saiy-PS by brandall76.
the class FetchIDOperation method getStatus.
/**
* Method to get an enrollment id.
*
* @return an {@link Pair} of which the first parameter will denote success and the second an
* {@link OperationStatus} object. If the request was unsuccessful,
* the second parameter may be null.
*/
public Pair<Boolean, OperationStatus> getStatus() {
if (DEBUG) {
MyLog.i(CLS_NAME, "getStatus");
}
final long then = System.nanoTime();
final RequestFuture<JSONObject> future = RequestFuture.newFuture();
final RequestQueue queue = Volley.newRequestQueue(mContext);
queue.start();
final JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, url, null, future, new Response.ErrorListener() {
@Override
public void onErrorResponse(final VolleyError error) {
if (DEBUG) {
MyLog.w(CLS_NAME, "onErrorResponse: " + error.toString());
FetchIDOperation.this.verboseError(error);
}
queue.stop();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
final Map<String, String> params = new HashMap<>();
params.put(CHARSET, ENCODING);
params.put(JSON_HEADER_ACCEPT, JSON_HEADER_VALUE_ACCEPT);
params.put(OCP_SUBSCRIPTION_KEY_HEADER, apiKey);
return params;
}
};
jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
queue.add(jsonObjReq);
JSONObject response = null;
try {
response = future.get(THREAD_TIMEOUT, TimeUnit.SECONDS);
} catch (final InterruptedException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "getStatus: InterruptedException");
e.printStackTrace();
}
} catch (final ExecutionException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "getStatus: ExecutionException");
e.printStackTrace();
}
} catch (final TimeoutException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "getStatus: TimeoutException");
e.printStackTrace();
}
} finally {
queue.stop();
}
if (DEBUG) {
MyLog.getElapsed(CLS_NAME, then);
}
if (response != null) {
if (DEBUG) {
MyLog.i(CLS_NAME, "response: " + response.toString());
}
final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
final OperationStatus operationStatus = gson.fromJson(response.toString(), OperationStatus.class);
if (DEBUG) {
MyLog.i(CLS_NAME, "response: getStatus: " + operationStatus.getStatus());
MyLog.i(CLS_NAME, "response: getCreated: " + operationStatus.getCreated());
MyLog.i(CLS_NAME, "response: getLastAction: " + operationStatus.getLastAction());
MyLog.i(CLS_NAME, "response: getMessage: " + operationStatus.getMessage());
final ProcessingResult processingResult = operationStatus.getProcessingResult();
if (processingResult != null) {
MyLog.i(CLS_NAME, "response: getEnrollmentStatus: " + processingResult.getEnrollmentStatus());
MyLog.i(CLS_NAME, "response: getConfidence: " + processingResult.getConfidence());
MyLog.i(CLS_NAME, "response: getProfileId: " + processingResult.getProfileId());
MyLog.i(CLS_NAME, "response: getEnrollmentSpeechTime: " + processingResult.getEnrollmentSpeechTime());
MyLog.i(CLS_NAME, "response: getRemainingSpeechTime: " + processingResult.getRemainingSpeechTime());
MyLog.i(CLS_NAME, "response: getSpeechTime: " + processingResult.getSpeechTime());
} else {
MyLog.i(CLS_NAME, "response: processingResult: null");
}
}
return new Pair<>(true, operationStatus);
} else {
if (DEBUG) {
MyLog.w(CLS_NAME, "response: failed");
}
return new Pair<>(false, null);
}
}
use of com.android.volley.DefaultRetryPolicy in project Saiy-PS by brandall76.
the class BVAuthRequest method getToken.
/**
* Method to get a temporary access token.
*
* @return an {@link Pair} of which the first parameter will denote success and the second an
* {@link BVCredentials} object, containing the token credentials. If the request was unsuccessful,
* the second parameter may be null.
*/
public Pair<Boolean, BVCredentials> getToken() {
if (DEBUG) {
MyLog.i(CLS_NAME, "getID");
}
final RequestFuture<String> future = RequestFuture.newFuture();
final RequestQueue queue = Volley.newRequestQueue(mContext);
queue.start();
final StringRequest request = new StringRequest(Request.Method.POST, AUTH_URL, future, new Response.ErrorListener() {
@Override
public void onErrorResponse(final VolleyError error) {
if (DEBUG) {
MyLog.w(CLS_NAME, "onErrorResponse: " + error.toString());
BVAuthRequest.this.verboseError(error);
}
queue.stop();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
final Map<String, String> params = new HashMap<>();
params.put(CHARSET, ENCODING);
params.put(CONTENT_TYPE, HEADER_CONTENT_TYPE);
return params;
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
final Map<String, String> params = new HashMap<>();
params.put(CHARSET, ENCODING);
params.put(API_KEY, apiKey);
params.put(GRANT_TYPE, CLIENT_CREDENTIALS);
return params;
}
};
request.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
queue.add(request);
String response = null;
try {
response = future.get(THREAD_TIMEOUT, TimeUnit.SECONDS);
} catch (final InterruptedException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "getID: InterruptedException");
e.printStackTrace();
}
} catch (final ExecutionException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "getID: ExecutionException");
e.printStackTrace();
}
} catch (final TimeoutException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "getID: TimeoutException");
e.printStackTrace();
}
} finally {
queue.stop();
}
if (response != null) {
if (DEBUG) {
MyLog.i(CLS_NAME, "response: " + response);
}
final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
final BVCredentials authResponse = gson.fromJson(response, BVCredentials.class);
if (DEBUG) {
MyLog.i(CLS_NAME, "onResponse: getAccessToken: " + authResponse.getAccessToken());
MyLog.i(CLS_NAME, "onResponse: getTokenType: " + authResponse.getTokenType());
MyLog.i(CLS_NAME, "onResponse: getExpiresIn: " + authResponse.getExpiresIn());
}
authResponse.setExpiryTime(authResponse.getExpiresIn());
SPH.setBeyondVerbalCredentials(mContext, gson.toJson(authResponse));
return new Pair<>(true, authResponse);
} else {
if (DEBUG) {
MyLog.w(CLS_NAME, "response: failed");
}
return new Pair<>(false, null);
}
}
use of com.android.volley.DefaultRetryPolicy in project Saiy-PS by brandall76.
the class BVStartRequest method getId.
/**
* Method to get a recording identifier.
*
* @return an {@link Pair} of which the first parameter will denote success and the second an
* {@link StartResponse} object, containing the recording id.
* <p>
* If the request was unsuccessful, the second parameter may be null.
*/
public Pair<Boolean, StartResponse> getId(@NonNull final JSONObject body) {
if (DEBUG) {
MyLog.i(CLS_NAME, "getId");
}
final RequestFuture<JSONObject> future = RequestFuture.newFuture();
final RequestQueue queue = Volley.newRequestQueue(mContext);
queue.start();
final JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST, START_URL, body, future, new Response.ErrorListener() {
@Override
public void onErrorResponse(final VolleyError error) {
if (DEBUG) {
MyLog.w(CLS_NAME, "onErrorResponse: " + error.toString());
BVStartRequest.this.verboseError(error);
}
queue.stop();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
final Map<String, String> params = new HashMap<>();
params.put(CHARSET, ENCODING);
params.put(AUTHORIZATION, BEARER_ + token);
return params;
}
};
jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
queue.add(jsonObjReq);
JSONObject response = null;
try {
response = future.get(THREAD_TIMEOUT, TimeUnit.SECONDS);
} catch (final InterruptedException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "execute: InterruptedException");
e.printStackTrace();
}
} catch (ExecutionException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "execute: ExecutionException");
e.printStackTrace();
}
} catch (TimeoutException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "execute: TimeoutException");
e.printStackTrace();
}
} finally {
queue.stop();
}
if (response != null) {
if (DEBUG) {
MyLog.i(CLS_NAME, "response: " + response);
}
final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
final StartResponse startResponse = gson.fromJson(response.toString(), StartResponse.class);
if (DEBUG) {
MyLog.i(CLS_NAME, "onResponse: getStatus: " + startResponse.getStatus());
}
if (startResponse.isSuccessful()) {
if (DEBUG) {
MyLog.i(CLS_NAME, "onResponse: getRecordingId: " + startResponse.getRecordingId());
}
} else {
if (DEBUG) {
MyLog.i(CLS_NAME, "onResponse: getReason: " + startResponse.getReason());
}
}
return new Pair<>(true, startResponse);
} else {
if (DEBUG) {
MyLog.w(CLS_NAME, "response: failed");
}
return new Pair<>(false, null);
}
}
Aggregations