Skip to content

Commit 98c385a

Browse files
committed
Add the school news feed and Move ClassregEvent get method to teaching file
1 parent 863269c commit 98c385a

7 files changed

Lines changed: 146 additions & 36 deletions

File tree

API.Test/TeachingTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,17 @@ public void GetLatestImportTime()
7676
else
7777
Assert.Fail();
7878
}
79+
80+
[Test]
81+
public void GetNewsFeed()
82+
{
83+
Client.LoginAsync(s_Server, s_LoginName, s_UserName, s_Password).Wait();
84+
85+
Task<News> news = Client.GetNewsFeedAsync(new DateTime(2023, 06, 23));
86+
news.Wait();
87+
if (news.Result != null)
88+
Assert.Pass();
89+
else
90+
Assert.Fail();
91+
}
7992
}

WebUntisAPI.Client/Models/News.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace WebUntisAPI.Client.Models
9+
{
10+
/// <summary>
11+
/// All news for a day
12+
/// </summary>
13+
public class News
14+
{
15+
/// <summary>
16+
/// A system message
17+
/// </summary>
18+
[JsonProperty("systemMessage")]
19+
public NewsMessage SystemMessage { get; set; }
20+
21+
/// <summary>
22+
/// All messages for the day
23+
/// </summary>
24+
[JsonProperty("messagesOfDay")]
25+
public List<NewsMessage> Messages { get; set; }
26+
}
27+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace WebUntisAPI.Client.Models
9+
{
10+
/// <summary>
11+
/// A news message
12+
/// </summary>
13+
public class NewsMessage
14+
{
15+
/// <summary>
16+
/// The id of the message
17+
/// </summary>
18+
[JsonProperty("id")]
19+
public int Id { get; set; }
20+
21+
/// <summary>
22+
/// The involved subject
23+
/// </summary>
24+
[JsonProperty("subject")]
25+
public string Subject { get; set; }
26+
27+
/// <summary>
28+
/// The normal text of the news in HTML
29+
/// </summary>
30+
[JsonProperty("text")]
31+
public string Text { get; set; }
32+
}
33+
}

WebUntisAPI.Client/WebUntisClient.Teaching.cs

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
using System;
1+
using Newtonsoft.Json.Linq;
2+
using System;
23
using System.Collections.Generic;
4+
using System.Net;
35
using System.Net.Http;
46
using System.Threading;
57
using System.Threading.Tasks;
68
using WebUntisAPI.Client.Exceptions;
9+
using WebUntisAPI.Client.Extensions;
710
using WebUntisAPI.Client.Models;
811

912
namespace WebUntisAPI.Client
@@ -22,7 +25,7 @@ public partial class WebUntisClient
2225
/// <exception cref="WebUntisException">Thrown when the WebUntis API returned an error</exception>
2326
public async Task<Subject[]> GetSubjectsAsync(string id = "getSubjects", CancellationToken ct = default)
2427
{
25-
List<Subject> subjects = await MakeRequestAsync<object, List<Subject>>(id, "getSubjects", new object(), ct);
28+
List<Subject> subjects = await MakeJSONRPCRequestAsync<object, List<Subject>>(id, "getSubjects", new object(), ct);
2629
return subjects.ToArray();
2730
}
2831

@@ -38,7 +41,7 @@ public async Task<Subject[]> GetSubjectsAsync(string id = "getSubjects", Cancell
3841
/// <exception cref="WebUntisException">Thrown when the WebUntis API returned an error</exception>
3942
public async Task<Class[]> GetClassesAsync(string id = "getClasses", CancellationToken ct = default)
4043
{
41-
List<Class> classes = await MakeRequestAsync<object, List<Class>>(id, "getKlassen", new object(), ct);
44+
List<Class> classes = await MakeJSONRPCRequestAsync<object, List<Class>>(id, "getKlassen", new object(), ct);
4245
return classes.ToArray();
4346
}
4447

@@ -55,7 +58,7 @@ public async Task<Class[]> GetClassesAsync(string id = "getClasses", Cancellatio
5558
/// <exception cref="WebUntisException">Thrown when the WebUntis API returned an error</exception>
5659
public async Task<Class[]> GetClassesAsync(SchoolYear schoolYear, string id = "getClassesBySchoolYear", CancellationToken ct = default)
5760
{
58-
List<Class> classes = await MakeRequestAsync<SchoolYearModel, List<Class>>(id, "getKlassen", new SchoolYearModel() { Id = schoolYear.Id }, ct);
61+
List<Class> classes = await MakeJSONRPCRequestAsync<SchoolYearModel, List<Class>>(id, "getKlassen", new SchoolYearModel() { Id = schoolYear.Id }, ct);
5962
return classes.ToArray();
6063
}
6164

@@ -71,7 +74,7 @@ public async Task<Class[]> GetClassesAsync(SchoolYear schoolYear, string id = "g
7174
/// <exception cref="WebUntisException">Thrown when the WebUntis API returned an error</exception>
7275
public async Task<Room[]> GetRoomsAsync(string id = "getRooms", CancellationToken ct = default)
7376
{
74-
List<Room> rooms = await MakeRequestAsync<object, List<Room>>(id, "getRooms", new object(), ct);
77+
List<Room> rooms = await MakeJSONRPCRequestAsync<object, List<Room>>(id, "getRooms", new object(), ct);
7578
return rooms.ToArray();
7679
}
7780

@@ -87,31 +90,42 @@ public async Task<Room[]> GetRoomsAsync(string id = "getRooms", CancellationToke
8790
/// <exception cref="WebUntisException">Thrown when the WebUntis API returned an error</exception>
8891
public async Task<Department[]> GetDepartmentsAsync(string id = "getDepartments", CancellationToken ct = default)
8992
{
90-
List<Department> departments = await MakeRequestAsync<object, List<Department>>(id, "getDepartments", new object(), ct);
93+
List<Department> departments = await MakeJSONRPCRequestAsync<object, List<Department>>(id, "getDepartments", new object(), ct);
9194
return departments.ToArray();
9295
}
9396

9497
/// <summary>
95-
/// Get all classreg events
98+
/// Get all the news for the school as string
9699
/// </summary>
97-
/// <param name="startDate">Start date for the requested events</param>
98-
/// <param name="endDate">End date for the requested events</param>
99-
/// <param name="id">Identifier for the request</param>
100-
/// <param name="ct">Cancellatio token</param>
101-
/// <returns>All classreg events in the given date range</returns>
100+
/// <param name="date">Date to get the news</param>
101+
/// <param name="ct">Cancellation token</param>
102+
/// <returns>The new at the school for the requested day</returns>
102103
/// <exception cref="ObjectDisposedException">Thrown when the instance was disposed</exception>
103104
/// <exception cref="UnauthorizedAccessException">Thrown when you're not logged in</exception>
104105
/// <exception cref="HttpRequestException">Thrown when an error happend while the http request</exception>
105-
/// <exception cref="WebUntisException">Thrown when the WebUntis API returned an error</exception>
106-
public async Task<ClassregEvent[]> GetClassregEventsAsync(DateTime startDate, DateTime endDate, string id = "getCLassregEvents", CancellationToken ct = default)
106+
public async Task<News> GetNewsFeedAsync(DateTime date, CancellationToken ct = default)
107107
{
108-
GetClassregEventsRequestModel model = new GetClassregEventsRequestModel()
109-
{
110-
StartDate = startDate,
111-
EndDate = endDate
112-
};
113-
List<ClassregEvent> classregEvents = await MakeRequestAsync<GetClassregEventsRequestModel, List<ClassregEvent>>(id, "getClassregEvents", model, ct);
114-
return classregEvents.ToArray();
108+
// Check for disposing
109+
if (_disposedValue)
110+
throw new ObjectDisposedException(GetType().FullName);
111+
112+
// Check if you logged in
113+
if (!LoggedIn)
114+
throw new UnauthorizedAccessException("You're not logged in");
115+
116+
date.ToWebUntisTimeFormat(out string dateString, out _);
117+
HttpResponseMessage response = await _client.GetAsync(ServerUrl + "/WebUntis/api/public/news/newsWidgetData?date=" + dateString, ct);
118+
119+
// Check cancellation token
120+
if (ct.IsCancellationRequested)
121+
return default;
122+
123+
// Verify response
124+
if (response.StatusCode != HttpStatusCode.OK)
125+
throw new HttpRequestException($"There was an error while the http request (Code: {response.StatusCode}).");
126+
127+
JToken data = JObject.Parse(await response.Content.ReadAsStringAsync()).GetValue("data");
128+
return data.ToObject<News>();
115129
}
116130
}
117131
}

WebUntisAPI.Client/WebUntisClient.TimeTable.cs

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public partial class WebUntisClient
2222
/// <exception cref="WebUntisException">Thrown when the WebUntis API returned an error</exception>
2323
public async Task<StatusData> GetStatusDataAsync(string id = "getStatusData", CancellationToken ct = default)
2424
{
25-
StatusData statusData = await MakeRequestAsync<object, StatusData>(id, "getStatusData", new object(), ct);
25+
StatusData statusData = await MakeJSONRPCRequestAsync<object, StatusData>(id, "getStatusData", new object(), ct);
2626
return statusData;
2727
}
2828

@@ -38,7 +38,7 @@ public async Task<StatusData> GetStatusDataAsync(string id = "getStatusData", Ca
3838
/// <exception cref="WebUntisException">Thrown when the WebUntis API returned an error</exception>
3939
public async Task<Timegrid> GetTimegridAsync(string id = "getTimegrid", CancellationToken ct = default)
4040
{
41-
Timegrid timeGrid = await MakeRequestAsync<object, Timegrid>(id, "getTimegridUnits", new object(), ct);
41+
Timegrid timeGrid = await MakeJSONRPCRequestAsync<object, Timegrid>(id, "getTimegridUnits", new object(), ct);
4242
return timeGrid;
4343
}
4444

@@ -54,7 +54,7 @@ public async Task<Timegrid> GetTimegridAsync(string id = "getTimegrid", Cancella
5454
/// <exception cref="WebUntisException">Thrown when the WebUntis API returned an error</exception>
5555
public async Task<SchoolYear[]> GetSchoolYearsAsync(string id = "getSchoolyears", CancellationToken ct = default)
5656
{
57-
List<SchoolYear> schoolYears = await MakeRequestAsync<object, List<SchoolYear>>(id, "getSchoolyears", new object(), ct);
57+
List<SchoolYear> schoolYears = await MakeJSONRPCRequestAsync<object, List<SchoolYear>>(id, "getSchoolyears", new object(), ct);
5858
return schoolYears.ToArray();
5959
}
6060

@@ -70,7 +70,7 @@ public async Task<SchoolYear[]> GetSchoolYearsAsync(string id = "getSchoolyears"
7070
/// <exception cref="WebUntisException">Thrown when the WebUntis API returned an error</exception>
7171
public async Task<SchoolYear> GetCurrentSchoolYearAsync(string id = "getCurrentSchoolyear", CancellationToken ct = default)
7272
{
73-
SchoolYear schoolYear = await MakeRequestAsync<object, SchoolYear>(id, "getCurrentSchoolyear", new object(), ct);
73+
SchoolYear schoolYear = await MakeJSONRPCRequestAsync<object, SchoolYear>(id, "getCurrentSchoolyear", new object(), ct);
7474
return schoolYear;
7575
}
7676

@@ -86,10 +86,33 @@ public async Task<SchoolYear> GetCurrentSchoolYearAsync(string id = "getCurrentS
8686
/// <exception cref="WebUntisException">Thrown when the WebUntis API returned an error</exception>
8787
public async Task<Holidays[]> GetHolidaysAsync(string id = "getHolidays", CancellationToken ct = default)
8888
{
89-
List<Holidays> holidays = await MakeRequestAsync<object, List<Holidays>>(id, "getHolidays", new object(), ct);
89+
List<Holidays> holidays = await MakeJSONRPCRequestAsync<object, List<Holidays>>(id, "getHolidays", new object(), ct);
9090
return holidays.ToArray();
9191
}
9292

93+
/// <summary>
94+
/// Get all classreg events
95+
/// </summary>
96+
/// <param name="startDate">Start date for the requested events</param>
97+
/// <param name="endDate">End date for the requested events</param>
98+
/// <param name="id">Identifier for the request</param>
99+
/// <param name="ct">Cancellatio token</param>
100+
/// <returns>All classreg events in the given date range</returns>
101+
/// <exception cref="ObjectDisposedException">Thrown when the instance was disposed</exception>
102+
/// <exception cref="UnauthorizedAccessException">Thrown when you're not logged in</exception>
103+
/// <exception cref="HttpRequestException">Thrown when an error happend while the http request</exception>
104+
/// <exception cref="WebUntisException">Thrown when the WebUntis API returned an error</exception>
105+
public async Task<ClassregEvent[]> GetClassregEventsAsync(DateTime startDate, DateTime endDate, string id = "getCLassregEvents", CancellationToken ct = default)
106+
{
107+
GetClassregEventsRequestModel model = new GetClassregEventsRequestModel()
108+
{
109+
StartDate = startDate,
110+
EndDate = endDate
111+
};
112+
List<ClassregEvent> classregEvents = await MakeJSONRPCRequestAsync<GetClassregEventsRequestModel, List<ClassregEvent>>(id, "getClassregEvents", model, ct);
113+
return classregEvents.ToArray();
114+
}
115+
93116
#region Timetable
94117
/// <summary>
95118
/// Get the timetable the user as their you logged in
@@ -140,7 +163,7 @@ public async Task<Period[]> GetTimetableForClassAsync(Class @class, DateTime sta
140163
StartDate = startDate,
141164
EndDate = endDate
142165
};
143-
List<Period> timetable = await MakeRequestAsync<TimetableRequestModel, List<Period>>(id, "getTimetable", requestModel, ct);
166+
List<Period> timetable = await MakeJSONRPCRequestAsync<TimetableRequestModel, List<Period>>(id, "getTimetable", requestModel, ct);
144167
return timetable.ToArray();
145168
}
146169

@@ -173,7 +196,7 @@ public async Task<Period[]> GetTimetableForTeacherAsync(Teacher teacher, DateTim
173196
StartDate = startDate,
174197
EndDate = endDate
175198
};
176-
List<Period> timetable = await MakeRequestAsync<TimetableRequestModel, List<Period>>(id, "getTimetable", requestModel, ct);
199+
List<Period> timetable = await MakeJSONRPCRequestAsync<TimetableRequestModel, List<Period>>(id, "getTimetable", requestModel, ct);
177200
return timetable.ToArray();
178201
}
179202

@@ -206,7 +229,7 @@ public async Task<Period[]> GetTimetableForSubjectAsync(Subject subject, DateTim
206229
StartDate = startDate,
207230
EndDate = endDate
208231
};
209-
List<Period> timetable = await MakeRequestAsync<TimetableRequestModel, List<Period>>(id, "getTimetable", requestModel, ct);
232+
List<Period> timetable = await MakeJSONRPCRequestAsync<TimetableRequestModel, List<Period>>(id, "getTimetable", requestModel, ct);
210233
return timetable.ToArray();
211234
}
212235

@@ -239,7 +262,7 @@ public async Task<Period[]> GetTimetableForRoomAsync(Room room, DateTime startDa
239262
StartDate = startDate,
240263
EndDate = endDate
241264
};
242-
List<Period> timetable = await MakeRequestAsync<TimetableRequestModel, List<Period>>(id, "getTimetable", requestModel, ct);
265+
List<Period> timetable = await MakeJSONRPCRequestAsync<TimetableRequestModel, List<Period>>(id, "getTimetable", requestModel, ct);
243266
return timetable.ToArray();
244267
}
245268

@@ -272,7 +295,7 @@ public async Task<Period[]> GetTimetableForStudentAsync(Student student, DateTim
272295
StartDate = startDate,
273296
EndDate = endDate
274297
};
275-
List<Period> timetable = await MakeRequestAsync<TimetableRequestModel, List<Period>>(id, "getTimetable", requestModel, ct);
298+
List<Period> timetable = await MakeJSONRPCRequestAsync<TimetableRequestModel, List<Period>>(id, "getTimetable", requestModel, ct);
276299
return timetable.ToArray();
277300
}
278301
#endregion

WebUntisAPI.Client/WebUntisClient.User.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public partial class WebUntisClient
4040
/// <exception cref="ObjectDisposedException">Thrown when the object is disposed</exception>
4141
public async Task<Student[]> GetStudentsAsync(string id = "getStudents", CancellationToken ct = default)
4242
{
43-
List<Student> students = await MakeRequestAsync<object, List<Student>>(id, "getStudents", new object(), ct);
43+
List<Student> students = await MakeJSONRPCRequestAsync<object, List<Student>>(id, "getStudents", new object(), ct);
4444
return students.ToArray();
4545
}
4646

@@ -56,7 +56,7 @@ public async Task<Student[]> GetStudentsAsync(string id = "getStudents", Cancell
5656
/// <exception cref="ObjectDisposedException">Thrown when the object is disposed</exception>
5757
public async Task<Teacher[]> GetTeachersAsync(string id = "getTeachers", CancellationToken ct = default)
5858
{
59-
List<Teacher> teachers = await MakeRequestAsync<object, List<Teacher>>(id, "getTeachers", new object(), ct);
59+
List<Teacher> teachers = await MakeJSONRPCRequestAsync<object, List<Teacher>>(id, "getTeachers", new object(), ct);
6060
return teachers.ToArray();
6161
}
6262

@@ -81,7 +81,7 @@ public async Task<int> GetPersonIdAsync(string forename, string surname, UserTyp
8181
Surname = surname,
8282
UserType = (int)type
8383
};
84-
return await MakeRequestAsync<GetPersonIdRequestModel, int>(id, "getPersonId", model, ct);
84+
return await MakeJSONRPCRequestAsync<GetPersonIdRequestModel, int>(id, "getPersonId", model, ct);
8585
}
8686
}
8787
}

WebUntisAPI.Client/WebUntisClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ public async Task LogoutAsync(string id = "getStudents", CancellationToken ct =
230230
/// <exception cref="WebUntisException">Thrown when the WebUntis API returned an error</exception>
231231
public async Task<DateTime> GetLatestImportTimeAsync(string id = "getLatestImportTime", CancellationToken ct = default)
232232
{
233-
long timestamp = await MakeRequestAsync<object, long>(id, "getLatestImportTime", new object(), ct);
233+
long timestamp = await MakeJSONRPCRequestAsync<object, long>(id, "getLatestImportTime", new object(), ct);
234234
return new DateTime(1970, 01, 01, 0, 0, 0, DateTimeKind.Utc).AddTicks(timestamp * 10000);
235235
}
236236

@@ -249,7 +249,7 @@ public async Task<DateTime> GetLatestImportTimeAsync(string id = "getLatestImpor
249249
/// <exception cref="UnauthorizedAccessException">Thrown when the client isn't logged in</exception>
250250
/// <exception cref="HttpRequestException">Thrown when an error happend while the http request</exception>
251251
/// <exception cref="WebUntisException">Thrown when the WebUntis API returned an error</exception>
252-
private async Task<TResult> MakeRequestAsync<TRequest, TResult>(string id, string methodName, TRequest requestParams, CancellationToken ct, string requestUrl = "/WebUntis/jsonrpc.do")
252+
private async Task<TResult> MakeJSONRPCRequestAsync<TRequest, TResult>(string id, string methodName, TRequest requestParams, CancellationToken ct, string requestUrl = "/WebUntis/jsonrpc.do")
253253
{
254254
// Check for disposing
255255
if (_disposedValue)

0 commit comments

Comments
 (0)