Skip to content

Commit acad1a6

Browse files
committed
added opportunity to set Headers for Patch Methods
1 parent 7b0d1fc commit acad1a6

File tree

8 files changed

+69
-8
lines changed

8 files changed

+69
-8
lines changed
Binary file not shown.

src/CommonLibrariesForNET/IJsonHttpClient.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public interface IJsonHttpClient: IDisposable
2121
Task<T> HttpBinaryDataPostAsync<T>(string urlSuffix, object inputObject, byte[] fileContents, string headerName, string fileName);
2222

2323
// PATCH
24-
Task<SuccessResponse> HttpPatchAsync(object inputObject, string urlSuffix);
25-
Task<SuccessResponse> HttpPatchAsync(object inputObject, Uri uri);
24+
Task<SuccessResponse> HttpPatchAsync(object inputObject, string urlSuffix, IDictionary<string, string> headers = default);
25+
Task<SuccessResponse> HttpPatchAsync(object inputObject, Uri uri, IDictionary<string, string> headers = default);
2626

2727
// DELETE
2828
Task<bool> HttpDeleteAsync(string urlSuffix);

src/CommonLibrariesForNET/Internals/BaseHttpClient.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Net;
34
using System.Net.Http;
45
using System.Net.Http.Headers;
@@ -79,7 +80,7 @@ protected async Task<string> HttpPostAsync(string payload, Uri uri)
7980
throw new BaseHttpClientException(response, responseMessage.StatusCode);
8081
}
8182

82-
protected async Task<string> HttpPatchAsync(string payload, Uri uri)
83+
protected async Task<string> HttpPatchAsync(string payload, Uri uri, IDictionary<string, string> headers = default)
8384
{
8485
var content = new StringContent(payload, Encoding.UTF8, _contentType);
8586

@@ -90,6 +91,11 @@ protected async Task<string> HttpPatchAsync(string payload, Uri uri)
9091
Content = content
9192
};
9293

94+
foreach (var keyValuePairHeader in headers ?? new Dictionary<string, string>())
95+
{
96+
request.Headers.TryAddWithoutValidation(keyValuePairHeader.Key, keyValuePairHeader.Value);
97+
}
98+
9399
var responseMessage = await HttpClient.SendAsync(request).ConfigureAwait(false);
94100
if (responseMessage.StatusCode == HttpStatusCode.NoContent)
95101
{

src/CommonLibrariesForNET/JsonHttpClient.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,13 @@ public async Task<T> HttpBinaryDataPostAsync<T>(string urlSuffix, object inputOb
174174

175175
// PATCH
176176

177-
public async Task<SuccessResponse> HttpPatchAsync(object inputObject, string urlSuffix)
177+
public async Task<SuccessResponse> HttpPatchAsync(object inputObject, string urlSuffix, IDictionary<string,string> headers = default)
178178
{
179179
var url = Common.FormatUrl(urlSuffix, InstanceUrl, ApiVersion);
180-
return await HttpPatchAsync(inputObject, url);
180+
return await HttpPatchAsync(inputObject, url, headers);
181181
}
182182

183-
public async Task<SuccessResponse> HttpPatchAsync(object inputObject, Uri uri)
183+
public async Task<SuccessResponse> HttpPatchAsync(object inputObject, Uri uri, IDictionary<string, string> headers = default)
184184
{
185185
var json = JsonConvert.SerializeObject(inputObject,
186186
Formatting.None,
@@ -192,7 +192,7 @@ public async Task<SuccessResponse> HttpPatchAsync(object inputObject, Uri uri)
192192
});
193193
try
194194
{
195-
var response = await base.HttpPatchAsync(json, uri);
195+
var response = await base.HttpPatchAsync(json, uri, headers);
196196
return string.IsNullOrEmpty(response) ?
197197
new SuccessResponse{ Id = "", Errors = "", Success = true } :
198198
JsonConvert.DeserializeObject<SuccessResponse>(response);

src/ForceToolkitForNET.FunctionalTests/ForceClientTests.cs

+36
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Configuration;
4+
using System.Diagnostics;
35
using System.Dynamic;
46
using System.Linq;
57
using System.Net;
@@ -330,6 +332,40 @@ public async Task Update_Account_NameChanged()
330332
Assert.True(result.Name == newName);
331333
}
332334

335+
[Test]
336+
public async Task UpdateWithHeaders_Lead_StatusChanged()
337+
{
338+
const string originalStatus = "Open - Not Contacted";
339+
const string newStatus = "In Touch";
340+
341+
var lead = new Lead {Email = "[email protected]", FirstName = "Lead", LastName = "Status", Company = "Test", Status = originalStatus};
342+
var successResponse = await _client.CreateAsync("Lead", lead);
343+
lead.Status = newStatus;
344+
await _client.UpdateAsync("Lead", successResponse.Id, lead, new Dictionary<string, string> {["Sforce-Auto-Assign"] = "false"});
345+
346+
var result = await _client.QueryByIdAsync<Lead>("Lead", successResponse.Id);
347+
348+
Assert.True(result.Status == newStatus);
349+
}
350+
351+
[Test]
352+
public async Task Delete_Lead_Deleted()
353+
{
354+
var lead = new Lead {Email = "[email protected]"};
355+
var queryResult = await _client.QueryAsync<Lead>($"SELECT Id, FirstName, LastName, Company, Status FROM Lead where email = '{lead.Email}'");
356+
var foundLead = queryResult.Records.FirstOrDefault();
357+
if (foundLead == null)
358+
{
359+
//todo: warning
360+
Debug.WriteLine("Lead was not created");
361+
return;
362+
}
363+
364+
var result = await _client.DeleteAsync("Lead", foundLead.Id);
365+
366+
Assert.True(result);
367+
}
368+
333369
[Test]
334370
public async Task Delete_Account_IsSuccess()
335371
{

src/ForceToolkitForNET.FunctionalTests/ForceToolkitForNET.FunctionalTests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
<Compile Include="Models\ContactsAccountNameQuery.cs" />
6969
<Compile Include="Models\DeletedRecord.cs" />
7070
<Compile Include="Models\Event.cs" />
71+
<Compile Include="Models\Lead.cs" />
7172
<Compile Include="Models\UpdatedRecord.cs" />
7273
<Compile Include="Properties\AssemblyInfo.cs" />
7374
</ItemGroup>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace Salesforce.Force.FunctionalTests.Models
2+
{
3+
public class Lead
4+
{
5+
public string Id { get; set; }
6+
public string Email { get; set; }
7+
public string FirstName { get; set; }
8+
public string LastName { get; set; }
9+
public string Company { get; set; }
10+
public string Status { get; set; }
11+
public string OwnerId { get; set; }
12+
}
13+
}

src/ForceToolkitForNET/ForceClient.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,17 @@ public async Task<SuccessResponse> CreateAsync(string objectName, object record)
9999
}
100100

101101
public Task<SuccessResponse> UpdateAsync(string objectName, string recordId, object record)
102+
{
103+
return UpdateAsync(objectName, recordId, record, null);
104+
}
105+
106+
public Task<SuccessResponse> UpdateAsync(string objectName, string recordId, object record, IDictionary<string, string> headers)
102107
{
103108
if (string.IsNullOrEmpty(objectName)) throw new ArgumentNullException("objectName");
104109
if (string.IsNullOrEmpty(recordId)) throw new ArgumentNullException("recordId");
105110
if (record == null) throw new ArgumentNullException("record");
106111

107-
return _jsonHttpClient.HttpPatchAsync(record, string.Format("sobjects/{0}/{1}", objectName, recordId));
112+
return _jsonHttpClient.HttpPatchAsync(record, string.Format("sobjects/{0}/{1}", objectName, recordId), headers);
108113
}
109114

110115
public Task<SuccessResponse> UpsertExternalAsync(string objectName, string externalFieldName, string externalId, object record)

0 commit comments

Comments
 (0)