Skip to content

Commit 913fd31

Browse files
committed
Add get unread messages count
1 parent 98bf01e commit 913fd31

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

API.Test/MessagesTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using static API.Test.AuthentificationTests;
7+
8+
namespace API.Test;
9+
10+
internal class MessagesTests
11+
{
12+
[Test]
13+
public void GetUnreadMessages()
14+
{
15+
Client.LoginAsync(s_Server, s_LoginName, s_UserName, s_Password).Wait();
16+
17+
Task<int> messages = Client.GetUnreadMessagesCountAsync();
18+
messages.Wait();
19+
if (messages.Result == 0)
20+
Assert.Pass();
21+
else
22+
Assert.Fail();
23+
}
24+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using Newtonsoft.Json.Linq;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Net;
6+
using System.Net.Http;
7+
using System.Text;
8+
using System.Threading;
9+
using System.Threading.Tasks;
10+
11+
namespace WebUntisAPI.Client
12+
{
13+
public partial class WebUntisClient
14+
{
15+
/// <summary>
16+
/// Get the count of unread messages
17+
/// </summary>
18+
/// <param name="ct">Cancellation token</param>
19+
/// <returns>The count of unread messages</returns>
20+
/// <exception cref="ObjectDisposedException">Thrown when the instance was disposed</exception>
21+
/// <exception cref="UnauthorizedAccessException">Thrown when you're logged in</exception>
22+
/// <exception cref="HttpRequestException">Thrown when an error happened while the http request</exception>
23+
public async Task<int> GetUnreadMessagesCountAsync(CancellationToken ct = default)
24+
{
25+
// Check for disposing
26+
if (_disposedValue)
27+
throw new ObjectDisposedException(GetType().FullName);
28+
29+
// Check if you logged in
30+
if (!LoggedIn)
31+
throw new UnauthorizedAccessException("You're not logged in");
32+
33+
HttpRequestMessage request = new HttpRequestMessage()
34+
{
35+
Method = HttpMethod.Get,
36+
RequestUri = new Uri(ServerUrl + "/WebUntis/api/rest/view/v1/messages/status")
37+
};
38+
SetRequestHeader(request.Headers, true);
39+
40+
HttpResponseMessage response = await _client.SendAsync(request, ct);
41+
42+
// Check cancellation token
43+
if (ct.IsCancellationRequested)
44+
return default;
45+
46+
// Verify response
47+
if (response.StatusCode != HttpStatusCode.OK)
48+
throw new HttpRequestException($"There was an error while the http request (Code: {response.StatusCode}).");
49+
50+
return JObject.Parse(await response.Content.ReadAsStringAsync()).Value<int>("unreadMessagesCount");
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)