remove retries/sleep
This commit is contained in:
parent
2aa9ce3eb7
commit
b14d017f43
@ -3,15 +3,12 @@ using System.Collections.Generic;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Threading;
|
|
||||||
|
|
||||||
namespace BinaryDad.Extensions
|
namespace BinaryDad.Extensions
|
||||||
{
|
{
|
||||||
public class RestUtility
|
public class RestUtility
|
||||||
{
|
{
|
||||||
public const int DefaultTimeout = 5000;
|
public const int DefaultTimeout = 5000;
|
||||||
public const int DefaultSleepDelay = 3000;
|
|
||||||
public const int DefaultRetries = 3;
|
|
||||||
|
|
||||||
#region Get
|
#region Get
|
||||||
|
|
||||||
@ -23,10 +20,7 @@ namespace BinaryDad.Extensions
|
|||||||
/// <param name="additionalHeaders"></param>
|
/// <param name="additionalHeaders"></param>
|
||||||
/// <param name="timeoutMs">Timeout of the request in milliseconds</param>
|
/// <param name="timeoutMs">Timeout of the request in milliseconds</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static T Get<T>(string url, Dictionary<string, string> additionalHeaders = null, int timeoutMs = DefaultTimeout, int sleepDelayMs = DefaultSleepDelay, int retriesAllowed = DefaultRetries)
|
public static T Get<T>(string url, Dictionary<string, string> additionalHeaders = null, int timeoutMs = DefaultTimeout) => Send(url, HttpMethod.Get, typeof(T), null, additionalHeaders, timeoutMs).To<T>();
|
||||||
{
|
|
||||||
return Send(url, HttpMethod.Get, typeof(T), null, additionalHeaders, timeoutMs, sleepDelayMs, retriesAllowed).To<T>();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Invokes a GET request with optional headers
|
/// Invokes a GET request with optional headers
|
||||||
@ -35,11 +29,9 @@ namespace BinaryDad.Extensions
|
|||||||
/// <param name="additionalHeaders"></param>
|
/// <param name="additionalHeaders"></param>
|
||||||
/// <param name="timeoutMs">Timeout of the request in milliseconds</param>
|
/// <param name="timeoutMs">Timeout of the request in milliseconds</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string Get(string url, Dictionary<string, string> additionalHeaders = null, int timeoutMs = DefaultTimeout, int sleepDelayMs = DefaultSleepDelay, int retriesAllowed = DefaultRetries)
|
public static string Get(string url, Dictionary<string, string> additionalHeaders = null, int timeoutMs = DefaultTimeout) =>
|
||||||
{
|
|
||||||
// response type is always a string if no returnObjectType is used
|
// response type is always a string if no returnObjectType is used
|
||||||
return Send(url, HttpMethod.Get, null, null, additionalHeaders, timeoutMs, sleepDelayMs, retriesAllowed) as string;
|
Send(url, HttpMethod.Get, null, null, additionalHeaders, timeoutMs) as string;
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Invokes a GET request with optional headers
|
/// Invokes a GET request with optional headers
|
||||||
@ -51,10 +43,7 @@ namespace BinaryDad.Extensions
|
|||||||
/// <param name="sleepDelayMs"></param>
|
/// <param name="sleepDelayMs"></param>
|
||||||
/// <param name="retries"></param>
|
/// <param name="retries"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static object Get(string url, Type returnObjectType, Dictionary<string, string> additionalHeaders = null, int timeoutMs = DefaultTimeout, int sleepDelayMs = DefaultSleepDelay, int retriesAllowed = DefaultRetries)
|
public static object Get(string url, Type returnObjectType, Dictionary<string, string> additionalHeaders = null, int timeoutMs = DefaultTimeout) => Send(url, HttpMethod.Get, returnObjectType, null, additionalHeaders, timeoutMs);
|
||||||
{
|
|
||||||
return Send(url, HttpMethod.Get, returnObjectType, null, additionalHeaders, timeoutMs, sleepDelayMs, retriesAllowed);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@ -69,10 +58,7 @@ namespace BinaryDad.Extensions
|
|||||||
/// <param name="additionalHeaders"></param>
|
/// <param name="additionalHeaders"></param>
|
||||||
/// <param name="timeoutMs">Timeout of the request in milliseconds</param>
|
/// <param name="timeoutMs">Timeout of the request in milliseconds</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static T Post<T>(string url, object body, Dictionary<string, string> additionalHeaders = null, int timeoutMs = DefaultTimeout, int sleepDelayMs = DefaultSleepDelay, int retriesAllowed = DefaultRetries)
|
public static T Post<T>(string url, object body, Dictionary<string, string> additionalHeaders = null, int timeoutMs = DefaultTimeout) => Send(url, HttpMethod.Post, typeof(T), body, additionalHeaders, timeoutMs).To<T>();
|
||||||
{
|
|
||||||
return Send(url, HttpMethod.Post, typeof(T), body, additionalHeaders, timeoutMs, sleepDelayMs, retriesAllowed).To<T>();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Invokes a POST request with optional headers
|
/// Invokes a POST request with optional headers
|
||||||
@ -82,11 +68,9 @@ namespace BinaryDad.Extensions
|
|||||||
/// <param name="additionalHeaders"></param>
|
/// <param name="additionalHeaders"></param>
|
||||||
/// <param name="timeoutMs">Timeout of the request in milliseconds</param>
|
/// <param name="timeoutMs">Timeout of the request in milliseconds</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string Post(string url, object body, Dictionary<string, string> additionalHeaders = null, int timeoutMs = DefaultTimeout, int sleepDelayMs = DefaultSleepDelay, int retriesAllowed = DefaultRetries)
|
public static string Post(string url, object body, Dictionary<string, string> additionalHeaders = null, int timeoutMs = DefaultTimeout) =>
|
||||||
{
|
|
||||||
// response type is always a string if no returnObjectType is used
|
// response type is always a string if no returnObjectType is used
|
||||||
return Send(url, HttpMethod.Post, null, body, additionalHeaders, timeoutMs, sleepDelayMs, retriesAllowed) as string;
|
Send(url, HttpMethod.Post, null, body, additionalHeaders, timeoutMs) as string;
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Invokes a POST request with optional headers
|
/// Invokes a POST request with optional headers
|
||||||
@ -99,10 +83,7 @@ namespace BinaryDad.Extensions
|
|||||||
/// <param name="sleepDelayMs"></param>
|
/// <param name="sleepDelayMs"></param>
|
||||||
/// <param name="retries"></param>
|
/// <param name="retries"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static object Post(string url, Type returnObjectType, object body, Dictionary<string, string> additionalHeaders, int timeoutMs = DefaultTimeout, int sleepDelayMs = DefaultSleepDelay, int retries = DefaultRetries)
|
public static object Post(string url, Type returnObjectType, object body, Dictionary<string, string> additionalHeaders, int timeoutMs = DefaultTimeout) => Send(url, HttpMethod.Post, returnObjectType, body, additionalHeaders, timeoutMs);
|
||||||
{
|
|
||||||
return Send(url, HttpMethod.Post, returnObjectType, body, additionalHeaders, timeoutMs, sleepDelayMs, retries);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@ -118,10 +99,7 @@ namespace BinaryDad.Extensions
|
|||||||
/// <param name="additionalHeaders"></param>
|
/// <param name="additionalHeaders"></param>
|
||||||
/// <param name="timeoutMs">Timeout of the request in milliseconds</param>
|
/// <param name="timeoutMs">Timeout of the request in milliseconds</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static T Send<T>(string url, HttpMethod method, object body = null, Dictionary<string, string> additionalHeaders = null, int timeoutMs = DefaultTimeout, int sleepDelayMs = DefaultSleepDelay, int retriesAllowed = DefaultRetries)
|
public static T Send<T>(string url, HttpMethod method, object body = null, Dictionary<string, string> additionalHeaders = null, int timeoutMs = DefaultTimeout) => Send(url, method, typeof(T), body, additionalHeaders, timeoutMs).To<T>();
|
||||||
{
|
|
||||||
return Send(url, method, typeof(T), body, additionalHeaders, timeoutMs, sleepDelayMs, retriesAllowed).To<T>();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Invokes a request with custom method/verb and optional headers
|
/// Invokes a request with custom method/verb and optional headers
|
||||||
@ -132,10 +110,7 @@ namespace BinaryDad.Extensions
|
|||||||
/// <param name="additionalHeaders"></param>
|
/// <param name="additionalHeaders"></param>
|
||||||
/// <param name="timeoutMs">Timeout of the request in milliseconds</param>
|
/// <param name="timeoutMs">Timeout of the request in milliseconds</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string Send(string url, HttpMethod method, object body = null, Dictionary<string, string> additionalHeaders = null, int timeoutMs = DefaultTimeout, int sleepDelayMs = DefaultSleepDelay, int retriesAllowed = DefaultRetries)
|
public static string Send(string url, HttpMethod method, object body = null, Dictionary<string, string> additionalHeaders = null, int timeoutMs = DefaultTimeout) => Send(url, method, null, body, additionalHeaders, timeoutMs) as string;
|
||||||
{
|
|
||||||
return Send(url, method, null, body, additionalHeaders, timeoutMs, sleepDelayMs, retriesAllowed) as string;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Invokes a request with custom method/verb and optional headers
|
/// Invokes a request with custom method/verb and optional headers
|
||||||
@ -149,96 +124,69 @@ namespace BinaryDad.Extensions
|
|||||||
/// <param name="sleepDelayMs"></param>
|
/// <param name="sleepDelayMs"></param>
|
||||||
/// <param name="retriesAllowed"></param>
|
/// <param name="retriesAllowed"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static object Send(string url, HttpMethod method, Type returnObjectType, object body = null, Dictionary<string, string> additionalHeaders = null, int timeoutMs = DefaultTimeout, int sleepDelayMs = DefaultSleepDelay, int retriesAllowed = DefaultRetries)
|
public static object Send(string url, HttpMethod method, Type returnObjectType, object body = null, Dictionary<string, string> additionalHeaders = null, int timeoutMs = DefaultTimeout)
|
||||||
{
|
{
|
||||||
var serializedResponse = string.Empty;
|
var serializedResponse = string.Empty;
|
||||||
var success = false;
|
|
||||||
|
|
||||||
for (var attempts = 1; attempts <= retriesAllowed && !success; attempts++)
|
var request = WebRequest.CreateHttp(url);
|
||||||
|
|
||||||
|
request.Method = method.ToString();
|
||||||
|
request.Timeout = timeoutMs;
|
||||||
|
request.ContentType = "application/json";
|
||||||
|
request.Accept = "application/json, text/javascript, *; q=0.01"; // Accept is a reserved header, so you must modify it rather than add
|
||||||
|
|
||||||
|
// add additional headers
|
||||||
|
if (additionalHeaders != null)
|
||||||
{
|
{
|
||||||
var request = WebRequest.CreateHttp(url);
|
foreach (var key in additionalHeaders.Keys)
|
||||||
|
|
||||||
request.Method = method.ToString();
|
|
||||||
request.Timeout = timeoutMs;
|
|
||||||
request.ContentType = "application/json";
|
|
||||||
request.Accept = "application/json, text/javascript, *; q=0.01"; // Accept is a reserved header, so you must modify it rather than add
|
|
||||||
|
|
||||||
// add additional headers
|
|
||||||
if (additionalHeaders != null)
|
|
||||||
{
|
{
|
||||||
foreach (var key in additionalHeaders.Keys)
|
if (additionalHeaders[key] != null)
|
||||||
{
|
{
|
||||||
if (additionalHeaders[key] != null)
|
request.Headers.Add(key, additionalHeaders[key]);
|
||||||
{
|
|
||||||
request.Headers.Add(key, additionalHeaders[key]);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
request.Headers.Add(key);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
else
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (body != null)
|
|
||||||
{
|
{
|
||||||
var serializedBody = body.Serialize();
|
request.Headers.Add(key);
|
||||||
var bytes = System.Text.Encoding.GetEncoding("iso-8859-1").GetBytes(serializedBody);
|
|
||||||
|
|
||||||
request.ContentLength = bytes.Length;
|
|
||||||
|
|
||||||
using (var writeStream = request.GetRequestStream())
|
|
||||||
{
|
|
||||||
writeStream.Write(bytes, 0, bytes.Length);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else if (method == HttpMethod.Post) // POST requires a content length, set to 0 for null body
|
|
||||||
{
|
|
||||||
request.ContentLength = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
using (var response = (HttpWebResponse)request.GetResponse())
|
|
||||||
{
|
|
||||||
if (response.StatusCode < HttpStatusCode.BadRequest)
|
|
||||||
{
|
|
||||||
// Success
|
|
||||||
using (var responseStream = response.GetResponseStream())
|
|
||||||
{
|
|
||||||
if (responseStream != null)
|
|
||||||
{
|
|
||||||
using (var reader = new StreamReader(responseStream))
|
|
||||||
{
|
|
||||||
serializedResponse = reader.ReadToEnd();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
success = true;
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
// only throw after we have reached our retry limit
|
|
||||||
if (attempts >= retriesAllowed)
|
|
||||||
{
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// if post failed, pause before another attempt
|
|
||||||
if (!success)
|
|
||||||
{
|
|
||||||
Thread.Sleep(sleepDelayMs);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (success && returnObjectType != null)
|
if (body != null)
|
||||||
|
{
|
||||||
|
var serializedBody = body.Serialize();
|
||||||
|
var bytes = System.Text.Encoding.GetEncoding("iso-8859-1").GetBytes(serializedBody);
|
||||||
|
|
||||||
|
request.ContentLength = bytes.Length;
|
||||||
|
|
||||||
|
using (var writeStream = request.GetRequestStream())
|
||||||
|
{
|
||||||
|
writeStream.Write(bytes, 0, bytes.Length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (method == HttpMethod.Post) // POST requires a content length, set to 0 for null body
|
||||||
|
{
|
||||||
|
request.ContentLength = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
using (var response = (HttpWebResponse)request.GetResponse())
|
||||||
|
{
|
||||||
|
if (response.StatusCode < HttpStatusCode.BadRequest)
|
||||||
|
{
|
||||||
|
// Success
|
||||||
|
using (var responseStream = response.GetResponseStream())
|
||||||
|
{
|
||||||
|
if (responseStream != null)
|
||||||
|
{
|
||||||
|
using (var reader = new StreamReader(responseStream))
|
||||||
|
{
|
||||||
|
serializedResponse = reader.ReadToEnd();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (returnObjectType != null)
|
||||||
{
|
{
|
||||||
return serializedResponse.Deserialize(returnObjectType);
|
return serializedResponse.Deserialize(returnObjectType);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user