@@ -36,57 +36,12 @@ public static class SchoolSearch
3636 /// <exception cref="HttpRequestException">Throws when an error happend while request</exception>
3737 public static async Task < School [ ] > SearchAsync ( string name , string id = "searchForSchool" , CancellationToken ct = default )
3838 {
39- StringWriter sw = new StringWriter ( ) ;
40- using ( JsonWriter writer = new JsonTextWriter ( sw ) )
39+ Action < JsonWriter > paramAction = new Action < JsonWriter > ( writer =>
4140 {
42- writer . WriteStartObject ( ) ;
43-
44- writer . WritePropertyName ( "id" ) ;
45- writer . WriteValue ( id ) ;
46-
47- writer . WritePropertyName ( "method" ) ;
48- writer . WriteValue ( "searchSchool" ) ;
49-
50- writer . WritePropertyName ( "params" ) ;
51- writer . WriteStartArray ( ) ;
52- writer . WriteStartObject ( ) ;
5341 writer . WritePropertyName ( "search" ) ;
5442 writer . WriteValue ( name ) ;
55- writer . WriteEndObject ( ) ;
56- writer . WriteEndArray ( ) ;
57-
58- writer . WritePropertyName ( "jsonrpc" ) ;
59- writer . WriteValue ( "2.0" ) ;
60-
61- writer . WriteEndObject ( ) ;
62- }
63-
64- StringContent requestContent = new StringContent ( sw . ToString ( ) , Encoding . UTF8 , "application/json" ) ;
65-
66- // Send request
67- HttpResponseMessage response ;
68- using ( HttpClient client = new HttpClient ( ) )
69- response = await client . PostAsync ( s_API_Url , requestContent , ct ) ;
70-
71- if ( ct . IsCancellationRequested )
72- return Array . Empty < School > ( ) ;
73-
74- // Verify response
75- if ( response . StatusCode != HttpStatusCode . OK )
76- throw new HttpRequestException ( $ "The request had an error (Code: { response . StatusCode } ).") ;
77-
78- JObject responseObject = JObject . Parse ( await response . Content . ReadAsStringAsync ( ) ) ;
79-
80- // Check for WebUntis error
81- if ( responseObject [ "error" ] ? . ToObject < WebUntisException > ( ) is WebUntisException error )
82- {
83- if ( error . Code == ( int ) WebUntisException . Codes . TooManyResults )
84- return null ;
85-
86- throw error ;
87- }
88-
89- return responseObject [ "result" ] [ "schools" ] . ToObject < School [ ] > ( ) ;
43+ } ) ;
44+ return await SearchForSchoolAsync ( paramAction , id , ct ) ;
9045 }
9146
9247 /// <summary>
0 commit comments