@@ -48,31 +48,42 @@ public async Task<T> GetAsync<T>(
4848 JsonSerializerSettings  serializerSettings  =  null , 
4949 CancellationToken  cancellationToken  =  default ) 
5050 { 
51-  EnsureHttpClient ( ) ; 
51+  var response = await SendAsync ( uri , HttpMethod . Get , queryParams , headers , cancellationToken : cancellationToken ) ; 
5252
53-  string  requestUri  =  queryParams  ==  null  ?  uri  :  QueryHelpers . AddQueryString ( uri ,  queryParams ) ; 
53+  return  await  response . ParseStreamAsync < T > ( serializerSettings ) ; 
54+  } 
5455
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 ( ) ; 
5659
57-  if  ( response . IsSuccessStatusCode ) 
60+  NotionApiErrorResponse  errorResponse  =  null ; 
61+  if  ( ! string . IsNullOrWhiteSpace ( errorBody ) ) 
5862 { 
59-  return  await  response . ParseStreamAsync < T > ( serializerSettings ) ; 
63+  try 
64+  { 
65+  errorResponse  =  JsonConvert . DeserializeObject < NotionApiErrorResponse > ( errorBody ) ; 
66+  } 
67+  catch 
68+  { 
69+  } 
6070 } 
6171
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 ) ; 
6773 } 
6874
6975 private  async  Task < HttpResponseMessage >  SendAsync ( 
7076 string  requestUri , 
7177 HttpMethod  httpMethod , 
78+  IDictionary < string ,  string >  queryParams  =  null , 
7279 IDictionary < string ,  string >  headers  =  null , 
7380 Action < HttpRequestMessage >  attachContent  =  null , 
7481 CancellationToken  cancellationToken  =  default ) 
7582 { 
83+  EnsureHttpClient ( ) ; 
84+ 85+  requestUri  =  AddQueryString ( requestUri ,  queryParams ) ; 
86+ 7687 HttpRequestMessage  httpRequest  =  new  HttpRequestMessage ( httpMethod ,  requestUri ) ; 
7788 httpRequest . Headers . Authorization  =  new  AuthenticationHeaderValue ( "Bearer" ,  _options . AuthToken ) ; 
7889 httpRequest . Headers . Add ( "Notion-Version" ,  _options . NotionVersion ) ; 
@@ -84,7 +95,14 @@ private async Task<HttpResponseMessage> SendAsync(
8495
8596 attachContent ? . Invoke ( httpRequest ) ; 
8697
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 ; 
88106 } 
89107
90108 private  static void  AddHeaders ( HttpRequestMessage  request ,  IDictionary < string ,  string >  headers ) 
@@ -103,55 +121,27 @@ public async Task<T> PostAsync<T>(
103121 JsonSerializerSettings  serializerSettings  =  null , 
104122 CancellationToken  cancellationToken  =  default ) 
105123 { 
106-  EnsureHttpClient ( ) ; 
107- 108124 void  AttachContent ( HttpRequestMessage  httpRequest ) 
109125 { 
110126 httpRequest . Content  =  new  StringContent ( JsonConvert . SerializeObject ( body ,  defaultSerializerSettings ) ,  Encoding . UTF8 ,  "application/json" ) ; 
111127 } 
112128
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 ) ; 
125130
126-  throw new NotionApiException ( response . StatusCode , message ) ; 
131+  return await response . ParseStreamAsync < T > ( serializerSettings ) ; 
127132 } 
128133
129134 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 ) 
130135 { 
131-  EnsureHttpClient ( ) ; 
132- 133136 void  AttachContent ( HttpRequestMessage  httpRequest ) 
134137 { 
135138 var  serializedBody  =  JsonConvert . SerializeObject ( body ,  defaultSerializerSettings ) ; 
136139 httpRequest . Content  =  new  StringContent ( serializedBody ,  Encoding . UTF8 ,  "application/json" ) ; 
137140 } 
138141
139-  string requestUri  =  queryParams == null ? uri : QueryHelpers . AddQueryString ( uri ,  queryParams ) ; 
142+  var response  =  await SendAsync ( uri , new HttpMethod ( "PATCH" ) ,  queryParams , headers , AttachContent , cancellationToken : cancellationToken ) ; 
140143
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 ) ; 
155145 } 
156146
157147 private  HttpClient  EnsureHttpClient ( ) 
@@ -164,5 +154,10 @@ private HttpClient EnsureHttpClient()
164154
165155 return  _httpClient ; 
166156 } 
157+ 158+  private  static string  AddQueryString ( string  uri ,  IDictionary < string ,  string >  queryParams ) 
159+  { 
160+  return  queryParams  ==  null  ?  uri  :  QueryHelpers . AddQueryString ( uri ,  queryParams ) ; 
161+  } 
167162 } 
168163} 
0 commit comments