Adding
This commit is contained in:
+90
@@ -0,0 +1,90 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace LCU.Library.Internal.CoreApiWrapperLib.Utilities
|
||||
{
|
||||
public static class HttpUtilities
|
||||
{
|
||||
private const int ParamCounterDefault = 1;
|
||||
|
||||
public static HttpClient GetClient(Uri uri)
|
||||
{
|
||||
var handler = new HttpClientHandler
|
||||
{
|
||||
SslProtocols = System.Security.Authentication.SslProtocols.Tls12 |
|
||||
System.Security.Authentication.SslProtocols.Tls11 |
|
||||
System.Security.Authentication.SslProtocols.Tls,
|
||||
ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true
|
||||
};
|
||||
|
||||
var httpClient = new HttpClient(handler)
|
||||
{
|
||||
Timeout = TimeSpan.FromSeconds(60),
|
||||
BaseAddress = uri
|
||||
};
|
||||
return httpClient;
|
||||
}
|
||||
|
||||
public static Uri BuildHttpsUri(string hostName, string endpoint)
|
||||
{
|
||||
return new Uri($"https://{hostName}{endpoint}");
|
||||
}
|
||||
|
||||
public static string BuildEndPointWithParams(string endpoint, Dictionary<string, string> parameters)
|
||||
{
|
||||
if(parameters == null || parameters.Count < 0)
|
||||
{
|
||||
return endpoint;
|
||||
}
|
||||
|
||||
var counter = ParamCounterDefault;
|
||||
foreach (var key in parameters.Keys)
|
||||
{
|
||||
var splitter = counter == ParamCounterDefault ? "?" : "&";
|
||||
endpoint = $"{endpoint}{splitter}{key}={parameters[key]}";
|
||||
counter++;
|
||||
}
|
||||
|
||||
return endpoint;
|
||||
}
|
||||
|
||||
public static string ReplaceRootParameter(string endpoint, string placeholder, string value)
|
||||
{
|
||||
return endpoint.Replace(placeholder, value);
|
||||
}
|
||||
|
||||
public static Dictionary<string, string> BuildDictionaryOfNonNullValues<T>(T model)
|
||||
{
|
||||
var bodyParams = new Dictionary<string, string>();
|
||||
var modelType = typeof(T);
|
||||
var properties = modelType.GetProperties();
|
||||
|
||||
foreach (var property in properties)
|
||||
{
|
||||
var fieldValue = model.GetType().GetProperty(property.Name).GetValue(model, null);
|
||||
if (fieldValue != null)
|
||||
{
|
||||
bodyParams.Add(property.Name, fieldValue.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
return bodyParams;
|
||||
}
|
||||
|
||||
public static string BuildJsonOfNonNullProperties<T>(T model)
|
||||
{
|
||||
var bodyParams = BuildDictionaryOfNonNullValues(model);
|
||||
|
||||
if (bodyParams.Count <= 0)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
return JsonConvert.SerializeObject(bodyParams);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
using Serilog;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Dynamic;
|
||||
|
||||
namespace LCU.Library.Internal.CoreApiWrapperLib.Utilities
|
||||
{
|
||||
public static class QueryUtilities
|
||||
{
|
||||
private const string QueryFolder = "Queries";
|
||||
private const string DynamicParamater = "param";
|
||||
|
||||
public static string GetQuery(string queryFileName)
|
||||
{
|
||||
var basePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory);
|
||||
var path = $"{basePath}//{QueryFolder}//{queryFileName}.sql";
|
||||
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
Log.Error($"Query was not found at location {path}");
|
||||
return "";
|
||||
}
|
||||
|
||||
var query = File.ReadAllText(path);
|
||||
return query;
|
||||
}
|
||||
|
||||
public static string BuildParameterListString(List<string> values)
|
||||
{
|
||||
var bindListBuilder = new StringBuilder();
|
||||
for (int currentCount = 0; currentCount < values.Count; ++currentCount)
|
||||
{
|
||||
var comma = "";
|
||||
if (currentCount != 0)
|
||||
{
|
||||
comma = ",";
|
||||
}
|
||||
|
||||
bindListBuilder.Append($"{comma}:{DynamicParamater}{currentCount}");
|
||||
}
|
||||
|
||||
return bindListBuilder.ToString();
|
||||
}
|
||||
|
||||
public static Dictionary<string, object> BuildDictionaryParameter(List<string> values)
|
||||
{
|
||||
var parameterDictionary = new Dictionary<string, object>();
|
||||
for (int currentCount = 0; currentCount < values.Count; ++currentCount)
|
||||
{
|
||||
parameterDictionary.Add($"{DynamicParamater}{currentCount}", values[currentCount]);
|
||||
}
|
||||
return parameterDictionary;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user