Add migrated to .NET 8.0 variant of Hcs.Client
This commit is contained in:
101
Hcs.ClientNet/Client/Api/Request/RequestHelper.cs
Normal file
101
Hcs.ClientNet/Client/Api/Request/RequestHelper.cs
Normal file
@ -0,0 +1,101 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace Hcs.ClientNet.Api.Request
|
||||
{
|
||||
internal static class RequestHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Подготовка заголовка сообщения отправляемого в ГИС ЖКХ с обязательными атрибутами.
|
||||
/// Заголовки могут быть разного типа для разных типов сообщений, но имена полей одинаковые.
|
||||
/// </summary>
|
||||
internal static THeader CreateHeader<THeader>(ClientBase client) where THeader : class
|
||||
{
|
||||
try
|
||||
{
|
||||
var instance = Activator.CreateInstance(typeof(THeader));
|
||||
foreach (var prop in instance.GetType().GetProperties())
|
||||
{
|
||||
switch (prop.Name)
|
||||
{
|
||||
case "Item":
|
||||
prop.SetValue(instance, client.OrgPPAGUID);
|
||||
break;
|
||||
|
||||
case "ItemElementName":
|
||||
prop.SetValue(instance, Enum.Parse(prop.PropertyType, "orgPPAGUID"));
|
||||
break;
|
||||
|
||||
case "MessageGUID":
|
||||
prop.SetValue(instance, Guid.NewGuid().ToString());
|
||||
break;
|
||||
|
||||
case "Date":
|
||||
prop.SetValue(instance, DateTime.Now);
|
||||
break;
|
||||
|
||||
case "IsOperatorSignatureSpecified":
|
||||
if (client.Role == OrganizationRole.RC || client.Role == OrganizationRole.RSO)
|
||||
{
|
||||
prop.SetValue(instance, true);
|
||||
}
|
||||
break;
|
||||
|
||||
case "IsOperatorSignature":
|
||||
if (client.Role == OrganizationRole.RC || client.Role == OrganizationRole.RSO)
|
||||
{
|
||||
prop.SetValue(instance, true);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return instance as THeader;
|
||||
}
|
||||
catch (ArgumentNullException e)
|
||||
{
|
||||
throw new ApplicationException($"Error occured while building request header: {e.Message}");
|
||||
}
|
||||
catch (SystemException e)
|
||||
{
|
||||
throw new ApplicationException($"Error occured while building request header: {e.GetBaseException().Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Для объекта запроса возвращает значение строки свойства version
|
||||
/// </summary>
|
||||
internal static string GetRequestVersionString(object requestObject)
|
||||
{
|
||||
if (requestObject == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var versionHost = requestObject;
|
||||
if (versionHost != null)
|
||||
{
|
||||
var versionProperty = versionHost.GetType().GetProperties().FirstOrDefault(x => x.Name == "version");
|
||||
if (versionProperty != null)
|
||||
{
|
||||
return versionProperty.GetValue(versionHost) as string;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var field in requestObject.GetType().GetFields())
|
||||
{
|
||||
versionHost = field.GetValue(requestObject);
|
||||
if (versionHost != null)
|
||||
{
|
||||
var versionProperty = versionHost.GetType().GetProperties().FirstOrDefault(x => x.Name == "version");
|
||||
if (versionProperty != null)
|
||||
{
|
||||
return versionProperty.GetValue(versionHost) as string;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user