forked from anthonyreilly/NetCoreForce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForceClientFixture.cs
83 lines (71 loc) · 2.81 KB
/
ForceClientFixture.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using System.IO;
using System.Collections.Generic;
using System.Threading.Tasks;
using Xunit;
using NetCoreForce.Client;
using NetCoreForce.Client.Models;
using Newtonsoft.Json;
namespace NetCoreForce.FunctionalTests
{
public class ForceClientFixture : IDisposable
{
public AuthInfo AuthInfo { get; private set; }
private ForceClient _forceClient;
public ForceClientFixture()
{
string filePath = null;
try
{
if (string.IsNullOrEmpty(filePath))
{
string executabledirectory = System.IO.Directory.GetCurrentDirectory();
string fileName = "credentials_dev.json";
filePath = Path.Combine(executabledirectory, fileName);
}
Console.WriteLine("Reading credentials file {0}", filePath);
string contents = File.ReadAllText(filePath);
AuthInfo info = JsonConvert.DeserializeObject<AuthInfo>(contents);
this.AuthInfo = info;
}
catch (Exception ex)
{
Console.WriteLine("Error reading credentials file: " + ex.Message);
throw;
}
}
public async Task<ForceClient> GetForceClient(string proxyUrl = null)
{
if (_forceClient == null)
{
System.Net.Http.HttpClient proxyClient = null;
if (!string.IsNullOrEmpty(proxyUrl))
{
proxyClient = HttpClientFactory.CreateHttpClient(true, proxyUrl);
}
AuthenticationClient auth = new AuthenticationClient();
await auth.UsernamePasswordAsync(AuthInfo.ClientId, AuthInfo.ClientSecret,
AuthInfo.Username, AuthInfo.Password, AuthInfo.TokenRequestEndpoint);
_forceClient = new ForceClient(auth.AccessInfo.InstanceUrl, auth.ApiVersion, auth.AccessInfo.AccessToken, proxyClient);
}
return _forceClient;
}
/// <summary>
/// Create and retreive sObject - to test any changes to object after creation and storage in SF
/// </summary>
/// <param name="sObjectTypeName"></param>
/// <param name="sObject"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public async Task<T> CreateAndRetrieveRecord<T>(string sObjectTypeName, T sObject)
{
ForceClient client = await GetForceClient();
CreateResponse createResp = await client.CreateRecord<T>(sObjectTypeName, sObject);
T retrievedObject = await client.GetObjectById<T>(sObjectTypeName, createResp.Id);
return retrievedObject;
}
public void Dispose()
{
}
}
}