Skip to content

Commit e333848

Browse files
committed
Improve school search handle with too many schools,
Improve login when login data was wrong, Add some error code definitions
1 parent 386aba1 commit e333848

5 files changed

Lines changed: 39 additions & 27 deletions

File tree

API.Test/AuthentificationTests.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ namespace API.Test;
1111
[TestFixture]
1212
internal class AuthentificationTests
1313
{
14-
public static bool s_Successfull = false;
15-
1614
// Login data to test
1715
public static string s_Server;
1816
public static string s_LoginName;
@@ -41,12 +39,9 @@ public void Authentification()
4139
}
4240
catch
4341
{
44-
s_Successfull = false;
4542
Assert.Fail();
4643
return;
4744
}
48-
49-
s_Successfull = true;
5045
Assert.Pass();
5146
}
5247
}

API.Test/SchoolSearchTests.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,12 @@ public void NormalSearch()
2929
[Test]
3030
public void ToManySchoolsFound()
3131
{
32-
bool success;
33-
try
34-
{
35-
SchoolSearch.SearchAsync("M", CancellationToken.None).Wait();
36-
success = false;
37-
}
38-
catch (AggregateException ex) when (ex.InnerException.GetType() == typeof(WebUntisException))
39-
{
40-
success = true;
41-
}
42-
43-
Assert.IsTrue(success);
32+
Task<School[]> schools = SchoolSearch.SearchAsync("M", CancellationToken.None);
33+
schools.Wait();
34+
if (schools.Result == null)
35+
Assert.Pass();
36+
else
37+
Assert.Fail();
4438
}
4539

4640
[Order(3)]

WebUntisAPI.Client/Exceptions/WebUntisException.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,32 @@ namespace WebUntisAPI.Client.Exceptions
1010
/// <summary>
1111
/// An error that the WebUntis server returned
1212
/// </summary>
13-
[DataContract]
1413
public class WebUntisException : Exception
1514
{
15+
/// <summary>
16+
/// Some error codes that the webuntis server can return
17+
/// </summary>
18+
public enum Codes
19+
{
20+
/// <summary>
21+
/// Too many results in a search
22+
/// </summary>
23+
TooManyResults = -6003,
24+
25+
/// <summary>
26+
/// Wrong data
27+
/// </summary>
28+
BadCredentials = -8504,
29+
}
30+
1631
/// <summary>
1732
/// Code of the error
1833
/// </summary>
19-
[DataMember]
2034
public int code { get; set; }
2135

2236
/// <summary>
2337
/// A message that describes the error
2438
/// </summary>
25-
[DataMember]
2639
public string message { get; set; }
2740

2841
/// <summary>

WebUntisAPI.Client/SchoolSearch.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public static class SchoolSearch
3030
/// <param name="name">Name to search</param>
3131
/// <param name="ct">Token to cancel the search request</param>
3232
/// <param name="id">Name to identifies the request</param>
33-
/// <returns>All schools found</returns>
34-
/// <exception cref="WebUntisException">Throws when the API returned an error</exception>
33+
/// <returns>All schools found, an empty array when no school found or <see langword="null"/> when too many schools found</returns>
34+
/// <exception cref="WebUntisException">Throws when the WebUntis API returned an error</exception>
3535
/// <exception cref="HttpRequestException">Throws when an error happend while request</exception>
3636
public static async Task<School[]> SearchAsync(string name, CancellationToken ct, string id = "search")
3737
{
@@ -64,7 +64,12 @@ public static async Task<School[]> SearchAsync(string name, CancellationToken ct
6464

6565
// Check for WebUntis error
6666
if (responeModel.error != null)
67+
{
68+
if (responeModel.error.code == (int)WebUntisException.Codes.TooManyResults)
69+
return null;
70+
6771
throw responeModel.error;
72+
}
6873

6974
// Evaluate response
7075
return responeModel.result.schools;

WebUntisAPI.Client/WebUntisClient.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public WebUntisClient(string clientName, int timeout = 500)
8989
/// <param name="password">Password of the user to login</param>
9090
/// <param name="ct">Cancelationtoken</param>
9191
/// <param name="id">Identifier for the request</param>
92-
/// <returns><see langword="true"/> when the login was successfull</returns>
92+
/// <returns><see langword="true"/> when the login was successfull. <see langword="false"/> when the <paramref name="username"/> or <paramref name="password"/> was invalid</returns>
9393
/// <exception cref="ArgumentException">The server name is invalid</exception>
9494
/// <exception cref="HttpRequestException">There was an error while the request</exception>
9595
/// <exception cref="WebUntisException">The WebUntis server returned an error</exception>
@@ -105,7 +105,7 @@ public async Task<bool> LoginAsync(School school, string username, string passwo
105105
/// <param name="password">Password of the user to login</param>
106106
/// <param name="ct">Cancelationtoken</param>
107107
/// <param name="id">Identifier for the request</param>
108-
/// <returns><see langword="true"/> when the login was successfull</returns>
108+
/// <returns><see langword="true"/> when the login was successfull. <see langword="false"/> when the <paramref name="username"/> or <paramref name="password"/> was invalid</returns>
109109
/// <exception cref="ArgumentException">The server name is invalid</exception>
110110
/// <exception cref="HttpRequestException">There was an error while the request</exception>
111111
/// <exception cref="WebUntisException">The WebUntis server returned an error</exception>
@@ -136,14 +136,13 @@ public async Task<bool> LoginAsync(string server, string loginName, string usern
136136
StringContent requestContent = new StringContent(JsonConvert.SerializeObject(requestModel), Encoding.UTF8, "application/json");
137137

138138
// Send request
139-
_client.BaseAddress = new Uri(serverUrl);
140139
string schoolName = loginName.ToLower().Replace(' ', '+');
141140
HttpResponseMessage response = await _client.PostAsync(serverUrl + "/WebUntis/jsonrpc.do?school=" + schoolName, requestContent, ct);
142141

143142
// Check cancellation token
144143
if (ct.IsCancellationRequested)
145144
return false;
146-
145+
147146
// Verify response
148147
if (response.StatusCode != HttpStatusCode.OK)
149148
throw new HttpRequestException($"There was an error while the http request (Code: {response.StatusCode}).");
@@ -152,7 +151,12 @@ public async Task<bool> LoginAsync(string server, string loginName, string usern
152151

153152
// Check for WebUntis error
154153
if (responseModel.error != null)
154+
{
155+
if (responseModel.error.code == (int)WebUntisException.Codes.BadCredentials) // Wrong login data
156+
return false;
157+
155158
throw responseModel.error;
159+
}
156160

157161
// Setup data and get default data
158162
_serverUrl = serverUrl;
@@ -167,7 +171,7 @@ public async Task<bool> LoginAsync(string server, string loginName, string usern
167171
/// Logout (You can reuse the client)
168172
/// </summary>
169173
/// <param name="id">Identifier for the request</param>
170-
/// <returns></returns>
174+
/// <returns>Task for the proccess</returns>
171175
/// <exception cref="HttpRequestException">There was an error while the request</exception>
172176
public async Task LogoutAsync(string id = "logout")
173177
{
@@ -193,6 +197,7 @@ public async Task LogoutAsync(string id = "logout")
193197
_loginName = null;
194198
_sessonId = null;
195199
_loggedIn = false;
200+
_client.BaseAddress = null;
196201

197202
// Verify response
198203
if (response.StatusCode != HttpStatusCode.OK)

0 commit comments

Comments
 (0)