Skip to content

Commit dbe7b94

Browse files
committed
Rework
1 parent a7a4e80 commit dbe7b94

File tree

5 files changed

+171
-119
lines changed

5 files changed

+171
-119
lines changed

Brain.cs

Lines changed: 84 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
using System.Text;
2-
using System.Text.Json;
1+
using System.Text.Json;
32

43
namespace TTMC.Kréta
54
{
65
public class Engine
76
{
8-
HttpClient httpClient = new();
7+
private HttpClient httpClient = new();
98
public Engine(string userAgent = "KretaAPI", string apiKey = "7856d350-1fda-45f5-822d-e1a2f3f1acf0")
109
{
1110
httpClient.DefaultRequestHeaders.Add("apiKey", apiKey);
@@ -41,88 +40,85 @@ public List<Institute> Institutes(params ushort[] ids)
4140
}
4241
public class Account
4342
{
44-
public LoginDetails? loginDetails = null;
43+
public LoginDetails loginDetails;
4544
public HttpClient client = new();
4645
private string institute = string.Empty;
4746
private Task? autoRefresh = null;
47+
private KretaAPI kretaAPI;
4848
public Account(string instituteCode, string username, string password, string userAgent = "KretaAPI")
4949
{
50+
kretaAPI = new(instituteCode);
5051
HttpClient httpClient = new();
5152
AuthorizationPolicy ap = new(instituteCode, username);
5253
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(userAgent);
5354
httpClient.DefaultRequestHeaders.Add("X-Authorizationpolicy-Key", ap.key);
5455
httpClient.DefaultRequestHeaders.Add("X-Authorizationpolicy-Version", ap.version);
5556
httpClient.DefaultRequestHeaders.Add("X-Authorizationpolicy-Nonce", ap.nonce);
56-
StringContent req = new StringContent("userName=" + username + "&password=" + password + "&institute_code=" + instituteCode + "&grant_type=password&client_id=" + KretaAPI.clientId, Encoding.UTF8, "application/x-www-form-urlencoded");
57-
HttpResponseMessage resp = httpClient.PostAsync(KretaAPI.login, req).Result;
57+
Dictionary<string, string> tmp = new() { { "userName", username }, { "password", password }, { "institute_code", instituteCode }, { "grant_type", "password" }, { "client_id", KretaAPI.clientId } };
58+
FormUrlEncodedContent content = new(tmp);
59+
HttpResponseMessage resp = httpClient.PostAsync(KretaAPI.login, content).Result;
5860
string json = resp.Content.ReadAsStringAsync().Result;
59-
loginDetails = JsonSerializer.Deserialize<LoginDetails>(json);
60-
if (loginDetails != null)
61+
loginDetails = Enhance<LoginDetails>(json);
62+
institute = instituteCode;
63+
client.DefaultRequestHeaders.UserAgent.ParseAdd(userAgent);
64+
client.DefaultRequestHeaders.Add("Authorization", loginDetails.token_type + " " + loginDetails.access_token);
65+
autoRefresh = new Task(() => AutoRefresh(loginDetails.expires_in));
66+
autoRefresh.Start();
67+
}
68+
internal static T Enhance<T>(string text)
69+
{
70+
if (text.StartsWith('{') && text.EndsWith('}'))
71+
{
72+
Error? error = JsonSerializer.Deserialize<Error>(text);
73+
if (error != null && !string.IsNullOrEmpty(error.error_description))
74+
{
75+
throw new Exception(error.error_description);
76+
}
77+
}
78+
T? nzx = JsonSerializer.Deserialize<T>(text);
79+
if (nzx != null)
6180
{
62-
institute = instituteCode;
63-
client.DefaultRequestHeaders.UserAgent.ParseAdd(userAgent);
64-
client.DefaultRequestHeaders.Add("Authorization", loginDetails.token_type + " " + loginDetails.access_token);
65-
autoRefresh = new Task(() => AutoRefresh(loginDetails.expires_in));
66-
autoRefresh.Start();
81+
return nzx;
6782
}
83+
throw new Exception(text);
6884
}
6985
private void AutoRefresh(int num)
7086
{
71-
Thread.Sleep(num);
87+
Thread.Sleep(num * 1000);
7288
RefreshToken();
7389
}
74-
public List<Timetable> OrarendElemek(DateTime datumTol, DateTime datumIg)
90+
public List<Lesson> Lessons(DateTime? fromDate, DateTime? toDate)
7591
{
7692
if (client.DefaultRequestHeaders.Contains("Authorization"))
7793
{
78-
string one = datumTol.ToString("u").Split(' ')[0];
79-
string two = datumIg.ToString("u").Split(' ')[0];
80-
string json = client.GetStringAsync("https://" + institute + ".e-kreta.hu/ellenorzo/V3/Sajat/OrarendElemek?datumTol=" + one + "&datumIg=" + two).Result;
81-
List<Timetable>? tt = JsonSerializer.Deserialize<List<Timetable>>(json);
82-
if (tt != null)
94+
string json = client.GetStringAsync(kretaAPI.Lessons(fromDate, toDate)).Result;
95+
List<Lesson>? lessons = JsonSerializer.Deserialize<List<Lesson>>(json);
96+
if (lessons != null)
8397
{
84-
return tt;
98+
return lessons;
8599
}
86100
}
87101
return new();
88102
}
89-
public List<Absences> Mulasztasok(DateTime? datumTol = null, DateTime? datumIg = null)
103+
public List<Omission> Omissions(DateTime? fromDate = null, DateTime? toDate = null)
90104
{
91105
if (client.DefaultRequestHeaders.Contains("Authorization"))
92106
{
93-
string url = "https://" + institute + ".e-kreta.hu/ellenorzo/V3/Sajat/Mulasztasok";
94-
if (datumTol != null)
95-
{
96-
url += "?datumTol=" + datumTol.Value.ToString("u").Split(' ')[0];
97-
}
98-
if (datumIg != null)
107+
string json = client.GetStringAsync(kretaAPI.Omissions(fromDate, toDate)).Result;
108+
List<Omission>? omissions = JsonSerializer.Deserialize<List<Omission>>(json);
109+
if (omissions != null)
99110
{
100-
url += "?datumTol=" + datumIg.Value.ToString("u").Split(' ')[0];
101-
}
102-
string json = client.GetStringAsync(url).Result;
103-
List<Absences>? absences = JsonSerializer.Deserialize<List<Absences>>(json);
104-
if (absences != null)
105-
{
106-
return absences;
111+
return omissions;
107112
}
108113
}
109114
return new();
110115
}
111-
public List<Evaluations> Ertekelesek(DateTime? datumTol = null, DateTime? datumIg = null)
116+
public List<Evaluation> Evaluations(DateTime? fromDate = null, DateTime? toDate = null)
112117
{
113118
if (client.DefaultRequestHeaders.Contains("Authorization"))
114119
{
115-
string url = "https://" + institute + ".e-kreta.hu/ellenorzo/V3/Sajat/Ertekelesek";
116-
if (datumTol != null)
117-
{
118-
url += "?datumTol=" + datumTol.Value.ToString("u").Split(' ')[0];
119-
}
120-
if (datumIg != null)
121-
{
122-
url += "?datumTol=" + datumIg.Value.ToString("u").Split(' ')[0];
123-
}
124-
string json = client.GetStringAsync(url).Result;
125-
List<Evaluations>? evaluations = JsonSerializer.Deserialize<List<Evaluations>>(json);
120+
string json = client.GetStringAsync(kretaAPI.Evaluations(fromDate, toDate)).Result;
121+
List<Evaluation>? evaluations = JsonSerializer.Deserialize<List<Evaluation>>(json);
126122
if (evaluations != null)
127123
{
128124
return evaluations;
@@ -132,11 +128,12 @@ public List<Evaluations> Ertekelesek(DateTime? datumTol = null, DateTime? datumI
132128
}
133129
public void RefreshToken()
134130
{
135-
if (client.DefaultRequestHeaders.Contains("Authorization") && loginDetails != null)
131+
if (client.DefaultRequestHeaders.Contains("Authorization") && !string.IsNullOrEmpty(loginDetails.refresh_token))
136132
{
137-
StringContent content = new("institute_code=" + institute + "&refresh_token=" + loginDetails.refresh_token + "&grant_type=refresh_token&client_id=kreta-ellenorzo-mobile-android", Encoding.UTF8, "application/x-www-form-urlencoded");
138-
HttpResponseMessage resp = client.PostAsync("https://idp.e-kreta.hu/connect/token", content).Result;
139-
loginDetails = JsonSerializer.Deserialize<LoginDetails>(resp.Content.ReadAsStringAsync().Result);
133+
Dictionary<string, string> tmp = new() { { "institute_code", institute }, { "refresh_token", loginDetails.refresh_token }, { "grant_type", "refresh_token" }, { "client_id", KretaAPI.clientId } };
134+
FormUrlEncodedContent content = new(tmp);
135+
HttpResponseMessage resp = client.PostAsync(KretaAPI.login, content).Result;
136+
loginDetails = Enhance<LoginDetails>(resp.Content.ReadAsStringAsync().Result);
140137
if (loginDetails != null)
141138
{
142139
client.DefaultRequestHeaders.Remove("Authorization");
@@ -146,11 +143,11 @@ public void RefreshToken()
146143
}
147144
}
148145
}
149-
public List<Note> Feljegyzesek()
146+
public List<Note> Notes(DateTime? fromDate = null, DateTime? toDate = null)
150147
{
151148
if (client.DefaultRequestHeaders.Contains("Authorization"))
152149
{
153-
string json = client.GetStringAsync(KretaAPI.notes(institute)).Result;
150+
string json = client.GetStringAsync(kretaAPI.Notes(fromDate, toDate)).Result;
154151
List<Note>? notes = JsonSerializer.Deserialize<List<Note>>(json);
155152
if (notes != null)
156153
{
@@ -159,11 +156,11 @@ public List<Note> Feljegyzesek()
159156
}
160157
return new();
161158
}
162-
public List<Message> Postaladaelemek(MessageType select)
159+
public List<Message> Messages(MessageType select)
163160
{
164161
if (client.DefaultRequestHeaders.Contains("Authorization"))
165162
{
166-
string json = client.GetStringAsync("https://eugyintezes.e-kreta.hu/api/v1/kommunikacio/postaladaelemek/" + select.type).Result;
163+
string json = client.GetStringAsync(Kreta.Admin + AdminEndpoints.getAllMessages(select.type)).Result;
167164
List<Message>? result = JsonSerializer.Deserialize<List<Message>>(json);
168165
if (result != null)
169166
{
@@ -177,12 +174,11 @@ public List<Message> Postaladaelemek(MessageType select)
177174
}
178175
return new();
179176
}
180-
public List<Message> Postaladaelemek(byte select)
177+
public List<Message> Messages(ulong select)
181178
{
182179
if (client.DefaultRequestHeaders.Contains("Authorization") & select >= 0 & select <= 2)
183180
{
184-
string[] list = { "beerkezett", "elkuldott", "torolt" };
185-
string json = client.GetStringAsync("https://eugyintezes.e-kreta.hu/api/v1/kommunikacio/postaladaelemek/" + list[select]).Result;
181+
string json = client.GetStringAsync(Kreta.Admin + AdminEndpoints.getMessage(select)).Result;
186182
List<Message>? result = JsonSerializer.Deserialize<List<Message>>(json);
187183
if (result != null)
188184
{
@@ -196,62 +192,63 @@ public List<Message> Postaladaelemek(byte select)
196192
}
197193
return new();
198194
}
199-
public List<Exam> BejelentettSzamonkeresek(DateTime? datumTol = null)
195+
public List<AnnouncedTest> AnnouncedTests(DateTime? fromDate = null, DateTime? toDate = null)
200196
{
201197
if (client.DefaultRequestHeaders.Contains("Authorization"))
202198
{
203-
string url = "https://" + institute + ".e-kreta.hu/ellenorzo/V3/Sajat/BejelentettSzamonkeresek";
204-
if (datumTol != null)
205-
{
206-
url += "?datumTol=" + datumTol.Value.ToString("u").Split(' ')[0];
207-
}
208-
string json = client.GetStringAsync(url).Result;
209-
List<Exam>? exams = JsonSerializer.Deserialize<List<Exam>>(json);
210-
if (exams != null)
199+
string json = client.GetStringAsync(kretaAPI.AnnouncedTests(fromDate, toDate)).Result;
200+
List<AnnouncedTest>? announcedTests = JsonSerializer.Deserialize<List<AnnouncedTest>>(json);
201+
if (announcedTests != null)
211202
{
212-
return exams;
203+
return announcedTests;
213204
}
214205
}
215206
return new();
216207
}
217-
public StudentInfo TanuloAdatlap()
208+
public List<Homework> Homeworks(DateTime? fromDate = null, DateTime? toDate = null)
218209
{
219210
if (client.DefaultRequestHeaders.Contains("Authorization"))
220211
{
221-
string json = client.GetStringAsync("https://" + institute + ".e-kreta.hu/ellenorzo/V3/Sajat/TanuloAdatlap").Result;
222-
StudentInfo? si = JsonSerializer.Deserialize<StudentInfo>(json);
223-
if (si != null)
212+
string json = client.GetStringAsync(kretaAPI.Homeworks(fromDate, toDate)).Result;
213+
List<Homework>? homework = JsonSerializer.Deserialize<List<Homework>>(json);
214+
if (homework != null)
224215
{
225-
return si;
216+
return homework;
226217
}
227218
}
228219
return new();
229220
}
230-
public List<Homework> HaziFeladatok(DateTime datumTol)
221+
public StudentInfo student
231222
{
232-
if (client.DefaultRequestHeaders.Contains("Authorization"))
223+
get
233224
{
234-
string json = client.GetStringAsync(KretaAPI.homework(institute, datumTol)).Result;
235-
List<Homework>? homework = JsonSerializer.Deserialize<List<Homework>>(json);
236-
if (homework != null)
225+
if (client.DefaultRequestHeaders.Contains("Authorization"))
237226
{
238-
return homework;
227+
string json = client.GetStringAsync(kretaAPI.student).Result;
228+
StudentInfo? si = JsonSerializer.Deserialize<StudentInfo>(json);
229+
if (si != null)
230+
{
231+
return si;
232+
}
239233
}
234+
return new();
240235
}
241-
return new();
242236
}
243-
public School Intezmenyek()
237+
public School capabilities
244238
{
245-
if (client.DefaultRequestHeaders.Contains("Authorization"))
239+
get
246240
{
247-
string json = client.GetStringAsync(KretaAPI.capabilities(institute)).Result;
248-
School? school = JsonSerializer.Deserialize<School>(json);
249-
if (school != null)
241+
if (client.DefaultRequestHeaders.Contains("Authorization"))
250242
{
251-
return school;
243+
string json = client.GetStringAsync(kretaAPI.capabilities).Result;
244+
School? school = JsonSerializer.Deserialize<School>(json);
245+
if (school != null)
246+
{
247+
return school;
248+
}
252249
}
250+
return new();
253251
}
254-
return new();
255252
}
256253
}
257254
}

Classes.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace TTMC.Kréta
44
{
55
public class MessageType
66
{
7-
internal string? type { get; set; }
7+
internal string type = string.Empty;
88
public static MessageType beerkezett = new() { type = "beerkezett" };
99
public static MessageType elkuldott = new() { type = "elkuldott" };
1010
public static MessageType torolt = new() { type = "torolt" };
@@ -116,7 +116,7 @@ public class MessageAttachments
116116
public int azonosito { get; set; }
117117
public string? fajlNev { get; set; }
118118
}
119-
public class Exam
119+
public class AnnouncedTest
120120
{
121121
public string? Uid { get; set; }
122122
public string? Datum { get; set; }
@@ -129,7 +129,7 @@ public class Exam
129129
public Three? Modja { get; set; }
130130
public One? OsztalyCsoport { get; set; }
131131
}
132-
public class Timetable
132+
public class Lesson
133133
{
134134
public string? Uid { get; set; }
135135
public string? Datum { get; set; }
@@ -221,7 +221,7 @@ public class CustomSettings
221221
public int ErtekelesekMegjelenitesenekKesleltetesenekMerteke { get; set; }
222222
public string? KovetkezoTelepitesDatuma { get; set; }
223223
}
224-
public class Absences
224+
public class Omission
225225
{
226226
public string? Uid { get; set; }
227227
public Four? Tantargy { get; set; }
@@ -242,7 +242,7 @@ public class Class
242242
public string? VegDatum { get; set; }
243243
public int? Oraszam { get; set; }
244244
}
245-
public class Evaluations
245+
public class Evaluation
246246
{
247247
public string? Uid { get; set; }
248248
public string? RogzitesDatuma { get; set; }
@@ -297,4 +297,9 @@ public class Note
297297
public string? Tartalom { get; set; }
298298
public Three? Tipus { get; set; }
299299
}
300+
public class Error
301+
{
302+
public string? error { get; set; }
303+
public string? error_description { get; set; }
304+
}
300305
}

0 commit comments

Comments
 (0)