Skip to content

Commit

Permalink
Some work to support Close' new way of handling custom fields
Browse files Browse the repository at this point in the history
  • Loading branch information
petefox committed Sep 22, 2023
1 parent 918fa4b commit b215d88
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Close/Close.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Nullable>disable</Nullable>
<AssemblyName>Close</AssemblyName>
<RootNamespace>Close</RootNamespace>
<Version>0.1.5</Version>
<Version>0.1.6</Version>
<Authors>Peter Schroeder</Authors>
<Company>Telzio, Inc.</Company>
<Copyright>© 2013-2023 Telzio, Inc.</Copyright>
Expand Down
8 changes: 4 additions & 4 deletions src/Close/Extensions/ParameterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ internal static List<KeyValuePair<string, object>> 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;
}
Expand All @@ -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)}";
}
}
}
5 changes: 3 additions & 2 deletions src/Close/Helpers/Request.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,7 +14,7 @@ public class Request<TEntity> where TEntity : ICloseEntity
private readonly HttpClient _httpClient;
private readonly JsonSerializerOptions _jsonSerializerOptions = new()
{
Converters = { new JsonStringEnumConverter() },
Converters = { new JsonStringEnumConverter(), new CustomFieldsJsonConverter() },
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault
};

Expand Down Expand Up @@ -76,7 +77,7 @@ internal async Task<TEntity> UpdateEntityAsync<TUpdateOptions>(string id, TUpdat
Method = HttpMethod.Put,
Content = content,
};

return await SendAsync<TEntity>(request, cancellationToken);
}

Expand Down
99 changes: 99 additions & 0 deletions src/Close/JsonConverters/CustomFieldsJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -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<CustomFields>
{
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;
}


}
}
}
5 changes: 5 additions & 0 deletions src/Close/Models/Common/CustomFields.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Close.Models.Common;

public class CustomFields : Dictionary<string, object>
{
}
7 changes: 4 additions & 3 deletions src/Close/Models/Leads/LeadUpdateOptions.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -26,7 +28,6 @@ public class LeadUpdateOptions : IUpdateOptions

[JsonPropertyName("url")]
public string Url { get; set; }

[JsonPropertyName("custom")]
public Dictionary<string, object> Custom { get; set; }

public CustomFields Custom { get; set; }
}

0 comments on commit b215d88

Please sign in to comment.