-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSalesforceUtility.cs
146 lines (125 loc) · 6.71 KB
/
SalesforceUtility.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using Newtonsoft.Json;
using SalesforcePushUpgrade.Model;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
namespace SalesforcePushUpgrade
{
public class SalesforceUtility
{
public static string SFDCLoginUrl = "https://login.salesforce.com/services/oauth2/token";
public static string SFDCQueryUrl = "/query/?q=";
public static string SFDCPushRequestUrl = "/sobjects/packagepushrequest/";
public static string SFDCPushJobUrl = "/sobjects/packagepushjob/";
public static string OauthAuthorizeUrl = "https://login.salesforce.com/services/oauth2/authorize";
public static string SFDCServiceUrl = "/services/data";
public static Salesforce Login(Salesforce sfdc) {
Salesforce sfdcout = new Salesforce();
String jsonResponse;
try {
using(var client = new HttpClient()) {
var request = new FormUrlEncodedContent(new Dictionary<string, string>
{
{"grant_type", "password"},
{"client_id", sfdc.ClientId},
{"client_secret", sfdc.ClientSecret},
{"username", sfdc.Username},
{"password", sfdc.Password + sfdc.Token}
}
);
request.Headers.Add("X-PrettyPrint", "1");
var response = client.PostAsync(SFDCLoginUrl, request).Result;
jsonResponse = response.Content.ReadAsStringAsync().Result;
}
var values = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonResponse);
sfdcout = sfdc;
sfdcout.AuthToken = values["access_token"];
sfdcout.InstanceUrl = values["instance_url"];
return sfdcout;
} catch(Exception) {
throw;
}
}
public static string InvokeSOQLQuery(string soqlQuery, string token, string instance, string apiVer) {
try {
var client = new HttpClient();
string url = instance + apiVer + SFDCQueryUrl + soqlQuery;
var request = new HttpRequestMessage(HttpMethod.Get, url);
request.Headers.Add("Authorization", "Bearer " + token);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Add("X-PrettyPrint", "1");
var response = client.SendAsync(request).Result;
return response.Content.ReadAsStringAsync().Result;
} catch(Exception) {
throw;
}
}
public static string PostPackagePushRequest(PushRequest pushRequest) {
string requestID = string.Empty;
try {
using(var client = new HttpClient()) {
string url = pushRequest.InstanceUrl + pushRequest.ApiVersion + SFDCPushRequestUrl;
var request = new HttpRequestMessage(HttpMethod.Post, url);
request.Headers.Add("Authorization", "Bearer " + pushRequest.AccessToken);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
PackagePushRequest pr = new PackagePushRequest();
if(pushRequest.ScheduledStartTime != null && pushRequest.ScheduledStartTime != DateTime.MinValue)
pr.ScheduledStartTime = pushRequest.ScheduledStartTime;
pr.PackageVersionId = pushRequest.PackageVersionId;
string jsonbody = JsonConvert.SerializeObject(pr);
request.Content = new StringContent(jsonbody, Encoding.UTF8, "application/json");
var response = client.SendAsync(request).Result;
requestID = response.Content.ReadAsStringAsync().Result;
}
} catch(Exception) {
throw;
}
return requestID;
}
public static string PostPackagePushJob(PushJob pushJob) {
string pushJobId = string.Empty;
try {
using(var client = new HttpClient()) {
string url = pushJob.InstanceUrl + pushJob.ApiVersion + SFDCPushJobUrl;
var request = new HttpRequestMessage(HttpMethod.Post, url);
request.Headers.Add("Authorization", "Bearer " + pushJob.AccessToken);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
PackagePushJob pJ = new PackagePushJob();
pJ.SubscriberOrganizationKey = pushJob.OrgKey;
pJ.PackagePushRequestId = pushJob.PushRequestJobId;
string jsonbody = JsonConvert.SerializeObject(pJ);
request.Content = new StringContent(jsonbody, Encoding.UTF8, "application/json");
var response = client.SendAsync(request).Result;
pushJobId = response.Content.ReadAsStringAsync().Result;
}
} catch(Exception) {
throw;
}
return pushJobId;
}
public static string PatchPackagePushRequest(PushRequest pushRequest) {
string requestID = string.Empty;
try {
using(var client = new HttpClient()) {
string url = pushRequest.InstanceUrl + pushRequest.ApiVersion + SFDCPushRequestUrl + pushRequest.PushReqId;
var request = new HttpRequestMessage(HttpMethod.Patch, url);
request.Headers.Add("Authorization", "Bearer " + pushRequest.AccessToken);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
PackagePushRequest pr = new PackagePushRequest();
pr.Status = pushRequest.Status;
if(pushRequest.ScheduledStartTime != null && pushRequest.ScheduledStartTime != DateTime.MinValue)
pr.ScheduledStartTime = pushRequest.ScheduledStartTime;
string jsonbody = JsonConvert.SerializeObject(pr);
request.Content = new StringContent(jsonbody, Encoding.UTF8, "application/json");
var response = client.SendAsync(request).Result;
requestID = response.Content.ReadAsStringAsync().Result;
}
} catch(Exception) {
throw;
}
return requestID;
}
}
}