Skip to content

Commit

Permalink
added opportunity to set Headers for Patch Methods
Browse files Browse the repository at this point in the history
  • Loading branch information
vcheshko committed Jul 14, 2021
1 parent 7b0d1fc commit acad1a6
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 8 deletions.
Binary file not shown.
4 changes: 2 additions & 2 deletions src/CommonLibrariesForNET/IJsonHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ 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, IDictionary<string, string> headers = default);
Task<SuccessResponse> HttpPatchAsync(object inputObject, Uri uri, 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 @@ -79,7 +80,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 = default)
{
var content = new StringContent(payload, Encoding.UTF8, _contentType);

Expand All @@ -90,6 +91,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
8 changes: 4 additions & 4 deletions src/CommonLibrariesForNET/JsonHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,13 @@ 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, 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 @@ -192,7 +192,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 Down
36 changes: 36 additions & 0 deletions src/ForceToolkitForNET.FunctionalTests/ForceClientTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Dynamic;
using System.Linq;
using System.Net;
Expand Down Expand Up @@ -330,6 +332,40 @@ public async Task Update_Account_NameChanged()
Assert.True(result.Name == newName);
}

[Test]
public async Task UpdateWithHeaders_Lead_StatusChanged()
{
const string originalStatus = "Open - Not Contacted";
const string newStatus = "In Touch";

var lead = new Lead {Email = "test@sf.com", FirstName = "Lead", LastName = "Status", Company = "Test", Status = originalStatus};
var successResponse = await _client.CreateAsync("Lead", lead);
lead.Status = newStatus;
await _client.UpdateAsync("Lead", successResponse.Id, lead, new Dictionary<string, string> {["Sforce-Auto-Assign"] = "false"});

var result = await _client.QueryByIdAsync<Lead>("Lead", successResponse.Id);

Assert.True(result.Status == newStatus);
}

[Test]
public async Task Delete_Lead_Deleted()
{
var lead = new Lead {Email = "test@sf.com"};
var queryResult = await _client.QueryAsync<Lead>($"SELECT Id, FirstName, LastName, Company, Status FROM Lead where email = '{lead.Email}'");
var foundLead = queryResult.Records.FirstOrDefault();
if (foundLead == null)
{
//todo: warning
Debug.WriteLine("Lead was not created");
return;
}

var result = await _client.DeleteAsync("Lead", foundLead.Id);

Assert.True(result);
}

[Test]
public async Task Delete_Account_IsSuccess()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<Compile Include="Models\ContactsAccountNameQuery.cs" />
<Compile Include="Models\DeletedRecord.cs" />
<Compile Include="Models\Event.cs" />
<Compile Include="Models\Lead.cs" />
<Compile Include="Models\UpdatedRecord.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
13 changes: 13 additions & 0 deletions src/ForceToolkitForNET.FunctionalTests/Models/Lead.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Salesforce.Force.FunctionalTests.Models
{
public class Lead
{
public string Id { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Company { get; set; }
public string Status { get; set; }
public string OwnerId { get; set; }
}
}
7 changes: 6 additions & 1 deletion src/ForceToolkitForNET/ForceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,17 @@ public async Task<SuccessResponse> CreateAsync(string objectName, object record)
}

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

0 comments on commit acad1a6

Please sign in to comment.