Skip to content

Commit d4964fa

Browse files
committed
Add basic request method for simpler requests, Add check for request id and json rpc version
1 parent 98e7ef1 commit d4964fa

4 files changed

Lines changed: 80 additions & 139 deletions

File tree

WebUntisAPI.Client/Converter/ClassJsonConverter.cs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,6 @@ namespace WebUntisAPI.Client.Converter
1616
/// </summary>
1717
public sealed class ClassJsonConverter : JsonConverter<Class>
1818
{
19-
///// <inheritdoc/>
20-
//public override int[] ReadJson(JsonReader reader, Type objectType, int[] existingValue, bool hasExistingValue, JsonSerializer serializer)
21-
//{
22-
// // Setup
23-
// JObject classObject = JObject.Load(reader);
24-
// List<int> teachers = new List<int>();
25-
26-
// // Read values
27-
// int index = 1;
28-
// while (classObject["teacher" + index++]?.ToObject<int>() is int teacher)
29-
// teachers.Add(teacher);
30-
31-
// return teachers.ToArray();
32-
//}
33-
34-
///// <inheritdoc/>
35-
//public override void WriteJson(JsonWriter writer, int[] value, JsonSerializer serializer)
36-
//{
37-
// JObject teacherObject = new JObject();
38-
39-
// for (int i = 0; i < value.Length; i++)
40-
// teacherObject.Add("teacher" + (i + 1), value[i]);
41-
42-
// teacherObject.WriteTo(writer);
43-
//}
44-
4519
/// <inheritdoc/>
4620
public override Class ReadJson(JsonReader reader, Type objectType, Class existingValue, bool hasExistingValue, JsonSerializer serializer)
4721
{

WebUntisAPI.Client/WebUntisClient.Teaching.cs

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -22,57 +22,32 @@ public partial class WebUntisClient
2222
/// <param name="ct">Cancellation token</param>
2323
/// <returns>All subjects</returns>
2424
/// <exception cref="ObjectDisposedException">Thrown when the instance was disposed</exception>
25-
/// <exception cref="UnauthorizedAccessException">Thrown when you aren't logged in</exception>
25+
/// <exception cref="UnauthorizedAccessException">Thrown when you're logged in</exception>
2626
/// <exception cref="HttpRequestException">Thrown when an error happened while the http request</exception>
2727
/// <exception cref="WebUntisException">Thrown when the WebUntis API returned an error</exception>
2828
public async Task<Subject[]> GetAllSubjectsAsync(string id = "getSubjects", CancellationToken ct = default)
2929
{
30-
// Check for disposing
31-
if (_disposedValue)
32-
throw new ObjectDisposedException(GetType().FullName);
33-
34-
// Check if you logged in
35-
if (!LoggedIn)
36-
throw new UnauthorizedAccessException("You're not logged in");
37-
38-
// Make request
39-
JSONRPCRequestModel<object> requestModel = new JSONRPCRequestModel<object>()
40-
{
41-
Id = id,
42-
Method = "getSubjects",
43-
Params = new object()
44-
};
45-
StringContent requestContent = new StringContent(JsonConvert.SerializeObject(requestModel), Encoding.UTF8, "application/json");
46-
47-
// Send request
48-
HttpResponseMessage response = await _client.PostAsync(ServerUrl + "/WebUntis/jsonrpc.do", requestContent, ct);
49-
50-
// Check cancellation token
51-
if (ct.IsCancellationRequested)
52-
return null;
53-
54-
// Verify response
55-
if (response.StatusCode != HttpStatusCode.OK)
56-
throw new HttpRequestException($"There was an error while the http request (Code: {response.StatusCode}).");
57-
58-
JSONRPCResponeModel<List<Subject>> responseModel = JsonConvert.DeserializeObject<JSONRPCResponeModel<List<Subject>>>(await response.Content.ReadAsStringAsync());
59-
60-
// Check for WebUntis error
61-
if (responseModel.Error != null)
62-
throw responseModel.Error;
63-
64-
return responseModel.Result.ToArray();
30+
List<Subject> subjects = await MakeRequestAsync<object, List<Subject>>(id, "getSubjects", new object(), ct);
31+
return subjects.ToArray();
6532
}
6633

34+
/// <summary>
35+
/// Get all classes on the school
36+
/// </summary>
37+
/// <param name="id">Identifier of the request</param>
38+
/// <param name="ct">Cancellation token</param>
39+
/// <returns>All classes on the school</returns>
40+
/// <exception cref="ObjectDisposedException">Thrown when the instance was disposed</exception>
41+
/// <exception cref="UnauthorizedAccessException">Thrown when you're not logged in</exception>
42+
/// <exception cref="HttpRequestException">Thrown when an error happend while the hppt request</exception>
43+
/// <exception cref="WebUntisException">Thrown when the WebUntis API returned an error</exception>
6744
public async Task<Class[]> GetAllClassesAsync(string id = "getClasses", CancellationToken ct = default)
6845
{
69-
// Check for disposing
70-
if (_disposedValue)
71-
throw new ObjectDisposedException(GetType().FullName);
46+
//TODO: School year overload
7247

73-
// Check if you logged in
74-
if (!LoggedIn)
75-
throw new UnauthorizedAccessException("You're not logged in");
48+
List<Class> classes = await MakeRequestAsync<object, List<Class>>(id, "getKlassen", new object(), ct);
49+
return classes.ToArray();
50+
}
7651

7752
// Make request
7853
JSONRPCRequestModel<object> requestModel = new JSONRPCRequestModel<object>()

WebUntisAPI.Client/WebUntisClient.User.cs

Lines changed: 5 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -44,41 +44,8 @@ public partial class WebUntisClient
4444
/// <exception cref="ObjectDisposedException">Thrown when the object is disposed</exception>
4545
public async Task<Student[]> GetAllStudentsAsync(string id = "getStudents", CancellationToken ct = default)
4646
{
47-
// Check for disposing
48-
if (_disposedValue)
49-
throw new ObjectDisposedException(GetType().FullName);
50-
51-
// Check if you logged in
52-
if (!LoggedIn)
53-
throw new UnauthorizedAccessException("You're not logged in");
54-
55-
// Make request
56-
JSONRPCRequestModel<object> requestModel = new JSONRPCRequestModel<object>()
57-
{
58-
Id = id,
59-
Method = "getStudents",
60-
Params = new object()
61-
};
62-
StringContent requestContent = new StringContent(JsonConvert.SerializeObject(requestModel), Encoding.UTF8, "application/json");
63-
64-
// Send request
65-
HttpResponseMessage response = await _client.PostAsync(ServerUrl + "/WebUntis/jsonrpc.do", requestContent, ct);
66-
67-
// Check cancellation token
68-
if (ct.IsCancellationRequested)
69-
return null;
70-
71-
// Verify response
72-
if (response.StatusCode != HttpStatusCode.OK)
73-
throw new HttpRequestException($"There was an error while the http request (Code: {response.StatusCode}).");
74-
75-
JSONRPCResponeModel<List<Student>> responseModel = JsonConvert.DeserializeObject<JSONRPCResponeModel<List<Student>>>(await response.Content.ReadAsStringAsync());
76-
77-
// Check for WebUntis error
78-
if (responseModel.Error != null)
79-
throw responseModel.Error;
80-
81-
return responseModel.Result.ToArray();
47+
List<Student> students = await MakeRequestAsync<object, List<Student>>(id, "getStudents", new object(), ct);
48+
return students.ToArray();
8249
}
8350

8451
/// <summary>
@@ -87,47 +54,14 @@ public async Task<Student[]> GetAllStudentsAsync(string id = "getStudents", Canc
8754
/// <param name="id">Identifier for the request</param>
8855
/// <param name="ct">Cancellation token</param>
8956
/// <returns>An array of all teachers</returns>
90-
/// <exception cref="UnauthorizedAccessException">Thrown when you don't logged in</exception>
57+
/// <exception cref="UnauthorizedAccessException">Thrown when you're not logged in</exception>
9158
/// <exception cref="HttpRequestException">Thrown when there was an error while the http request</exception>
9259
/// <exception cref="WebUntisException">Thrown when the WebUntis API returned an error</exception>
9360
/// <exception cref="ObjectDisposedException">Thrown when the object is disposed</exception>
9461
public async Task<Teacher[]> GetAllTeachersAsync(string id = "getTeachers", CancellationToken ct = default)
9562
{
96-
// Check for disposing
97-
if (_disposedValue)
98-
throw new ObjectDisposedException(GetType().FullName);
99-
100-
// Check if you logged in
101-
if (!LoggedIn)
102-
throw new UnauthorizedAccessException("You're not logged in");
103-
104-
// Make request
105-
JSONRPCRequestModel<object> requestModel = new JSONRPCRequestModel<object>()
106-
{
107-
Id = id,
108-
Method = "getTeachers",
109-
Params = new object()
110-
};
111-
StringContent requestContent = new StringContent(JsonConvert.SerializeObject(requestModel), Encoding.UTF8, "application/json");
112-
113-
// Send request
114-
HttpResponseMessage response = await _client.PostAsync(ServerUrl + "/WebUntis/jsonrpc.do", requestContent, ct);
115-
116-
// Check cancellation token
117-
if (ct.IsCancellationRequested)
118-
return null;
119-
120-
// Verify response
121-
if (response.StatusCode != HttpStatusCode.OK)
122-
throw new HttpRequestException($"There was an error while the http request (Code: {response.StatusCode}).");
123-
124-
JSONRPCResponeModel<List<Teacher>> responseModel = JsonConvert.DeserializeObject<JSONRPCResponeModel<List<Teacher>>>(await response.Content.ReadAsStringAsync());
125-
126-
// Check for WebUntis error
127-
if (responseModel.Error != null)
128-
throw responseModel.Error;
129-
130-
return responseModel.Result.ToArray();
63+
List<Teacher> teachers = await MakeRequestAsync<object, List<Teacher>>(id, "getTeachers", new object(), ct);
64+
return teachers.ToArray();
13165
}
13266
}
13367
}

WebUntisAPI.Client/WebUntisClient.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,64 @@ public async Task LogoutAsync(string id = "getStudents", CancellationToken ct =
227227
throw new HttpRequestException($"There was an error while the http request (Code: {response.StatusCode}).");
228228
}
229229

230+
/// <summary>
231+
/// Make an internal basic request to the WebUntis server
232+
/// </summary>
233+
/// <typeparam name="TRequest">Type of the request parameter</typeparam>
234+
/// <typeparam name="TResult">Type of the returned result</typeparam>
235+
/// <param name="requestUrl">Path to the API on the server</param>
236+
/// <param name="id">Identifier of the request</param>
237+
/// <param name="methodName">Name of the request method</param>
238+
/// <param name="requestParams">Parameter of the request</param>
239+
/// <param name="ct">Cancellation token</param>
240+
/// <returns>The result</returns>
241+
/// <exception cref="ObjectDisposedException">Thrown when the instance was disposed</exception>
242+
/// <exception cref="UnauthorizedAccessException">Thrown when the client isn't logged in</exception>
243+
/// <exception cref="HttpRequestException">Thrown when an error happend while the http request</exception>
244+
/// <exception cref="WebUntisException">Thrown when the WebUntis API returned an error</exception>
245+
private async Task<TResult> MakeRequestAsync<TRequest, TResult>(string id, string methodName, TRequest requestParams, CancellationToken ct, string requestUrl = "/WebUntis/jsonrpc.do")
246+
{
247+
// Check for disposing
248+
if (_disposedValue)
249+
throw new ObjectDisposedException(GetType().FullName);
250+
251+
// Check if you logged in
252+
if (!LoggedIn)
253+
throw new UnauthorizedAccessException("You're not logged in");
254+
255+
// Make request
256+
JSONRPCRequestModel<TRequest> requestModel = new JSONRPCRequestModel<TRequest>()
257+
{
258+
Id = id,
259+
Method = methodName,
260+
Params = requestParams
261+
};
262+
StringContent requestContent = new StringContent(JsonConvert.SerializeObject(requestModel), Encoding.UTF8, "application/json");
263+
264+
// Send request
265+
HttpResponseMessage response = await _client.PostAsync(ServerUrl + requestUrl, requestContent, ct);
266+
267+
// Check cancellation token
268+
if (ct.IsCancellationRequested)
269+
return default;
270+
271+
// Verify response
272+
if (response.StatusCode != HttpStatusCode.OK)
273+
throw new HttpRequestException($"There was an error while the http request (Code: {response.StatusCode}).");
274+
275+
JSONRPCResponeModel<TResult> responseModel = JsonConvert.DeserializeObject<JSONRPCResponeModel<TResult>>(await response.Content.ReadAsStringAsync());
276+
277+
// Check JSON RPC version and request id
278+
if (requestModel.JSONRPC != responseModel.JSONRPC || requestModel.Id != responseModel.Id)
279+
throw new HttpRequestException("The WebUntis API returned a wrong result");
280+
281+
// Check for WebUntis error
282+
if (responseModel.Error != null)
283+
throw responseModel.Error;
284+
285+
return responseModel.Result;
286+
}
287+
230288
#region IDisposable
231289
private bool _disposedValue;
232290

0 commit comments

Comments
 (0)