Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add_headers_optional (issue #342) #400

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/CommonLibrariesForNET/IJsonHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ public interface IJsonHttpClient : IDisposable
Task<T> HttpBinaryDataPostAsync<T>(string urlSuffix, object inputObject, byte[] fileContents, string headerName, string fileName);

// PATCH
Task<SuccessResponse> HttpPatchAsync(object inputObject, string urlSuffix);
Task<SuccessResponse> HttpPatchAsync(object inputObject, Uri uri);
Task<SuccessResponse> HttpPatchAsync(object inputObject, string urlSuffix, bool ignoreNull);
Task<SuccessResponse> HttpPatchAsync(object inputObject, Uri uri, NullValueHandling nullValueHandling);
Task<SuccessResponse> HttpPatchAsync(object inputObject, string urlSuffix, IDictionary<string, string> headers = default);
Task<SuccessResponse> HttpPatchAsync(object inputObject, Uri uri, IDictionary<string, string> headers = default);
Task<SuccessResponse> HttpPatchAsync(object inputObject, string urlSuffix, bool ignoreNull, IDictionary<string, string> headers = default);
Task<SuccessResponse> HttpPatchAsync(object inputObject, Uri uri, NullValueHandling nullValueHandling, IDictionary<string, string> headers = default);

// DELETE
Task<bool> HttpDeleteAsync(string urlSuffix);
Expand Down
8 changes: 7 additions & 1 deletion src/CommonLibrariesForNET/Internals/BaseHttpClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
Expand Down Expand Up @@ -82,7 +83,7 @@ protected async Task<string> HttpPostAsync(string payload, Uri uri)
throw new BaseHttpClientException(response, responseMessage.StatusCode);
}

protected async Task<string> HttpPatchAsync(string payload, Uri uri)
protected async Task<string> HttpPatchAsync(string payload, Uri uri, IDictionary<string, string> headers)
{
var content = new StringContent(payload, Encoding.UTF8, _contentType);

Expand All @@ -93,6 +94,11 @@ protected async Task<string> HttpPatchAsync(string payload, Uri uri)
Content = content
};

foreach (var keyValuePairHeader in headers ?? new Dictionary<string, string>())
{
request.Headers.TryAddWithoutValidation(keyValuePairHeader.Key, keyValuePairHeader.Value);
}

var responseMessage = await HttpClient.SendAsync(request).ConfigureAwait(false);
if (responseMessage.StatusCode == HttpStatusCode.NoContent)
{
Expand Down
37 changes: 18 additions & 19 deletions src/CommonLibrariesForNET/JsonHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,28 +198,28 @@ public async Task<T> HttpBinaryDataPostAsync<T>(string urlSuffix, object inputOb

// PATCH

public async Task<SuccessResponse> HttpPatchAsync(object inputObject, string urlSuffix)
public async Task<SuccessResponse> HttpPatchAsync(object inputObject, string urlSuffix, IDictionary<string, string> headers = default)
{
var url = Common.FormatUrl(urlSuffix, InstanceUrl, ApiVersion);
return await HttpPatchAsync(inputObject, url);
return await HttpPatchAsync(inputObject, url, headers);
}

public async Task<SuccessResponse> HttpPatchAsync(object inputObject, string urlSuffix, bool ignoreNull)
public async Task<SuccessResponse> HttpPatchAsync(object inputObject, string urlSuffix, bool ignoreNull, IDictionary<string, string> 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<SuccessResponse> HttpPatchAsync(object inputObject, Uri uri)
public async Task<SuccessResponse> HttpPatchAsync(object inputObject, Uri uri, IDictionary<string, string> headers = default)
{
var json = JsonConvert.SerializeObject(inputObject,
Formatting.None,
Expand All @@ -231,7 +231,7 @@ public async Task<SuccessResponse> 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<SuccessResponse>(response);
Expand All @@ -242,22 +242,21 @@ public async Task<SuccessResponse> HttpPatchAsync(object inputObject, Uri uri)
}
}

public async Task<SuccessResponse> HttpPatchAsync(object inputObject, Uri uri, NullValueHandling nullValueHandling )
public async Task<SuccessResponse> HttpPatchAsync(object inputObject, Uri uri, NullValueHandling nullValueHandling, IDictionary<string, string> 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<SuccessResponse>(response);
Expand Down
7 changes: 6 additions & 1 deletion src/ForceToolkitForNET/ForceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,17 @@ public async Task<SaveResponse> CreateAsync(string objectName, CreateRequest req
}

public Task<SuccessResponse> UpdateAsync(string objectName, string recordId, object record)
{
return UpdateAsync(objectName, recordId, record, null);
}

public Task<SuccessResponse> UpdateAsync(string objectName, string recordId, object record, IDictionary<string, string> 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<SuccessResponse> UpsertExternalAsync(string objectName, string externalFieldName, string externalId, object record)
Expand Down
1 change: 1 addition & 0 deletions src/ForceToolkitForNET/IForceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public interface IForceClient : IDisposable
Task<SuccessResponse> CreateAsync(string objectName, object record);
Task<SaveResponse> CreateAsync(string objectName, CreateRequest request);
Task<SuccessResponse> UpdateAsync(string objectName, string recordId, object record);
Task<SuccessResponse> UpdateAsync(string objectName, string recordId, object record, IDictionary<string, string> headers);
Task<SuccessResponse> UpsertExternalAsync(string objectName, string externalFieldName, string externalId, object record);
Task<SuccessResponse> UpsertExternalAsync(string objectName, string externalFieldName, string externalId, object record, bool ignoreNull);
Task<bool> DeleteAsync(string objectName, string recordId);
Expand Down