Skip to content

Commit fb3d029

Browse files
committed
Add GetAllStudents method
1 parent fe1026a commit fb3d029

7 files changed

Lines changed: 199 additions & 11 deletions

File tree

API.Test/AuthentificationTests.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,7 @@ namespace API.Test;
1111
[TestFixture]
1212
internal class AuthentificationTests
1313
{
14-
// Login data to test
15-
public static string s_Server;
16-
public static string s_LoginName;
17-
public static string s_UserName;
18-
public static string s_Password;
19-
20-
[SetUp]
21-
public void Setup()
14+
static AuthentificationTests()
2215
{
2316
// Load a file where i saved login data
2417
using StreamReader str = new("LoginData.txt");
@@ -28,6 +21,12 @@ public void Setup()
2821
s_Password = str.ReadLine();
2922
}
3023

24+
// Login data to test
25+
public static string s_Server;
26+
public static string s_LoginName;
27+
public static string s_UserName;
28+
public static string s_Password;
29+
3130
[Test]
3231
public void Authentification()
3332
{

API.Test/UserTest.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using NUnit.Framework.Constraints;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using WebUntisAPI.Client;
8+
using WebUntisAPI.Client.Models;
9+
using static API.Test.AuthentificationTests;
10+
11+
namespace API.Test;
12+
13+
[Order(4)]
14+
[TestFixture]
15+
internal class UserTest
16+
{
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+
26+
[Test]
27+
[Order(1)]
28+
public void GetStudents()
29+
{
30+
Client.LoginAsync(s_Server, s_LoginName, s_UserName, s_Password, CancellationToken.None).Wait();
31+
32+
Task<Student[]> students = Client.GetAllStudentsAsync(CancellationToken.None);
33+
students.Wait();
34+
if (students.Result.Length > 0)
35+
Assert.Pass();
36+
else
37+
Assert.Fail();
38+
}
39+
40+
[Order(3)]
41+
[Test]
42+
public void TearUp()
43+
{
44+
Client.LogoutAsync(CancellationToken.None).Wait();
45+
}
46+
}

WebUntisAPI.Client/Enums.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,20 @@ public enum PersonType
2121
/// </summary>
2222
Student = 5
2323
}
24+
25+
/// <summary>
26+
/// The gender of a student
27+
/// </summary>
28+
public enum Gender
29+
{
30+
/// <summary>
31+
/// Male
32+
/// </summary>
33+
Male = 0,
34+
35+
/// <summary>
36+
/// Female
37+
/// </summary>
38+
Female = 1,
39+
}
2440
}

WebUntisAPI.Client/Models/IUser.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,39 @@
1-
using System;
1+
using Newtonsoft.Json;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Text;
56
using System.Threading.Tasks;
67

78
namespace WebUntisAPI.Client.Models
89
{
10+
/// <summary>
11+
/// Student or teacher
12+
/// </summary>
913
public interface IUser
1014
{
11-
int id { get; set; }
15+
/// <summary>
16+
/// Id of the user
17+
/// </summary>
18+
[JsonProperty("id")]
19+
int Id { get; set; }
20+
21+
/// <summary>
22+
/// WebUntis internal name of the user
23+
/// </summary>
24+
[JsonProperty("name")]
25+
string Name { get; set; }
26+
27+
/// <summary>
28+
/// Fore name of the user
29+
/// </summary>
30+
[JsonProperty("foreName")]
31+
string ForeName { get; set; }
32+
33+
/// <summary>
34+
/// Last name of the user
35+
/// </summary>
36+
[JsonProperty("longName")]
37+
string LongName { get; set; }
1238
}
1339
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 student
12+
/// </summary>
13+
public sealed class Student : IUser
14+
{
15+
/// <inheritdoc/>
16+
public int Id { get; set; }
17+
18+
/// <inheritdoc/>
19+
public string Name { get; set; }
20+
21+
/// <inheritdoc/>
22+
public string ForeName { get; set; }
23+
24+
/// <inheritdoc/>
25+
public string LongName { get; set; }
26+
27+
/// <summary>
28+
/// Idk
29+
/// </summary>
30+
[JsonProperty("key")]
31+
public string Key { get; set; }
32+
33+
/// <summary>
34+
/// The gender of the student
35+
/// </summary>
36+
[JsonProperty("gender")]
37+
public Gender? Gender { get; set; }
38+
}
39+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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.Exceptions;
12+
13+
namespace WebUntisAPI.Client
14+
{
15+
public partial class WebUntisClient
16+
{
17+
/// <summary>
18+
/// Get all students on the school
19+
/// </summary>
20+
/// <param name="ct">Cancellation token</param>
21+
/// <param name="id">Identifier of the request</param>
22+
/// <returns>An array of all students on the school</returns>
23+
/// <exception cref="WebUntisException">Thrown when the WebUntis API returned an error</exception>
24+
/// <exception cref="UnauthorizedAccessException">Thrown when you don't logged in</exception>
25+
/// <exception cref="HttpRequestException">Thrown when there was an error while the http request</exception>
26+
public async Task<Student[]> GetAllStudentsAsync(CancellationToken ct, string id = "getStudents")
27+
{
28+
if (!LoggedIn)
29+
throw new UnauthorizedAccessException("You're not logged in");
30+
31+
// Make request
32+
JSONRPCRequestModel<object> requestModel = new JSONRPCRequestModel<object>()
33+
{
34+
Id = id,
35+
Method = "getStudents",
36+
Params = new object()
37+
};
38+
StringContent requestContent = new StringContent(JsonConvert.SerializeObject(requestModel), Encoding.UTF8, "application/json");
39+
40+
// Send request
41+
HttpResponseMessage response = await _client.PostAsync(ServerUrl + "/WebUntis/jsonrpc.do", requestContent, ct);
42+
43+
// Check cancellation token
44+
if (ct.IsCancellationRequested)
45+
return null;
46+
47+
// Verify response
48+
if (response.StatusCode != HttpStatusCode.OK)
49+
throw new HttpRequestException($"There was an error while the http request (Code: {response.StatusCode}).");
50+
51+
JSONRPCResponeModel<List<Student>> responseModel = JsonConvert.DeserializeObject<JSONRPCResponeModel<List<Student>>>(await response.Content.ReadAsStringAsync());
52+
53+
// Check for WebUntis error
54+
if (responseModel.Error != null)
55+
throw responseModel.Error;
56+
57+
return responseModel.Result.ToArray();
58+
}
59+
}
60+
61+
}

WebUntisAPI.Client/WebUntisClient.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Newtonsoft.Json;
1313
using WebUntisAPI.Client.Exceptions;
1414
using System.Threading;
15+
using System.ComponentModel;
1516

1617
namespace WebUntisAPI.Client
1718
{
@@ -26,7 +27,7 @@ namespace WebUntisAPI.Client
2627
/// Under no circumstances should 10 req. per sec., more than 1800req. per hr (but in no case more than 3600 req. per hr). If the specifications are exceeded, access to WebUntis is permanently blocked by the WebUntis API.
2728
/// </para>
2829
/// </remarks>
29-
public class WebUntisClient : IDisposable
30+
public partial class WebUntisClient : IDisposable
3031
{
3132
/// <summary>
3233
/// Unique identifier for the client app

0 commit comments

Comments
 (0)