Add analyzers and fix bad code

Fixes #297
This commit is contained in:
Michael Bisbjerg 2021-04-06 00:16:35 +02:00
parent 00a98d603f
commit d279b491c4
9 changed files with 31 additions and 17 deletions

View File

@ -3,7 +3,7 @@
<PropertyGroup>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<!-- Common NuGet properties -->
<PropertyGroup>
<Authors>LordMike</Authors>
@ -21,17 +21,17 @@
<DebugType>portable</DebugType>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<MinVerTagPrefix>v</MinVerTagPrefix>
<!-- Github packages does not support symbols, so we embed pdbs in nupkg (https://github.community/t/does-github-packages-dotnet-nuget-supports-to-publish-snupkg/123286/6) -->
<AllowedOutputExtensionsInPackageBuildOutputFolder Condition="'$(Configuration)'=='Debug'">$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MinVer" Version="2.5.0" PrivateAssets="all" />
</ItemGroup>
<!-- Sourcelink -->
<PropertyGroup>
<PublishRepositoryUrl>true</PublishRepositoryUrl>

4
TMDbLib/.editorconfig Normal file
View File

@ -0,0 +1,4 @@
[*.cs]
# CAC001: ConfigureAwaitChecker
dotnet_diagnostic.CAC001.severity = error

View File

@ -158,7 +158,7 @@ namespace TMDbLib.Client
public async Task<TMDbConfig> GetConfigAsync()
{
TMDbConfig config = await _client.Create("configuration").GetOfT<TMDbConfig>(CancellationToken.None);
TMDbConfig config = await _client.Create("configuration").GetOfT<TMDbConfig>(CancellationToken.None).ConfigureAwait(false);
if (config == null)
throw new Exception("Unable to retrieve configuration");
@ -180,10 +180,10 @@ namespace TMDbLib.Client
{
Uri url = GetImageUrl(size, filePath, useSsl);
HttpResponseMessage response = await _client.HttpClient.GetAsync(url, HttpCompletionOption.ResponseContentRead, token);
using HttpResponseMessage response = await _client.HttpClient.GetAsync(url, HttpCompletionOption.ResponseContentRead, token).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsByteArrayAsync();
return await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
}
private void Initialize(string baseUrl, bool useSsl, string apiKey)

View File

@ -84,7 +84,7 @@ namespace TMDbLib.Client
public async Task<IList<Change>> GetMovieChangesAsync(int movieId, int page = 0, DateTime? startDate = null, DateTime? endDate = null, CancellationToken cancellationToken = default)
{
ChangesContainer changesContainer = await GetChangesInternal<ChangesContainer>("movie", page, movieId, startDate, endDate, cancellationToken);
ChangesContainer changesContainer = await GetChangesInternal<ChangesContainer>("movie", page, movieId, startDate, endDate, cancellationToken).ConfigureAwait(false);
return changesContainer.Changes;
}

View File

@ -56,7 +56,7 @@ namespace TMDbLib.Client
public async Task<Person> GetPersonAsync(int personId, PersonMethods extraMethods = PersonMethods.Undefined,
CancellationToken cancellationToken = default)
{
return await GetPersonAsync(personId, DefaultLanguage, extraMethods, cancellationToken);
return await GetPersonAsync(personId, DefaultLanguage, extraMethods, cancellationToken).ConfigureAwait(false);
}
public async Task<Person> GetPersonAsync(int personId, string language, PersonMethods extraMethods = PersonMethods.Undefined, CancellationToken cancellationToken = default)

View File

@ -159,7 +159,8 @@ namespace TMDbLib.Rest
// Body
if (method == HttpMethod.Post && _bodyObj != null)
{
MemoryStream ms = new MemoryStream();
using MemoryStream ms = new MemoryStream();
using (StreamWriter sw = new StreamWriter(ms, _client.Encoding, 4096, true))
using (JsonTextWriter tw = new JsonTextWriter(sw))
{
@ -199,7 +200,7 @@ namespace TMDbLib.Rest
return resp;
if (isJson)
statusMessage = JsonConvert.DeserializeObject<TMDbStatusMessage>(await resp.Content.ReadAsStringAsync());
statusMessage = JsonConvert.DeserializeObject<TMDbStatusMessage>(await resp.Content.ReadAsStringAsync().ConfigureAwait(false));
else
statusMessage = null;

View File

@ -8,6 +8,15 @@
<Description>.NET Client library for The Movie Database (https://www.themoviedb.org/)</Description>
</PropertyGroup>
<!-- Analyzers -->
<ItemGroup>
<PackageReference Include="ConfigureAwaitChecker.Analyzer" Version="5.0.0" PrivateAssets="All" />
<PackageReference Include="IDisposableAnalyzers" Version="3.4.13">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
</ItemGroup>

View File

@ -59,16 +59,16 @@ namespace TMDbLibTests
[Fact]
public async Task ClientConstructorUrlTest()
{
TMDbClient clientA = new TMDbClient(TestConfig.APIKey, false, "http://api.themoviedb.org") { MaxRetryCount = 2 };
using TMDbClient clientA = new TMDbClient(TestConfig.APIKey, false, "http://api.themoviedb.org") { MaxRetryCount = 2 };
await clientA.GetConfigAsync();
TMDbClient clientB = new TMDbClient(TestConfig.APIKey, true, "http://api.themoviedb.org") { MaxRetryCount = 2 };
using TMDbClient clientB = new TMDbClient(TestConfig.APIKey, true, "http://api.themoviedb.org") { MaxRetryCount = 2 };
await clientB.GetConfigAsync();
TMDbClient clientC = new TMDbClient(TestConfig.APIKey, false, "https://api.themoviedb.org") { MaxRetryCount = 2 };
using TMDbClient clientC = new TMDbClient(TestConfig.APIKey, false, "https://api.themoviedb.org") { MaxRetryCount = 2 };
await clientC.GetConfigAsync();
TMDbClient clientD = new TMDbClient(TestConfig.APIKey, true, "https://api.themoviedb.org") { MaxRetryCount = 2 };
using TMDbClient clientD = new TMDbClient(TestConfig.APIKey, true, "https://api.themoviedb.org") { MaxRetryCount = 2 };
await clientD.GetConfigAsync();
}

View File

@ -18,7 +18,7 @@ namespace TestApplication
{
// Instantiate a new client, all that's needed is an API key, but it's possible to
// also specify if SSL should be used, and if another server address should be used.
TMDbClient client = new TMDbClient("c6b31d1cdad6a56a23f0c913e2482a31");
using TMDbClient client = new TMDbClient("c6b31d1cdad6a56a23f0c913e2482a31");
// We need the config from TMDb in case we want to get stuff like images
// The config needs to be fetched for each new client we create, but we can cache it to a file (as in this example).