33using System ;
44using System . Collections . Generic ;
55using System . IO ;
6+ using System . Linq ;
67using System . Net ;
78using System . Net . Http ;
89using 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