Skip to content

Commit b822a6c

Browse files
committed
Add general account informations
1 parent cb4903c commit b822a6c

4 files changed

Lines changed: 353 additions & 1 deletion

File tree

API.Test/ProfileTests.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Threading.Tasks;
66
using static API.Test.AuthentificationTests;
77
using SixLabors.ImageSharp;
8+
using WebUntisAPI.Client.Models;
89

910
namespace API.Test;
1011

@@ -25,4 +26,46 @@ public void GetRecipientProfileImgTest()
2526
imgDownload.Wait();
2627
imgDownload.Result.SaveAsPng("DownloadImg.png");
2728
}
29+
30+
[Test]
31+
public void GetSupportedLanguages()
32+
{
33+
Client.LoginAsync(s_Server, s_LoginName, s_UserName, s_Password).Wait();
34+
35+
Task<Dictionary<string, string>> languages = Client.GetAvailableLanguagesAsync();
36+
languages.Wait();
37+
38+
if (languages.Result.Count > 0)
39+
Assert.Pass();
40+
else
41+
Assert.Fail();
42+
}
43+
44+
[Test]
45+
public void GetAccountConfiguration()
46+
{
47+
Client.LoginAsync(s_Server, s_LoginName, s_UserName, s_Password).Wait();
48+
49+
Task<AccountConfig> accountConfig = Client.GetAccountConfigAsync();
50+
accountConfig.Wait();
51+
52+
if (accountConfig.Result is not null)
53+
Assert.Pass();
54+
else
55+
Assert.Fail();
56+
}
57+
58+
[Test]
59+
public void GetGenerallyInformation()
60+
{
61+
Client.LoginAsync(s_Server, s_LoginName, s_UserName, s_Password).Wait();
62+
63+
Task<GeneralAccount> account = Client.GetGenerallyAccountInformationAsync();
64+
account.Wait();
65+
66+
if (account.Result is not null)
67+
Assert.Pass();
68+
else
69+
Assert.Fail();
70+
}
2871
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
/// Account configuration details
12+
/// </summary>
13+
public class AccountConfig
14+
{
15+
/// <summary>
16+
/// Can display the contact details for this account
17+
/// </summary>
18+
[JsonProperty("canReadContactDetail")]
19+
public bool CanReadContactDetails { get; set; } = false;
20+
}
21+
}
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Linq;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace WebUntisAPI.Client.Models
10+
{
11+
/// <summary>
12+
/// General information about a account
13+
/// </summary>
14+
public class GeneralAccount
15+
{
16+
/// <summary>
17+
/// The name of the user
18+
/// </summary>
19+
public string Name { get; set; }
20+
21+
/// <summary>
22+
/// The user group of the user
23+
/// </summary>
24+
public string UserGroup { get; set; }
25+
26+
/// <summary>
27+
/// The id of the role of the user
28+
/// </summary>
29+
public int RoleId { get; set; }
30+
31+
/// <summary>
32+
/// The name of the department (An empty value means the user has not department)
33+
/// </summary>
34+
public string Department { get; set; }
35+
36+
/// <summary>
37+
/// The language code of the user
38+
/// </summary>
39+
/// <remarks>
40+
/// That are no normal language codes. You must use the values from <see cref="WebUntisClient.GetAvailableLanguagesAsync(System.Threading.CancellationToken)"/>
41+
/// </remarks>
42+
public string LanguageCode { get; set; }
43+
44+
/// <summary>
45+
/// The email of the user
46+
/// </summary>
47+
public string Email { get; set; }
48+
49+
/// <summary>
50+
/// The max open bookings
51+
/// </summary>
52+
public int EffectiveMaxBookings { get; set; }
53+
54+
/// <summary>
55+
/// The current open bookings
56+
/// </summary>
57+
public int OpenBookings { get; set; }
58+
59+
/// <summary>
60+
/// Forward the messages to the users <see cref="Email"/>
61+
/// </summary>
62+
public bool ForwardMessageToMail { get; set; }
63+
64+
/// <summary>
65+
/// Notifications from the task and ticket system
66+
/// </summary>
67+
public bool UserTaskNotifications { get; set; }
68+
69+
/// <summary>
70+
/// Is a password change allowed
71+
/// </summary>
72+
public bool PasswordChangeAllowed { get; set; }
73+
74+
/// <summary>
75+
/// Forward system mails
76+
/// </summary>
77+
public bool SystemMailForwarding { get; set; }
78+
79+
/// <summary>
80+
/// The gender of the user
81+
/// </summary>
82+
public Gender UserGender { get; set; }
83+
84+
/// <summary>
85+
/// The count of items on the start page
86+
/// </summary>
87+
public int ItemsOnStartPage { get; set; }
88+
89+
/// <summary>
90+
/// Show the lessons of the day
91+
/// </summary>
92+
public bool ShowLessonsOfDay { get; set; }
93+
94+
/// <summary>
95+
/// Show the next day periods
96+
/// </summary>
97+
public bool ShowNextDayPeriods { get; set; }
98+
99+
internal static GeneralAccount ReadFromJson(JsonReader reader)
100+
{
101+
JObject data = JObject.Load(reader);
102+
JObject profile = data["profile"].Value<JObject>();
103+
104+
return new GeneralAccount
105+
{
106+
Name = profile["name"].Value<string>(),
107+
UserGroup = profile["userGroup"].Value<string>(),
108+
RoleId = profile["userRoleId"].Value<int>(),
109+
Department = profile["department"].Value<string>(),
110+
LanguageCode = profile["languageCode"].Value<string>(),
111+
Email = profile["email"].Value<string>(),
112+
EffectiveMaxBookings = profile["effectiveMaxBookings"].Value<int>(),
113+
OpenBookings = profile["openBookings"].Value<int>(),
114+
ForwardMessageToMail = profile["forwardMessageToEmail"].Value<bool>(),
115+
UserTaskNotifications = profile["userTaskNotifications"].Value<bool>(),
116+
PasswordChangeAllowed = data["pwChangeAllowed"]?.Value<bool>() ?? profile["pwChangeAllowed"].Value<bool>(),
117+
SystemMailForwarding = profile["systemMailForwarding"].Value<bool>(),
118+
UserGender = profile["gender"].ToObject<Gender>(),
119+
ItemsOnStartPage = profile["itemOnStartPage"].Value<int>(),
120+
ShowLessonsOfDay = profile["showLessonsOfDay"].Value<bool>(),
121+
ShowNextDayPeriods = profile["showNextDayPeriods"].Value<bool>()
122+
};
123+
}
124+
125+
/// <summary>
126+
/// A user gender
127+
/// </summary>
128+
public class Gender
129+
{
130+
/// <summary>
131+
/// The gender id
132+
/// </summary>
133+
[JsonProperty("id")]
134+
public int Id { get; set; }
135+
136+
/// <summary>
137+
/// The long label
138+
/// </summary>
139+
[JsonProperty("longLabel")]
140+
public string LongLabel { get; set; }
141+
142+
/// <summary>
143+
/// the short label
144+
/// </summary>
145+
[JsonProperty("shortLabel")]
146+
public string ShortMale { get; set; }
147+
148+
/// <summary>
149+
/// The icon
150+
/// </summary>
151+
[JsonProperty("icon")]
152+
public string Icon { get; set; }
153+
}
154+
}
155+
}

WebUntisAPI.Client/WebUntisclient.Profile.cs

Lines changed: 134 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99
using System.Threading.Tasks;
1010
using WebUntisAPI.Client.Models.Messages;
1111
using System.Drawing.Imaging;
12+
using Newtonsoft.Json.Linq;
13+
using WebUntisAPI.Client.Models;
14+
using Newtonsoft.Json;
15+
using System.IO;
1216
#if NET47 || NET481
1317
using System.Drawing.Drawing2D;
1418
using System.Drawing;
15-
using System.IO;
1619
#elif NET6_0_OR_GREATER
1720
using SixLabors.Fonts;
1821
using SixLabors.ImageSharp;
@@ -173,6 +176,136 @@ public async Task<Image> GetMessagePersonProfileImageAsync(MessagePerson person,
173176
#endif
174177
}
175178

179+
/// <summary>
180+
/// Get all by WebUntis supported languages
181+
/// </summary>
182+
/// <param name="ct">Cancellation token</param>
183+
/// <returns>The languages (<see cref="KeyValuePair{TKey, TValue}.Key"/> is the WebUntis internal name of the language and <see cref="KeyValuePair{TKey, TValue}.Value"/> is the full name)</returns>
184+
/// <exception cref="ObjectDisposedException">Thrown when the instance was disposed</exception>
185+
/// <exception cref="UnauthorizedAccessException">Thrown when you're logged in</exception>
186+
/// <exception cref="HttpRequestException">Thrown when an error happened while the http request</exception>
187+
public async Task<Dictionary<string, string>> GetAvailableLanguagesAsync(CancellationToken ct = default)
188+
{
189+
string responseString = await MakeAPIGetRequestAsync("/WebUntis/api/profile/languages", ct);
190+
JArray languageObjects = JObject.Parse(responseString)["data"]["languages"].Value<JArray>();
191+
192+
Dictionary<string, string> languages = new Dictionary<string, string>();
193+
foreach (JObject language in languageObjects.Cast<JObject>())
194+
languages.Add(language.Value<string>("key"), language.Value<string>("name"));
195+
196+
return languages;
197+
}
198+
199+
/// <summary>
200+
/// Get the account configuration
201+
/// </summary>
202+
/// <param name="ct">Cancellation token</param>
203+
/// <returns>The account configuration</returns>
204+
/// <exception cref="ObjectDisposedException">Thrown when the instance was disposed</exception>
205+
/// <exception cref="UnauthorizedAccessException">Thrown when you're logged in</exception>
206+
/// <exception cref="HttpRequestException">Thrown when an error happened while the http request</exception>
207+
public async Task<AccountConfig> GetAccountConfigAsync(CancellationToken ct = default)
208+
{
209+
string responseString = await MakeAPIGetRequestAsync("/WebUntis/api/profile/config", ct);
210+
return JObject.Parse(responseString)["data"].ToObject<AccountConfig>();
211+
}
212+
213+
/// <summary>
214+
/// Get the general information about the current account
215+
/// </summary>
216+
/// <param name="ct">Cancellation token</param>
217+
/// <returns>The information</returns>
218+
public async Task<GeneralAccount> GetGenerallyAccountInformationAsync(CancellationToken ct = default)
219+
{
220+
string responseString = await MakeAPIGetRequestAsync("/WebUntis/api/profile/general", ct);
221+
return GeneralAccount.ReadFromJson(JObject.Parse(responseString)["data"].CreateReader());
222+
}
223+
224+
/// <summary>
225+
/// Update the general account information
226+
/// </summary>
227+
/// <remarks>
228+
/// When a value shouldn't change then must you set the current value
229+
/// </remarks>
230+
/// <param name="email"><see cref="GeneralAccount.Email"/></param>
231+
/// <param name="forwardMessageToEmail"><see cref="GeneralAccount.ForwardMessageToMail"/></param>
232+
/// <param name="itemsOnStartPage"><see cref="GeneralAccount.ItemsOnStartPage"/></param>
233+
/// <param name="languageCode"><see cref="GeneralAccount.LanguageCode"/></param>
234+
/// <param name="showLessonOfDay"><see cref="GeneralAccount.ShowLessonsOfDay"/></param>
235+
/// <param name="showNextDayPeriods"><see cref="GeneralAccount.ShowNextDayPeriods"/></param>
236+
/// <param name="userTaskNotifications"><see cref="GeneralAccount.UserTaskNotifications"/></param>
237+
/// <param name="ct">Cancellation token</param>
238+
/// <returns>Was the update successful</returns>
239+
public async Task<bool> UpdateGenerallyAccountInformationAsync(string email, bool forwardMessageToEmail, int itemsOnStartPage, string languageCode, bool showLessonOfDay, bool showNextDayPeriods, bool userTaskNotifications, CancellationToken ct = default)
240+
{
241+
// Check for disposing
242+
if (_disposedValue)
243+
throw new ObjectDisposedException(GetType().FullName);
244+
245+
// Check if you logged in
246+
if (!LoggedIn)
247+
throw new UnauthorizedAccessException("You're not logged in");
248+
249+
// Write request
250+
StringWriter sw = new StringWriter();
251+
using (JsonWriter writer = new JsonTextWriter(sw))
252+
{
253+
writer.WriteStartObject();
254+
255+
writer.WritePropertyName("email");
256+
writer.WriteValue(email);
257+
258+
writer.WritePropertyName("forwardMessageToEmail");
259+
writer.WriteValue(forwardMessageToEmail);
260+
261+
writer.WritePropertyName("itemOnStartPage");
262+
writer.WriteValue(itemsOnStartPage);
263+
264+
writer.WritePropertyName("languageCode");
265+
writer.WriteValue(languageCode);
266+
267+
writer.WritePropertyName("showLessonsOfDay");
268+
writer.WriteValue(showLessonOfDay);
269+
270+
writer.WritePropertyName("showNextDayPeriods");
271+
writer.WriteValue(showNextDayPeriods);
272+
273+
writer.WritePropertyName("userTaskNotifications");
274+
writer.WriteValue(userTaskNotifications);
275+
276+
writer.WriteEndObject();
277+
}
278+
279+
HttpRequestMessage request = new HttpRequestMessage()
280+
{
281+
Method = HttpMethod.Post,
282+
RequestUri = new Uri(ServerUrl + "/WebUntis/api/profile/general"),
283+
Content = new StringContent(sw.ToString())
284+
};
285+
286+
request.Headers.Add("JSESSIONID", _sessionId);
287+
request.Headers.Add("schoolname", _schoolName);
288+
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _bearerToken);
289+
290+
HttpResponseMessage response = await _client.SendAsync(request, ct);
176291

292+
// Check cancellation token
293+
if (ct.IsCancellationRequested)
294+
return false;
295+
296+
// Verify response
297+
if (response.StatusCode == HttpStatusCode.Unauthorized || response.StatusCode == HttpStatusCode.Forbidden)
298+
{
299+
_ = LogoutAsync();
300+
throw new UnauthorizedAccessException("You're not logged in");
301+
}
302+
303+
if (response.StatusCode != HttpStatusCode.OK)
304+
throw new HttpRequestException($"There was an error while the http request (Code: {response.StatusCode}).");
305+
306+
string responseString = await response.Content.ReadAsStringAsync();
307+
JObject responseObject = JObject.Parse(responseString);
308+
return responseObject["data"]["success"].Value<bool>();
309+
}
177310
}
178311
}

0 commit comments

Comments
 (0)