From 51f464bad01cfc666b3a4755ea3ad57b3c7c9c14 Mon Sep 17 00:00:00 2001 From: Matthias Langhard Date: Tue, 15 Jun 2021 11:48:04 +0200 Subject: [PATCH] chore: moves to ExpandoObject for updating entities because not all properties need to be part of the payload --- src/ClientContacts/ClientContactsApi.cs | 3 ++- src/ClientContacts/IClientContactsApi.cs | 3 ++- src/Clients/ClientsApi.cs | 3 ++- src/Clients/IClientsApi.cs | 3 ++- src/Shared/BaseApi.cs | 11 +++++++++-- src/Shared/IBaseApi.cs | 3 ++- src/Tasks/ITasksApi.cs | 3 ++- src/Tasks/TasksApi.cs | 3 ++- tests/ClientsApiTests.cs | 4 +++- tests/TasksApiTests.cs | 5 ++++- 10 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/ClientContacts/ClientContactsApi.cs b/src/ClientContacts/ClientContactsApi.cs index 781862d..12f8bfd 100644 --- a/src/ClientContacts/ClientContactsApi.cs +++ b/src/ClientContacts/ClientContactsApi.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Dynamic; using System.Linq; using System.Threading.Tasks; using Novaloop.PaymoApi.ClientContacts.Models; @@ -41,7 +42,7 @@ namespace Novaloop.PaymoApi.ClientContacts } /// - public async Task UpdateClientContact(ClientContact clientContact, int clientContactId) + public async Task UpdateClientContact(ExpandoObject clientContact, int clientContactId) { await _baseApi.Update(clientContact, clientContactId); } diff --git a/src/ClientContacts/IClientContactsApi.cs b/src/ClientContacts/IClientContactsApi.cs index e4c0660..f018dfc 100644 --- a/src/ClientContacts/IClientContactsApi.cs +++ b/src/ClientContacts/IClientContactsApi.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Dynamic; using System.Threading.Tasks; using Novaloop.PaymoApi.ClientContacts.Models; @@ -36,6 +37,6 @@ namespace Novaloop.PaymoApi.ClientContacts /// /// /// - Task UpdateClientContact(ClientContact clientContact, int clientContactId); + Task UpdateClientContact(ExpandoObject clientContact, int clientContactId); } } \ No newline at end of file diff --git a/src/Clients/ClientsApi.cs b/src/Clients/ClientsApi.cs index e688974..d7b40cb 100644 --- a/src/Clients/ClientsApi.cs +++ b/src/Clients/ClientsApi.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Dynamic; using System.Linq; using System.Threading.Tasks; using Novaloop.PaymoApi.Clients.Models; @@ -41,7 +42,7 @@ namespace Novaloop.PaymoApi.Clients } /// - public async Task UpdateClient(Client client, int clientId) + public async Task UpdateClient(ExpandoObject client, int clientId) { await _baseApi.Update(client, clientId); } diff --git a/src/Clients/IClientsApi.cs b/src/Clients/IClientsApi.cs index b6d45c6..3b1b765 100644 --- a/src/Clients/IClientsApi.cs +++ b/src/Clients/IClientsApi.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Dynamic; using System.Threading.Tasks; using Novaloop.PaymoApi.Clients.Models; @@ -36,6 +37,6 @@ namespace Novaloop.PaymoApi.Clients /// /// /// - Task UpdateClient(Client client, int clientId); + Task UpdateClient(ExpandoObject client, int clientId); } } \ No newline at end of file diff --git a/src/Shared/BaseApi.cs b/src/Shared/BaseApi.cs index 424b880..11d1106 100644 --- a/src/Shared/BaseApi.cs +++ b/src/Shared/BaseApi.cs @@ -1,6 +1,10 @@ +using System.Dynamic; using System.Net.Http; +using System.Text; using System.Threading.Tasks; using Microsoft.Extensions.Options; +using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; using Novaloop.PaymoApi.Extensions; namespace Novaloop.PaymoApi.Shared @@ -70,10 +74,13 @@ namespace Novaloop.PaymoApi.Shared /// entity information to update the entity with /// id of the entity to update /// - public async Task Update(TCreatType entity, int id) + public async Task Update(ExpandoObject entity, int id) { _client.SetApiKeyHeader(_options.ApiToken); - var response = await _client.PutAsJsonAsync($"api/{ResourceUri}/{id}", entity); + var serializerSettings = new JsonSerializerSettings {ContractResolver = new CamelCasePropertyNamesContractResolver()}; + var body = JsonConvert.SerializeObject(entity, serializerSettings); + var stringContent = new StringContent(body, Encoding.UTF8, "application/json"); + var response = await _client.PutAsync($"api/{ResourceUri}/{id}", stringContent); await response.ThrowExceptionWithDetailsIfUnsuccessful(); } } diff --git a/src/Shared/IBaseApi.cs b/src/Shared/IBaseApi.cs index fbf7b58..842e55a 100644 --- a/src/Shared/IBaseApi.cs +++ b/src/Shared/IBaseApi.cs @@ -1,3 +1,4 @@ +using System.Dynamic; using System.Threading.Tasks; namespace Novaloop.PaymoApi.Shared @@ -36,6 +37,6 @@ namespace Novaloop.PaymoApi.Shared /// entity information to update the entity with /// id of the entity to update /// - Task Update( TCreatType entity, int id); + Task Update(ExpandoObject entity, int id); } } \ No newline at end of file diff --git a/src/Tasks/ITasksApi.cs b/src/Tasks/ITasksApi.cs index 7aed1d6..d657678 100644 --- a/src/Tasks/ITasksApi.cs +++ b/src/Tasks/ITasksApi.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Dynamic; using System.Threading.Tasks; namespace Novaloop.PaymoApi.Tasks @@ -35,6 +36,6 @@ namespace Novaloop.PaymoApi.Tasks /// /// /// - Task UpdateTask(Novaloop.PaymoApi.Tasks.Models.Task task, int taskId); + Task UpdateTask(ExpandoObject task, int taskId); } } \ No newline at end of file diff --git a/src/Tasks/TasksApi.cs b/src/Tasks/TasksApi.cs index 103506c..fde8897 100644 --- a/src/Tasks/TasksApi.cs +++ b/src/Tasks/TasksApi.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Dynamic; using System.Linq; using System.Threading.Tasks; using Novaloop.PaymoApi.Shared; @@ -45,7 +46,7 @@ namespace Novaloop.PaymoApi.Tasks /// - public async Task UpdateTask(Novaloop.PaymoApi.Tasks.Models.Task task, int taskId) + public async Task UpdateTask(ExpandoObject task, int taskId) { await _baseApi.Update(task, taskId); } diff --git a/tests/ClientsApiTests.cs b/tests/ClientsApiTests.cs index d60e24a..319d113 100644 --- a/tests/ClientsApiTests.cs +++ b/tests/ClientsApiTests.cs @@ -1,3 +1,4 @@ +using System.Dynamic; using System.Linq; using Novaloop.PaymoApi.Clients; using Novaloop.PaymoApi.Clients.Models; @@ -84,7 +85,8 @@ namespace Novaloop.PaymoApi.Tests { // Arrange var createdClient = await _clientsApi.CreateClient(_testClient); - var clientUpdateInfo = new Client {Name = "Updated"}; + dynamic clientUpdateInfo = new ExpandoObject(); + clientUpdateInfo.Name = "Updated"; // Act await _clientsApi.UpdateClient(clientUpdateInfo, createdClient.Id); diff --git a/tests/TasksApiTests.cs b/tests/TasksApiTests.cs index 566a929..9cf2728 100644 --- a/tests/TasksApiTests.cs +++ b/tests/TasksApiTests.cs @@ -1,3 +1,4 @@ +using System.Dynamic; using System.Linq; using Novaloop.PaymoApi.Shared; using Novaloop.PaymoApi.Tasks; @@ -95,7 +96,9 @@ namespace Novaloop.PaymoApi.Tests var existingTaskListId = (await _tasksApi.GetTasks()).First().TasklistId; _testTask.TasklistId = existingTaskListId; var createdTask = await _tasksApi.CreateTask(_testTask); - var taskUpdateInfo = new Task {Name = "Updated", TasklistId = _testTask.TasklistId}; + dynamic taskUpdateInfo = new ExpandoObject(); + taskUpdateInfo.Name = "Updated"; + taskUpdateInfo.TaskListId = _testTask.TasklistId; // Act await _tasksApi.UpdateTask(taskUpdateInfo, createdTask.Id);