Example #1
0
        // ReSharper disable once UnusedParameter.Local
        private static void ThrowIfServerIsInvalid(ApiConnectionServer server)
        {
            if (server.Host == null)
            {
                // ReSharper disable once NotResolvedInText
                throw new ArgumentNullException("connectionSettings",
                                                "ApiConnectionServer.Host of one of the servers is null.");
            }

            if (string.IsNullOrEmpty(server.Host))
            {
                // ReSharper disable once NotResolvedInText
                throw new ArgumentNullException("connectionSettings",
                                                "ApiConnectionServer.Host of one of the servers is empty");
            }
        }
Example #2
0
 // ReSharper disable once UnusedParameter.Local
 private static void ThrowIfServerIsInvalid(ApiConnectionServer server)
 {
     if (server.Host == null)
     {
         // ReSharper disable once NotResolvedInText
         throw new ArgumentNullException("connectionSettings",
                                         "ApiConnectionServer.Host of one of the servers is null.");
     }
     if (string.IsNullOrEmpty(server.Host))
     {
         // ReSharper disable once NotResolvedInText
         throw new ArgumentNullException("connectionSettings",
                                         "ApiConnectionServer.Host of one of the servers is empty");
     }
     if (server.Timeout < 0 && server.Timeout != System.Threading.Timeout.Infinite)
     {
         // ReSharper disable once NotResolvedInText
         throw new ArgumentOutOfRangeException("connectionSettings",
                                               "ApiConnectionServer.Timeout of one of the servers is less than zero and is not System.Threading.Timeout.Infinite");
     }
 }
Example #3
0
        private bool TrySendRequest(ApiConnectionServer server, Request request, ServerType serverType,
                                    out IApiResponse response)
        {
            Logger.LogDebug(
                string.Format(
                    "Trying to get response from server ({0}): '{1}:{2}' (uses HTTPS: {3})...",
                    serverType,
                    server.Host,
                    server.RealPort,
                    server.UseHttps));

            response = null;

            List <Exception> exceptionsList;

            switch (serverType)
            {
            case ServerType.MainServer:
                exceptionsList = request.MainServerExceptions;
                break;

            case ServerType.CacheServer:
                exceptionsList = request.CacheServersExceptions;
                break;

            default:
                throw new ArgumentOutOfRangeException(serverType.ToString(), serverType, null);
            }

            try
            {
                var uri = new UriBuilder
                {
                    Scheme = server.UseHttps ? "https" : "http",
                    Host   = server.Host,
                    Path   = request.Path,
                    Query  = request.Query,
                    Port   = server.RealPort
                }.Uri;

                var httpResponse = MakeResponse(uri, request);

                Logger.LogDebug("Received response. Checking whether it is valid...");
                Logger.LogTrace(
                    string.Format(
                        "Response status code: {0}",
                        httpResponse.StatusCode));

                if (IsResponseValid(httpResponse, serverType))
                {
                    Logger.LogDebug("Response is valid.");
                    response = new ApiResponse(httpResponse);
                    return(true);
                }

                Logger.LogWarning("Response is not valid.");

                if (IsResponseUnexpectedError(httpResponse, serverType))
                {
                    throw new ApiResponseException((int)httpResponse.StatusCode);
                }

                throw new ApiServerConnectionException(
                          string.Format(
                              "Server \'{0}\' returned code {1}",
                              server.Host,
                              (int)httpResponse.StatusCode));
            }
            catch (WebException webException)
            {
                Logger.LogWarning("Error while connecting to the API server.", webException);
                exceptionsList.Add(webException);
                return(false);
            }
            catch (ApiServerConnectionException e)
            {
                Logger.LogWarning("Error while connecting to the API server.", e);
                exceptionsList.Add(e);
                return(false);
            }
        }
Example #4
0
 private bool TryGetResponseFromCacheServer(ApiConnectionServer server, Request request,
                                            out IApiResponse response)
 {
     return(TryGetResponse(server, request, ServerType.CacheServer, out response));
 }
Example #5
0
        private bool TryGetResponse(ApiConnectionServer server, Request request, ServerType serverType,
                                    out IApiResponse response)
        {
            Logger.LogDebug(
                $"Trying to get response from server ({serverType}): '{server.Host}:{server.RealPort}' (uses HTTPS: {server.UseHttps})...");

            response = null;

            List <Exception> exceptionsList;

            switch (serverType)
            {
            case ServerType.MainServer:
                exceptionsList = request.MainServerExceptions;
                break;

            case ServerType.CacheServer:
                exceptionsList = request.CacheServersExceptions;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(serverType), serverType, null);
            }

            try
            {
                var uri = new UriBuilder
                {
                    Scheme = server.UseHttps ? "https" : "http",
                    Host   = server.Host,
                    Path   = request.Path,
                    Query  = request.Query,
                    Port   = server.RealPort
                }.Uri;

                var httpRequest = CreateHttpRequest(uri);

                httpRequest.Timeout = server.Timeout * request.TimeoutMultipler;
                Logger.LogTrace($"Setting request timeout to {httpRequest.Timeout}ms");

                var httpResponse = httpRequest.GetResponse();

                Logger.LogDebug("Received response. Checking whether it is valid...");
                Logger.LogTrace($"Response status code: {httpResponse.StatusCode}");

                if (IsResponseValid(httpResponse, serverType))
                {
                    Logger.LogDebug("Response is valid.");
                    response = new ApiResponse(httpResponse);
                    return(true);
                }

                Logger.LogWarning("Response is not valid. Checking whether it is API error...");

                if (IsResponseUnexpectedError(httpResponse, serverType))
                {
                    Logger.LogError("API error. Unable to get valid response.");
                    throw new ApiResponseException((int)httpResponse.StatusCode);
                }

                Logger.LogDebug(
                    "Error is not related to API. Probably it was caused by connection problems.");

                throw new ApiServerConnectionException(
                          $"Server \'{server.Host}\' returned code {(int) httpResponse.StatusCode}");
            }
            catch (WebException webException)
            {
                Logger.LogWarning("Unable to get any response from server.", webException);
                exceptionsList.Add(webException);
                return(false);
            }
            catch (ApiServerConnectionException e)
            {
                Logger.LogWarning("Unable to get valid response from server.", e);
                exceptionsList.Add(e);
                return(false);
            }
        }