Skip to content

Commit 7e58baf

Browse files
committed
Add rooms an their get method
1 parent d4964fa commit 7e58baf

3 files changed

Lines changed: 74 additions & 28 deletions

File tree

API.Test/TeachingTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,17 @@ public void GetClass()
3737
else
3838
Assert.Fail();
3939
}
40+
41+
[Test]
42+
public void GetRoom()
43+
{
44+
Client.LoginAsync(s_Server, s_LoginName, s_UserName, s_Password).Wait();
45+
46+
Task<Room[]> rooms = Client.GetAllRoomsAsync();
47+
rooms.Wait();
48+
if (rooms.Result.Length > 0)
49+
Assert.Pass();
50+
else
51+
Assert.Fail();
52+
}
4053
}

WebUntisAPI.Client/Models/Room.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
9+
namespace WebUntisAPI.Client.Models
10+
{
11+
/// <summary>
12+
/// A Room
13+
/// </summary>
14+
[DebuggerDisplay("Name = {LongName}")]
15+
public struct Room
16+
{
17+
/// <summary>
18+
/// Id of the room
19+
/// </summary>
20+
[JsonProperty("id")]
21+
public int Id { get; set; }
22+
23+
/// <summary>
24+
/// Short name of the room
25+
/// </summary>
26+
[JsonProperty("name")]
27+
public string Name { get; set; }
28+
29+
/// <summary>
30+
/// Normal name of the room
31+
/// </summary>
32+
[JsonProperty("longName")]
33+
public string LongName { get; set; }
34+
35+
/// <summary>
36+
/// Is the room active or not
37+
/// </summary>
38+
[JsonProperty("active")]
39+
public bool Active { get; set; }
40+
41+
/// <summary>
42+
/// The building where the room is
43+
/// </summary>
44+
[JsonProperty("building")]
45+
public string Building { get; set; }
46+
}
47+
}

WebUntisAPI.Client/WebUntisClient.Teaching.cs

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -49,34 +49,20 @@ public async Task<Class[]> GetAllClassesAsync(string id = "getClasses", Cancella
4949
return classes.ToArray();
5050
}
5151

52-
// Make request
53-
JSONRPCRequestModel<object> requestModel = new JSONRPCRequestModel<object>()
54-
{
55-
Id = id,
56-
Method = "getKlassen",
57-
Params = new object()
58-
};
59-
StringContent requestContent = new StringContent(JsonConvert.SerializeObject(requestModel), Encoding.UTF8, "application/json");
60-
61-
// Send request
62-
HttpResponseMessage response = await _client.PostAsync(ServerUrl + "/WebUntis/jsonrpc.do", requestContent, ct);
63-
64-
// Check cancellation token
65-
if (ct.IsCancellationRequested)
66-
return null;
67-
68-
// Verify response
69-
if (response.StatusCode != HttpStatusCode.OK)
70-
throw new HttpRequestException($"There was an error while the http request (Code: {response.StatusCode}).");
71-
72-
JSONRPCResponeModel<List<Class>> responseModel = JsonConvert.DeserializeObject<JSONRPCResponeModel<List<Class>>>(await response.Content.ReadAsStringAsync());
73-
74-
// Check for WebUntis error
75-
if (responseModel.Error != null)
76-
throw responseModel.Error;
77-
78-
return responseModel.Result.ToArray();
52+
/// <summary>
53+
/// Get all rooms on the school
54+
/// </summary>
55+
/// <param name="id">Identifier of the request</param>
56+
/// <param name="ct">Cancellation token</param>
57+
/// <returns>All rooms on the school</returns>
58+
/// <exception cref="ObjectDisposedException">Thrown when the instance was disposed</exception>
59+
/// <exception cref="UnauthorizedAccessException">Thrown when you're not logged in</exception>
60+
/// <exception cref="HttpRequestException">Thrown when an error happend while the http request</exception>
61+
/// <exception cref="WebUntisException">Thrown when the WebUntis API returned an error</exception>
62+
public async Task<Room[]> GetAllRoomsAsync(string id = "getRooms", CancellationToken ct = default)
63+
{
64+
List<Room> rooms = await MakeRequestAsync<object, List<Room>>(id, "getRooms", new object(), ct);
65+
return rooms.ToArray();
7966
}
8067
}
81-
8268
}

0 commit comments

Comments
 (0)