Skip to content

Commit f216650

Browse files
committed
Add timegrid commentary,
Add status data
1 parent 8fc6a39 commit f216650

5 files changed

Lines changed: 203 additions & 0 deletions

File tree

API.Test/TimeTableTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@ namespace API.Test;
1212
[TestFixture]
1313
internal class TimeTableTests
1414
{
15+
[Test]
16+
public void GetStatusData()
17+
{
18+
Client.LoginAsync(s_Server, s_LoginName, s_UserName, s_Password).Wait();
19+
20+
Task<StatusData> status = Client.GetStatusDataAsync();
21+
status.Wait();
22+
if (status.Result != null)
23+
Assert.Pass();
24+
else
25+
Assert.Fail();
26+
}
27+
1528
[Test]
1629
public void GetTimeGrid()
1730
{
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Linq;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Drawing;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
using WebUntisAPI.Client.Models;
10+
11+
namespace WebUntisAPI.Client.Converter
12+
{
13+
/// <summary>
14+
/// A converter for the get status data response
15+
/// </summary>
16+
internal class StatusDataJsonConverter : JsonConverter<StatusData>
17+
{
18+
public override StatusData ReadJson(JsonReader reader, Type objectType, StatusData existingValue, bool hasExistingValue, JsonSerializer serializer)
19+
{
20+
// Setup json object
21+
JObject statusDataObject = JObject.Load(reader);
22+
IEnumerable<JObject> lessonTypes = statusDataObject["lstypes"].Values<JObject>();
23+
IEnumerable<JObject> codes = statusDataObject["codes"].Values<JObject>();
24+
25+
return new StatusData
26+
{
27+
LessonColors = lessonTypes.First(obj => obj.ContainsKey("ls"))["ls"].ToObject<ForeBackColors>(),
28+
OhColors = lessonTypes.First(obj => obj.ContainsKey("oh"))["oh"].ToObject<ForeBackColors>(),
29+
SbColors = lessonTypes.First(obj => obj.ContainsKey("sb"))["sb"].ToObject<ForeBackColors>(),
30+
BsColors = lessonTypes.First(obj => obj.ContainsKey("bs"))["bs"].ToObject<ForeBackColors>(),
31+
CancelledLessonColors = codes.First(obj => obj.ContainsKey("cancelled"))["cancelled"].ToObject<ForeBackColors>(),
32+
IrregularLessonColors = codes.First(obj => obj.ContainsKey("irregular"))["irregular"].ToObject<ForeBackColors>()
33+
};
34+
}
35+
36+
public override void WriteJson(JsonWriter writer, StatusData value, JsonSerializer serializer)
37+
{
38+
writer.WriteStartObject();
39+
40+
writer.WritePropertyName("lstypes");
41+
writer.WriteStartArray();
42+
43+
writer.WriteStartObject();
44+
writer.WritePropertyName("ls");
45+
serializer.Serialize(writer, value.LessonColors);
46+
writer.WriteEndObject();
47+
48+
writer.WriteStartObject();
49+
writer.WritePropertyName("oh");
50+
serializer.Serialize(writer, value.OhColors);
51+
writer.WriteEndObject();
52+
53+
writer.WriteStartObject();
54+
writer.WritePropertyName("sb");
55+
serializer.Serialize(writer, value.SbColors);
56+
writer.WriteEndObject();
57+
58+
writer.WriteStartObject();
59+
writer.WritePropertyName("bs");
60+
serializer.Serialize(writer, value.BsColors);
61+
writer.WriteEndObject();
62+
63+
writer.WriteEndArray();
64+
65+
writer.WritePropertyName("codes");
66+
writer.WriteStartArray();
67+
68+
writer.WriteStartObject();
69+
writer.WritePropertyName("cancelled");
70+
serializer.Serialize(writer, value.CancelledLessonColors);
71+
writer.WriteEndObject();
72+
73+
writer.WriteStartObject();
74+
writer.WritePropertyName("irregular");
75+
serializer.Serialize(writer, value.IrregularLessonColors);
76+
writer.WriteEndObject();
77+
78+
writer.WriteEndArray();
79+
writer.WriteEndObject();
80+
}
81+
}
82+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Drawing;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using WebUntisAPI.Client.Converter;
9+
10+
namespace WebUntisAPI.Client.Models
11+
{
12+
/// <summary>
13+
/// Fore- and background color fore something
14+
/// </summary>
15+
public struct ForeBackColors
16+
{
17+
/// <summary>
18+
/// Foreground color
19+
/// </summary>
20+
[JsonConverter(typeof(ColorJsonConverter))]
21+
[JsonProperty("foreColor")]
22+
public Color ForeColor { get; set; }
23+
24+
/// <summary>
25+
/// Background color
26+
/// </summary>
27+
[JsonConverter(typeof(ColorJsonConverter))]
28+
[JsonProperty("backColor")]
29+
public Color BackColor { get; set; }
30+
}
31+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Drawing;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using WebUntisAPI.Client.Converter;
9+
10+
namespace WebUntisAPI.Client.Models
11+
{
12+
/// <summary>
13+
/// Information about lesson types and period codes and their colors
14+
/// </summary>
15+
[JsonConverter(typeof(StatusDataJsonConverter))]
16+
public class StatusData
17+
{
18+
/// <summary>
19+
/// Colors for a normal lesson
20+
/// </summary>
21+
public ForeBackColors LessonColors { get; set; }
22+
23+
/// <summary>
24+
/// idk
25+
/// </summary>
26+
public ForeBackColors OhColors { get; set; }
27+
28+
/// <summary>
29+
/// idk
30+
/// </summary>
31+
public ForeBackColors SbColors { get; set; }
32+
33+
/// <summary>
34+
/// idk
35+
/// </summary>
36+
public ForeBackColors BsColors { get; set; }
37+
38+
/// <summary>
39+
/// Colors for cancelled lessons
40+
/// </summary>
41+
public ForeBackColors CancelledLessonColors { get; set; }
42+
43+
/// <summary>
44+
/// Colors for irregular lessons
45+
/// </summary>
46+
public ForeBackColors IrregularLessonColors { get; set; }
47+
48+
}
49+
}

WebUntisAPI.Client/WebUntisClient.TimeTable.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,43 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Net.Http;
45
using System.Text;
56
using System.Threading;
67
using System.Threading.Tasks;
8+
using WebUntisAPI.Client.Exceptions;
79
using WebUntisAPI.Client.Models;
810

911
namespace WebUntisAPI.Client
1012
{
1113
public partial class WebUntisClient
1214
{
15+
/// <summary>
16+
/// Get lesson types, periods and their colors
17+
/// </summary>
18+
/// <param name="id">Identifier for the request</param>
19+
/// <param name="ct">Cancellation token</param>
20+
/// <returns>Get status data</returns>
21+
/// <exception cref="ObjectDisposedException">Thrown when thew instance was disposed</exception>
22+
/// <exception cref="UnauthorizedAccessException">Thrown when you're not logged in</exception>
23+
/// <exception cref="HttpRequestException">Thrown when an error happend while the http request</exception>
24+
/// <exception cref="WebUntisException">Thrown when the WebUntis API returned an error</exception>
25+
public async Task<StatusData> GetStatusDataAsync(string id = "getStatusData", CancellationToken ct = default)
26+
{
27+
StatusData statusData = await MakeRequestAsync<object, StatusData>(id, "getStatusData", new object(), ct);
28+
return statusData;
29+
}
30+
31+
/// <summary>
32+
/// Get the timegrid for the school
33+
/// </summary>
34+
/// <param name="id">Identier for the request</param>
35+
/// <param name="ct">Cancellation token</param>
36+
/// <returns>The timegrid for all days</returns>
37+
/// <exception cref="ObjectDisposedException">Thrown when thew instance was disposed</exception>
38+
/// <exception cref="UnauthorizedAccessException">Thrown when you're not logged in</exception>
39+
/// <exception cref="HttpRequestException">Thrown when an error happend while the http request</exception>
40+
/// <exception cref="WebUntisException">Thrown when the WebUntis API returned an error</exception>
1341
public async Task<Timegrid> GetTimegridAsync(string id = "getTimegrid", CancellationToken ct = default)
1442
{
1543
Timegrid timeGrid = await MakeRequestAsync<object, Timegrid>(id, "getTimegridUnits", new object(), ct);

0 commit comments

Comments
 (0)