Search in sources :

Example 16 with DefaultRetryPolicy

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

Example 17 with DefaultRetryPolicy

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);
    }
}
Also used : VolleyError(com.android.volley.VolleyError) HashMap(java.util.HashMap) StringRequest(com.android.volley.toolbox.StringRequest) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DefaultRetryPolicy(com.android.volley.DefaultRetryPolicy) Response(com.android.volley.Response) NetworkResponse(com.android.volley.NetworkResponse) RequestQueue(com.android.volley.RequestQueue)

Example 18 with DefaultRetryPolicy

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);
    }
}
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)

Example 19 with DefaultRetryPolicy

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);
    }
}
Also used : VolleyError(com.android.volley.VolleyError) HashMap(java.util.HashMap) GsonBuilder(com.google.gson.GsonBuilder) StringRequest(com.android.volley.toolbox.StringRequest) DefaultRetryPolicy(com.android.volley.DefaultRetryPolicy) Gson(com.google.gson.Gson) Response(com.android.volley.Response) NetworkResponse(com.android.volley.NetworkResponse) BVCredentials(ai.saiy.android.cognitive.emotion.provider.beyondverbal.containers.BVCredentials) RequestQueue(com.android.volley.RequestQueue) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) Pair(android.util.Pair)

Example 20 with DefaultRetryPolicy

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

Aggregations

DefaultRetryPolicy (com.android.volley.DefaultRetryPolicy)23 VolleyError (com.android.volley.VolleyError)15 Response (com.android.volley.Response)13 NetworkResponse (com.android.volley.NetworkResponse)10 RequestQueue (com.android.volley.RequestQueue)10 HashMap (java.util.HashMap)10 JSONObject (org.json.JSONObject)10 ExecutionException (java.util.concurrent.ExecutionException)9 TimeoutException (java.util.concurrent.TimeoutException)9 Pair (android.util.Pair)8 StringRequest (com.android.volley.toolbox.StringRequest)8 GsonBuilder (com.google.gson.GsonBuilder)8 JsonObjectRequest (com.android.volley.toolbox.JsonObjectRequest)7 Gson (com.google.gson.Gson)7 JsonRestRequest (com.wordpress.rest.JsonRestRequest)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 JSONException (org.json.JSONException)4 ErrorListener (com.android.volley.Response.ErrorListener)3 RestRequest (com.wordpress.rest.RestRequest)3 ProfileItem (ai.saiy.android.cognitive.identity.provider.microsoft.containers.ProfileItem)2