-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
167 lines (151 loc) · 6.11 KB
/
Program.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using DataAPIClientExample;
using DataAPIClientExample.Models;
using Microsoft.Rest;
using Newtonsoft.Json;
using NRApiClient.Helpers;
using NRApiClient.Models;
namespace DataAPIClientExample
{
class Program
{
static void Main(string[] args)
{
string server, projectId, username, password;
if (args == null || args.Length < 4)
{
Console.WriteLine("Missing arguments.");
return;
}
else
{
server = args[0];
projectId = args[1];
username = args[2];
password = args[3];
}
Console.WriteLine("----------------------NR CSV to API----------------------");
Console.WriteLine("Authenticating...");
DataAPIClient apiClient = new DataAPIClient(new System.Net.Http.HttpClient(), true)
{
BaseUri = new Uri(server)
};
apiClient.HttpClient.Timeout = Timeout.InfiniteTimeSpan;
//authorize
TokenResponse tokenResponse = AccountExtensions.GetToken(apiClient.Account, new TokenRequest(username, password));
apiClient.HttpClient.DefaultRequestHeaders.Authorization
= new AuthenticationHeaderValue("Bearer", tokenResponse.AccessToken);
if (tokenResponse.AccessToken == null || tokenResponse.AccessToken == "")
{
Console.WriteLine("AUTHENTICATION FAILED");
return;
}
else
{
Console.WriteLine("Authentication COMPLETED!");
}
Console.WriteLine("Mapping data from CSV...");
string filelocation = Path.Combine(Environment.CurrentDirectory + "\\..\\..\\Data\\csv\\test.csv");
//Mapping CSV data into C# object
List<CSVData> csvData = DataMapper.getMappedDataFromCSV(filelocation);
JSONModel uploadData = new JSONModel();
//Mapping MetaData for JSONModel
DataMapper.getMetadataForImport().ForEach(uploadData.MetaData.Add);
//Mapping CaseData for JSONModel
DataMapper.getCasedataForImport(csvData).ForEach(uploadData.CaseData.Add);
//Serializing JSONModel C# Object to JSON Object
var json = new JavaScriptSerializer().Serialize(uploadData);
Console.WriteLine("Mapping COMPLETED!");
//Import API of JSON Object data
Console.WriteLine("Importing data to the server");
dynamic importInput = json;
Response importResponse = ImportExtensions.PostImport(apiClient.Import, importInput, projectId);
if (importResponse.Data == null || tokenResponse.AccessToken == "")
{
Console.WriteLine("DATA IMPORT FAILED");
return;
}
else
{
Console.WriteLine("Import COMPLETED!");
}
//GET Import Batches API
PagedInputOfBatchFilterInput batchFilterInput = new PagedInputOfBatchFilterInput()
{
Paging = new PagingInput()
{
PageNumber = 1,
PageSize = 10
}
};
Response batches = ImportExtensions.Batches(apiClient.Import, batchFilterInput, projectId);
//Isolating ImportId for activation from batches.Data string
var Ids = batches.Data.ToString();
Ids = Ids.Substring(Ids.IndexOf("ImportID") + 11, Ids.Length - (Ids.IndexOf("ImportID") + 11));
Ids = Ids.Substring(0, Ids.IndexOf("\r") - 1);
string[] parsedIds = Ids.Split(',');
List<long?> IdsToActivates = new List<long?>();
foreach (string s in parsedIds)
{
if (Int32.TryParse(s, out int result))
{
IdsToActivates.Add(result);
}
}
Console.WriteLine("Activating data on the server...");
//Activate Import Batches API
ActivateInput activateInput = new ActivateInput()
{
IdsToActivate = IdsToActivates,
IdsToDeactivate = new List<long?>()
};
ActivationExtensions.Activate(apiClient.Activation, activateInput, projectId);
//GET Activation History API
PagedInputOfActivationHistoryFilterInput historyFilterInput = new PagedInputOfActivationHistoryFilterInput()
{
Paging = new PagingInput()
{
PageNumber = 1,
PageSize = 10
}
,
Sorting = new SortingInput()
{
OrderBy = "ActivationTime",
Ascending = false
}
};
Response activationHistory = ActivationExtensions.History(apiClient.Activation, historyFilterInput, projectId);
//Isolating last history record for valdiation
var history = activationHistory.Data.ToString();
history = history.Substring(history.IndexOf("ActivationTime") + 18, history.Length - (history.IndexOf("ActivationTime") + 18));
history = history.Substring(0, history.IndexOf("\r") - 3);
if (DateTime.TryParse(history,out DateTime lastHistory))
{
if (lastHistory > DateTime.Now.AddMinutes(-60))
{
Console.WriteLine("Data activation COMPLETED!");
}
else
{
Console.WriteLine("DATA ACTIVATION FAILED");
return;
}
}
else
{
Console.WriteLine("DATA ACTIVATION FAILED");
return;
}
}
}
}