Skip to content

Commit 9eef067

Browse files
committed
Add school search methods for login name and id
1 parent 1b64f4f commit 9eef067

2 files changed

Lines changed: 98 additions & 3 deletions

File tree

WebUntisAPI.Client/Models/School.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class School
1919
/// Id of the school
2020
/// </summary>
2121
[JsonProperty("schoolId")]
22-
public long SchoolId { get; set; }
22+
public int SchoolId { get; set; }
2323

2424
/// <summary>
2525
/// Login name of the school

WebUntisAPI.Client/SchoolSearch.cs

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Collections.Generic;
55
using System.IO;
6+
using System.Linq;
67
using System.Net;
78
using System.Net.Http;
89
using System.Runtime.CompilerServices;
@@ -28,12 +29,12 @@ public static class SchoolSearch
2829
/// Search for schools by the given name
2930
/// </summary>
3031
/// <param name="name">Name to search</param>
31-
/// <param name="ct">Token to cancel the search request</param>
32+
/// <param name="ct">Cancellation token</param>
3233
/// <param name="id">Identifier for the request</param>
3334
/// <returns>All schools found, an empty array when no school found or <see langword="null"/> when too many schools found</returns>
3435
/// <exception cref="WebUntisException">Throws when the WebUntis API returned an error</exception>
3536
/// <exception cref="HttpRequestException">Throws when an error happend while request</exception>
36-
public static async Task<School[]> SearchAsync(string name, string id = "getStudents", CancellationToken ct = default)
37+
public static async Task<School[]> SearchAsync(string name, string id = "searchForSchool", CancellationToken ct = default)
3738
{
3839
StringWriter sw = new StringWriter();
3940
using (JsonWriter writer = new JsonTextWriter(sw))
@@ -87,5 +88,99 @@ public static async Task<School[]> SearchAsync(string name, string id = "getStud
8788

8889
return responseObject["result"]["schools"].ToObject<School[]>();
8990
}
91+
92+
/// <summary>
93+
/// Get a school by the <see cref="School.LoginName"/>
94+
/// </summary>
95+
/// <param name="schoolName">The <see cref="School.LoginName"/> (also lower-upper case is important)</param>
96+
/// <param name="id">Identifier for the request</param>
97+
/// <param name="ct">Cancellation token</param>
98+
/// <returns>The school that was found (<see langword="null"/> when no school was found)</returns>
99+
/// <exception cref="WebUntisException">Throws when the WebUntis API returned an error</exception>
100+
/// <exception cref="HttpRequestException">Throws when an error happend while request</exception>
101+
public static async Task<School> GetSchoolByNameAsync(string schoolName, string id = "getSchoolByName", CancellationToken ct = default)
102+
{
103+
Action<JsonWriter> paramAction = new Action<JsonWriter>(writer =>
104+
{
105+
writer.WritePropertyName("schoolname");
106+
writer.WriteValue(schoolName);
107+
});
108+
return (await SearchForSchoolAsync(paramAction, id, ct)).FirstOrDefault();
109+
}
110+
111+
/// <summary>
112+
/// Get a school by the <see cref="School.SchoolId"/>
113+
/// </summary>
114+
/// <param name="schoolId">The <see cref="School.SchoolId"/></param>
115+
/// <param name="id">Identifier for the request</param>
116+
/// <param name="ct">Cancellation token</param>
117+
/// <returns>The school that was found (<see langword="null"/> when no school was found)</returns>
118+
/// <exception cref="WebUntisException">Throws when the WebUntis API returned an error</exception>
119+
/// <exception cref="HttpRequestException">Throws when an error happend while request</exception>
120+
public static async Task<School> GetSchoolByIdAsync(int schoolId, string id = "getSchoolById", CancellationToken ct = default)
121+
{
122+
Action<JsonWriter> paramAction = new Action<JsonWriter>(writer =>
123+
{
124+
writer.WritePropertyName("schoolid");
125+
writer.WriteValue(schoolId);
126+
});
127+
return (await SearchForSchoolAsync(paramAction, id, ct)).FirstOrDefault();
128+
}
129+
130+
private static async Task<School[]> SearchForSchoolAsync(Action<JsonWriter> paramWriter, string id, CancellationToken ct)
131+
{
132+
StringWriter sw = new StringWriter();
133+
using (JsonWriter writer = new JsonTextWriter(sw))
134+
{
135+
writer.WriteStartObject();
136+
137+
writer.WritePropertyName("id");
138+
writer.WriteValue(id);
139+
140+
writer.WritePropertyName("method");
141+
writer.WriteValue("searchSchool");
142+
143+
writer.WritePropertyName("params");
144+
writer.WriteStartArray();
145+
writer.WriteStartObject();
146+
147+
paramWriter(writer);
148+
149+
writer.WriteEndObject();
150+
writer.WriteEndArray();
151+
152+
writer.WritePropertyName("jsonrpc");
153+
writer.WriteValue("2.0");
154+
155+
writer.WriteEndObject();
156+
}
157+
158+
StringContent requestContent = new StringContent(sw.ToString(), Encoding.UTF8, "application/json");
159+
160+
// Send request
161+
HttpResponseMessage response;
162+
using (HttpClient client = new HttpClient())
163+
response = await client.PostAsync(s_API_Url, requestContent, ct);
164+
165+
if (ct.IsCancellationRequested)
166+
return Array.Empty<School>();
167+
168+
// Verify response
169+
if (response.StatusCode != HttpStatusCode.OK)
170+
throw new HttpRequestException($"The request had an error (Code: {response.StatusCode}).");
171+
172+
JObject responseObject = JObject.Parse(await response.Content.ReadAsStringAsync());
173+
174+
// Check for WebUntis error
175+
if (responseObject["error"]?.ToObject<WebUntisException>() is WebUntisException error)
176+
{
177+
if (error.Code == (int)WebUntisException.Codes.TooManyResults)
178+
return null;
179+
180+
throw error;
181+
}
182+
183+
return responseObject["result"]["schools"].ToObject<School[]>();
184+
}
90185
}
91186
}

0 commit comments

Comments
 (0)