1+ using Newtonsoft . Json ;
2+ using System ;
3+ using System . Collections . Generic ;
4+ using System . Linq ;
5+ using System . Net . Http ;
6+ using System . Net ;
7+ using System . Text ;
8+ using System . Threading ;
9+ using System . Threading . Tasks ;
10+ using WebUntisAPI . Client . Models ;
11+ using WebUntisAPI . Client . Converter ;
12+ using WebUntisAPI . Client . Exceptions ;
13+
14+ namespace WebUntisAPI . Client
15+ {
16+ public partial class WebUntisClient
17+ {
18+ /// <summary>
19+ /// Get all subjects on the school
20+ /// </summary>
21+ /// <param name="id">Identifier for the request</param>
22+ /// <param name="ct">Cancellation token</param>
23+ /// <returns>All subjects</returns>
24+ /// <exception cref="ObjectDisposedException">Thrown when the instance was disposed</exception>
25+ /// <exception cref="UnauthorizedAccessException">Thrown when you aren't logged in</exception>
26+ /// <exception cref="HttpRequestException">Thrown when an error happened while the http request</exception>
27+ /// <exception cref="WebUntisException">Thrown when the WebUntis API returned an error</exception>
28+ public async Task < Subject [ ] > GetAllSubjectsAsync ( string id = "getSubjects" , CancellationToken ct = default )
29+ {
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+ JsonSerializerSettings settings = new JsonSerializerSettings ( ) ;
59+ settings . Converters . Add ( new ColorJsonConverter ( ) ) ;
60+ JSONRPCResponeModel < List < Subject > > responseModel = JsonConvert . DeserializeObject < JSONRPCResponeModel < List < Subject > > > ( await response . Content . ReadAsStringAsync ( ) , settings ) ;
61+
62+ // Check for WebUntis error
63+ if ( responseModel . Error != null )
64+ throw responseModel . Error ;
65+
66+ return responseModel . Result . ToArray ( ) ;
67+ }
68+ }
69+
70+ }
0 commit comments