Skip to content

Commit

Permalink
Update on CustomField and CustomFieldCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
klaasvandeweerdt committed Aug 16, 2022
1 parent 9928c00 commit 4588149
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 23 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<DateTime>("hire_date", out DateTime date);
double workingHours = person.CustomFields.Get<double>("working_hours");
```

### Upload an attachment
```csharp
Request request = client.Requests.Get(123);
Expand Down
61 changes: 45 additions & 16 deletions Source/Sdk4me/Entities/CustomFields/CustomFIeldCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ namespace Sdk4me
/// A <see cref="CustomField"/> collection.
/// </summary>
[DebuggerDisplay("Count = {Count}")]
public sealed class CustomFieldCollection : IDictionary<string, string>
public sealed class CustomFieldCollection : IDictionary<string, object>
{
private readonly Dictionary<string, string> collection = new Dictionary<string, string>();
private readonly Dictionary<string, object> collection = new Dictionary<string, object>();

/// <summary>
/// A change that occurs when a value is added, updated and deleted.
Expand Down Expand Up @@ -46,7 +46,7 @@ internal CustomFieldCollection(List<CustomField> customFields)
/// </summary>
/// <param name="identifier">The identifier of the element to get or set.</param>
/// <returns>The element with the specified identifier.</returns>
public string this[string identifier]
public object this[string identifier]
{
get => collection[identifier];
set
Expand All @@ -57,6 +57,17 @@ public string this[string identifier]
}
}

/// <summary>
/// Gets the element with the specified identifier.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="identifier">The identifier of the element to get.</param>
/// <returns>The element with the specified identifier.</returns>
public T Get<T>(string identifier)
{
return (T)collection[identifier];
}

/// <summary>
/// Gets a collection containing the identifiers in the <see cref="CustomFieldCollection"/>.
/// </summary>
Expand All @@ -68,7 +79,7 @@ public ICollection<string> Keys
/// <summary>
/// Gets a collection containing the values in the <see cref="CustomFieldCollection"/>.
/// </summary>
public ICollection<string> Values
public ICollection<object> Values
{
get => collection.Values;
}
Expand All @@ -86,15 +97,15 @@ public int Count
/// </summary>
public bool IsReadOnly
{
get => ((IDictionary<string, string>)collection).IsReadOnly;
get => ((IDictionary<string, object>)collection).IsReadOnly;
}

/// <summary>
/// Adds the specified identifier and value to the collection.
/// </summary>
/// <param name="id">The identifier of the element to add.</param>
/// <param name="value">The value of the element to add. The value can be null for reference types.</param>
public void Add(string id, string value)
public void Add(string id, object value)
{
collection.Add(id, value);
Changed?.Invoke(this, EventArgs.Empty);
Expand All @@ -105,7 +116,7 @@ public void Add(string id, string value)
/// </summary>
/// <param name="id">The identifier of the element to add or update.</param>
/// <param name="value">The value of the element to add or update. The value can be null for reference types.</param>
public void AddOrUpdate(string id, string value)
public void AddOrUpdate(string id, object value)
{
if (collection.ContainsKey(id))
{
Expand All @@ -124,7 +135,7 @@ public void AddOrUpdate(string id, string value)
/// Adds the specified identifier and value to the collection.
/// </summary>
/// <param name="item">The object to add to the <see cref="CustomFieldCollection"/>.</param>
public void Add(KeyValuePair<string, string> item)
public void Add(KeyValuePair<string, object> item)
{
Add(item.Key, item.Value);
}
Expand All @@ -133,7 +144,7 @@ public void Add(KeyValuePair<string, string> item)
/// Adds the specified identifier and value to the collection; or updates the value when the identifier already exists.
/// </summary>
/// <param name="item">The object to add to the <see cref="CustomFieldCollection"/>.</param>
public void AddOrUpdate(KeyValuePair<string, string> item)
public void AddOrUpdate(KeyValuePair<string, object> item)
{
AddOrUpdate(item.Key, item.Value);
}
Expand All @@ -152,7 +163,7 @@ public void Clear()
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public bool Contains(KeyValuePair<string, string> item)
public bool Contains(KeyValuePair<string, object> item)
{
return collection.Contains(item);
}
Expand All @@ -172,16 +183,16 @@ public bool ContainsKey(string id)
/// </summary>
/// <param name="array">The one-dimensional System.Array that is the destination of the elements copied from <see cref="CustomFieldCollection"/>. The System.Array must have zero-based indexing.</param>
/// <param name="arrayIndex">The zero-based index in array at which copying begins.</param>
public void CopyTo(KeyValuePair<string, string>[] array, int arrayIndex)
public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
{
((IDictionary<string, string>)collection).CopyTo(array, arrayIndex);
((IDictionary<string, object>)collection).CopyTo(array, arrayIndex);
}

/// <summary>
/// Returns an enumerator that iterates through the <see cref="CustomFieldCollection"/>.
/// </summary>
/// <returns>A <see cref="CustomFieldCollection"/>.Enumerator structure for the <see cref="CustomFieldCollection"/>.</returns>
public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
{
return collection.GetEnumerator();
}
Expand All @@ -204,21 +215,39 @@ public bool Remove(string identifier)
/// </summary>
/// <param name="item">The object to remove from the <see cref="CustomFieldCollection"/>.</param>
/// <returns>true if item was successfully removed from the <see cref="CustomFieldCollection"/>; otherwise, false. This method also returns false if item is not found in the original <see cref="CustomFieldCollection"/>.</returns>
public bool Remove(KeyValuePair<string, string> item)
public bool Remove(KeyValuePair<string, object> item)
{
bool result = ((IDictionary<string, string>)collection).Remove(item);
bool result = ((IDictionary<string, object>)collection).Remove(item);
if (result)
Changed?.Invoke(this, EventArgs.Empty);
return result;
}

/// <summary>
/// Gets the value associated with the specified identifier.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="identifier">The identifier whose value to get.</param>
/// <param name="value">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.</param>
/// <returns>True if the <see cref="CustomFieldCollection"/> contains an element with the specified identifier; otherwise, false.</returns>
public bool TryGetValue<T>(string identifier, out T value)
{
if (collection.TryGetValue(identifier, out object v))
{
value = (T)v;
return true;
}
value = default(T);
return false;
}

/// <summary>
/// Gets the value associated with the specified identifier.
/// </summary>
/// <param name="identifier">The identifier whose value to get.</param>
/// <param name="value">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.</param>
/// <returns>True if the <see cref="CustomFieldCollection"/> contains an element with the specified identifier; otherwise, false.</returns>
public bool TryGetValue(string identifier, out string value)
public bool TryGetValue(string identifier, out object value)
{
return collection.TryGetValue(identifier, out value);
}
Expand Down
4 changes: 2 additions & 2 deletions Source/Sdk4me/Entities/CustomFields/CustomField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Sdk4me
public sealed class CustomField
{
private string id;
private string value;
private object value;

#region id

Expand All @@ -30,7 +30,7 @@ public string ID
/// The value of the <see cref="CustomField"/>.
/// </summary>
[JsonProperty("value")]
public string Value
public object Value
{
get => value;
set => this.value = value;
Expand Down
6 changes: 3 additions & 3 deletions Source/Sdk4me/Sdk4me.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@

<PropertyGroup>
<LangVersion>7.3</LangVersion>
<AssemblyVersion>2.0.3.2</AssemblyVersion>
<FileVersion>2.0.3.2</FileVersion>
<AssemblyVersion>2.0.4.0</AssemblyVersion>
<FileVersion>2.0.4.0</FileVersion>
<AnalysisLevel>6.0</AnalysisLevel>
<Authors>Klaas Vandeweerdt</Authors>
<Description>A .NET client for accessing the 4me v1 REST API</Description>
<Copyright>MIT License</Copyright>
<Version>2.0.3.2</Version>
<Version>2.0.4</Version>
<ProduceReferenceAssembly>True</ProduceReferenceAssembly>
<RepositoryType>git</RepositoryType>
<PackageIcon>LogoDark128x218.png</PackageIcon>
Expand Down

0 comments on commit 4588149

Please sign in to comment.