Skip to content

Commit de8a584

Browse files
committed
Add method to get message permissions
1 parent 913fd31 commit de8a584

3 files changed

Lines changed: 127 additions & 1 deletion

File tree

API.Test/MessagesTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Text;
55
using System.Threading.Tasks;
6+
using WebUntisAPI.Client.Models;
67
using static API.Test.AuthentificationTests;
78

89
namespace API.Test;
@@ -21,4 +22,17 @@ public void GetUnreadMessages()
2122
else
2223
Assert.Fail();
2324
}
25+
26+
[Test]
27+
public void GetMessagePermissions()
28+
{
29+
Client.LoginAsync(s_Server, s_LoginName, s_UserName, s_Password).Wait();
30+
31+
Task<MessagePermissions> permissions = Client.GetMessagePermissionsAsync();
32+
permissions.Wait();
33+
if (permissions.Result != null)
34+
Assert.Pass();
35+
else
36+
Assert.Fail();
37+
}
2438
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
/// <summary>
11+
/// Information about the permissions you have for messages
12+
/// </summary>
13+
public class MessagePermissions
14+
{
15+
/// <summary>
16+
/// To wich persons you could send the message
17+
/// </summary>
18+
/// <remarks>
19+
/// Possible values are:
20+
/// <list type="bullet">
21+
/// <item>TEACHER</item>
22+
/// <item>STUDENT</item>
23+
/// <item>CLASS</item>
24+
/// </list>
25+
/// </remarks>
26+
[JsonProperty("recipientOptions")]
27+
public string[] RecipientOptions { get; set; }
28+
29+
/// <summary>
30+
/// allow request read confirmation
31+
/// </summary>
32+
[JsonProperty("allowRequestReadConfirmation")]
33+
public bool AllowRequestReadConfirmation { get; set; }
34+
35+
/// <summary>
36+
/// How much recipient are listed in the search result
37+
/// </summary>
38+
[JsonProperty("recipientSearchMaxResult")]
39+
public int RecipientSearchMaxResult { get; set; }
40+
41+
/// <summary>
42+
/// Can you save drafts
43+
/// </summary>
44+
[JsonProperty("showDraftsTab")]
45+
public bool ShowDraftsTab { get; set; }
46+
47+
/// <summary>
48+
/// Can you send messages
49+
/// </summary>
50+
[JsonProperty("showSentTab")]
51+
public bool ShowSentTab { get; set; }
52+
53+
/// <summary>
54+
/// Can you forbid replies
55+
/// </summary>
56+
[JsonProperty("canForbidReplies")]
57+
public bool CanForbidReplies { get; set; }
58+
59+
/// <summary>
60+
/// The maximum size of a file you could attach in bytes
61+
/// </summary>
62+
[JsonProperty("maxFileSize")]
63+
public long MaxFileSize { get; set; }
64+
65+
/// <summary>
66+
/// The maximum count of files you could attach
67+
/// </summary>
68+
[JsonProperty("maxFileCount")]
69+
public int MaxFileCount { get; set; }
70+
}
71+
}
72+

WebUntisAPI.Client/WebUntisClient.Messages.cs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Newtonsoft.Json.Linq;
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Linq;
23
using System;
34
using System.Collections.Generic;
45
using System.Linq;
@@ -7,6 +8,7 @@
78
using System.Text;
89
using System.Threading;
910
using System.Threading.Tasks;
11+
using WebUntisAPI.Client.Models;
1012

1113
namespace WebUntisAPI.Client
1214
{
@@ -49,5 +51,43 @@ public async Task<int> GetUnreadMessagesCountAsync(CancellationToken ct = defaul
4951

5052
return JObject.Parse(await response.Content.ReadAsStringAsync()).Value<int>("unreadMessagesCount");
5153
}
54+
55+
/// <summary>
56+
/// Get the permissions you have to send messages
57+
/// </summary>
58+
/// <param name="ct">Cancllation token</param>
59+
/// <returns></returns>
60+
/// <exception cref="ObjectDisposedException">Thrown when the instance was disposed</exception>
61+
/// <exception cref="UnauthorizedAccessException">Thrown when you're logged in</exception>
62+
/// <exception cref="HttpRequestException">Thrown when an error happened while the http request</exception>
63+
public async Task<MessagePermissions> GetMessagePermissionsAsync(CancellationToken ct = default)
64+
{
65+
// Check for disposing
66+
if (_disposedValue)
67+
throw new ObjectDisposedException(GetType().FullName);
68+
69+
// Check if you logged in
70+
if (!LoggedIn)
71+
throw new UnauthorizedAccessException("You're not logged in");
72+
73+
HttpRequestMessage request = new HttpRequestMessage()
74+
{
75+
Method = HttpMethod.Get,
76+
RequestUri = new Uri(ServerUrl + "/WebUntis/api/rest/view/v1/messages/permissions")
77+
};
78+
SetRequestHeader(request.Headers, true);
79+
80+
HttpResponseMessage response = await _client.SendAsync(request, ct);
81+
82+
// Check cancellation token
83+
if (ct.IsCancellationRequested)
84+
return default;
85+
86+
// Verify response
87+
if (response.StatusCode != HttpStatusCode.OK)
88+
throw new HttpRequestException($"There was an error while the http request (Code: {response.StatusCode}).");
89+
90+
return JsonConvert.DeserializeObject<MessagePermissions>(await response.Content.ReadAsStringAsync());
91+
}
5292
}
5393
}

0 commit comments

Comments
 (0)