Example #1
0
        /// <summary>
        /// Calls the API with the specified resource and method.  Retries 3 times on "Unable to connect to the remote server"
        /// </summary>
        /// <param name="resource">The target resource.</param>
        /// <param name="method">The HTTP method to perform on the target resource.</param>
        /// <param name="isUsingLoadIds">When true, the target resource is identified by a client specified LoadId.</param>
        /// <param name="data">The data body for POST and PUT methods.</param>
        /// <param name="callParams">Paramater string to be added to the end of the API call.</param>
        /// <returns>The response string from the API call.</returns>
        public string CallWithRetry(
            string resource,
            string method,
            bool isUsingLoadIds = false,
            IApiEntity data     = null,
            string callParams   = null)
        {
            var exceptions = new List <Exception>();
            int doTries    = 5;

            for (int retry = 0; retry < doTries; retry++)
            {
                try
                {
                    return(Call(resource, method, isUsingLoadIds, data, callParams));
                }
                catch (WebException ex)
                {
                    // If something other than cannot reach server, throw immediately
                    if (!ex.Message.Contains("Unable to connect to the remote server"))
                    {
                        throw;
                    }

                    exceptions.Add(ex);

                    // Pause for increasingly long time (2, 4, 8, 16ish seconds, etc)
                    Thread.Sleep(new Random().Next(900, 1100) * (retry + 1));
                }
            }

            throw new AggregateException(exceptions);
        }
Example #2
0
        /// <summary>
        /// Calls the API with the specified resource and method.
        /// </summary>
        /// <param name="resource">The target resource.</param>
        /// <param name="method">The HTTP method to perform on the target resource.</param>
        /// <param name="isUsingLoadIds">When true, the target resource is identified by a client specified LoadId.</param>
        /// <param name="data">The data body for POST and PUT methods.</param>
        /// <param name="callParams">Paramater string to be added to the end of the API call.</param>
        /// <returns>The response string from the API call.</returns>
        public string Call(
            string resource,
            string method,
            bool isUsingLoadIds = false,
            IApiEntity data     = null,
            string callParams   = null)
        {
            // Remove whitespace, get rid of leading ? or &
            callParams = string.IsNullOrWhiteSpace(callParams)
                ? ""
                : new Regex(@"\s+").Replace(callParams, "").TrimStart('?', '&').Insert(0, "&");

            // Add the 'app' paramater if we have it
            if (!string.IsNullOrWhiteSpace(App))
            {
                callParams += string.Concat("&app=", App);
            }

            var requestUri = string.Format("{0}/{1}?apikey={2}&isloadid={3}{4}", Uri, resource, ApiKey, isUsingLoadIds, callParams);
            var req        = WebRequest.Create(requestUri) as HttpWebRequest;

            req.KeepAlive   = false;
            req.ContentType = "application/" + _interfaceFormat.ToString().ToLower();
            req.Method      = method.ToUpper();

            if (method == "POST" || method == "PUT")
            {
                var json = data != null
                    ? data.ToString(_interfaceFormat)
                    : null;

                var buffer = !string.IsNullOrEmpty(json)
                    ? Encoding.UTF8.GetBytes(json)
                    : new byte[] { };

                req.ContentLength = buffer.Length;
                using (Stream postData = req.GetRequestStream())
                    postData.Write(buffer, 0, buffer.Length);
            }

            try
            {
                using (var resp = req.GetResponse() as HttpWebResponse)
                    using (var respStream = new StreamReader(resp.GetResponseStream()))
                        return(respStream.ReadToEnd());
            }
            catch (WebException ex)
            {
                var response = ex.Response as HttpWebResponse;
                if (response != null)
                {
                    throw new HttpResourceFaultException(response.StatusCode, response.StatusDescription, ex);
                }
                throw;
            }
        }
        /// <summary>
        /// Calls the API with the specified resource and method.
        /// </summary>
        /// <param name="resource">The target resource.</param>
        /// <param name="method">The HTTP method to perform on the target resource.</param>
        /// <param name="isUsingLoadIds">When true, the target resource is identified by a client specified LoadId.</param>
        /// <param name="data">The data body for POST and PUT methods.</param>
        /// <param name="callParams">Paramater string to be added to the end of the API call.</param>
        /// <returns>The response string from the API call.</returns>
        public string Call(
            string resource, 
            string method, 
            bool isUsingLoadIds = false, 
            IApiEntity data = null, 
            string callParams = null)
        {
            // Remove whitespace, get rid of leading ? or &
            callParams = string.IsNullOrWhiteSpace(callParams)
                ? ""
                : new Regex(@"\s+").Replace(callParams, "").TrimStart('?', '&').Insert(0, "&");

            // Add the 'app' paramater if we have it
            if (!string.IsNullOrWhiteSpace(App))
                callParams += string.Concat("&app=", App);

            var requestUri = string.Format("{0}/{1}?apikey={2}&isloadid={3}{4}", Uri, resource, ApiKey, isUsingLoadIds, callParams);
            var req = WebRequest.Create(requestUri) as HttpWebRequest;
            req.KeepAlive = false;
            req.ContentType = "application/" + _interfaceFormat.ToString().ToLower();
            req.Method = method.ToUpper();

            if (method == "POST" || method == "PUT")
            {
                var json = data != null
                    ? data.ToString(_interfaceFormat)
                    : null;

                var buffer = !string.IsNullOrEmpty(json)
                    ? Encoding.UTF8.GetBytes(json)
                    : new byte[] { };

                req.ContentLength = buffer.Length;
                using (Stream postData = req.GetRequestStream())
                    postData.Write(buffer, 0, buffer.Length);
            }

            try
            {
                using (var resp = req.GetResponse() as HttpWebResponse)
                using (var respStream = new StreamReader(resp.GetResponseStream()))
                    return respStream.ReadToEnd();
            }
            catch (WebException ex)
            {
                var response = ex.Response as HttpWebResponse;
                if (response != null)
                    throw new HttpResourceFaultException(response.StatusCode, response.StatusDescription, ex);
                throw;
            }
        }
        /// <summary>
        /// Calls the API with the specified resource and method.  Retries 3 times on "Unable to connect to the remote server"
        /// </summary>
        /// <param name="resource">The target resource.</param>
        /// <param name="method">The HTTP method to perform on the target resource.</param>
        /// <param name="isUsingLoadIds">When true, the target resource is identified by a client specified LoadId.</param>
        /// <param name="data">The data body for POST and PUT methods.</param>
        /// <param name="callParams">Paramater string to be added to the end of the API call.</param>
        /// <returns>The response string from the API call.</returns>
        public string CallWithRetry(
            string resource, 
            string method, 
            bool isUsingLoadIds = false, 
            IApiEntity data = null, 
            string callParams = null)
        {
            var exceptions = new List<Exception>();
            int doTries = 5;

            for (int retry = 0; retry < doTries; retry++)
                try
                {
                    return Call(resource, method, isUsingLoadIds, data, callParams);
                }
                catch (WebException ex)
                {
                    // If something other than cannot reach server, throw immediately
                    if (!ex.Message.Contains("Unable to connect to the remote server"))
                        throw;

                    exceptions.Add(ex);

                    // Pause for increasingly long time (2, 4, 8, 16ish seconds, etc)
                    Thread.Sleep(new Random().Next(900, 1100) * (retry + 1));
                }

            throw new AggregateException(exceptions);
        }