Search in sources :

Example 1 with ProcessingResult

use of ai.saiy.android.cognitive.identity.provider.microsoft.containers.ProcessingResult 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);
    }
}
Also used : VolleyError(com.android.volley.VolleyError) HashMap(java.util.HashMap) GsonBuilder(com.google.gson.GsonBuilder) ProcessingResult(ai.saiy.android.cognitive.identity.provider.microsoft.containers.ProcessingResult) DefaultRetryPolicy(com.android.volley.DefaultRetryPolicy) Gson(com.google.gson.Gson) Response(com.android.volley.Response) NetworkResponse(com.android.volley.NetworkResponse) JSONObject(org.json.JSONObject) RequestQueue(com.android.volley.RequestQueue) OperationStatus(ai.saiy.android.cognitive.identity.provider.microsoft.containers.OperationStatus) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) Pair(android.util.Pair)

Aggregations

OperationStatus (ai.saiy.android.cognitive.identity.provider.microsoft.containers.OperationStatus)1 ProcessingResult (ai.saiy.android.cognitive.identity.provider.microsoft.containers.ProcessingResult)1 Pair (android.util.Pair)1 DefaultRetryPolicy (com.android.volley.DefaultRetryPolicy)1 NetworkResponse (com.android.volley.NetworkResponse)1 RequestQueue (com.android.volley.RequestQueue)1 Response (com.android.volley.Response)1 VolleyError (com.android.volley.VolleyError)1 JsonObjectRequest (com.android.volley.toolbox.JsonObjectRequest)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 HashMap (java.util.HashMap)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 JSONObject (org.json.JSONObject)1