Skip to content

Commit 9c41475

Browse files
committed
Improve some details at the contact detail request, Add a method to get the own profile image
1 parent a1c09f2 commit 9c41475

2 files changed

Lines changed: 72 additions & 7 deletions

File tree

API.Test/ProfileTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,15 @@ public void GetContactDetails()
8282
else
8383
Assert.Fail();
8484
}
85+
86+
[Test]
87+
public void GetOwnProfileImage()
88+
{
89+
Client.LoginAsync(s_Server, s_LoginName, s_UserName, s_Password).Wait();
90+
91+
Task<(Image? image, bool read, bool write)> image = Client.GetOwnProfileImageAsync();
92+
image.Wait();
93+
94+
image.Result.image.SaveAsPngAsync("ProfileImg.png");
95+
}
8596
}

WebUntisAPI.Client/WebUntisclient.Profile.cs

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -228,22 +228,76 @@ public async Task<GeneralAccount> GetGenerallyAccountInformationAsync(Cancellati
228228
/// Get the contact details for this account
229229
/// </summary>
230230
/// <param name="ct">Cancellation token</param>
231-
/// <returns>When read is false the contact <see langword="null"/></returns>
231+
/// <returns>If canRead is false, the contact is <see langword="null"/></returns>
232232
/// <exception cref="ObjectDisposedException">Thrown when the instance was disposed</exception>
233233
/// <exception cref="UnauthorizedAccessException">Thrown when you're logged in</exception>
234234
/// <exception cref="HttpRequestException">Thrown when an error happened while the http request</exception>
235-
public async Task<(ContactDetails contact, bool read, bool write)> GetContactDetailsAsync(CancellationToken ct = default)
235+
public async Task<(ContactDetails contact, bool canRead, bool canWrite)> GetContactDetailsAsync(CancellationToken ct = default)
236236
{
237-
string responseString = await MakeAPIGetRequestAsync("/WebUntis/api/profile/contactdetails?personId=3299&isRequestForStudent=false", ct);
237+
string responseString = await MakeAPIGetRequestAsync($"/WebUntis/api/profile/contactdetails?personId={User.Id}&isRequestForStudent={UserType is Client.UserType.Student}", ct);
238238
JObject data = JObject.Parse(responseString)["data"].Value<JObject>();
239239

240-
if (data["read"].Value<bool>())
240+
if (!data["read"].Value<bool>()) // Return null when you do not have a read permission
241+
return (null, false, data["write"].Value<bool>());
242+
243+
ContactDetails contact = data["address"].ToObject<ContactDetails>();
244+
return (contact, true, data["write"].Value<bool>());
245+
}
246+
247+
/// <summary>
248+
/// Get your own profile image
249+
/// </summary>
250+
/// <remarks>
251+
/// If you have not specified a profile picture, the image would be null
252+
/// </remarks>
253+
/// <param name="ct">Cancellation token</param>
254+
/// <returns>If canRead is false, the image is <see langword="null"/></returns>
255+
/// <exception cref="ObjectDisposedException">Thrown when the instance was disposed</exception>
256+
/// <exception cref="UnauthorizedAccessException">Thrown when you're logged in</exception>
257+
/// <exception cref="HttpRequestException">Thrown when an error happened while the http request</exception>
258+
#if NET47 || NET481
259+
public async Task<(Image image, bool canRead, bool canWrite)> GetOwnProfileImageAsync(CancellationToken ct = default)
260+
#elif NET6_0_OR_GREATER
261+
public async Task<(Image image, bool canRead, bool canWrite)> GetOwnProfileImageAsync(CancellationToken ct = default)
262+
#endif
263+
{
264+
string responseString = await MakeAPIGetRequestAsync($"/WebUntis/api/profile/image?type={(int)UserType}&id={User.Id}", ct);
265+
JObject data = JObject.Parse(responseString)["data"].Value<JObject>();
266+
267+
if (!data["read"].Value<bool>()) // Return null when you do not have a read permission
268+
return (null, false, data["write"].Value<bool>());
269+
270+
HttpRequestMessage request = new HttpRequestMessage { Method = HttpMethod.Get };
271+
request.Headers.Add("JSESSIONID", _sessionId);
272+
request.Headers.Add("schoolname", _schoolName);
273+
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _bearerToken);
274+
275+
if (data["imageId"].Value<int>() < 0) // Return the default image when the image isn't set
276+
return (null, true, data["write"].Value<bool>());
277+
278+
request.RequestUri = new Uri(ServerUrl + $"/WebUntis/image.do?cat={data["categoryId"].Value<int>()}&id={data["imageId"].Value<int>()}");
279+
HttpResponseMessage response = await _client.SendAsync(request, ct);
280+
281+
// Check cancellation token
282+
if (ct.IsCancellationRequested)
283+
return default;
284+
285+
// Verify response
286+
if (response.StatusCode == HttpStatusCode.Unauthorized || response.StatusCode == HttpStatusCode.Forbidden)
241287
{
242-
ContactDetails contact = data["address"].ToObject<ContactDetails>();
243-
return (contact, true, data["write"].Value<bool>());
288+
_ = LogoutAsync();
289+
throw new UnauthorizedAccessException("You're not logged in");
244290
}
245291

246-
return (null, false, data["write"].Value<bool>());
292+
if (response.StatusCode != HttpStatusCode.OK)
293+
throw new HttpRequestException($"There was an error while the http request (Code: {response.StatusCode}).");
294+
295+
#if NET47 || NET481
296+
Image image = Image.FromStream(await response.Content.ReadAsStreamAsync());
297+
#elif NET6_0_OR_GREATER
298+
Image image = await Image.LoadAsync(await response.Content.ReadAsStreamAsync(ct), ct);
299+
#endif
300+
return (image, true, data["write"].Value<bool>());
247301
}
248302
}
249303
}

0 commit comments

Comments
 (0)