Skip to content

Commit b8ac2cc

Browse files
committed
Add get subjects method and their test
1 parent d17d9e8 commit b8ac2cc

8 files changed

Lines changed: 161 additions & 20 deletions

File tree

API.Test/AuthentificationTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ namespace API.Test;
1111
[TestFixture]
1212
internal class AuthentificationTests
1313
{
14+
public static WebUntisClient Client { get; set; } = new("WebUntisAPI_TEST");
15+
1416
static AuthentificationTests()
1517
{
1618
// Load a file where i saved login data

API.Test/TeachingTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using WebUntisAPI.Client.Models;
7+
using static API.Test.AuthentificationTests;
8+
9+
namespace API.Test;
10+
11+
[Order(5)]
12+
[TestFixture]
13+
internal class TeachingTests
14+
{
15+
[Test]
16+
public void GetSubject()
17+
{
18+
Client.LoginAsync(s_Server, s_LoginName, s_UserName, s_Password).Wait();
19+
20+
Task<Subject[]> subjects = Client.GetAllSubjectsAsync();
21+
subjects.Wait();
22+
if (subjects.Result.Length > 0)
23+
Assert.Pass();
24+
else
25+
Assert.Fail();
26+
}
27+
}

API.Test/UserTest.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,6 @@ namespace API.Test;
1414
[TestFixture]
1515
internal class UserTest
1616
{
17-
private static WebUntisClient Client { get; set; }
18-
19-
[Order(0)]
20-
[SetUp]
21-
public void Setup()
22-
{
23-
Client = new("WebUntisAPI_TEST");
24-
}
25-
2617
[Test]
2718
[Order(1)]
2819
public void GetStudents()

WebUntisAPI.Client/Models/Student.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace WebUntisAPI.Client.Models
1212
/// A student
1313
/// </summary>
1414
[DebuggerDisplay("Name = {ForeName} {LongName}")]
15-
public sealed class Student : IUser
15+
public class Student : IUser
1616
{
1717
/// <inheritdoc/>
1818
public int Id { get; set; }
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Diagnostics;
5+
using System.Drawing;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace WebUntisAPI.Client.Models
11+
{
12+
/// <summary>
13+
/// A subject
14+
/// </summary>
15+
[DebuggerDisplay("Name = {LongName}")]
16+
public struct Subject
17+
{
18+
/// <summary>
19+
/// Id of the subject
20+
/// </summary>
21+
[JsonProperty("id")]
22+
public int Id { get; set; }
23+
24+
/// <summary>
25+
/// Internal name of the subject
26+
/// </summary>
27+
[JsonProperty("name")]
28+
public string Name { get; set; }
29+
30+
/// <summary>
31+
/// Full name of the subject
32+
/// </summary>
33+
[JsonProperty("longName")]
34+
public string LongName { get; set; }
35+
36+
/// <summary>
37+
/// Alternative name of the subject
38+
/// </summary>
39+
[JsonProperty("alternateName")]
40+
public string AlternateName { get; set; }
41+
42+
/// <summary>
43+
/// Is the subject active
44+
/// </summary>
45+
[JsonProperty("active")]
46+
public bool Active { get; set; }
47+
48+
/// <summary>
49+
/// Fore color of the subject
50+
/// </summary>
51+
[JsonProperty("foreColor")]
52+
public Color ForeColor { get; set; }
53+
54+
/// <summary>
55+
/// Back color of the subject
56+
/// </summary>
57+
[JsonProperty("backColor")]
58+
public Color BackColor { get; set; }
59+
}
60+
}

WebUntisAPI.Client/WebUntisAPI.Client.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@
3232
</None>
3333
</ItemGroup>
3434

35-
<ItemGroup>
36-
<Folder Include="Extensions\" />
37-
</ItemGroup>
38-
3935
<ItemGroup>
4036
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
4137
<PackageReference Include="System.Net.Http" Version="4.3.4" />
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Net.Http;
6+
using System.Net;
7+
using System.Text;
8+
using System.Threading;
9+
using System.Threading.Tasks;
10+
using WebUntisAPI.Client.Models;
11+
using WebUntisAPI.Client.Converter;
12+
using WebUntisAPI.Client.Exceptions;
13+
14+
namespace WebUntisAPI.Client
15+
{
16+
public partial class WebUntisClient
17+
{
18+
/// <summary>
19+
/// Get all subjects on the school
20+
/// </summary>
21+
/// <param name="id">Identifier for the request</param>
22+
/// <param name="ct">Cancellation token</param>
23+
/// <returns>All subjects</returns>
24+
/// <exception cref="ObjectDisposedException">Thrown when the instance was disposed</exception>
25+
/// <exception cref="UnauthorizedAccessException">Thrown when you aren't logged in</exception>
26+
/// <exception cref="HttpRequestException">Thrown when an error happened while the http request</exception>
27+
/// <exception cref="WebUntisException">Thrown when the WebUntis API returned an error</exception>
28+
public async Task<Subject[]> GetAllSubjectsAsync(string id = "getSubjects", CancellationToken ct = default)
29+
{
30+
// Check for disposing
31+
if (_disposedValue)
32+
throw new ObjectDisposedException(GetType().FullName);
33+
34+
// Check if you logged in
35+
if (!LoggedIn)
36+
throw new UnauthorizedAccessException("You're not logged in");
37+
38+
// Make request
39+
JSONRPCRequestModel<object> requestModel = new JSONRPCRequestModel<object>()
40+
{
41+
Id = id,
42+
Method = "getSubjects",
43+
Params = new object()
44+
};
45+
StringContent requestContent = new StringContent(JsonConvert.SerializeObject(requestModel), Encoding.UTF8, "application/json");
46+
47+
// Send request
48+
HttpResponseMessage response = await _client.PostAsync(ServerUrl + "/WebUntis/jsonrpc.do", requestContent, ct);
49+
50+
// Check cancellation token
51+
if (ct.IsCancellationRequested)
52+
return null;
53+
54+
// Verify response
55+
if (response.StatusCode != HttpStatusCode.OK)
56+
throw new HttpRequestException($"There was an error while the http request (Code: {response.StatusCode}).");
57+
58+
JsonSerializerSettings settings = new JsonSerializerSettings();
59+
settings.Converters.Add(new ColorJsonConverter());
60+
JSONRPCResponeModel<List<Subject>> responseModel = JsonConvert.DeserializeObject<JSONRPCResponeModel<List<Subject>>>(await response.Content.ReadAsStringAsync(), settings);
61+
62+
// Check for WebUntis error
63+
if (responseModel.Error != null)
64+
throw responseModel.Error;
65+
66+
return responseModel.Result.ToArray();
67+
}
68+
}
69+
70+
}

WebUntisAPI.Client/WebUntisClient.User.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,7 @@ public partial class WebUntisClient
2727
/// The user as that you currently logged in
2828
/// </summary>
2929
/// <remarks>
30-
/// <para>
31-
/// <see langword="null"/> when you're not logged in
32-
/// </para>
33-
/// <para>
34-
/// Don't change any value!
35-
/// </para>
30+
/// <see langword="null"/> when you're not logged in
3631
/// </remarks>
3732
public IUser User => _user;
3833
private IUser _user = null;

0 commit comments

Comments
 (0)