@@ -48,31 +48,42 @@ public async Task<T> GetAsync<T>(
48
48
JsonSerializerSettings serializerSettings = null ,
49
49
CancellationToken cancellationToken = default )
50
50
{
51
- EnsureHttpClient ( ) ;
51
+ var response = await SendAsync ( uri , HttpMethod . Get , queryParams , headers , cancellationToken : cancellationToken ) ;
52
52
53
- string requestUri = queryParams == null ? uri : QueryHelpers . AddQueryString ( uri , queryParams ) ;
53
+ return await response . ParseStreamAsync < T > ( serializerSettings ) ;
54
+ }
54
55
55
- var response = await SendAsync ( requestUri , HttpMethod . Get , headers , cancellationToken : cancellationToken ) ;
56
+ private static async Task < Exception > BuildException ( HttpResponseMessage response )
57
+ {
58
+ var errorBody = await response . Content . ReadAsStringAsync ( ) ;
56
59
57
- if ( response . IsSuccessStatusCode )
60
+ NotionApiErrorResponse errorResponse = null ;
61
+ if ( ! string . IsNullOrWhiteSpace ( errorBody ) )
58
62
{
59
- return await response . ParseStreamAsync < T > ( serializerSettings ) ;
63
+ try
64
+ {
65
+ errorResponse = JsonConvert . DeserializeObject < NotionApiErrorResponse > ( errorBody ) ;
66
+ }
67
+ catch
68
+ {
69
+ }
60
70
}
61
71
62
- var message = ! string . IsNullOrWhiteSpace ( response . ReasonPhrase )
63
- ? response . ReasonPhrase
64
- : await response . Content . ReadAsStringAsync ( ) ;
65
-
66
- throw new NotionApiException ( response . StatusCode , message ) ;
72
+ return new NotionApiException ( response . StatusCode , errorResponse ? . ErrorCode , errorResponse . Message ) ;
67
73
}
68
74
69
75
private async Task < HttpResponseMessage > SendAsync (
70
76
string requestUri ,
71
77
HttpMethod httpMethod ,
78
+ IDictionary < string , string > queryParams = null ,
72
79
IDictionary < string , string > headers = null ,
73
80
Action < HttpRequestMessage > attachContent = null ,
74
81
CancellationToken cancellationToken = default )
75
82
{
83
+ EnsureHttpClient ( ) ;
84
+
85
+ requestUri = AddQueryString ( requestUri , queryParams ) ;
86
+
76
87
HttpRequestMessage httpRequest = new HttpRequestMessage ( httpMethod , requestUri ) ;
77
88
httpRequest . Headers . Authorization = new AuthenticationHeaderValue ( "Bearer" , _options . AuthToken ) ;
78
89
httpRequest . Headers . Add ( "Notion-Version" , _options . NotionVersion ) ;
@@ -84,7 +95,14 @@ private async Task<HttpResponseMessage> SendAsync(
84
95
85
96
attachContent ? . Invoke ( httpRequest ) ;
86
97
87
- return await _httpClient . SendAsync ( httpRequest , cancellationToken ) ;
98
+ var response = await _httpClient . SendAsync ( httpRequest , cancellationToken ) ;
99
+
100
+ if ( ! response . IsSuccessStatusCode )
101
+ {
102
+ throw await BuildException ( response ) ;
103
+ }
104
+
105
+ return response ;
88
106
}
89
107
90
108
private static void AddHeaders ( HttpRequestMessage request , IDictionary < string , string > headers )
@@ -103,55 +121,27 @@ public async Task<T> PostAsync<T>(
103
121
JsonSerializerSettings serializerSettings = null ,
104
122
CancellationToken cancellationToken = default )
105
123
{
106
- EnsureHttpClient ( ) ;
107
-
108
124
void AttachContent ( HttpRequestMessage httpRequest )
109
125
{
110
126
httpRequest . Content = new StringContent ( JsonConvert . SerializeObject ( body , defaultSerializerSettings ) , Encoding . UTF8 , "application/json" ) ;
111
127
}
112
128
113
- string requestUri = queryParams == null ? uri : QueryHelpers . AddQueryString ( uri , queryParams ) ;
114
-
115
- var response = await SendAsync ( requestUri , HttpMethod . Post , headers , AttachContent , cancellationToken : cancellationToken ) ;
116
-
117
- if ( response . IsSuccessStatusCode )
118
- {
119
- return await response . ParseStreamAsync < T > ( serializerSettings ) ;
120
- }
121
-
122
- var message = ! string . IsNullOrWhiteSpace ( response . ReasonPhrase )
123
- ? response . ReasonPhrase
124
- : await response . Content . ReadAsStringAsync ( ) ;
129
+ var response = await SendAsync ( uri , HttpMethod . Post , queryParams , headers , AttachContent , cancellationToken : cancellationToken ) ;
125
130
126
- throw new NotionApiException ( response . StatusCode , message ) ;
131
+ return await response . ParseStreamAsync < T > ( serializerSettings ) ;
127
132
}
128
133
129
134
public async Task < T > PatchAsync < T > ( string uri , object body , IDictionary < string , string > queryParams = null , IDictionary < string , string > headers = null , JsonSerializerSettings serializerSettings = null , CancellationToken cancellationToken = default )
130
135
{
131
- EnsureHttpClient ( ) ;
132
-
133
136
void AttachContent ( HttpRequestMessage httpRequest )
134
137
{
135
138
var serializedBody = JsonConvert . SerializeObject ( body , defaultSerializerSettings ) ;
136
139
httpRequest . Content = new StringContent ( serializedBody , Encoding . UTF8 , "application/json" ) ;
137
140
}
138
141
139
- string requestUri = queryParams == null ? uri : QueryHelpers . AddQueryString ( uri , queryParams ) ;
142
+ var response = await SendAsync ( uri , new HttpMethod ( "PATCH" ) , queryParams , headers , AttachContent , cancellationToken : cancellationToken ) ;
140
143
141
- var response = await SendAsync ( requestUri , new HttpMethod ( "PATCH" ) , headers , AttachContent , cancellationToken : cancellationToken ) ;
142
-
143
- if ( response . IsSuccessStatusCode )
144
- {
145
- return await response . ParseStreamAsync < T > ( serializerSettings ) ;
146
- }
147
-
148
- var message = ! string . IsNullOrWhiteSpace ( response . ReasonPhrase )
149
- ? response . ReasonPhrase
150
- : await response . Content . ReadAsStringAsync ( ) ;
151
-
152
- var errorMessage = await response . Content . ReadAsStringAsync ( ) ;
153
-
154
- throw new NotionApiException ( response . StatusCode , message ) ;
144
+ return await response . ParseStreamAsync < T > ( serializerSettings ) ;
155
145
}
156
146
157
147
private HttpClient EnsureHttpClient ( )
@@ -164,5 +154,10 @@ private HttpClient EnsureHttpClient()
164
154
165
155
return _httpClient ;
166
156
}
157
+
158
+ private static string AddQueryString ( string uri , IDictionary < string , string > queryParams )
159
+ {
160
+ return queryParams == null ? uri : QueryHelpers . AddQueryString ( uri , queryParams ) ;
161
+ }
167
162
}
168
163
}
0 commit comments