コード例 #1
0
        /// <summary>
        /// Calls a specified REST backend at the specified url and using the specified method and params and parses the response to the appropriate output type
        /// </summary>
        /// <typeparam name="TOut"></typeparam>
        /// <param name="url"></param>
        /// <param name="method"></param>
        /// <param name="queryParams"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        protected internal virtual async Task <RestApiCallOutput <TOut> > ApiCallAsync <TOut>(string url, Method method = Method.GET, Dictionary <string, string> queryParams = null, object data = null)
        {
            try
            {
                var response = await RestApiClientUtils.ApiCallAsync <TOut>(url, method, queryParams, data);

                if (response.Response.IsSuccessful)
                {
                    LogLastHealthyResponse();
                }

                return(response);
            }
            catch (ApiCallException aex)
            {
                //status to be passed to a custom handler
                HealthStatus?status = null;
                var          msg    = string.Empty;

                if (
                    aex.ResponseStatus == HttpStatusCode.InternalServerError ||
                    aex.ResponseStatus == HttpStatusCode.ServiceUnavailable ||
                    aex.ResponseStatus == HttpStatusCode.GatewayTimeout ||
                    aex.ResponseStatus == HttpStatusCode.BadGateway
                    )
                {
                    status = ApiClient.HealthStatus.Dead; //potentially could mark as unhealthy and let a farm handle marking as dead
                    msg    = aex.Message;
                }

                if (
#if NETCOREAPP3_1
                    aex.ResponseStatus == HttpStatusCode.TooManyRequests ||
#else
                    (int)aex.ResponseStatus == 429 ||  //Too many requests
#endif

                    aex.ResponseStatus == HttpStatusCode.RequestTimeout
                    )
                {
                    status = ApiClient.HealthStatus.Unhealthy;
                }


                //other codes ignored, so 400, 404, etc by default will not cause marking a client as unhealthy.

                //custom err handling as required
                var(customStatus, customMessage) = HandleCustomErrors(status, aex);

                if (customStatus.HasValue)
                {
                    status = customStatus;
                }

                if (!string.IsNullOrWhiteSpace(customMessage))
                {
                    msg = customMessage;
                }

                switch (status)
                {
                case ApiClient.HealthStatus.Unhealthy:
                    MarkAsUnHealthy();
                    break;

                case ApiClient.HealthStatus.Dead:
                    MarkAsDead(aex.ResponseStatus ?? 0, msg);
                    break;
                }

                throw;
            }
        }
コード例 #2
0
 /// <summary>
 /// Calls a specified REST backend at the specified url and using the specified method and params and parses the response to the appropriate output type
 /// </summary>
 /// <typeparam name="TOut"></typeparam>
 /// <param name="url"></param>
 /// <param name="method"></param>
 /// <param name="queryParams"></param>
 /// <param name="data"></param>
 /// <returns></returns>
 protected internal virtual async Task <RestApiCallOutput <TOut> > ApiCallAsync <TOut>(string url, Method method = Method.GET, Dictionary <string, string> queryParams = null, object data = null)
 {
     return(await RestApiClientUtils.ApiCallAsync <TOut>(url, method, queryParams, data));
 }