Skip to content

Commit 69c96d6

Browse files
committed
Add check for the authenticated error code in WebUntis exception and when detected, logout
1 parent 98c385a commit 69c96d6

2 files changed

Lines changed: 13 additions & 13 deletions

File tree

WebUntisAPI.Client/Exceptions/WebUntisException.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ public enum Codes
3030
/// </summary>
3131
MethodNotFound = -32601,
3232

33-
/// <summary>
34-
/// Access denied for the requested timetable
35-
/// </summary>
36-
NoRightForTimetable = -8509,
37-
3833
/// <summary>
3934
/// The element that you requested was not found
4035
/// </summary>
@@ -43,7 +38,12 @@ public enum Codes
4338
/// <summary>
4439
/// Access dinied for the method
4540
/// </summary>
46-
NoRightForMethod = -8509
41+
NoRightForMethod = -8509,
42+
43+
/// <summary>
44+
/// You're not authticated
45+
/// </summary>
46+
NotAuthticated = -8520
4747
}
4848

4949
/// <summary>

WebUntisAPI.Client/WebUntisClient.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,8 @@ public async Task<bool> LoginAsync(string server, string loginName, string usern
182182
/// <param name="id">Identifier for the request</param>
183183
/// <param name="ct">Cancellation token</param>
184184
/// <returns>Task for the proccess</returns>
185-
/// <exception cref="HttpRequestException">There was an error while the request</exception>
186185
/// <exception cref="ObjectDisposedException">Thrown when the object is disposed</exception>
187-
public async Task LogoutAsync(string id = "getStudents", CancellationToken ct = default)
186+
public async Task LogoutAsync(string id = "Logout", CancellationToken ct = default)
188187
{
189188
// Check for disposing
190189
if (_disposedValue)
@@ -205,17 +204,13 @@ public async Task LogoutAsync(string id = "getStudents", CancellationToken ct =
205204
requestContent.Headers.Add("jsessionid", _sessonId);
206205

207206
// Send request
208-
HttpResponseMessage response = await _client.PostAsync(ServerUrl + "/WebUntis/jsonrpc.do", requestContent, ct);
207+
_ = await _client.PostAsync(ServerUrl + "/WebUntis/jsonrpc.do", requestContent, ct);
209208

210209
// Clear data
211210
_serverUrl = null;
212211
_loginName = null;
213212
_sessonId = null;
214213
_loggedIn = false;
215-
216-
// Verify response
217-
if (response.StatusCode != HttpStatusCode.OK)
218-
throw new HttpRequestException($"There was an error while the http request (Code: {response.StatusCode}).");
219214
}
220215

221216
/// <summary>
@@ -287,7 +282,12 @@ private async Task<TResult> MakeJSONRPCRequestAsync<TRequest, TResult>(string id
287282

288283
// Check for WebUntis error
289284
if (responseModel.Error != null)
285+
{
286+
if (responseModel.Error.Code == (int)WebUntisException.Codes.NotAuthticated) // Logout when not authenticated
287+
_ = LogoutAsync();
288+
290289
throw responseModel.Error;
290+
}
291291

292292
return responseModel.Result;
293293
}

0 commit comments

Comments
 (0)