1010using System . Threading . Tasks ;
1111using System . Linq ;
1212using CouchDB . Driver . Settings ;
13+ using CouchDB . Driver . DTOs ;
14+ using CouchDB . Driver . Exceptions ;
1315
1416namespace CouchDB . Driver
1517{
@@ -34,7 +36,9 @@ public partial class CouchClient : IDisposable
3436 public CouchClient ( string connectionString , Action < CouchSettings > couchSettingsFunc = null , Action < ClientFlurlHttpSettings > flurlSettingsFunc = null )
3537 {
3638 if ( string . IsNullOrEmpty ( connectionString ) )
39+ {
3740 throw new ArgumentNullException ( nameof ( connectionString ) ) ;
41+ }
3842
3943 _settings = new CouchSettings ( ) ;
4044 couchSettingsFunc ? . Invoke ( _settings ) ;
@@ -68,11 +72,13 @@ public CouchClient(string connectionString, Action<CouchSettings> couchSettingsF
6872 public CouchDatabase < TSource > GetDatabase < TSource > ( string database ) where TSource : CouchDocument
6973 {
7074 if ( database == null )
75+ {
7176 throw new ArgumentNullException ( nameof ( database ) ) ;
77+ }
7278
7379 if ( _settings . CheckDatabaseExists )
7480 {
75- var dbs = AsyncContext . Run ( ( ) => GetDatabasesNamesAsync ( ) ) ;
81+ IEnumerable < string > dbs = AsyncContext . Run ( ( ) => GetDatabasesNamesAsync ( ) ) ;
7682 if ( ! dbs . Contains ( database ) )
7783 {
7884 return AsyncContext . Run ( ( ) => CreateDatabaseAsync < TSource > ( database ) ) ;
@@ -94,28 +100,37 @@ public CouchDatabase<TSource> GetDatabase<TSource>(string database) where TSourc
94100 public async Task < CouchDatabase < TSource > > CreateDatabaseAsync < TSource > ( string database , int ? shards = null , int ? replicas = null ) where TSource : CouchDocument
95101 {
96102 if ( database == null )
103+ {
97104 throw new ArgumentNullException ( nameof ( database ) ) ;
105+ }
98106
99107 if ( ! _systemDatabases . Contains ( database ) && ! new Regex ( @"^[a-z][a-z0-9_$()+/-]*$" ) . IsMatch ( database ) )
100108 {
101- throw new ArgumentException ( nameof ( database ) , $ "Name { database } contains invalid characters. Please visit: https://docs.couchdb.org/en/stable/api/database/common.html#put--db") ;
109+ throw new ArgumentException ( $ "Name { database } contains invalid characters. Please visit: https://docs.couchdb.org/en/stable/api/database/common.html#put--db", nameof ( database ) ) ;
102110 }
103111
104- var request = NewRequest ( )
112+ IFlurlRequest request = NewRequest ( )
105113 . AppendPathSegment ( database ) ;
106114
107115 if ( shards . HasValue )
108116 {
109- request . SetQueryParam ( "q" , shards . Value ) ;
117+ request = request . SetQueryParam ( "q" , shards . Value ) ;
110118 }
111119 if ( replicas . HasValue )
112120 {
113- request . SetQueryParam ( "n" , replicas . Value ) ;
121+ request = request . SetQueryParam ( "n" , replicas . Value ) ;
114122 }
115123
116- await request
124+ OperationResult result = await request
117125 . PutAsync ( null )
118- . SendRequestAsync ( ) ;
126+ . ReceiveJson < OperationResult > ( )
127+ . SendRequestAsync ( )
128+ . ConfigureAwait ( false ) ;
129+
130+ if ( ! result . Ok )
131+ {
132+ throw new CouchException ( "Something went wrong during the creation" ) ;
133+ }
119134
120135 return new CouchDatabase < TSource > ( _flurlClient , _settings , ConnectionString , database ) ;
121136 }
@@ -129,12 +144,20 @@ await request
129144 public async Task DeleteDatabaseAsync < TSource > ( string database ) where TSource : CouchDocument
130145 {
131146 if ( database == null )
147+ {
132148 throw new ArgumentNullException ( nameof ( database ) ) ;
149+ }
133150
134- await NewRequest ( )
151+ OperationResult result = await NewRequest ( )
135152 . AppendPathSegment ( database )
136153 . DeleteAsync ( )
137- . SendRequestAsync ( ) ;
154+ . ReceiveJson < OperationResult > ( )
155+ . SendRequestAsync ( )
156+ . ConfigureAwait ( false ) ;
157+
158+ if ( ! result . Ok ) {
159+ throw new CouchException ( "Something went wrong during the delete." , "S" ) ;
160+ }
138161 }
139162
140163 #endregion
@@ -175,7 +198,7 @@ public Task DeleteDatabaseAsync<TSource>() where TSource : CouchDocument
175198 }
176199 private string GetClassName < TSource > ( )
177200 {
178- var type = typeof ( TSource ) ;
201+ Type type = typeof ( TSource ) ;
179202 return type . GetName ( _settings ) ;
180203 }
181204
@@ -216,13 +239,14 @@ public async Task<bool> IsUpAsync()
216239 {
217240 try
218241 {
219- await NewRequest ( )
242+ StatusResult result = await NewRequest ( )
220243 . AppendPathSegment ( "/_up" )
221- . GetAsync ( )
222- . SendRequestAsync ( ) ;
223- return true ;
244+ . GetJsonAsync < StatusResult > ( )
245+ . SendRequestAsync ( )
246+ . ConfigureAwait ( false ) ;
247+ return result . Status == "ok" ;
224248 }
225- catch
249+ catch ( CouchNotFoundException )
226250 {
227251 return false ;
228252 }
@@ -237,7 +261,8 @@ public async Task<IEnumerable<string>> GetDatabasesNamesAsync()
237261 return await NewRequest ( )
238262 . AppendPathSegment ( "_all_dbs" )
239263 . GetJsonAsync < IEnumerable < string > > ( )
240- . SendRequestAsync ( ) ;
264+ . SendRequestAsync ( )
265+ . ConfigureAwait ( false ) ;
241266 }
242267
243268 /// <summary>
@@ -249,16 +274,17 @@ public async Task<IEnumerable<CouchActiveTask>> GetActiveTasksAsync()
249274 return await NewRequest ( )
250275 . AppendPathSegment ( "_active_tasks" )
251276 . GetJsonAsync < IEnumerable < CouchActiveTask > > ( )
252- . SendRequestAsync ( ) ;
277+ . SendRequestAsync ( )
278+ . ConfigureAwait ( false ) ;
253279 }
254280
255281 #endregion
256-
282+
257283 #endregion
258-
284+
259285 #region Implementations
260286
261- private IFlurlRequest NewRequest ( )
287+ private IFlurlRequest NewRequest ( )
262288 {
263289 return _flurlClient . Request ( ConnectionString ) ;
264290 }
@@ -267,11 +293,22 @@ private IFlurlRequest NewRequest()
267293 /// Performs the logout and disposes the HTTP client.
268294 /// </summary>
269295 public void Dispose ( )
296+ {
297+ Dispose ( true ) ;
298+ GC . SuppressFinalize ( this ) ;
299+ }
300+
301+ protected virtual void Dispose ( bool disposing )
270302 {
271303 AsyncContext . Run ( ( ) => LogoutAsync ( ) ) ;
272304 _flurlClient . Dispose ( ) ;
273305 }
274306
307+ ~ CouchClient ( )
308+ {
309+ Dispose ( false ) ;
310+ }
311+
275312 #endregion
276313 }
277314}
0 commit comments