-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
221 lines (186 loc) · 8.45 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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NJsonSchema;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net;
namespace Breaker
{
class Program
{
/// <summary>
/// Path to a config file
/// </summary>
static string configFilePath = "";
/// <summary>
/// Log file content
/// </summary>
static string log = "";
/// <summary>
/// Path to log file
/// </summary>
static string logFile = "";
/// <summary>
/// Path to diff folder
/// </summary>
static string diffFolder = "";
static void Main(string[] args)
{
if (args.Length < 1) { Console.WriteLine("Application accepts a parameter. The parameter should be a url of the configuration file"); return; }
if (!File.Exists(args[0])) { Console.WriteLine("You need to provide existing configuration file url as a parameter."); return; }
configFilePath = args[0];
PrepareFilesAndFolders();
string config = File.ReadAllText(configFilePath);
string[] lines = config.Split(new char[] { '\r' });
string authorization = lines[0].Replace("\n", "").Trim();
HttpClient client = prepareClient(authorization);
string urlPartToRemove = lines[1].Replace("\n", "").Trim();
for (var i = 2; i < lines.Length; i++)
{
HttpResponseMessage response = null;
var endpointUrl = lines[i].Replace("\n", "").Trim();
try
{
response = client.GetAsync(endpointUrl).Result;
if (response.StatusCode == HttpStatusCode.OK)
{
var schema = JsonSchema.FromSampleJson(response.Content.ReadAsStringAsync().Result);
var currentSchema = schema.ToJson().ToString();
var oldSchemaFilePath = GetSchemaFilePath(endpointUrl);
if (File.Exists(oldSchemaFilePath))
{
string oldSchema = File.ReadAllText(oldSchemaFilePath);
if (oldSchema != currentSchema)
{
if (!Directory.Exists(GetSchemaFolderPath() + "schema-old\\")) Directory.CreateDirectory(GetSchemaFolderPath() + "schema-old\\");
if (!Directory.Exists(GetSchemaFolderPath() + "schema-current\\")) Directory.CreateDirectory(GetSchemaFolderPath() + "schema-current\\");
string oldSchemaDiffFilePath = GetSchemaFolderPath() + "schema-old\\" + GetSchemaFileName(endpointUrl.Replace(urlPartToRemove, "")) + ".txt";
string currentSchemaDiffFilePath = GetSchemaFolderPath() + "schema-current\\" + GetSchemaFileName(endpointUrl.Replace(urlPartToRemove, "")) + ".txt";
File.WriteAllText(oldSchemaDiffFilePath, endpointUrl + "\r\n\r\n" + oldSchema);
File.WriteAllText(currentSchemaDiffFilePath, endpointUrl + "\r\n\r\n" + currentSchema);
showError(endpointUrl, "Different schema\r\n\r\nOld:\r\n\r\n" + oldSchema + "\r\n\r\nNew:\r\n\r\n" + currentSchema);
}
}
if (args.Length > 1 && args[1] == "save")
{
if (!System.IO.Directory.Exists(GetSchemaFolderPath())) System.IO.Directory.CreateDirectory(GetSchemaFolderPath());
File.WriteAllText(oldSchemaFilePath, currentSchema);
}
}
else
{
showError(endpointUrl, response.StatusCode.ToString() + "\r\n" + response);
}
}
catch (Exception ex)
{
showError(endpointUrl, ex.ToString());
}
}
File.WriteAllText(logFile, log);
Console.WriteLine("Processing done. Log saved to " + logFile);
}
/// <summary>
/// Adds information to a log file about changes in one endpoint
/// </summary>
/// <param name="url">Endpoint url</param>
/// <param name="erro">Information about the endpoint</param>
static void showError(string url, string erro)
{
var line = "********************************************************************\r\n";
line += url + "\r\n";
line += "********************************************************************\r\n";
line += erro;
line += "\r\n********************************************************************\r\n";
Console.WriteLine(line);
log += line;
}
/// <summary>
/// Gets a safe file name from a text
/// </summary>
/// <param name="name"></param>
/// <param name="replace"></param>
/// <returns></returns>
static string GetSafeFileName(string name, char replace = '_')
{
char[] invalids = Path.GetInvalidFileNameChars();
return new string(name.Select(c => invalids.Contains(c) ? replace : c).ToArray());
}
/// <summary>
/// Prepares file of a log and diff folder
/// </summary>
static void PrepareFilesAndFolders()
{
var logFolder = Path.GetDirectoryName(configFilePath);
if (logFolder != "") logFolder += "\\logs\\";
logFolder += "logs\\";
if (!System.IO.Directory.Exists(logFolder)) System.IO.Directory.CreateDirectory(logFolder);
logFile = logFolder + "log-" + GetSafeFileName(DateTime.Now.ToShortDateString() + "-" + DateTime.Now.ToShortTimeString()) + ".txt";
diffFolder = logFolder + "\\" + GetSafeFileName(DateTime.Now.ToShortDateString() + "-" + DateTime.Now.ToShortTimeString());
System.IO.Directory.CreateDirectory(diffFolder);
}
/// <summary>
/// Gets last schema of an endpoint from a snapshot file
/// </summary>
/// <returns></returns>
static string GetLastSchema(string endpointUrl)
{
var fileName = GetSchemaFilePath(endpointUrl);
if (File.Exists(fileName))
{
string oldSchema = File.ReadAllText(fileName);
return oldSchema;
}
else
{
return "";
}
}
/// <summary>
/// Gets the path to the file with snapshot
/// </summary>
/// <param name="endpointUrl"></param>
/// <returns></returns>
static string GetSchemaFilePath(string endpointUrl)
{
return GetSchemaFolderPath() + GetSchemaFileName(endpointUrl) + ".txt";
}
/// <summary>
/// Gets schema file name
/// </summary>
/// <param name="endpointUrl"></param>
/// <returns></returns>
static string GetSchemaFileName(string endpointUrl)
{
return GetSafeFileName(endpointUrl);
}
/// <summary>
/// Gets the snapshot schema folder path
/// </summary>
/// <returns></returns>
static string GetSchemaFolderPath()
{
var folderPath = Path.GetDirectoryName(configFilePath);
if (folderPath != "") folderPath = folderPath + "\\";
folderPath += "snapshot\\";
return folderPath;
}
/// <summary>
/// Prepares client to make requests to endpoints
/// </summary>
/// <param name="authorizationString">Adds authorization header with provided text</param>
/// <returns></returns>
private static HttpClient prepareClient(string authorizationString)
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Authorization", authorizationString);
return client;
}
}
}