Skip to content

Commit

Permalink
Add analyzers and fix bad code
Browse files Browse the repository at this point in the history
Fixes #297
  • Loading branch information
LordMike committed Apr 5, 2021
1 parent 00a98d6 commit d279b49
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 17 deletions.
10 changes: 5 additions & 5 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<!-- Common NuGet properties -->
<PropertyGroup>
<Authors>LordMike</Authors>
Expand All @@ -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>
Expand Down
4 changes: 4 additions & 0 deletions TMDbLib/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[*.cs]

# CAC001: ConfigureAwaitChecker
dotnet_diagnostic.CAC001.severity = error
6 changes: 3 additions & 3 deletions TMDbLib/Client/TMDbClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private void AddSessionId(RestRequest req, SessionType targetType = SessionType.

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");
Expand All @@ -180,10 +180,10 @@ public async Task<byte[]> GetImageBytes(string size, string filePath, bool useSs
{
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)
Expand Down
2 changes: 1 addition & 1 deletion TMDbLib/Client/TMDbClientChanges.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public async Task<SearchContainer<ChangesListItem>> GetTvChangesAsync(int page =

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;
}

Expand Down
2 changes: 1 addition & 1 deletion TMDbLib/Client/TMDbClientPeople.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public async Task<Person> GetLatestPersonAsync(CancellationToken cancellationTok
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)
Expand Down
5 changes: 3 additions & 2 deletions TMDbLib/Rest/RestRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ private HttpRequestMessage PrepRequest(HttpMethod method)
// 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))
{
Expand Down Expand Up @@ -199,7 +200,7 @@ private async Task<HttpResponseMessage> SendInternal(HttpMethod method, Cancella
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;

Expand Down
9 changes: 9 additions & 0 deletions TMDbLib/TMDbLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
8 changes: 4 additions & 4 deletions TMDbLibTests/ClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ public void SetConfigTest()
[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();
}

Expand Down
2 changes: 1 addition & 1 deletion TestApplication/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ private static async Task Main(string[] args)
{
// 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).
Expand Down

0 comments on commit d279b49

Please sign in to comment.