Extensions/BinaryDad.Extensions/RestUtility.cs

197 lines
8.3 KiB
C#
Raw Normal View History

2020-09-05 01:19:24 +00:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
2021-04-20 21:10:57 +00:00
using System.Text;
2020-09-05 01:19:24 +00:00
namespace BinaryDad.Extensions
{
public class RestUtility
{
public const int DefaultTimeout = 30000;
2020-09-05 01:19:24 +00:00
#region Get
/// <summary>
/// Invokes a GET request with optional headers
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="url"></param>
/// <param name="additionalHeaders"></param>
/// <param name="timeout">Timeout of the request in milliseconds</param>
2020-09-05 01:19:24 +00:00
/// <returns></returns>
public static T Get<T>(string url, Dictionary<string, string> additionalHeaders = null, int timeout = DefaultTimeout) => Send(url, HttpMethod.Get, typeof(T), null, additionalHeaders, timeout).To<T>();
2020-09-05 01:19:24 +00:00
/// <summary>
/// Invokes a GET request with optional headers
/// </summary>
/// <param name="url"></param>
/// <param name="additionalHeaders"></param>
/// <param name="timeout">Timeout of the request in milliseconds</param>
2020-09-05 01:19:24 +00:00
/// <returns></returns>
public static string Get(string url, Dictionary<string, string> additionalHeaders = null, int timeout = DefaultTimeout) =>
2020-09-05 01:19:24 +00:00
// response type is always a string if no returnObjectType is used
Send(url, HttpMethod.Get, null, null, additionalHeaders, timeout) as string;
2020-09-05 01:19:24 +00:00
/// <summary>
/// Invokes a GET request with optional headers
/// </summary>
/// <param name="url"></param>
/// <param name="returnObjectType"></param>
/// <param name="additionalHeaders"></param>
/// <param name="timeout">Timeout of the request in milliseconds</param>
2020-09-05 01:19:24 +00:00
/// <returns></returns>
public static object Get(string url, Type returnObjectType, Dictionary<string, string> additionalHeaders = null, int timeout = DefaultTimeout) => Send(url, HttpMethod.Get, returnObjectType, null, additionalHeaders, timeout);
2020-09-05 01:19:24 +00:00
#endregion
#region Post
/// <summary>
/// Invokes a POST request with optional headers
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="url"></param>
/// <param name="body"></param>
/// <param name="additionalHeaders"></param>
/// <param name="timeout">Timeout of the request in milliseconds</param>
2020-09-05 01:19:24 +00:00
/// <returns></returns>
public static T Post<T>(string url, object body, Dictionary<string, string> additionalHeaders = null, int timeout = DefaultTimeout) => Send(url, HttpMethod.Post, typeof(T), body, additionalHeaders, timeout).To<T>();
2020-09-05 01:19:24 +00:00
/// <summary>
/// Invokes a POST request with optional headers
/// </summary>
/// <param name="url"></param>
/// <param name="body"></param>
/// <param name="additionalHeaders"></param>
/// <param name="timeout">Timeout of the request in milliseconds</param>
2020-09-05 01:19:24 +00:00
/// <returns></returns>
public static string Post(string url, object body, Dictionary<string, string> additionalHeaders = null, int timeout = DefaultTimeout) =>
2020-09-05 01:19:24 +00:00
// response type is always a string if no returnObjectType is used
Send(url, HttpMethod.Post, null, body, additionalHeaders, timeout) as string;
2020-09-05 01:19:24 +00:00
/// <summary>
/// Invokes a POST request with optional headers
/// </summary>
/// <param name="url"></param>
/// <param name="returnObjectType"></param>
/// <param name="body"></param>
/// <param name="additionalHeaders"></param>
/// <param name="timeout">Timeout of the request in milliseconds</param>
2020-09-05 01:19:24 +00:00
/// <returns></returns>
public static object Post(string url, Type returnObjectType, object body, Dictionary<string, string> additionalHeaders, int timeout = DefaultTimeout) => Send(url, HttpMethod.Post, returnObjectType, body, additionalHeaders, timeout);
2020-09-05 01:19:24 +00:00
#endregion
#region Send
/// <summary>
/// Invokes a request with custom method/verb and optional headers
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="url"></param>
/// <param name="method"></param>
/// <param name="body"></param>
/// <param name="additionalHeaders"></param>
/// <param name="timeout">Timeout of the request in milliseconds</param>
2020-09-05 01:19:24 +00:00
/// <returns></returns>
public static T Send<T>(string url, HttpMethod method, object body = null, Dictionary<string, string> additionalHeaders = null, int timeout = DefaultTimeout) => Send(url, method, typeof(T), body, additionalHeaders, timeout).To<T>();
2020-09-05 01:19:24 +00:00
/// <summary>
/// Invokes a request with custom method/verb and optional headers
/// </summary>
/// <param name="url"></param>
/// <param name="method"></param>
/// <param name="body"></param>
/// <param name="additionalHeaders"></param>
/// <param name="timeout">Timeout of the request in milliseconds</param>
2020-09-05 01:19:24 +00:00
/// <returns></returns>
public static string Send(string url, HttpMethod method, object body = null, Dictionary<string, string> additionalHeaders = null, int timeout = DefaultTimeout) => Send(url, method, null, body, additionalHeaders, timeout) as string;
2020-09-05 01:19:24 +00:00
/// <summary>
/// Invokes a request with custom method/verb and optional headers
/// </summary>
/// <param name="url"></param>
/// <param name="method"></param>
/// <param name="returnObjectType"></param>
/// <param name="body"></param>
/// <param name="additionalHeaders"></param>
/// <param name="timeout">Timeout of the request in milliseconds</param>
2020-09-05 01:19:24 +00:00
/// <returns></returns>
public static object Send(string url, HttpMethod method, Type returnObjectType, object body = null, Dictionary<string, string> additionalHeaders = null, int timeout = DefaultTimeout)
2020-09-05 01:19:24 +00:00
{
var serializedResponse = string.Empty;
2020-09-08 03:41:07 +00:00
var request = WebRequest.CreateHttp(url);
2020-09-05 01:19:24 +00:00
2020-09-08 03:41:07 +00:00
request.Method = method.ToString();
request.Timeout = timeout;
2020-09-08 03:41:07 +00:00
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
2020-09-05 01:19:24 +00:00
2020-09-08 03:41:07 +00:00
// add additional headers
if (additionalHeaders != null)
{
foreach (var key in additionalHeaders.Keys)
2020-09-05 01:19:24 +00:00
{
2020-09-08 03:41:07 +00:00
if (additionalHeaders[key] != null)
2020-09-05 01:19:24 +00:00
{
2020-09-08 03:41:07 +00:00
request.Headers.Add(key, additionalHeaders[key]);
}
else
{
request.Headers.Add(key);
2020-09-05 01:19:24 +00:00
}
}
2020-09-08 03:41:07 +00:00
}
2020-09-05 01:19:24 +00:00
2020-09-08 03:41:07 +00:00
if (body != null)
{
var serializedBody = body.Serialize();
2021-04-20 21:10:57 +00:00
var bytes = Encoding.UTF8.GetBytes(serializedBody);
2020-09-05 01:19:24 +00:00
2020-09-08 03:41:07 +00:00
request.ContentLength = bytes.Length;
2020-09-05 01:19:24 +00:00
2020-09-08 03:41:07 +00:00
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;
}
2020-09-05 01:19:24 +00:00
2020-09-08 03:41:07 +00:00
using (var response = (HttpWebResponse)request.GetResponse())
{
if (response.StatusCode < HttpStatusCode.BadRequest)
{
// Success
using (var responseStream = response.GetResponseStream())
2020-09-05 01:19:24 +00:00
{
2020-09-08 03:41:07 +00:00
if (responseStream != null)
2020-09-05 01:19:24 +00:00
{
2020-09-08 03:41:07 +00:00
using (var reader = new StreamReader(responseStream))
2020-09-05 01:19:24 +00:00
{
2020-09-08 03:41:07 +00:00
serializedResponse = reader.ReadToEnd();
2020-09-05 01:19:24 +00:00
}
}
}
}
}
2020-09-08 03:41:07 +00:00
if (returnObjectType != null)
2020-09-05 01:19:24 +00:00
{
return serializedResponse.Deserialize(returnObjectType);
}
else
{
return serializedResponse;
}
}
#endregion
}
}