-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRosettaAPIWorker.cs
More file actions
288 lines (226 loc) · 10 KB
/
RosettaAPIWorker.cs
File metadata and controls
288 lines (226 loc) · 10 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
using System.Text.Json;
using DotNetEnv;
namespace UCDRosettaAPI;
public class RosettaAPIWorker
{
public string? Base_Url {get;set;}
private string? Token_Url {get;set;}
private string? _Client_ID {get;set;}
private string? _Client_Secret {get;set;}
private string? _OAuth_Token {get;set;}
private string? _OAuth_Scopes {get;set;}
public string? Test_ID {get;set;}
public long Expires_in_Ticks {get;set;}
public RosettaAPIWorker()
{
//Load Environment Variables File
Env.Load();
//Load API Information
_Client_ID = Environment.GetEnvironmentVariable("ROSETTA_CLIENT_ID");
_Client_Secret = Environment.GetEnvironmentVariable("ROSETTA_CLIENT_SECRET");
_OAuth_Scopes = Environment.GetEnvironmentVariable("ROSETTA_SCOPES");
Base_Url = Environment.GetEnvironmentVariable("ROSETTA_BASE_URL");
Token_Url = Environment.GetEnvironmentVariable("ROSETTA_OAUTH_URL");
Test_ID = Environment.GetEnvironmentVariable("ROSETTA_TEST_ID");
//Configure Inital Ticks Value
Expires_in_Ticks = 0;
}
public enum SearchBy
{
iamid,
loginid,
email,
employeeid,
studentid,
mailid,
department
}
public bool CheckOAuthToken()
{
//Var for Return Value
bool bTokenStatus = true;
//Check Ticks
if(DateTime.Now.AddMinutes(1).Ticks >= Expires_in_Ticks)
{
//Initiate Http Client to Get OAuth Token
using(var client = new HttpClient())
{
//Add Required Header Values
client.DefaultRequestHeaders.Add("client_id",_Client_ID);
client.DefaultRequestHeaders.Add("client_secret",_Client_Secret);
client.DefaultRequestHeaders.Add("grant_type","CLIENT_CREDENTIALS");
client.DefaultRequestHeaders.Add("scope",_OAuth_Scopes);
//Post Sent to Token Url
HttpResponseMessage response = client.PostAsync(Token_Url, null).Result;
if(response.StatusCode == System.Net.HttpStatusCode.OK)
{
//Read Response Body
string responsebody = response.Content.ReadAsStringAsync().Result;
//Parse Response Body Json
var jnOAuthPayload = JsonDocument.Parse(responsebody);
//Get Root Json Element
var jnOAuthRoot = jnOAuthPayload.RootElement;
//Make Sure Access Token and Expires Values are Included in Payload
if(jnOAuthRoot.TryGetProperty("access_token", out JsonElement accessTokenElement) &&
jnOAuthRoot.TryGetProperty("expires_in", out JsonElement expiresInElement))
{
//Load OAuth Token
_OAuth_Token = accessTokenElement.GetString();
//Determine When It Expires
if(expiresInElement.TryGetDouble(out double dblExpiresIn))
{
Expires_in_Ticks = DateTime.Now.AddSeconds(dblExpiresIn).Ticks;
}
else
{
Expires_in_Ticks = 0;
bTokenStatus = false;
}
}
else
{
Expires_in_Ticks = 0;
bTokenStatus = false;
}//End of Access_Token and Expires_In Checks
}
else
{
Expires_in_Ticks = 0;
bTokenStatus = false;
}//End of Status Code Check
}//End of HttpClient
}//End of Ticks Check
return bTokenStatus;
}
public RosettaPerson ParseRosettaPersonJson(JsonElement jePeople)
{
//Initialize Person to Return
RosettaPerson rosettaPerson = new();
//Retrieve Display Name
if(jePeople.TryGetProperty("displayname",out JsonElement jeDisplayName))
{
rosettaPerson.DisplayName = jeDisplayName.GetString();
}
//Retrieve IAM ID
if(jePeople.TryGetProperty("iam_id",out JsonElement jeIAMID))
{
rosettaPerson.IAM_ID = jeIAMID.GetString();
}
//Retrieve Provisioning Statuses
if(jePeople.TryGetProperty("provisioning_status",out JsonElement jeProvisioningStatus))
{
//Retrieve Primary Provisioning Status
if(jeProvisioningStatus.TryGetProperty("primary",out JsonElement jeProvisioningStatusPrimary))
{
rosettaPerson.Provisioning_Status_Primary = jeProvisioningStatusPrimary.GetString() ?? "";
}
//Retrieve Employee Provisioning Status
if(jeProvisioningStatus.TryGetProperty("employee",out JsonElement jeProvisioningStatusEmployee))
{
rosettaPerson.Provisioning_Status_Employee = jeProvisioningStatusEmployee.GetString() ?? "";
}
}//End of Provisioning Status
//Retrieve Affiliation
if(jePeople.TryGetProperty("affiliation",out JsonElement jeAffiliation))
{
//Loop Through Each Affiliation
foreach(JsonElement jeAffil in jeAffiliation.EnumerateArray())
{
//Check For Employee Status
if(jeAffil.GetString() == "employee")
{
rosettaPerson.Affiliation_Employee = true;
}
//Many More to Come
//
//
}//End of Affiliation Enumerate Array
}//End of Affiliations
return rosettaPerson;
}
public List<RosettaPerson> GetPeopleBySearchTerm(SearchBy searchBy, string searchTerm)
{
//Var for Return List
List<RosettaPerson> lRosettaPeople = new();
//Var for Search Result Limit
int nSrchRsltLimit = 100;
//Var for Search Result Offset
int nSrchRsltOffset = 0;
//Var for Retrieve More Search Results
bool bRetrMoreSrchRslts = true;
do
{
//Check OAuth Token
if(CheckOAuthToken() == true)
{
//Initiate Http Client to Get People Information
using(var client = new HttpClient())
{
//Var for Bearer Token
string bearerToken = "Bearer " + _OAuth_Token;
//Add Required Header Values
client.DefaultRequestHeaders.Add("Authorization",bearerToken);
//Var for People Url
string peopleURL = Base_Url + "people?"+ searchBy.ToString() + "=" + searchTerm + "&offset=" + nSrchRsltOffset.ToString() + "&limit=" + nSrchRsltLimit.ToString() + "&count=true";
//Get to People Endpoint
HttpResponseMessage response = client.GetAsync(peopleURL).Result;
//Check Response Status Code
if(response.StatusCode == System.Net.HttpStatusCode.OK)
{
//Pull X-Total-Count and x-response-count
if(response.Headers.TryGetValues("x-total-count", out var xtcount)
&& response.Headers.TryGetValues("x-response-count", out var xrpcount)
&& int.TryParse(xtcount.First(),out int nTotalCnt)
&& int.TryParse(xrpcount.First(),out int nRspnCnt))
{
//Check Total and Response Counts are Not Empty
if(nTotalCnt > 0 && nRspnCnt > 0)
{
//Read Response Body
string responsebody = response.Content.ReadAsStringAsync().Result;
//Parse the Response Body
using(JsonDocument jdPeople = JsonDocument.Parse(responsebody))
{
//Access the Array at the Root
JsonElement root = jdPeople.RootElement;
//Iterate Through the Array
foreach(JsonElement element in root.EnumerateArray())
{
//Add Rosetta Person to Returned People List
lRosettaPeople.Add(ParseRosettaPersonJson(element));
}//End of Root Enumerate Array
}//End Parse Response Body
//Increment Offset
nSrchRsltOffset += nSrchRsltLimit;
//Check Offset to Total Count
if(nSrchRsltOffset >= nTotalCnt)
{
bRetrMoreSrchRslts = false;
}
}
else
{
bRetrMoreSrchRslts = false;
}//End of nTotalCnt and nRspnCnt Empty Checks
}
else
{
bRetrMoreSrchRslts = false;
}//End of Return Header Counts Checks
}
else
{
bRetrMoreSrchRslts = false;
}//End of Status Code Check
}//End of HttpClient
}
else
{
bRetrMoreSrchRslts = false;
}//End of CheckOAuthToken
}
while(bRetrMoreSrchRslts == true);
//Return Person
return lRosettaPeople;
}
}