Skip to content

Commit 53b4ac9

Browse files
committed
Add get Teachers method and implement it to current logged in user, Add teacher tests, fix spelling mistake
1 parent a788ba2 commit 53b4ac9

5 files changed

Lines changed: 107 additions & 3 deletions

File tree

API.Test/UserTest.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ public void GetStudents()
3737
Assert.Fail();
3838
}
3939

40+
[Test]
41+
[Order(2)]
42+
public void GetTeachers()
43+
{
44+
Client.LoginAsync(s_Server, s_LoginName, s_UserName, s_Password).Wait();
45+
46+
Task<Teacher[]> teachers = Client.GetAllTeachersAsync();
47+
teachers.Wait();
48+
if (teachers.Result.Length > 0)
49+
Assert.Pass();
50+
else
51+
Assert.Fail();
52+
}
53+
4054
[Order(3)]
4155
[Test]
4256
public void TearUp()

WebUntisAPI.Client/Models/LoginResultModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ internal struct LoginResultModel
2525
public int PersonType { get; set; }
2626

2727
/// <summary>
28-
/// Is of the person
28+
/// Id of the person
2929
/// </summary>
3030
[JsonProperty("personId")]
3131
public int PersonId { get; set; }
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Diagnostics;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace WebUntisAPI.Client.Models
10+
{
11+
/// <summary>
12+
/// A teacher
13+
/// </summary>
14+
[DebuggerDisplay("Name = {ForeName} {LongName}")]
15+
public class Teacher : IUser
16+
{
17+
/// <inheritdoc/>
18+
public int Id { get; set; }
19+
20+
/// <inheritdoc/>
21+
public string Name { get; set; }
22+
23+
/// <inheritdoc/>
24+
public string ForeName { get; set; }
25+
26+
/// <inheritdoc/>
27+
public string LongName { get; set; }
28+
29+
/// <summary>
30+
/// I think a special title
31+
/// </summary>
32+
[JsonProperty("title")]
33+
public string Title { get; set; }
34+
35+
/// <summary>
36+
/// Is the teacher active
37+
/// </summary>
38+
[JsonProperty("active")]
39+
public bool Active { get; set; }
40+
41+
/// <summary>
42+
/// Idk
43+
/// </summary>
44+
[JsonProperty("dids")]
45+
public List<object> Dids { get; set; }
46+
}
47+
}

WebUntisAPI.Client/WebUntisClient.User.cs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,48 @@ public async Task<Student[]> GetAllStudentsAsync(string id = "getStudents", Canc
7979

8080
return responseModel.Result.ToArray();
8181
}
82-
}
8382

83+
/// <summary>
84+
/// Get all teachers on the school
85+
/// </summary>
86+
/// <param name="id">Identifier for the request</param>
87+
/// <param name="ct">Cancellation token</param>
88+
/// <returns>An array of all teachers</returns>
89+
/// <exception cref="UnauthorizedAccessException">Thrown when you don't logged in</exception>
90+
/// <exception cref="HttpRequestException">Thrown when there was an error while the http request</exception>
91+
/// <exception cref="WebUntisException">Thrown when the WebUntis API returned an error</exception>
92+
public async Task<Teacher[]> GetAllTeachersAsync(string id = "getTeachers", CancellationToken ct = default)
93+
{
94+
if (!LoggedIn)
95+
throw new UnauthorizedAccessException("You're not logged in");
96+
97+
// Make request
98+
JSONRPCRequestModel<object> requestModel = new JSONRPCRequestModel<object>()
99+
{
100+
Id = id,
101+
Method = "getTeachers",
102+
Params = new object()
103+
};
104+
StringContent requestContent = new StringContent(JsonConvert.SerializeObject(requestModel), Encoding.UTF8, "application/json");
105+
106+
// Send request
107+
HttpResponseMessage response = await _client.PostAsync(ServerUrl + "/WebUntis/jsonrpc.do", requestContent, ct);
108+
109+
// Check cancellation token
110+
if (ct.IsCancellationRequested)
111+
return null;
112+
113+
// Verify response
114+
if (response.StatusCode != HttpStatusCode.OK)
115+
throw new HttpRequestException($"There was an error while the http request (Code: {response.StatusCode}).");
116+
117+
JSONRPCResponeModel<List<Teacher>> responseModel = JsonConvert.DeserializeObject<JSONRPCResponeModel<List<Teacher>>>(await response.Content.ReadAsStringAsync());
118+
119+
// Check for WebUntis error
120+
if (responseModel.Error != null)
121+
throw responseModel.Error;
122+
123+
return responseModel.Result.ToArray();
124+
}
125+
}
84126
}

WebUntisAPI.Client/WebUntisClient.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ public async Task<bool> LoginAsync(string server, string loginName, string usern
173173

174174
// Get logged in user data
175175
_userType = (UserType)responseModel.Result.PersonType;
176-
_user = (await GetAllStudentsAsync(ct: ct)).FirstOrDefault(s => s.Id == responseModel.Result.PersonId);
176+
IUser[] users = _userType == Client.UserType.Student ? (IUser[])await GetAllStudentsAsync(ct: ct) : await GetAllTeachersAsync(ct: ct);
177+
_user = users.FirstOrDefault(u => u.Id == responseModel.Result.PersonId);
177178

178179
return true;
179180
}

0 commit comments

Comments
 (0)