Cordonel EOL API
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using RestSharp;
|
||||
using Newtonsoft.Json;
|
||||
using RestSharp;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
@@ -322,39 +323,50 @@ namespace Xylem.Common.Logic.SoftwareAccessHelper
|
||||
|
||||
}
|
||||
|
||||
public static IRestResponse PutAsJson(this Object json, String url, Action<RestRequestHeaders> headersHandler = null, Int32? timeoutMs = null)
|
||||
#region HTTP String extensions
|
||||
public static HttpResponse<T> DeleteAsJson<T>(this String url, Object json = null, Action<RestRequestHeaders> headersHandler = null, Int32? timeoutMs = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var client = new RestClient(url);
|
||||
var request = new RestRequest(Method.PUT);
|
||||
var headers = new RestRequestHeaders(request)
|
||||
{
|
||||
ContentType = "application/json",
|
||||
Accept = "application/json"
|
||||
};
|
||||
|
||||
headersHandler?.Invoke(headers);
|
||||
|
||||
if (json != null)
|
||||
{
|
||||
request.AddJsonBody(json);
|
||||
}
|
||||
|
||||
ServicePointManager.Expect100Continue = false;
|
||||
|
||||
return client.Execute(request);
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
return new RestResponse
|
||||
{
|
||||
StatusCode = HttpStatusCode.InternalServerError,
|
||||
Content = error.ToString()
|
||||
};
|
||||
}
|
||||
return Method.DELETE.AsJson<T>(url, headersHandler, json, timeoutMs);
|
||||
}
|
||||
|
||||
public static HttpResponse<T> GetAsJson<T>(this String url, Object json = null, Action<RestRequestHeaders> headersHandler = null, Int32? timeoutMs = null)
|
||||
{
|
||||
return Method.GET.AsJson<T>(url, headersHandler, json, timeoutMs);
|
||||
}
|
||||
|
||||
public static HttpResponse<T> PostAsJson<T>(this String url, Object json = null, Action<RestRequestHeaders> headersHandler = null, Int32? timeoutMs = null)
|
||||
{
|
||||
return Method.POST.AsJson<T>(url, headersHandler, json, timeoutMs);
|
||||
}
|
||||
|
||||
public static HttpResponse<T> PutAsJson<T>(this String url, Object json = null, Action<RestRequestHeaders> headersHandler = null, Int32? timeoutMs = null)
|
||||
{
|
||||
return Method.PUT.AsJson<T>(url, headersHandler, json, timeoutMs);
|
||||
}
|
||||
#endregion HTTP String extensions
|
||||
|
||||
#region HTTP Object extensions
|
||||
public static HttpResponse<T> DeleteAsJson<T>(this Object json, String url, Action<RestRequestHeaders> headersHandler = null, Int32? timeoutMs = null)
|
||||
{
|
||||
return Method.DELETE.AsJson<T>(url, headersHandler, json, timeoutMs);
|
||||
}
|
||||
|
||||
public static HttpResponse<T> GetAsJson<T>(this Object json, String url, Action<RestRequestHeaders> headersHandler = null, Int32? timeoutMs = null)
|
||||
{
|
||||
return Method.GET.AsJson<T>(url, headersHandler, json, timeoutMs);
|
||||
}
|
||||
|
||||
public static HttpResponse<T> PostAsJson<T>(this Object json, String url, Action<RestRequestHeaders> headersHandler = null, Int32? timeoutMs = null)
|
||||
{
|
||||
return Method.POST.AsJson<T>(url, headersHandler, json, timeoutMs);
|
||||
}
|
||||
|
||||
public static HttpResponse<T> PutAsJson<T>(this Object json, String url, Action<RestRequestHeaders> headersHandler = null, Int32? timeoutMs = null)
|
||||
{
|
||||
return Method.PUT.AsJson<T>(url, headersHandler, json, timeoutMs);
|
||||
}
|
||||
#endregion HTTP Object extensions
|
||||
|
||||
/// <summary>
|
||||
/// Web request
|
||||
/// </summary>
|
||||
@@ -430,5 +442,80 @@ namespace Xylem.Common.Logic.SoftwareAccessHelper
|
||||
//throw new ApplicationException($"Timeout { r.StatusDescription} {Environment.NewLine} PingState: {presult.Status} {Environment.NewLine} RoundtripTime: {presult.RoundtripTime} ");
|
||||
return r.Content;
|
||||
}
|
||||
|
||||
private static HttpResponse<T> AsJson<T>(this Method method, String url, Action<RestRequestHeaders> headersHandler = null, Object model = null, Int32? timeoutMs = null)
|
||||
{
|
||||
var httpResponse = new HttpResponse<T>();
|
||||
var response = default(IRestResponse);
|
||||
|
||||
try
|
||||
{
|
||||
var client = new RestClient(url);
|
||||
var request = new RestRequest(method);
|
||||
var headers = new RestRequestHeaders(request)
|
||||
{
|
||||
ContentType = "application/json",
|
||||
Accept = "application/json"
|
||||
};
|
||||
|
||||
headersHandler?.Invoke(headers);
|
||||
|
||||
if (model != null)
|
||||
{
|
||||
if (method == Method.GET)
|
||||
{
|
||||
request.AddObject(model);
|
||||
}
|
||||
else
|
||||
{
|
||||
request.AddJsonBody(model);
|
||||
}
|
||||
}
|
||||
|
||||
ServicePointManager.Expect100Continue = false;
|
||||
|
||||
response = client.Execute(request);
|
||||
httpResponse.HttpStatusCode = response.StatusCode;
|
||||
|
||||
if (HttpStatusCode.OK <= response.StatusCode && response.StatusCode < HttpStatusCode.BadRequest)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(response.Content))
|
||||
{
|
||||
httpResponse.Value = JsonConvert.DeserializeObject<T>(response.Content);
|
||||
}
|
||||
else
|
||||
{
|
||||
httpResponse.Errors = new Dictionary<String, String>()
|
||||
{
|
||||
{ nameof(response.Content), $"{response.Content}" },
|
||||
};
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
httpResponse.Errors = JsonConvert.DeserializeObject<Dictionary<String, String>>(response.Content);
|
||||
|
||||
if (httpResponse.Errors is null)
|
||||
{
|
||||
httpResponse.Errors = new Dictionary<String, String>()
|
||||
{
|
||||
{ nameof(response.ErrorException), $"{response.ErrorException}" },
|
||||
{ nameof(response.ErrorMessage), $"{response.ErrorMessage}" },
|
||||
{ nameof(response.Content), $"{response.Content}" },
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
httpResponse.HttpStatusCode = HttpStatusCode.InternalServerError;
|
||||
httpResponse.Errors = new Dictionary<String, String>()
|
||||
{
|
||||
{ nameof(Exception), $"{error}" }
|
||||
};
|
||||
}
|
||||
|
||||
return httpResponse;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user