All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.moesif.api.controllers.HealthController Maven / Gradle / Ivy

There is a newer version: 1.8.2
Show newest version
/*
 * MoesifAPILib
 *
 *
 */
package com.moesif.api.controllers;

import java.io.*;
import java.util.*;

import com.fasterxml.jackson.core.type.TypeReference;

import com.moesif.api.*;
import com.moesif.api.models.*;
import com.moesif.api.exceptions.*;
import com.moesif.api.http.client.HttpContext;
import com.moesif.api.http.request.HttpRequest;
import com.moesif.api.http.response.HttpResponse;
import com.moesif.api.http.response.HttpStringResponse;
import com.moesif.api.http.client.APICallBack;
import com.moesif.api.controllers.syncwrapper.APICallBackCatcher;

public class HealthController extends BaseController implements IHealthController {
    //private static variables for the singleton pattern
    private static Object syncObject = new Object();
    private static HealthController instance = null;

    /**
     * Singleton pattern implementation 
     * @return The singleton instance of the HealthController class 
     */
    public static HealthController getInstance() {
        synchronized (syncObject) {
            if (null == instance) {
                instance = new HealthController();
            }
        }
        return instance;
    }

    /**
     * Health Probe
     * @return  The health probe
     */
    public StatusModel getHealthProbe(
    ) throws Throwable {
        APICallBackCatcher callback = new APICallBackCatcher();
        getHealthProbeAsync(callback);
        if(!callback.isSuccess())
            throw callback.getError();
        return callback.getResult();
    }

    /**
     * Health Probe Async
     * @param    callBack Called after the HTTP response is received
     */
    public void getHealthProbeAsync(
                final APICallBack callBack
    ) {
        //the base uri for api requests
        String _baseUri = Configuration.BaseUri;

        //prepare query string for API call
        StringBuilder _queryBuilder = new StringBuilder(_baseUri);
        _queryBuilder.append("/health/probe");
        //validate and preprocess url
        String _queryUrl = APIHelper.cleanUrl(_queryBuilder);

        //load all headers for the outgoing API request
        Map _headers = new HashMap() {
            private static final long serialVersionUID = 5262405786134955276L;
            {
                    put( "accept", "application/json" );
                    put( "X-Moesif-Application-Id", Configuration.ApplicationId);
            }
        };

        //prepare and invoke the API call request to fetch the response
        final HttpRequest _request = getClientInstance().get(_queryUrl, _headers, null);

        //invoke the callback before request if its not null
        if (getHttpCallBack() != null)
        {
            getHttpCallBack().OnBeforeRequest(_request);
        }

        //invoke request and get response
        Runnable _responseTask = new Runnable() {
            public void run() {
                //make the API call
                getClientInstance().executeAsStringAsync(_request, new APICallBack() {
                    public void onSuccess(HttpContext _context, HttpResponse _response) {
                        try {

                            //invoke the callback after response if its not null
                            if (getHttpCallBack() != null)	
                            {
                                getHttpCallBack().OnAfterResponse(_context);
                            }

                            //handle errors defined at the API level
                            validateResponse(_response, _context);

                            //extract result from the http response
                            String _responseBody = ((HttpStringResponse)_response).getBody();
                            StatusModel _result = APIHelper.deserialize(_responseBody,
                                                        new TypeReference(){});

                            //let the caller know of the success
                            callBack.onSuccess(_context, _result);
                        } catch (APIException error) {
                            //let the caller know of the error
                            callBack.onFailure(_context, error);
                        } catch (IOException ioException) {
                            //let the caller know of the caught IO Exception
                            callBack.onFailure(_context, ioException);
                        } catch (Exception exception) {
                            //let the caller know of the caught Exception
                            callBack.onFailure(_context, exception);
                        }
                    }
                    public void onFailure(HttpContext _context, Throwable _error) {
                        //invoke the callback after response if its not null
                        if (getHttpCallBack() != null)	
                            {
                            getHttpCallBack().OnAfterResponse(_context);
                        }

                        //let the caller know of the failure
                        callBack.onFailure(_context, _error);
                    }
                });
            }
        };

        //execute async using thread pool
        APIHelper.getScheduler().execute(_responseTask);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy