Skip to content

Commit 279f8d6

Browse files
committed
Add creation, read and update methods for drafts (Basically everything for drafts)
1 parent 3e8f225 commit 279f8d6

11 files changed

Lines changed: 652 additions & 174 deletions

File tree

API.Test/MessagesTests.cs

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

99
namespace API.Test;
@@ -15,7 +15,7 @@ public void GetUnreadMessages()
1515
{
1616
Client.LoginAsync(s_Server, s_LoginName, s_UserName, s_Password).Wait();
1717

18-
Task<int> messages = Client.MessageClient.GetUnreadMessagesCountAsync();
18+
Task<int> messages = Client.GetUnreadMessagesCountAsync();
1919
messages.Wait();
2020
if (messages.Result == 0)
2121
Assert.Pass();
@@ -28,7 +28,7 @@ public void GetMessagePermissions()
2828
{
2929
Client.LoginAsync(s_Server, s_LoginName, s_UserName, s_Password).Wait();
3030

31-
Task<MessagePermissions> permissions = Client.MessageClient.GetMessagePermissionsAsync();
31+
Task<MessagePermissions> permissions = Client.GetMessagePermissionsAsync();
3232
permissions.Wait();
3333
if (permissions.Result != null)
3434
Assert.Pass();
@@ -41,7 +41,7 @@ public void GetMessageInbox()
4141
{
4242
Client.LoginAsync(s_Server, s_LoginName, s_UserName, s_Password).Wait();
4343

44-
Task<MessagePreview[]> messages = Client.MessageClient.GetMessageInboxAsync();
44+
Task<MessagePreview[]> messages = Client.GetMessageInboxAsync();
4545
messages.Wait();
4646
if (messages.Result.Length > 0)
4747
Assert.Pass();
@@ -54,7 +54,7 @@ public void GetFullMessage()
5454
{
5555
Client.LoginAsync(s_Server, s_LoginName, s_UserName, s_Password).Wait();
5656

57-
Task<MessagePreview[]> messages = Client.MessageClient.GetMessageInboxAsync();
57+
Task<MessagePreview[]> messages = Client.GetMessageInboxAsync();
5858
messages.Wait();
5959
Task<Message> msg = messages.Result.First(msg => msg.Subject == "Test").GetFullMessageAsync(Client);
6060
msg.Wait();
@@ -67,11 +67,24 @@ public void GetReceptionPeople()
6767
{
6868
Client.LoginAsync(s_Server, s_LoginName, s_UserName, s_Password).Wait();
6969

70-
Task<KeyValuePair<string, MessagePerson[]>[]> people = Client.MessageClient.GetMessagePeopleAsync();
70+
Task<KeyValuePair<string, MessagePerson[]>[]> people = Client.GetMessagePeopleAsync();
7171
people.Wait();
7272
if (people.Result.Length > 0)
7373
Assert.Pass();
7474
else
7575
Assert.Fail();
7676
}
77+
78+
[Test]
79+
public void GetDrafts()
80+
{
81+
Client.LoginAsync(s_Server, s_LoginName, s_UserName, s_Password).Wait();
82+
83+
Task<DraftPreview[]> drafts = Client.GetSavedDraftsAsync();
84+
drafts.Wait();
85+
if (drafts.Result != null)
86+
Assert.Pass();
87+
else
88+
Assert.Fail();
89+
}
7790
}

WebUntisAPI.Client/MessageClient.cs

Lines changed: 0 additions & 97 deletions
This file was deleted.

WebUntisAPI.Client/Models/Attachment.cs renamed to WebUntisAPI.Client/Models/Messages/Attachment.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
using System.Threading;
1414
using System.Threading.Tasks;
1515

16-
namespace WebUntisAPI.Client.Models
16+
namespace WebUntisAPI.Client.Models.Messages
1717
{
1818
/// <summary>
1919
/// An attachment from a message
@@ -31,7 +31,7 @@ public struct Attachment
3131
/// The attachment id
3232
/// </summary>
3333
[JsonProperty("id")]
34-
private readonly string _id;
34+
public string Id { get; set; }
3535

3636
/// <summary>
3737
/// Get the content of the attachment as stream
@@ -45,11 +45,11 @@ public struct Attachment
4545
/// <exception cref="HttpRequestException">Thrown when an error happened while the http request</exception>
4646
public async Task<MemoryStream> DownloadContentAsStreamAsync(WebUntisClient client, int timeout = 2000, CancellationToken ct = default)
4747
{
48-
string storageResponseString = await client.MakeAPIGetRequestAsync($"/WebUntis/api/rest/view/v1/messages/{_id}/attachmentstorageurl", ct);
48+
string storageResponseString = await client.MakeAPIGetRequestAsync($"/WebUntis/api/rest/view/v1/messages/{Id}/attachmentstorageurl", ct);
4949

5050
JObject data = JObject.Parse(storageResponseString);
5151
JArray headerArray = data.Value<JArray>("additionalHeaders");
52-
52+
5353
HttpRequestMessage request = new HttpRequestMessage()
5454
{
5555
Method = HttpMethod.Get,
@@ -80,7 +80,6 @@ public async Task<MemoryStream> DownloadContentAsStreamAsync(WebUntisClient clie
8080

8181
if (response.StatusCode == HttpStatusCode.Unauthorized || response.StatusCode == HttpStatusCode.Forbidden)
8282
{
83-
8483
string detail = Regex.Match(await response.Content.ReadAsStringAsync(), @"<Message>([a-zA-z0-9\s]+)</Message>").Groups[1].Value; // Get the error message
8584
throw new UnauthorizedAccessException($"Invalid authentication. Detail: {detail}");
8685
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Diagnostics;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using WebUntisAPI.Client.Converter;
9+
10+
namespace WebUntisAPI.Client.Models.Messages
11+
{
12+
/// <summary>
13+
/// A draft
14+
/// </summary>
15+
[DebuggerDisplay("Subject: {Subject, nq}, Sender: {Sender, nq}, Send time: {SentTime, nq}")]
16+
public class Draft
17+
{
18+
/// <summary>
19+
/// The id of the message
20+
/// </summary>
21+
[JsonProperty("id")]
22+
public int Id { get; set; }
23+
24+
/// <summary>
25+
/// The subject of the message
26+
/// </summary>
27+
[JsonProperty("subject")]
28+
public string Subject { get; set; }
29+
30+
/// <summary>
31+
/// The sender of the message (only for messages in the inbox)
32+
/// </summary>
33+
[JsonProperty("sender")]
34+
public MessagePerson Sender { get; set; } = null;
35+
36+
/// <summary>
37+
/// Is the message a reply
38+
/// </summary>
39+
[JsonProperty("isReply")]
40+
public bool IsReply { get; set; }
41+
42+
/// <summary>
43+
/// Can you reply the message
44+
/// </summary>
45+
[JsonProperty("isReplyAllowed")]
46+
public bool IsReplyAllowed { get; set; }
47+
48+
/// <summary>
49+
/// The send time of the message
50+
/// </summary>
51+
[JsonProperty("sentDateTime")]
52+
[JsonConverter(typeof(APIDateTimeJsonConverter))]
53+
DateTime SentTime { get; set; }
54+
55+
/// <summary>
56+
/// Is allowed to delete the message
57+
/// </summary>
58+
[JsonProperty("allowMessageDeletion")]
59+
public bool AllowMessageDeletion { get; set; }
60+
61+
/// <summary>
62+
/// Idk
63+
/// </summary>
64+
[JsonProperty("recipientGroups")]
65+
public List<object> RecipientGroups { get; set; }
66+
67+
/// <summary>
68+
/// All the recipients for a message (only for self-sends messages)
69+
/// </summary>
70+
/// <remarks>
71+
/// When its <see langword="null"/> is the recipient the current user
72+
/// </remarks>
73+
[JsonProperty("recipientPersons")]
74+
public List<MessagePerson> Recipients { get; set; } = null;
75+
76+
/// <summary>
77+
/// The full content of the draft
78+
/// </summary>
79+
[JsonProperty("content")]
80+
public string Content { get; set; }
81+
82+
/// <summary>
83+
/// The file attachments of the draft
84+
/// </summary>
85+
[JsonProperty("storageAttachments")]
86+
public List<Attachment> Attachments { get; set; }
87+
88+
/// <summary>
89+
/// The option for the recipient
90+
/// </summary>
91+
[JsonProperty("recipientOption")]
92+
public string RecipientOption { get; set; }
93+
94+
/// <summary>
95+
/// Forbid the reply (you need the permission)
96+
/// </summary>
97+
[JsonProperty("forbidReply")]
98+
public bool ForbidReply { get; set; }
99+
100+
/// <summary>
101+
/// Idk
102+
/// </summary>
103+
[JsonProperty("copyToStudents")]
104+
public bool CopyToStudents { get; set; }
105+
}
106+
}

0 commit comments

Comments
 (0)