Skip to content

Commit 7471918

Browse files
committed
Add get Person id method, rename LoginModel to LoginRequestModel
1 parent 376a55c commit 7471918

6 files changed

Lines changed: 63 additions & 7 deletions

File tree

API.Test/UserTest.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,17 @@ public void GetTeachers()
4242
Assert.Fail();
4343
}
4444

45-
[Order(3)]
4645
[Test]
47-
public void TearUp()
46+
[Order(3)]
47+
public void GetPersonId()
4848
{
49-
Client.LogoutAsync().Wait();
49+
Client.LoginAsync(s_Server, s_LoginName, s_UserName, s_Password).Wait();
50+
51+
Task<int> personId = Client.GetPersonIdAsync(Client.User.ForeName, Client.User.LongName, Client.UserType ?? UserType.Student);
52+
personId.Wait();
53+
if (personId.Result == Client.User.Id)
54+
Assert.Pass();
55+
else
56+
Assert.Fail();
5057
}
5158
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
internal struct GetPersonIdRequestModel
11+
{
12+
[JsonProperty("fn")]
13+
public string Forename { get; set; }
14+
15+
[JsonProperty("sn")]
16+
public string Surname { get; set; }
17+
18+
[JsonProperty("type")]
19+
public int UserType { get; set; }
20+
21+
[JsonProperty("dob")]
22+
public const int s_Bithdate = 0;
23+
}
24+
}

WebUntisAPI.Client/Models/LoginModel.cs renamed to WebUntisAPI.Client/Models/LoginRequestModel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ namespace WebUntisAPI.Client.Models
55
/// <summary>
66
/// Model for the param of the login
77
/// </summary>
8-
internal struct LoginModel
8+
internal struct LoginRequestModel
9+
910
{
1011
/// <summary>
1112
/// Username to login

WebUntisAPI.Client/WebUntisClient.TimeTable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public async Task<Holidays[]> GetAllHolidaysAsync(string id = "getHolidays", Can
103103
/// <exception cref="UnauthorizedAccessException">Thrown when you're not logged in</exception>
104104
/// <exception cref="HttpRequestException">Thrown when an error happend while the http request</exception>
105105
/// <exception cref="WebUntisException">Thrown when the WebUntis API returned an error</exception>
106-
public async Task<Period[]> GetOwnTimetableAsync(DateTime startDate = default, DateTime endDate = default, string id = "GetTimtableForClass", CancellationToken ct = default)
106+
public async Task<Period[]> GetOwnTimetableAsync(DateTime startDate = default, DateTime endDate = default, string id = "GetOwnTimtable", CancellationToken ct = default)
107107
{
108108
if (UserType == Client.UserType.Student)
109109
return await GetTimetableForStudentAsync((Student)User, startDate, endDate, id, ct);

WebUntisAPI.Client/WebUntisClient.User.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,29 @@ public async Task<Teacher[]> GetAllTeachersAsync(string id = "getTeachers", Canc
5959
List<Teacher> teachers = await MakeRequestAsync<object, List<Teacher>>(id, "getTeachers", new object(), ct);
6060
return teachers.ToArray();
6161
}
62+
63+
/// <summary>
64+
/// Search for the id of a user where you know fore- and surname
65+
/// </summary>
66+
/// <param name="forename">Forename of the user (It doesn't matter if it is upper or lower case)</param>
67+
/// <param name="surname">Surname of the user (It doesn't matter if it is upper or lower case)</param>
68+
/// <param name="type">Type of the user</param>
69+
/// <param name="id">Identifier for the request</param>
70+
/// <param name="ct">Cancellation token</param>
71+
/// <returns>The <see cref="IUser.Id"/> of the requested user. 0 if the user isn't found</returns>
72+
/// <exception cref="UnauthorizedAccessException">Thrown when you're not logged in</exception>
73+
/// <exception cref="HttpRequestException">Thrown when there was an error while the http request</exception>
74+
/// <exception cref="WebUntisException">Thrown when the WebUntis API returned an error</exception>
75+
/// <exception cref="ObjectDisposedException">Thrown when the object is disposed</exception>
76+
public async Task<int> GetPersonIdAsync(string forename, string surname, UserType type, string id = "getPersonId", CancellationToken ct = default)
77+
{
78+
GetPersonIdRequestModel model = new GetPersonIdRequestModel()
79+
{
80+
Forename = forename,
81+
Surname = surname,
82+
UserType = (int)type
83+
};
84+
return await MakeRequestAsync<GetPersonIdRequestModel, int>(id, "getPersonId", model, ct);
85+
}
6286
}
6387
}

WebUntisAPI.Client/WebUntisClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,11 @@ public async Task<bool> LoginAsync(string server, string loginName, string usern
126126
string serverUrl = "https://" + serverName.Value;
127127

128128
// Make request for login
129-
JSONRPCRequestModel<LoginModel> requestModel = new JSONRPCRequestModel<LoginModel>()
129+
JSONRPCRequestModel<LoginRequestModel> requestModel = new JSONRPCRequestModel<LoginRequestModel>()
130130
{
131131
Id = id,
132132
Method = "authenticate",
133-
Params = new LoginModel()
133+
Params = new LoginRequestModel()
134134
{
135135
User = username,
136136
Password = password,

0 commit comments

Comments
 (0)