From 0d7de13cb249a3a59f1cbb1519a4bf7af1651ec6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BD=D1=8E=D1=82=D0=B8=D0=BD=20=D0=9C=D0=B0=D0=BA?= =?UTF-8?q?=D1=81=D0=B8=D0=BC=20=D0=9D=D0=B8=D0=BA=D0=BE=D0=BB=D0=B0=D0=B5?= =?UTF-8?q?=D0=B2=D0=B8=D1=87?= Date: Tue, 28 Sep 2021 15:15:32 +0300 Subject: [PATCH] Added method GetPersonRoles --- EsiaNET/EsiaClientExtensions.cs | 55 +++++++++++++++++ EsiaNET/EsiaHelpers.cs | 6 ++ EsiaNET/PersonRole.cs | 104 ++++++++++++++++++++++++++++++++ 3 files changed, 165 insertions(+) create mode 100644 EsiaNET/PersonRole.cs diff --git a/EsiaNET/EsiaClientExtensions.cs b/EsiaNET/EsiaClientExtensions.cs index a2bd4c6..9942ad7 100644 --- a/EsiaNET/EsiaClientExtensions.cs +++ b/EsiaNET/EsiaClientExtensions.cs @@ -322,5 +322,60 @@ public static async Task> GetPersonVehiclesAsync(this E return result; } + + /// + /// Get vehicles of authorized user + /// + /// + /// VehicleInfo array + public static Task> GetPersonRolesAsync(this EsiaClient client) + { + if (client.Token == null) + { + throw new ArgumentNullException("Token"); + } + + return client.GetPersonRolesAsync(client.Token.SbjId); + } + + /// + /// Get person roles of specified user + /// + /// + /// User oid + /// VehicleInfo array + public static async Task> GetPersonRolesAsync(this EsiaClient client, string oid) + { + if (string.IsNullOrEmpty(oid)) + { + throw new ArgumentNullException(nameof(oid)); + } + + var uri = $"{EsiaHelpers.NormalizeUri(client.Options.RestUri, client.Options.PrnsSfx)}{oid}/roles"; + var result = new List(); + var response = await client.GetAsync(uri); + + if (response == null) + { + return result; + } + + IDictionary vehicleDictionary = JObject.Parse(response); + + if (!vehicleDictionary.ContainsKey("elements")) + { + return result; + } + + foreach ( var vehicle in vehicleDictionary["elements"] ) + { + if (vehicle is JObject personRole) + { + result.Add(new PersonRole(personRole)); + } + } + + return result; + } } } \ No newline at end of file diff --git a/EsiaNET/EsiaHelpers.cs b/EsiaNET/EsiaHelpers.cs index 6f0d432..bc0d3a9 100644 --- a/EsiaNET/EsiaHelpers.cs +++ b/EsiaNET/EsiaHelpers.cs @@ -12,6 +12,12 @@ public static string PropertyValueIfExists(string property, IDictionary dictionary) + { + var value = PropertyValueIfExists(property, dictionary); + + return !string.IsNullOrWhiteSpace(value) && value.ToLowerInvariant() == "true"; + } public static DateTime DateFromUnixSeconds(double seconds) { diff --git a/EsiaNET/PersonRole.cs b/EsiaNET/PersonRole.cs new file mode 100644 index 0000000..6d64276 --- /dev/null +++ b/EsiaNET/PersonRole.cs @@ -0,0 +1,104 @@ +using System; +using Newtonsoft.Json.Linq; + +namespace EsiaNET +{ + /// + /// Provides personal role information + /// + public class PersonRole + { + internal PersonRole(JObject personRole) + { + if (personRole == null) + { + return; + } + + Oid = Convert.ToInt64(EsiaHelpers.PropertyValueIfExists("oid", personRole)); + PersonOid = Convert.ToInt64(EsiaHelpers.PropertyValueIfExists("prnOid", personRole)); + FullName = EsiaHelpers.PropertyValueIfExists("fullName", personRole); + ShortName = EsiaHelpers.PropertyValueIfExists("shortName", personRole); + Ogrn = EsiaHelpers.PropertyValueIfExists("ogrn", personRole); + Type = EsiaHelpers.PropertyValueIfExists("type", personRole); + Chief = EsiaHelpers.PropertyBoolValueIfExists("chief", personRole); + Admin = EsiaHelpers.PropertyBoolValueIfExists("admin", personRole); + Phone = EsiaHelpers.PropertyValueIfExists("phone", personRole); + Email = EsiaHelpers.PropertyValueIfExists("email", personRole); + Active = EsiaHelpers.PropertyBoolValueIfExists("active", personRole); + HasRightOfSubstitution = EsiaHelpers.PropertyBoolValueIfExists("hasRightOfSubstitution", personRole); + HasApprovalTabAccess = EsiaHelpers.PropertyBoolValueIfExists("hasApprovalTabAccess", personRole); + IsLiquidated = EsiaHelpers.PropertyBoolValueIfExists("isLiquidated", personRole); + } + + /// + /// Organization identifier + /// + public long Oid { get; set; } + + /// + /// Person Organization identifier + /// + public long PersonOid { get; set; } + + /// + /// Full name for organization + /// + public string FullName { get; set; } + + /// + /// Short name for organization + /// + public string ShortName { get; set; } + + /// + /// Ogrn + /// + public string Ogrn { get; set; } + + /// + /// Type for organization + /// + public string Type { get; set; } + + /// + /// Chief current organization + /// + public bool Chief { get; set; } + + /// + /// Administrator for current organization + /// + public bool Admin { get; set; } + + /// + /// Contact phone number + /// + public string Phone { get; set; } + + /// + /// Contact Email + /// + public string Email { get; set; } + + /// + /// Flag determinate what a company have active status + /// + public bool Active { get; set; } + + /// + /// Flag determinate what a company has right of substitution + /// + public bool? HasRightOfSubstitution { get; set; } + + /// + /// Flag determinate what a company has approval tab access + /// + public bool HasApprovalTabAccess { get; set; } + + /// + /// Flag determinate what a company is liquidated + /// + public bool IsLiquidated { get; set; } + } +} \ No newline at end of file