33using System . Net . Http ;
44using System . Net . Http . Headers ;
55using System . Text ;
6+ using System . Threading ;
67using System . Threading . Tasks ;
78using Newtonsoft . Json ;
9+ using Notion . Client . Extensions ;
810using Notion . Client . http ;
911
1012namespace Notion . Client
1113{
1214 public interface IRestClient
1315 {
14- Task < T > GetAsync < T > ( string uri , Dictionary < string , string > queryParams = null ) ;
15- Task < T > PostAsync < T > ( string uri , object body ) ;
16+ Task < T > GetAsync < T > (
17+ string uri ,
18+ IDictionary < string , string > queryParams = null ,
19+ IDictionary < string , string > headers = null ,
20+ JsonSerializerSettings serializerSettings = null ,
21+ CancellationToken cancellationToken = default ) ;
22+
23+ Task < T > PostAsync < T > (
24+ string uri ,
25+ object body ,
26+ IDictionary < string , string > queryParams = null ,
27+ IDictionary < string , string > headers = null ,
28+ JsonSerializerSettings serializerSettings = null ,
29+ CancellationToken cancellationToken = default ) ;
1630 }
1731
1832 public class RestClient : IRestClient
1933 {
2034 private HttpClient _httpClient ;
2135 private readonly ClientOptions _options ;
22- private readonly List < JsonConverter > jsonConverters = new List < JsonConverter > ( ) ;
2336
24- private readonly JsonSerializerSettings serializerSettings = new JsonSerializerSettings
37+ private readonly JsonSerializerSettings defaultSerializerSettings = new JsonSerializerSettings
2538 {
2639 NullValueHandling = NullValueHandling . Ignore
2740 } ;
@@ -41,32 +54,89 @@ private static ClientOptions MergeOptions(ClientOptions options)
4154 } ;
4255 }
4356
44- public async Task < T > GetAsync < T > ( string uri , Dictionary < string , string > queryParams = null )
57+ public async Task < T > GetAsync < T > (
58+ string uri ,
59+ IDictionary < string , string > queryParams = null ,
60+ IDictionary < string , string > headers = null ,
61+ JsonSerializerSettings serializerSettings = null ,
62+ CancellationToken cancellationToken = default )
4563 {
4664 EnsureHttpClient ( ) ;
4765
48- uri = queryParams == null ? uri : QueryHelpers . AddQueryString ( uri , queryParams ) ;
66+ string requestUri = queryParams == null ? uri : QueryHelpers . AddQueryString ( uri , queryParams ) ;
4967
50- using ( var stream = await _httpClient . GetStreamAsync ( uri ) )
68+ var response = await SendAsync ( requestUri , HttpMethod . Get , headers , cancellationToken : cancellationToken ) ;
69+
70+ if ( response . IsSuccessStatusCode )
5171 {
52- return SerializerHelper . Deserialize < T > ( stream , jsonConverters ) ;
72+ return await response . ParseStreamAsync < T > ( serializerSettings ) ;
5373 }
74+
75+ var message = ! string . IsNullOrWhiteSpace ( response . ReasonPhrase )
76+ ? response . ReasonPhrase
77+ : await response . Content . ReadAsStringAsync ( ) ;
78+
79+ throw new NotionApiException ( response . StatusCode , message ) ;
5480 }
5581
56- public async Task < T > PostAsync < T > ( string uri , object body )
82+ private async Task < HttpResponseMessage > SendAsync (
83+ string requestUri ,
84+ HttpMethod httpMethod ,
85+ IDictionary < string , string > headers = null ,
86+ Action < HttpRequestMessage > attachContent = null ,
87+ CancellationToken cancellationToken = default )
5788 {
58- EnsureHttpClient ( ) ;
89+ HttpRequestMessage httpRequest = new HttpRequestMessage ( httpMethod , requestUri ) ;
90+ httpRequest . Headers . Authorization = new AuthenticationHeaderValue ( "Bearer" , _options . AuthToken ) ;
91+ httpRequest . Headers . Add ( "Notion-Version" , _options . NotionVersion ) ;
92+
93+ if ( headers != null )
94+ {
95+ AddHeaders ( httpRequest , headers ) ;
96+ }
97+
98+ attachContent ? . Invoke ( httpRequest ) ;
99+
100+ return await _httpClient . SendAsync ( httpRequest , cancellationToken ) ;
101+ }
102+
103+ private static void AddHeaders ( HttpRequestMessage request , IDictionary < string , string > headers )
104+ {
105+ foreach ( var header in headers )
106+ {
107+ request . Headers . Add ( header . Key , header . Value ) ;
108+ }
109+ }
59110
60- var content = new StringContent ( JsonConvert . SerializeObject ( body , serializerSettings ) , Encoding . UTF8 , "application/json" ) ;
111+ public async Task < T > PostAsync < T > (
112+ string uri ,
113+ object body ,
114+ IDictionary < string , string > queryParams = null ,
115+ IDictionary < string , string > headers = null ,
116+ JsonSerializerSettings serializerSettings = null ,
117+ CancellationToken cancellationToken = default )
118+ {
119+ EnsureHttpClient ( ) ;
61120
62- using ( var response = await _httpClient . PostAsync ( uri , content ) )
121+ void AttachContent ( HttpRequestMessage httpRequest )
63122 {
64- response . EnsureSuccessStatusCode ( ) ;
123+ httpRequest . Content = new StringContent ( JsonConvert . SerializeObject ( body , defaultSerializerSettings ) , Encoding . UTF8 , "application/json" ) ;
124+ }
125+
126+ string requestUri = queryParams == null ? uri : QueryHelpers . AddQueryString ( uri , queryParams ) ;
65127
66- var stream = await response . Content . ReadAsStreamAsync ( ) ;
128+ var response = await SendAsync ( requestUri , HttpMethod . Post , headers , AttachContent , cancellationToken : cancellationToken ) ;
67129
68- return SerializerHelper . Deserialize < T > ( stream , jsonConverters ) ;
130+ if ( response . IsSuccessStatusCode )
131+ {
132+ return await response . ParseStreamAsync < T > ( serializerSettings ) ;
69133 }
134+
135+ var message = ! string . IsNullOrWhiteSpace ( response . ReasonPhrase )
136+ ? response . ReasonPhrase
137+ : await response . Content . ReadAsStringAsync ( ) ;
138+
139+ throw new NotionApiException ( response . StatusCode , message ) ;
70140 }
71141
72142 private HttpClient EnsureHttpClient ( )
@@ -75,8 +145,6 @@ private HttpClient EnsureHttpClient()
75145 {
76146 _httpClient = new HttpClient ( ) ;
77147 _httpClient . BaseAddress = new Uri ( _options . BaseUrl ) ;
78- _httpClient . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , _options . AuthToken ) ;
79- _httpClient . DefaultRequestHeaders . Add ( "Notion-Version" , _options . NotionVersion ) ;
80148 }
81149
82150 return _httpClient ;
0 commit comments