diff --git a/README.md b/README.md index 0eedd8e..8fbfc7d 100644 --- a/README.md +++ b/README.md @@ -37,12 +37,11 @@ Filtering and field selection requires references to fields. All endpoints and f The client supports the usage of multiple authentication tokens. The number of API requests is limited to 3600 request per hour, which in some cases in not enough. When multiple tokens are used, the client will always use the token with the highest remaining request value. More information about rate limiting can be found on the [4me developer website](https://developer.4me.com/v1/#rate-limiting). #### Response timing -The 4me REST API limits the number of requests to 10 per second. The client will keep track of the response time and lock the process to make sure it takes at lease 116 milliseconds per request. +The 4me REST API limits the number of requests to 10 per second. The client will keep track of the response time and lock the process to make sure it takes at lease 100 milliseconds per request. #### Exception handling A custom, Sdk4meException, is implemented. It will convert the API exception response to a string value. - # Sdk4meClient ### Minimum example ```csharp @@ -155,6 +154,18 @@ bool result = client.Organizations.DeleteAddress(organization, addresses[0]); ``` The client exposes a Delete and DeleteAll method. Those can only be used to delete child or relational objects not an object itself. +### Custom fields +```csharp +Person person = client.People.Get(new Filter("EmployeeID", FilterCondition.Equality, 100)).First(); +people.CustomFields.AddOrUpdate("support_countries", new JArray() { "BE", "NL" }); +people.CustomFields.AddOrUpdate("hire_date", new DateTime(2022, 08, 01)); +people.CustomFields.AddOrUpdate("working_hours", 8.45); +person = client.People.Update(person); + +person.CustomFields.TryGetValue("hire_date", out DateTime date); +double workingHours = person.CustomFields.Get("working_hours"); +``` + ### Upload an attachment ```csharp Request request = client.Requests.Get(123); diff --git a/Source/Sdk4me/Entities/CustomFields/CustomFIeldCollection.cs b/Source/Sdk4me/Entities/CustomFields/CustomFIeldCollection.cs index 0eba676..1c01435 100644 --- a/Source/Sdk4me/Entities/CustomFields/CustomFIeldCollection.cs +++ b/Source/Sdk4me/Entities/CustomFields/CustomFIeldCollection.cs @@ -10,9 +10,9 @@ namespace Sdk4me /// A collection. /// [DebuggerDisplay("Count = {Count}")] - public sealed class CustomFieldCollection : IDictionary + public sealed class CustomFieldCollection : IDictionary { - private readonly Dictionary collection = new Dictionary(); + private readonly Dictionary collection = new Dictionary(); /// /// A change that occurs when a value is added, updated and deleted. @@ -46,7 +46,7 @@ internal CustomFieldCollection(List customFields) /// /// The identifier of the element to get or set. /// The element with the specified identifier. - public string this[string identifier] + public object this[string identifier] { get => collection[identifier]; set @@ -57,6 +57,17 @@ public string this[string identifier] } } + /// + /// Gets the element with the specified identifier. + /// + /// + /// The identifier of the element to get. + /// The element with the specified identifier. + public T Get(string identifier) + { + return (T)collection[identifier]; + } + /// /// Gets a collection containing the identifiers in the . /// @@ -68,7 +79,7 @@ public ICollection Keys /// /// Gets a collection containing the values in the . /// - public ICollection Values + public ICollection Values { get => collection.Values; } @@ -86,7 +97,7 @@ public int Count /// public bool IsReadOnly { - get => ((IDictionary)collection).IsReadOnly; + get => ((IDictionary)collection).IsReadOnly; } /// @@ -94,7 +105,7 @@ public bool IsReadOnly /// /// The identifier of the element to add. /// The value of the element to add. The value can be null for reference types. - public void Add(string id, string value) + public void Add(string id, object value) { collection.Add(id, value); Changed?.Invoke(this, EventArgs.Empty); @@ -105,7 +116,7 @@ public void Add(string id, string value) /// /// The identifier of the element to add or update. /// The value of the element to add or update. The value can be null for reference types. - public void AddOrUpdate(string id, string value) + public void AddOrUpdate(string id, object value) { if (collection.ContainsKey(id)) { @@ -124,7 +135,7 @@ public void AddOrUpdate(string id, string value) /// Adds the specified identifier and value to the collection. /// /// The object to add to the . - public void Add(KeyValuePair item) + public void Add(KeyValuePair item) { Add(item.Key, item.Value); } @@ -133,7 +144,7 @@ public void Add(KeyValuePair item) /// Adds the specified identifier and value to the collection; or updates the value when the identifier already exists. /// /// The object to add to the . - public void AddOrUpdate(KeyValuePair item) + public void AddOrUpdate(KeyValuePair item) { AddOrUpdate(item.Key, item.Value); } @@ -152,7 +163,7 @@ public void Clear() /// /// /// - public bool Contains(KeyValuePair item) + public bool Contains(KeyValuePair item) { return collection.Contains(item); } @@ -172,16 +183,16 @@ public bool ContainsKey(string id) /// /// The one-dimensional System.Array that is the destination of the elements copied from . The System.Array must have zero-based indexing. /// The zero-based index in array at which copying begins. - public void CopyTo(KeyValuePair[] array, int arrayIndex) + public void CopyTo(KeyValuePair[] array, int arrayIndex) { - ((IDictionary)collection).CopyTo(array, arrayIndex); + ((IDictionary)collection).CopyTo(array, arrayIndex); } /// /// Returns an enumerator that iterates through the . /// /// A .Enumerator structure for the . - public IEnumerator> GetEnumerator() + public IEnumerator> GetEnumerator() { return collection.GetEnumerator(); } @@ -204,21 +215,39 @@ public bool Remove(string identifier) /// /// The object to remove from the . /// true if item was successfully removed from the ; otherwise, false. This method also returns false if item is not found in the original . - public bool Remove(KeyValuePair item) + public bool Remove(KeyValuePair item) { - bool result = ((IDictionary)collection).Remove(item); + bool result = ((IDictionary)collection).Remove(item); if (result) Changed?.Invoke(this, EventArgs.Empty); return result; } + /// + /// Gets the value associated with the specified identifier. + /// + /// + /// The identifier whose value to get. + /// When this method returns, the value associated with the specified identifier, if the identifier is found; otherwise, the default value for the type of the value parameter. This parameter is passed uninitialized. + /// True if the contains an element with the specified identifier; otherwise, false. + public bool TryGetValue(string identifier, out T value) + { + if (collection.TryGetValue(identifier, out object v)) + { + value = (T)v; + return true; + } + value = default(T); + return false; + } + /// /// Gets the value associated with the specified identifier. /// /// The identifier whose value to get. /// When this method returns, the value associated with the specified identifier, if the identifier is found; otherwise, the default value for the type of the value parameter. This parameter is passed uninitialized. /// True if the contains an element with the specified identifier; otherwise, false. - public bool TryGetValue(string identifier, out string value) + public bool TryGetValue(string identifier, out object value) { return collection.TryGetValue(identifier, out value); } diff --git a/Source/Sdk4me/Entities/CustomFields/CustomField.cs b/Source/Sdk4me/Entities/CustomFields/CustomField.cs index b817e4e..d37e004 100644 --- a/Source/Sdk4me/Entities/CustomFields/CustomField.cs +++ b/Source/Sdk4me/Entities/CustomFields/CustomField.cs @@ -8,7 +8,7 @@ namespace Sdk4me public sealed class CustomField { private string id; - private string value; + private object value; #region id @@ -30,7 +30,7 @@ public string ID /// The value of the . /// [JsonProperty("value")] - public string Value + public object Value { get => value; set => this.value = value; diff --git a/Source/Sdk4me/Sdk4me.csproj b/Source/Sdk4me/Sdk4me.csproj index 5841abe..566ee45 100644 --- a/Source/Sdk4me/Sdk4me.csproj +++ b/Source/Sdk4me/Sdk4me.csproj @@ -22,13 +22,13 @@ 7.3 - 2.0.3.2 - 2.0.3.2 + 2.0.4.0 + 2.0.4.0 6.0 Klaas Vandeweerdt A .NET client for accessing the 4me v1 REST API MIT License - 2.0.3.2 + 2.0.4 True git LogoDark128x218.png