@@ -79,6 +79,48 @@ public async Task<Student[]> GetAllStudentsAsync(string id = "getStudents", Canc
7979
8080 return responseModel . Result . ToArray ( ) ;
8181 }
82- }
8382
83+ /// <summary>
84+ /// Get all teachers on the school
85+ /// </summary>
86+ /// <param name="id">Identifier for the request</param>
87+ /// <param name="ct">Cancellation token</param>
88+ /// <returns>An array of all teachers</returns>
89+ /// <exception cref="UnauthorizedAccessException">Thrown when you don't logged in</exception>
90+ /// <exception cref="HttpRequestException">Thrown when there was an error while the http request</exception>
91+ /// <exception cref="WebUntisException">Thrown when the WebUntis API returned an error</exception>
92+ public async Task < Teacher [ ] > GetAllTeachersAsync ( string id = "getTeachers" , CancellationToken ct = default )
93+ {
94+ if ( ! LoggedIn )
95+ throw new UnauthorizedAccessException ( "You're not logged in" ) ;
96+
97+ // Make request
98+ JSONRPCRequestModel < object > requestModel = new JSONRPCRequestModel < object > ( )
99+ {
100+ Id = id ,
101+ Method = "getTeachers" ,
102+ Params = new object ( )
103+ } ;
104+ StringContent requestContent = new StringContent ( JsonConvert . SerializeObject ( requestModel ) , Encoding . UTF8 , "application/json" ) ;
105+
106+ // Send request
107+ HttpResponseMessage response = await _client . PostAsync ( ServerUrl + "/WebUntis/jsonrpc.do" , requestContent , ct ) ;
108+
109+ // Check cancellation token
110+ if ( ct . IsCancellationRequested )
111+ return null ;
112+
113+ // Verify response
114+ if ( response . StatusCode != HttpStatusCode . OK )
115+ throw new HttpRequestException ( $ "There was an error while the http request (Code: { response . StatusCode } ).") ;
116+
117+ JSONRPCResponeModel < List < Teacher > > responseModel = JsonConvert . DeserializeObject < JSONRPCResponeModel < List < Teacher > > > ( await response . Content . ReadAsStringAsync ( ) ) ;
118+
119+ // Check for WebUntis error
120+ if ( responseModel . Error != null )
121+ throw responseModel . Error ;
122+
123+ return responseModel . Result . ToArray ( ) ;
124+ }
125+ }
84126}
0 commit comments