Example #1
0
        private IApiResponse SendRequest(Func <Request> requestBuilder)
        {
            try
            {
                var request = requestBuilder();

                IApiResponse apiResponse;

                bool retry;

                do
                {
                    if (!TrySendRequest(_connectionSettings.MainServer, request, ServerType.MainServer,
                                        out apiResponse))
                    {
                        if (_connectionSettings.CacheServers != null &&
                            request.Method == RequestMethod.Get)
                        {
                            foreach (var cacheServer in _connectionSettings.CacheServers)
                            {
                                if (TrySendRequest(cacheServer, request, ServerType.CacheServer, out apiResponse))
                                {
                                    break;
                                }
                            }
                        }
                    }

                    if (apiResponse == null)
                    {
                        Logger.LogWarning(
                            "Connection attempt to every server has failed. Checking whether retry is possible...");
                        RequestTimeoutCalculator.OnRequestFailure();
                        RequestRetryStrategy.OnRequestFailure();

                        retry = RequestRetryStrategy.ShouldRetry;

                        if (!retry)
                        {
                            Logger.LogError("Retry is not possible.");
                            throw new ApiConnectionException(request.MainServerExceptions,
                                                             request.CacheServersExceptions);
                        }

                        Logger.LogDebug(
                            string.Format(
                                "Retry is possible. Waiting {0}ms before next attempt...",
                                RequestRetryStrategy.DelayBeforeNextTry));

                        Thread.Sleep(RequestRetryStrategy.DelayBeforeNextTry);

                        Logger.LogDebug("Trying to get response from servers once again...");
                    }
                    else
                    {
                        retry = false;
                    }
                } while (retry);

                Logger.LogDebug("Successfully got response.");
                Logger.LogTrace(
                    string.Format("Response body: {0}", apiResponse.Body));

                RequestTimeoutCalculator.OnRequestSuccess();
                RequestRetryStrategy.OnRequestSuccess();

                return(apiResponse);
            }
            catch (Exception e)
            {
                Logger.LogError("Failed to get response.", e);
                throw;
            }
        }
        /// <summary>
        /// Retrieves specified resource from API.
        /// </summary>
        /// <param name="path">The path to the resource.</param>
        /// <param name="query">The query of the resource.</param>
        /// <returns>Response with resource result.</returns>
        /// <exception cref="ApiConnectionException">Could not connect to API.</exception>
        public IApiResponse GetResponse(string path, string query)
        {
            try
            {
                Logger.LogDebug("Getting response for path: '" + path + "' and query: '" + query + "'...");

                var request = new Request
                {
                    Path  = path,
                    Query = query,
                    MainServerExceptions   = new List <Exception>(),
                    CacheServersExceptions = new List <Exception>()
                };

                IApiResponse apiResponse;

                bool retry;

                do
                {
                    if (!TryGetResponse(_connectionSettings.MainServer, request, ServerType.MainServer,
                                        out apiResponse))
                    {
                        if (_connectionSettings.CacheServers != null)
                        {
                            foreach (var cacheServer in _connectionSettings.CacheServers)
                            {
                                if (TryGetResponse(cacheServer, request, ServerType.CacheServer, out apiResponse))
                                {
                                    break;
                                }
                            }
                        }
                    }

                    if (apiResponse == null)
                    {
                        Logger.LogWarning(
                            "Connection attempt to every server has failed. Checking whether retry is possible...");
                        RequestTimeoutCalculator.OnRequestFailure();
                        RequestRetryStrategy.OnRequestFailure();

                        retry = RequestRetryStrategy.ShouldRetry;

                        if (!retry)
                        {
                            Logger.LogError("Retry is not possible.");
                            throw new ApiConnectionException(request.MainServerExceptions,
                                                             request.CacheServersExceptions);
                        }

                        Logger.LogDebug(
                            "Retry is possible. Waiting " + RequestRetryStrategy.DelayBeforeNextTry + "ms before next attempt...");

                        Thread.Sleep(RequestRetryStrategy.DelayBeforeNextTry);

                        Logger.LogDebug("Trying to get response from servers once again...");
                    }
                    else
                    {
                        retry = false;
                    }
                } while (retry);

                Logger.LogDebug("Successfully got response.");
                Logger.LogTrace("Response body: " + apiResponse.Body);

                RequestTimeoutCalculator.OnRequestSuccess();
                RequestRetryStrategy.OnRequestSuccess();

                return(apiResponse);
            }
            catch (Exception e)
            {
                Logger.LogError("Failed to get response.", e);
                throw;
            }
        }