diff --git a/src/CommonLibrariesForNET/IJsonHttpClient.cs b/src/CommonLibrariesForNET/IJsonHttpClient.cs index fe997322..be985316 100644 --- a/src/CommonLibrariesForNET/IJsonHttpClient.cs +++ b/src/CommonLibrariesForNET/IJsonHttpClient.cs @@ -22,10 +22,10 @@ public interface IJsonHttpClient : IDisposable Task HttpBinaryDataPostAsync(string urlSuffix, object inputObject, byte[] fileContents, string headerName, string fileName); // PATCH - Task HttpPatchAsync(object inputObject, string urlSuffix); - Task HttpPatchAsync(object inputObject, Uri uri); - Task HttpPatchAsync(object inputObject, string urlSuffix, bool ignoreNull); - Task HttpPatchAsync(object inputObject, Uri uri, NullValueHandling nullValueHandling); + Task HttpPatchAsync(object inputObject, string urlSuffix, IDictionary headers = default); + Task HttpPatchAsync(object inputObject, Uri uri, IDictionary headers = default); + Task HttpPatchAsync(object inputObject, string urlSuffix, bool ignoreNull, IDictionary headers = default); + Task HttpPatchAsync(object inputObject, Uri uri, NullValueHandling nullValueHandling, IDictionary headers = default); // DELETE Task HttpDeleteAsync(string urlSuffix); diff --git a/src/CommonLibrariesForNET/Internals/BaseHttpClient.cs b/src/CommonLibrariesForNET/Internals/BaseHttpClient.cs index fad4dcbd..b888a6a5 100644 --- a/src/CommonLibrariesForNET/Internals/BaseHttpClient.cs +++ b/src/CommonLibrariesForNET/Internals/BaseHttpClient.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Net; using System.Net.Http; using System.Net.Http.Headers; @@ -82,7 +83,7 @@ protected async Task HttpPostAsync(string payload, Uri uri) throw new BaseHttpClientException(response, responseMessage.StatusCode); } - protected async Task HttpPatchAsync(string payload, Uri uri) + protected async Task HttpPatchAsync(string payload, Uri uri, IDictionary headers) { var content = new StringContent(payload, Encoding.UTF8, _contentType); @@ -93,6 +94,11 @@ protected async Task HttpPatchAsync(string payload, Uri uri) Content = content }; + foreach (var keyValuePairHeader in headers ?? new Dictionary()) + { + request.Headers.TryAddWithoutValidation(keyValuePairHeader.Key, keyValuePairHeader.Value); + } + var responseMessage = await HttpClient.SendAsync(request).ConfigureAwait(false); if (responseMessage.StatusCode == HttpStatusCode.NoContent) { diff --git a/src/CommonLibrariesForNET/JsonHttpClient.cs b/src/CommonLibrariesForNET/JsonHttpClient.cs index 26d67132..61f31060 100644 --- a/src/CommonLibrariesForNET/JsonHttpClient.cs +++ b/src/CommonLibrariesForNET/JsonHttpClient.cs @@ -198,28 +198,28 @@ public async Task HttpBinaryDataPostAsync(string urlSuffix, object inputOb // PATCH - public async Task HttpPatchAsync(object inputObject, string urlSuffix) + public async Task HttpPatchAsync(object inputObject, string urlSuffix, IDictionary headers = default) { var url = Common.FormatUrl(urlSuffix, InstanceUrl, ApiVersion); - return await HttpPatchAsync(inputObject, url); + return await HttpPatchAsync(inputObject, url, headers); } - public async Task HttpPatchAsync(object inputObject, string urlSuffix, bool ignoreNull) + public async Task HttpPatchAsync(object inputObject, string urlSuffix, bool ignoreNull, IDictionary headers = default) { var url = Common.FormatUrl(urlSuffix, InstanceUrl, ApiVersion); if (ignoreNull == true) { - return await HttpPatchAsync(inputObject, url); + return await HttpPatchAsync(inputObject, url, headers); } else { - return await HttpPatchAsync(inputObject, url, NullValueHandling.Include); + return await HttpPatchAsync(inputObject, url, NullValueHandling.Include, headers); } - // return await HttpPatchAsync(inputObject, url, ignoreNull); + // return await HttpPatchAsync(inputObject, url, ignoreNull, headers); } - public async Task HttpPatchAsync(object inputObject, Uri uri) + public async Task HttpPatchAsync(object inputObject, Uri uri, IDictionary headers = default) { var json = JsonConvert.SerializeObject(inputObject, Formatting.None, @@ -231,7 +231,7 @@ public async Task HttpPatchAsync(object inputObject, Uri uri) }); try { - var response = await base.HttpPatchAsync(json, uri); + var response = await base.HttpPatchAsync(json, uri, headers); return string.IsNullOrEmpty(response) ? new SuccessResponse{ Id = "", Errors = "", Success = true } : JsonConvert.DeserializeObject(response); @@ -242,22 +242,21 @@ public async Task HttpPatchAsync(object inputObject, Uri uri) } } - public async Task HttpPatchAsync(object inputObject, Uri uri, NullValueHandling nullValueHandling ) + public async Task HttpPatchAsync(object inputObject, Uri uri, NullValueHandling nullValueHandling, IDictionary headers = default) { - - var json = JsonConvert.SerializeObject(inputObject, - Formatting.None, - new JsonSerializerSettings - { - NullValueHandling = nullValueHandling, - ContractResolver = new UpdateableContractResolver(), - DateFormatString = DateFormat - }); + var json = JsonConvert.SerializeObject(inputObject, + Formatting.None, + new JsonSerializerSettings + { + NullValueHandling = nullValueHandling, + ContractResolver = new UpdateableContractResolver(), + DateFormatString = DateFormat + }); try { - var response = await base.HttpPatchAsync(json, uri); + var response = await base.HttpPatchAsync(json, uri, headers); return string.IsNullOrEmpty(response) ? new SuccessResponse { Id = "", Errors = "", Success = true } : JsonConvert.DeserializeObject(response); diff --git a/src/ForceToolkitForNET/ForceClient.cs b/src/ForceToolkitForNET/ForceClient.cs index b30135cf..66c61d06 100644 --- a/src/ForceToolkitForNET/ForceClient.cs +++ b/src/ForceToolkitForNET/ForceClient.cs @@ -147,12 +147,17 @@ public async Task CreateAsync(string objectName, CreateRequest req } public Task UpdateAsync(string objectName, string recordId, object record) + { + return UpdateAsync(objectName, recordId, record, null); + } + + public Task UpdateAsync(string objectName, string recordId, object record, IDictionary headers) { if (string.IsNullOrEmpty(objectName)) throw new ArgumentNullException("objectName"); if (string.IsNullOrEmpty(recordId)) throw new ArgumentNullException("recordId"); if (record == null) throw new ArgumentNullException("record"); - return _jsonHttpClient.HttpPatchAsync(record, string.Format("sobjects/{0}/{1}", objectName, recordId)); + return _jsonHttpClient.HttpPatchAsync(record, string.Format("sobjects/{0}/{1}", objectName, recordId), headers); } public Task UpsertExternalAsync(string objectName, string externalFieldName, string externalId, object record) diff --git a/src/ForceToolkitForNET/IForceClient.cs b/src/ForceToolkitForNET/IForceClient.cs index aa53de42..a2ffb0ef 100644 --- a/src/ForceToolkitForNET/IForceClient.cs +++ b/src/ForceToolkitForNET/IForceClient.cs @@ -21,6 +21,7 @@ public interface IForceClient : IDisposable Task CreateAsync(string objectName, object record); Task CreateAsync(string objectName, CreateRequest request); Task UpdateAsync(string objectName, string recordId, object record); + Task UpdateAsync(string objectName, string recordId, object record, IDictionary headers); Task UpsertExternalAsync(string objectName, string externalFieldName, string externalId, object record); Task UpsertExternalAsync(string objectName, string externalFieldName, string externalId, object record, bool ignoreNull); Task DeleteAsync(string objectName, string recordId);