Search in sources :

Example 1 with ProfileList

use of ai.saiy.android.cognitive.identity.provider.microsoft.containers.ProfileList in project Saiy-PS by brandall76.

the class ListIDProfiles method getProfiles.

/**
 * Method to get all enrollment profile information.
 *
 * @return an {@link Pair} of which the first parameter will denote success and the second an
 * {@link ProfileList} object, containing the list of profiles. If the request was unsuccessful,
 * the second parameter may be null.
 */
public Pair<Boolean, ProfileList> getProfiles() {
    if (DEBUG) {
        MyLog.i(CLS_NAME, "getProfiles");
    }
    final long then = System.nanoTime();
    final RequestFuture<String> future = RequestFuture.newFuture();
    final RequestQueue queue = Volley.newRequestQueue(mContext);
    queue.start();
    final StringRequest request = new StringRequest(Request.Method.GET, LIST_URL, future, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(final VolleyError error) {
            if (DEBUG) {
                MyLog.w(CLS_NAME, "onErrorResponse: " + error.toString());
                ListIDProfiles.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(OCP_SUBSCRIPTION_KEY_HEADER, apiKey);
            return params;
        }
    };
    request.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * OTT_MULTIPLIER, 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, "getProfiles: InterruptedException");
            e.printStackTrace();
        }
    } catch (final ExecutionException e) {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "getProfiles: ExecutionException");
            e.printStackTrace();
        }
    } catch (final TimeoutException e) {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "getProfiles: TimeoutException");
            e.printStackTrace();
        }
    } finally {
        queue.stop();
    }
    if (DEBUG) {
        MyLog.getElapsed(CLS_NAME, then);
    }
    if (response != null) {
        if (DEBUG) {
            MyLog.i(CLS_NAME, "response: " + response);
        }
        final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        final Type type = new TypeToken<List<ProfileItem>>() {
        }.getType();
        final ProfileList profileList = new ProfileList(gson.<List<ProfileItem>>fromJson(response, type));
        if (DEBUG) {
            MyLog.i(CLS_NAME, "onResponse: profileList size: " + profileList.getItems().size());
        }
        return new Pair<>(true, profileList);
    } else {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "response: failed");
        }
        return new Pair<>(false, null);
    }
}
Also used : VolleyError(com.android.volley.VolleyError) ProfileItem(ai.saiy.android.cognitive.identity.provider.microsoft.containers.ProfileItem) 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) Type(java.lang.reflect.Type) RequestQueue(com.android.volley.RequestQueue) ProfileList(ai.saiy.android.cognitive.identity.provider.microsoft.containers.ProfileList) List(java.util.List) ProfileList(ai.saiy.android.cognitive.identity.provider.microsoft.containers.ProfileList) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) Pair(android.util.Pair)

Aggregations

ProfileItem (ai.saiy.android.cognitive.identity.provider.microsoft.containers.ProfileItem)1 ProfileList (ai.saiy.android.cognitive.identity.provider.microsoft.containers.ProfileList)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 StringRequest (com.android.volley.toolbox.StringRequest)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 Type (java.lang.reflect.Type)1 HashMap (java.util.HashMap)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1