Skip to content

Commit ec908ce

Browse files
Merge pull request #30 from matteobortolazzo/code-analyzer
Adds code analyzers.
2 parents d88b588 + 00cb876 commit ec908ce

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+662
-168
lines changed

CHANGELOG.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
# 1.0.1-beta.1 (2019-04-03)
1+
# 1.0.1-beta.3 (2019-04-03)
2+
3+
## Breaking Changes
4+
* **_find:** from r.prop.FieldExists() to r.FieldExists("someprop").
5+
6+
## Features
7+
* **CouchClient:** implements protected virtual void Dispose(bool disposing).
8+
9+
## Improvements
10+
* **Global:** FxCop analizers add to the project.
11+
12+
# 1.0.1-beta.2 (2019-04-03)
213

314
## Bug Fixes
415
* **_find:** Guid support, and all other constants ([#PR26](https://github.com/matteobortolazzo/couchdb-net/pull/26))

LATEST_CHANGE.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
1-
## Bug Fixes
2-
* **_find:** Guid support, and all other constants ([#PR26](https://github.com/matteobortolazzo/couchdb-net/pull/26))
1+
## Breaking Changes
2+
* **_find:** from r.prop.FieldExists() to r.FieldExists("someprop").
3+
4+
## Features
5+
* **CouchClient:** implements protected virtual void Dispose(bool disposing).
6+
7+
## Improvements
8+
* **Global:** FxCop analizers add to the project.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ If the Where method is not called in the expression, it will at an empty selecto
151151
| $ne | != |
152152
| $gte | >= |
153153
| $gt | > |
154-
| $exists | o.FieldExists() |
154+
| $exists | o.FieldExists(s) |
155155
| $type | o.IsCouchType(...) |
156156
| $in | o.In(list) |
157157
| $nin | !o.In(list) |

src/CouchDB.Driver/CouchClient.cs

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
using System.Threading.Tasks;
1111
using System.Linq;
1212
using CouchDB.Driver.Settings;
13+
using CouchDB.Driver.DTOs;
14+
using CouchDB.Driver.Exceptions;
1315

1416
namespace 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
}
Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1-
using CouchDB.Driver.Settings;
1+
using CouchDB.Driver.DTOs;
2+
using CouchDB.Driver.Exceptions;
3+
using CouchDB.Driver.Settings;
24
using Flurl.Http;
35
using Nito.AsyncEx;
46
using System;
57
using System.Linq;
8+
using System.Net.Http;
69
using System.Text.RegularExpressions;
710
using System.Threading.Tasks;
811

912
namespace CouchDB.Driver
1013
{
1114
public partial class CouchClient
1215
{
13-
protected virtual void OnBeforeCall(HttpCall call)
16+
protected virtual void OnBeforeCall(HttpCall httpCall)
1417
{
1518
// If session requests no authorization needed
16-
if (call.Request.RequestUri.ToString().Contains("_session"))
19+
if (httpCall.Request.RequestUri.ToString().Contains("_session"))
1720
{
1821
return;
1922
}
@@ -22,7 +25,7 @@ protected virtual void OnBeforeCall(HttpCall call)
2225
case AuthenticationType.None:
2326
break;
2427
case AuthenticationType.Basic:
25-
call.FlurlRequest.WithBasicAuth(_settings.Username, _settings.Password);
28+
httpCall.FlurlRequest.WithBasicAuth(_settings.Username, _settings.Password);
2629
break;
2730
case AuthenticationType.Cookie:
2831
var isTokenExpired =
@@ -32,7 +35,7 @@ protected virtual void OnBeforeCall(HttpCall call)
3235
{
3336
AsyncContext.Run(() => LoginAsync());
3437
}
35-
call.FlurlRequest.EnableCookies().WithCookie("AuthSession", _cookieToken);
38+
httpCall.FlurlRequest.EnableCookies().WithCookie("AuthSession", _cookieToken);
3639
break;
3740
default:
3841
throw new NotSupportedException($"Authentication of type {_settings.AuthenticationType} is not supported.");
@@ -41,21 +44,22 @@ protected virtual void OnBeforeCall(HttpCall call)
4144

4245
private async Task LoginAsync()
4346
{
44-
var response = await _flurlClient.Request(ConnectionString)
47+
HttpResponseMessage response = await _flurlClient.Request(ConnectionString)
4548
.AppendPathSegment("_session")
4649
.PostJsonAsync(new
4750
{
4851
name = _settings.Username,
4952
password = _settings.Password
50-
});
53+
})
54+
.ConfigureAwait(false);
5155

5256
_cookieCreationDate = DateTime.Now;
5357

5458
if (response.Headers.TryGetValues("Set-Cookie", out var values))
5559
{
5660
var dirtyToken = values.First();
5761
var regex = new Regex(@"^AuthSession=(.+); Version=1; .*Path=\/; HttpOnly$");
58-
var match = regex.Match(dirtyToken);
62+
Match match = regex.Match(dirtyToken);
5963
if (match.Success)
6064
{
6165
_cookieToken = match.Groups[1].Value;
@@ -67,9 +71,16 @@ private async Task LoginAsync()
6771
}
6872
private async Task LogoutAsync()
6973
{
70-
await _flurlClient.Request(ConnectionString)
74+
OperationResult result = await _flurlClient.Request(ConnectionString)
7175
.AppendPathSegment("_session")
72-
.DeleteAsync();
76+
.DeleteAsync()
77+
.ReceiveJson<OperationResult>()
78+
.ConfigureAwait(false);
79+
80+
if (!result.Ok)
81+
{
82+
throw new CouchDeleteException();
83+
}
7384
}
7485
}
7586
}

src/CouchDB.Driver/CouchDB.Driver.csproj

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<Authors>Matteo Bortolazzo</Authors>
77
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
88
<Description>A .NET Standard driver for CouchDB.</Description>
9-
<PackageProjectUrl>https://github.com/matteobortolazzo/couchdb-ne</PackageProjectUrl>
9+
<PackageProjectUrl>https://github.com/matteobortolazzo/couchdb-net</PackageProjectUrl>
1010
<RepositoryUrl>https://github.com/matteobortolazzo/couchdb-ne</RepositoryUrl>
1111
<PackageTags>couchdb,driver,nosql,netstandard,pouchdb,xamarin</PackageTags>
1212
<PackageReleaseNotes>Complete rewrite.
@@ -16,11 +16,18 @@ All selectors support.</PackageReleaseNotes>
1616
<ApplicationIcon />
1717
<OutputType>Library</OutputType>
1818
<StartupObject />
19+
<NeutralLanguage>en</NeutralLanguage>
20+
</PropertyGroup>
21+
22+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
23+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
24+
<WarningsAsErrors />
1925
</PropertyGroup>
2026

2127
<ItemGroup>
2228
<PackageReference Include="Flurl.Http" Version="2.4.1" />
2329
<PackageReference Include="Humanizer.Core" Version="2.6.2" />
30+
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.1" />
2431
<PackageReference Include="Nito.AsyncEx.Context" Version="1.1.0" />
2532
</ItemGroup>
2633

0 commit comments

Comments
 (0)