namespace Shared.Models { public class HeadersModel { public HeadersModel(string name, string val) { this.name = name; this.val = val; } public string name { get; set; } public string val { get; set; } #region Init public static List Init(string name, string val) { if (string.IsNullOrWhiteSpace(val)) return new List(); return new List() { new HeadersModel(name, val)}; } public static List Init(List headers) { return headers ?? new List(); } public static List Init(params (string name, string val)[] headers) { var h = new List(headers.Count()); foreach (var i in headers) { if (!string.IsNullOrWhiteSpace(i.val)) h.Add(new HeadersModel(i.name, i.val)); } return h; } public static List Init(Dictionary defaultHeaders, params (string name, string val)[] headers) { return Join(Init(headers), defaultHeaders); } public static List Init(IEnumerable> headers) { if (headers == null || !headers.Any()) return new List(); var h = new List(headers.Count()); foreach (var i in headers) { if (!string.IsNullOrWhiteSpace(i.Value)) h.Add(new HeadersModel(i.Key, i.Value)); } return h; } #endregion #region Join public static List Join(List h1, List h2) { if (h1 == null) return h2 ?? new List(); if (h2 == null) return h1 ?? new List(); var result = new List(h1); result.AddRange(h2); return result; } public static List Join(List h1, Dictionary h2) { if (h1 == null) { if (h2 == null) return new List(); return Init(h2); } if (h2 == null) return h1 ?? new List(); var result = new List(h1); foreach (var _h2 in h2) { if (!string.IsNullOrWhiteSpace(_h2.Value)) result.Add(new HeadersModel(_h2.Key, _h2.Value)); } return result; } #endregion #region InitOrNull public static List InitOrNull(Dictionary headers) { if (headers == null || headers.Count == 0) return null; var h = new List(headers.Count); foreach (var i in headers) { if (!string.IsNullOrWhiteSpace(i.Value)) h.Add(new HeadersModel(i.Key, i.Value)); } if (h.Count == 0) return null; return h; } #endregion } }