From b215d88ab39018b79336fdb19572a9537a6df8f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Rank=20Schr=C3=B8der?= Date: Fri, 22 Sep 2023 02:27:44 -0700 Subject: [PATCH] Some work to support Close' new way of handling custom fields --- src/Close/Close.csproj | 2 +- src/Close/Extensions/ParameterExtensions.cs | 8 +- src/Close/Helpers/Request.cs | 5 +- .../CustomFieldsJsonConverter.cs | 99 +++++++++++++++++++ src/Close/Models/Common/CustomFields.cs | 5 + src/Close/Models/Leads/LeadUpdateOptions.cs | 7 +- 6 files changed, 116 insertions(+), 10 deletions(-) create mode 100644 src/Close/JsonConverters/CustomFieldsJsonConverter.cs create mode 100644 src/Close/Models/Common/CustomFields.cs diff --git a/src/Close/Close.csproj b/src/Close/Close.csproj index 55fc1fb..7428631 100644 --- a/src/Close/Close.csproj +++ b/src/Close/Close.csproj @@ -5,7 +5,7 @@ disable Close Close - 0.1.5 + 0.1.6 Peter Schroeder Telzio, Inc. © 2013-2023 Telzio, Inc. diff --git a/src/Close/Extensions/ParameterExtensions.cs b/src/Close/Extensions/ParameterExtensions.cs index f11a37a..558f248 100644 --- a/src/Close/Extensions/ParameterExtensions.cs +++ b/src/Close/Extensions/ParameterExtensions.cs @@ -113,7 +113,7 @@ internal static List> FlattenParamsList(IEnumerable var index = 0; foreach (var value in list) { - var newPrefix = $"{keyPrefix}[{index}]"; + var newPrefix = $"{keyPrefix}.{index}"; flatParams.AddRange(FlattenParamsValue(value, newPrefix)); index += 1; } @@ -140,14 +140,14 @@ internal static string NewPrefix(string key, string keyPrefix) return key; } - var i = key.IndexOf("[", StringComparison.Ordinal); + var i = key.IndexOf(".", StringComparison.Ordinal); if (i == -1) { - return $"{keyPrefix}[{key}]"; + return $"{keyPrefix}.{key}"; } else { - return $"{keyPrefix}[{key.Substring(0, i)}]{key.Substring(i)}"; + return $"{keyPrefix}.{key.Substring(0, i)}]{key.Substring(i)}"; } } } \ No newline at end of file diff --git a/src/Close/Helpers/Request.cs b/src/Close/Helpers/Request.cs index 68a2d29..5d0781b 100644 --- a/src/Close/Helpers/Request.cs +++ b/src/Close/Helpers/Request.cs @@ -3,6 +3,7 @@ using System.Text.Json.Serialization; using Close.Extensions; using Close.Interfaces; +using Close.JsonConverters; using Close.Models.Common; namespace Close.Helpers; @@ -13,7 +14,7 @@ public class Request where TEntity : ICloseEntity private readonly HttpClient _httpClient; private readonly JsonSerializerOptions _jsonSerializerOptions = new() { - Converters = { new JsonStringEnumConverter() }, + Converters = { new JsonStringEnumConverter(), new CustomFieldsJsonConverter() }, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault }; @@ -76,7 +77,7 @@ internal async Task UpdateEntityAsync(string id, TUpdat Method = HttpMethod.Put, Content = content, }; - + return await SendAsync(request, cancellationToken); } diff --git a/src/Close/JsonConverters/CustomFieldsJsonConverter.cs b/src/Close/JsonConverters/CustomFieldsJsonConverter.cs new file mode 100644 index 0000000..e807031 --- /dev/null +++ b/src/Close/JsonConverters/CustomFieldsJsonConverter.cs @@ -0,0 +1,99 @@ +using System.Collections; +using System.Text.Json; +using System.Text.Json.Serialization; +using Close.Extensions; +using Close.Models.Common; + +namespace Close.JsonConverters; + +public class CustomFieldsJsonConverter : JsonConverter +{ + public override bool CanConvert(Type objectType) + { + return (objectType == typeof(CustomFields)); + } + + public override CustomFields Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + if (reader.TokenType != JsonTokenType.StartObject) + throw new JsonException(); + + var result = new CustomFields(); + + // var hej = reader.Read().FlattenParamsValue("custom"); + // + // while (reader.Read()) + // { + // if (reader.TokenType == JsonTokenType.EndObject) + // return result; + // + // // Get the key. + // if (reader.TokenType != JsonTokenType.PropertyName) + // throw new JsonException(); + // + // var propertyName = reader.GetString(); + // + // reader.Read(); + // var value = reader.Read(); + // + // // Add to dictionary. + // result.Add(propertyName!, value.FlattenParamsValue("custom")); + // } + + return result; + } + + public override void Write(Utf8JsonWriter writer, CustomFields value, JsonSerializerOptions options) + { + writer.WriteNullValue(); + + var keyValuePairs = value.FlattenParamsValue("custom"); + + foreach (var keyValuePair in keyValuePairs) + { + switch (keyValuePair.Value) + { + case string s: + writer.WriteString(keyValuePair.Key, s); + break; + case int i: + writer.WriteNumber(keyValuePair.Key, i); + break; + case long i: + writer.WriteNumber(keyValuePair.Key, i); + break; + case decimal i: + writer.WriteNumber(keyValuePair.Key, i); + break; + case double i: + writer.WriteNumber(keyValuePair.Key, i); + break; + case bool b: + writer.WriteBoolean(keyValuePair.Key, b); + break; + case DateTime d: + writer.WriteString(keyValuePair.Key, d.ToString("O")); + break; + case DateTimeOffset d: + writer.WriteString(keyValuePair.Key, d.ToString("O")); + break; + case TimeSpan d: + writer.WriteString(keyValuePair.Key, d.ToString("hh:mm:ss")); + break; + case IEnumerable enumerable: + writer.WriteStartArray(); + foreach (var item in enumerable) + { + writer.WriteString(keyValuePair.Key, item.ToString()); + } + writer.WriteEndArray(); + break; + default: + writer.WriteString(keyValuePair.Key, keyValuePair.Value.ToString()); + break; + } + + + } + } +} \ No newline at end of file diff --git a/src/Close/Models/Common/CustomFields.cs b/src/Close/Models/Common/CustomFields.cs new file mode 100644 index 0000000..c56f2e0 --- /dev/null +++ b/src/Close/Models/Common/CustomFields.cs @@ -0,0 +1,5 @@ +namespace Close.Models.Common; + +public class CustomFields : Dictionary +{ +} \ No newline at end of file diff --git a/src/Close/Models/Leads/LeadUpdateOptions.cs b/src/Close/Models/Leads/LeadUpdateOptions.cs index ef4c4e7..f98448e 100644 --- a/src/Close/Models/Leads/LeadUpdateOptions.cs +++ b/src/Close/Models/Leads/LeadUpdateOptions.cs @@ -1,5 +1,7 @@ using System.Text.Json.Serialization; using Close.Interfaces; +using Close.JsonConverters; +using Close.Models.Common; using Close.Models.Contacts; namespace Close.Models.Leads; @@ -26,7 +28,6 @@ public class LeadUpdateOptions : IUpdateOptions [JsonPropertyName("url")] public string Url { get; set; } - - [JsonPropertyName("custom")] - public Dictionary Custom { get; set; } + + public CustomFields Custom { get; set; } } \ No newline at end of file