@@ -66,18 +66,15 @@ public CouchClient(string connectionString, Action<CouchSettings> couchSettingsF
6666 #region CRUD
6767
6868 /// <summary>
69- /// Returns an instance of the CouchDB database with the given name.
69+ /// Returns an instance of the CouchDB database with the given name.
7070 /// If EnsureDatabaseExists is configured, it creates the database if it doesn't exists.
7171 /// </summary>
7272 /// <typeparam name="TSource">The type of database documents.</typeparam>
7373 /// <param name="database">The database name.</param>
7474 /// <returns>An instance of the CouchDB database with given name.</returns>
7575 public CouchDatabase < TSource > GetDatabase < TSource > ( string database ) where TSource : CouchDocument
7676 {
77- if ( database == null )
78- {
79- throw new ArgumentNullException ( nameof ( database ) ) ;
80- }
77+ database = EscapeDatabaseName ( database ) ;
8178
8279 if ( _settings . CheckDatabaseExists )
8380 {
@@ -102,15 +99,7 @@ public CouchDatabase<TSource> GetDatabase<TSource>(string database) where TSourc
10299 /// <returns>A task that represents the asynchronous operation. The task result contains the newly created CouchDB database.</returns>
103100 public async Task < CouchDatabase < TSource > > CreateDatabaseAsync < TSource > ( string database , int ? shards = null , int ? replicas = null ) where TSource : CouchDocument
104101 {
105- if ( database == null )
106- {
107- throw new ArgumentNullException ( nameof ( database ) ) ;
108- }
109-
110- if ( ! _systemDatabases . Contains ( database ) && ! new Regex ( @"^[a-z][a-z0-9_$()+/-]*$" ) . IsMatch ( database ) )
111- {
112- throw new ArgumentException ( $ "Name { database } contains invalid characters. Please visit: https://docs.couchdb.org/en/stable/api/database/common.html#put--db", nameof ( database ) ) ;
113- }
102+ database = EscapeDatabaseName ( database ) ;
114103
115104 IFlurlRequest request = NewRequest ( )
116105 . AppendPathSegment ( database ) ;
@@ -119,6 +108,7 @@ public async Task<CouchDatabase<TSource>> CreateDatabaseAsync<TSource>(string da
119108 {
120109 request = request . SetQueryParam ( "q" , shards . Value ) ;
121110 }
111+
122112 if ( replicas . HasValue )
123113 {
124114 request = request . SetQueryParam ( "n" , replicas . Value ) ;
@@ -146,10 +136,7 @@ public async Task<CouchDatabase<TSource>> CreateDatabaseAsync<TSource>(string da
146136 /// <returns>A task that represents the asynchronous operation.</returns>
147137 public async Task DeleteDatabaseAsync < TSource > ( string database ) where TSource : CouchDocument
148138 {
149- if ( database == null )
150- {
151- throw new ArgumentNullException ( nameof ( database ) ) ;
152- }
139+ database = EscapeDatabaseName ( database ) ;
153140
154141 OperationResult result = await NewRequest ( )
155142 . AppendPathSegment ( database )
@@ -158,7 +145,8 @@ public async Task DeleteDatabaseAsync<TSource>(string database) where TSource :
158145 . SendRequestAsync ( )
159146 . ConfigureAwait ( false ) ;
160147
161- if ( ! result . Ok ) {
148+ if ( ! result . Ok )
149+ {
162150 throw new CouchException ( "Something went wrong during the delete." , "S" ) ;
163151 }
164152 }
@@ -168,7 +156,7 @@ public async Task DeleteDatabaseAsync<TSource>(string database) where TSource :
168156 #region CRUD reflection
169157
170158 /// <summary>
171- /// Returns an instance of the CouchDB database of the given type.
159+ /// Returns an instance of the CouchDB database of the given type.
172160 /// If EnsureDatabaseExists is configured, it creates the database if it doesn't exists.
173161 /// </summary>
174162 /// <typeparam name="TSource">The type of database documents.</typeparam>
@@ -179,7 +167,7 @@ public CouchDatabase<TSource> GetDatabase<TSource>() where TSource : CouchDocume
179167 }
180168
181169 /// <summary>
182- /// Creates a new database of the given type in the server.
170+ /// Creates a new database of the given type in the server.
183171 /// The name must begin with a lowercase letter and can contains only lowercase characters, digits or _, $, (, ), +, - and /.s
184172 /// </summary>
185173 /// <typeparam name="TSource">The type of database documents.</typeparam>
@@ -235,7 +223,7 @@ public CouchDatabase<TUser> GetUsersDatabase<TUser>() where TUser : CouchUser
235223 #region Utils
236224
237225 /// <summary>
238- /// Determines whether the server is up, running, and ready to respond to requests.
226+ /// Determines whether the server is up, running, and ready to respond to requests.
239227 /// </summary>
240228 /// <returns>true is the server is not in maintenance_mode; otherwise, false.</returns>
241229 public async Task < bool > IsUpAsync ( )
@@ -249,7 +237,7 @@ public async Task<bool> IsUpAsync()
249237 . ConfigureAwait ( false ) ;
250238 return result . Status == "ok" ;
251239 }
252- catch ( CouchNotFoundException )
240+ catch ( CouchNotFoundException )
253241 {
254242 return false ;
255243 }
@@ -292,6 +280,21 @@ private IFlurlRequest NewRequest()
292280 return _flurlClient . Request ( ConnectionString ) ;
293281 }
294282
283+ private string EscapeDatabaseName ( string database )
284+ {
285+ if ( database == null )
286+ {
287+ throw new ArgumentNullException ( nameof ( database ) ) ;
288+ }
289+
290+ if ( ! _systemDatabases . Contains ( database ) && ! new Regex ( @"^[a-z][a-z0-9_$()+/-]*$" ) . IsMatch ( database ) )
291+ {
292+ throw new ArgumentException ( $ "Name { database } contains invalid characters. Please visit: https://docs.couchdb.org/en/stable/api/database/common.html#put--db", nameof ( database ) ) ;
293+ }
294+
295+ return Uri . EscapeDataString ( database ) ;
296+ }
297+
295298 /// <summary>
296299 /// Performs the logout and disposes the HTTP client.
297300 /// </summary>
@@ -305,7 +308,7 @@ protected virtual void Dispose(bool disposing)
305308 {
306309 if ( _settings . AuthenticationType == AuthenticationType . Cookie && _settings . LogOutOnDispose )
307310 {
308- AsyncContext . Run ( ( ) => LogoutAsync ( ) . ConfigureAwait ( false ) ) ;
311+ _ = AsyncContext . Run ( ( ) => LogoutAsync ( ) . ConfigureAwait ( false ) ) ;
309312 }
310313 _flurlClient . Dispose ( ) ;
311314 }
0 commit comments